import cnrg.itx.signal.*;
import java.net.*;
import java.io.*;
import cnrg.itx.ITXException;

public class proxy
{

	public static final int CEP_SETPEERADDR = 0;
	public static final int CEP_INVITATION = 1;
	public static final int CEP_HANGUP = 2;
	public static final int CEP_ACCEPT = 3;
	public static final int CEP_REJECT = 4;
	public static final int CEP_DISCONNECT = 5;
	public static final int CE_SAMPLESIZE = 64;
	
	public proxy() {
		ServerSocket Server;
		try {
			Server = new ServerSocket(19796);
		} catch (IOException ioe) {
			ioe.printStackTrace();
			return;
		}
		byte Buffer[] = new byte[106];
		while(true) {
			try {
				client NewClient = new client();
				NewClient.sock = Server.accept();
				if(NewClient.sock == null) continue;
				System.out.println("Got Something!");
				NewClient.in = new DataInputStream(NewClient.sock.getInputStream());
				NewClient.out = new DataOutputStream(NewClient.sock.getOutputStream());
				NewClient.in.read(Buffer, 0, 106);
				NewClient.port = (Buffer[1] << 8) + Buffer[0];
				NewClient.username = new String(Buffer, 6, Buffer[2]);
				NewClient.password = new String(Buffer, 86, Buffer[4]);
				NewClient.ip = NewClient.sock.getInetAddress().getHostAddress();
				try {
					NewClient.ds = new DesktopSignaling(NewClient, NewClient.username, NewClient.password);
					NewClient.start();
				} catch (ITXException e) {
					byte packet[] = new byte[106];
					packet[1] = CEP_DISCONNECT;
					NewClient.out.write(packet);
					NewClient.sock.close();
				}
			}
			catch (IOException ioe) {
				ioe.printStackTrace();
				continue;
			}
		}
	}
	
	public static void main (String[] args) throws Exception
	{
		proxy p = new proxy();
	}
}
