package client;

import java.util.LinkedList;


/**
 * Stores byte arrays sent from client/server
 * @author julian
 *
 */
public class ByteMonitor {
	private LinkedList<byte[]> fromServer;
	private LinkedList<byte[]> toServer;
	
	
	public ByteMonitor(){
		fromServer = new LinkedList<byte[]>();
		toServer = new LinkedList<byte[]>();
	}
	
	
	/**
	 * Messages sent from server are put here.
	 * @param msg
	 */
	public synchronized void putArrayFromServer(byte[] msg){
		fromServer.add(msg);
		notifyAll();
	}
	
	/**
	 * Messages sent from server are read here.
	 * @return
	 */
	public synchronized byte[] readArrayFromServer(){
		while(fromServer.isEmpty()){
			try{
				wait();
			} catch (InterruptedException e){
				e.printStackTrace();
			}
		}
		byte[] tmp = fromServer.poll();
		notifyAll();
		return tmp;
	}
	
	

	/**
	 * Messages sent from client are put here.
	 * @param msg
	 */
	public synchronized void putArrayToServer(byte[] msg){
		toServer.add(msg);
		notifyAll();
	}
	
	/**
	 * Messages sent from client are read here.
	 * @return
	 */
	public synchronized byte[] readArrayToServer(){
		while(toServer.isEmpty()){
			try{
				wait();
			} catch (InterruptedException e){
				e.printStackTrace();
			}
		}
		byte[] tmp = toServer.poll();
		notifyAll();
		return tmp;
	}

}

@@ -10,16 +10,106 @@
 lient;%0A%0A
+import java.io.IOException;%0Aimport java.net.Socket;%0Aimport java.net.UnknownHostException;%0A
 import j
@@ -311,16 +311,64 @@
 Server;%0A
+%09private Socket socket;%0A%09private String ipport;%0A
 %09%0A%09%0A%09pub
@@ -383,16 +383,29 @@
 Monitor(
+String ipport
 )%7B%0A%09%09fro
@@ -475,24 +475,415 @@
 %3Cbyte%5B%5D%3E();%0A
+%09%09this.ipport = ipport;%0A%09%7D%0A%09%0A%09public boolean initConnection()%7B%0A%09%09int split = ipport.indexOf(':');%0A%09%09String ip = ipport.substring(0, split);%0A%09%09int port = Integer.parseInt(ipport.substring(split+1));%0A%09%09try %7B%0A%09%09%09socket = new Socket(ip, port);%0A%09%09%7D catch (UnknownHostException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09return socket.isConnected();%0A%09%09%0A
 %09%7D%0A%09%0A%09%0A%09/**%0A

add initConn