import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; import java.io.*; import java.util.zip.*; public class Client extends JFrame implements Runnable { private Thread thread = new Thread(this); private boolean running = true; private Socket s = null; private ObjectInputStream in = null; private ObjectOutputStream out = null; private static String host = "atlas.dsv.su.se"; private static int port = 4848; private static String encoding = "gsm"; private Sound sound; private JButton recordButton = new JButton("Record"); private JButton stopButton = new JButton("Stop&Send"); private static boolean USE_ZIP = true; public static void main(String[] args) { if(args.length != 0) { host = args[0]; if(args.length != 1) { port = Integer.parseInt(args[1]); if(args.length != 2) { encoding = args[2]; if(args.length != 3) { if(args[3].equals("no_zip")) USE_ZIP = false; } } } } new Client(); } public Client() { setDefaultCloseOperation(EXIT_ON_CLOSE); recordButton.setEnabled(true); stopButton.setEnabled(false); recordButton.addActionListener(new LRecord()); stopButton.addActionListener(new LStop()); getContentPane().setLayout(new GridLayout()); getContentPane().add(recordButton); getContentPane().add(stopButton); pack(); show(); sound = new Sound(encoding); try { s = new Socket(host, port); setTitle("CONNECTED TO: " + host + " - ON PORT: " + port); out = new ObjectOutputStream(s.getOutputStream()); } catch (IOException ioe) { System.out.println("COULD NOT CONNECT: "+ ioe); System.exit(1); } thread.start(); } public void run() { while(running) { try { System.out.println("Waiting for or receiving sound object!"); // Obs! Detta maste vara har!!! in = new ObjectInputStream(s.getInputStream()); Storage storage = (Storage)in.readObject(); System.out.println("Received sound object, size: " + storage.getData().length); if(USE_ZIP) storage = decompress(storage); System.out.println("Playing sound!"); sound.load(storage); sound.play(); } catch(Exception e) { } } } private Storage compress(Storage storage) { System.out.println("ZIP-compressing!"); byte[] output = new byte[storage.getData().length]; Deflater compresser = new Deflater(); compresser.setInput(storage.getData()); compresser.finish(); int compressedDataLength = compresser.deflate(output); byte[] newData = new byte[compressedDataLength]; for(int i = 0; i < compressedDataLength; i++) newData[i] = output[i]; System.out.println("ZIP-compressing done, size: " + newData.length); return new Storage(newData); } private Storage decompress(Storage storage) { try { System.out.println("ZIP-decompressing!"); Inflater decompresser = new Inflater(); decompresser.setInput(storage.getData(), 0, storage.getData().length); byte[] result = new byte[2 * storage.getData().length]; int resultLength = decompresser.inflate(result); decompresser.end(); byte[] newData = new byte[resultLength]; for(int i = 0; i < resultLength; i++) newData[i] = result[i]; System.out.println("ZIP-decompression done, size: " + newData.length); return new Storage(newData); } catch(DataFormatException dfe) { return null; } } class LRecord implements ActionListener { public void actionPerformed(ActionEvent e) { recordButton.setEnabled(false); stopButton.setEnabled(true); System.out.println("Recording sound!"); sound.record(); } } class LStop implements ActionListener { public void actionPerformed(ActionEvent e) { recordButton.setEnabled(true); stopButton.setEnabled(false); sound.stop(); while(sound.recorderRunning) { try {Thread.sleep(100);} catch(Exception ee){} } Storage storage = sound.get(); try { System.out.println("Recorded sound size: " + storage.getData().length); if(USE_ZIP) storage = compress(storage); System.out.println("Sending sound object, size: " + storage.getData().length); out.writeObject(storage); System.out.println("Sent sound object!"); } catch(IOException ioe) { System.err.println("I/O problems: " + e); } } } }