
import AUSS.*;
import java.io.*;

public class AUSSExample {
	String host;
	int port;
	static int BUFF = 4096;
	public static void main(String [] args) {
		if (args.length < 2) {
			System.err.println("2 arguements required: <host> <port>");
		}
		String host = args[0];
		int port = Integer.parseInt(args[1]);
		AUSSExample ex = new AUSSExample(host,port);
		ex.run();
	}

	public  AUSSExample(String host,int port) { //BASE PORT
		this.host = host;
		this.port = port;
	}
	public void run() {
		Thread thread1 = new Thread() {
			public void run() {
				try {
					Sleeper sleeper = new Sleeper();
					initFilter();
					for (;;) {
						System.err.print("-");
						runFilter();
						try {
							sleeper.sleep(1);
						} catch (InterruptedException e) {

						}
					}
				} catch (IOException e) {
					System.err.println(e);
				}

			}
		};
		Thread thread2 = new Thread() {
			public void run() {
				try {
					Sleeper sleeper = new Sleeper();
					initInput();
					for (;;) {
						System.err.print("+");
						runInput();
						try {
							sleeper.sleep(1);
						} catch (InterruptedException e) {

						}
					}
				} catch (IOException e) {
					System.err.println(e);
				}
			}
		};
		thread1.start();
		thread2.start();
		Sleeper sleeper = new Sleeper();
		try {
			sleeper.sleep(0);
		} catch (InterruptedException e) {

		}
	}
	AUSS.Connection filterConnection;
	AUSS.Connection inputConnection;
	public void initFilter() throws IOException {
 		filterConnection = new Connection();
		filterConnection.connect(host,port+2);
	}
	public void initInput() throws IOException {
 		inputConnection = new Connection();
		inputConnection.connect(host,port);
	}
	short [] filterOut = new short[BUFF];
	short [] inputOut = new short[BUFF];
	void runFilter() throws IOException {
		filterConnection.readLEShort(filterOut,BUFF);
		for (int i = 0; i < BUFF;i++) {
			//filterOut[i] = (short)((filterOut[i] >> 8 )<< 8);
			//filterOut[i] = (short)(2*filterOut[i]);
			//filterOut[i] = (short)(2*filterOut[i]);
			filterOut[i] = (short)(filterOut[i]>>3);

		}
		filterConnection.writeLEShort(filterOut,BUFF);
	}
	void runInput() throws IOException {
		for (int i = 0; i < BUFF;i++) {
			inputOut[i] = (short)(32000-65000*Math.random());
		}
		inputConnection.writeLEShort(inputOut,BUFF);
	}
	class Sleeper {
		synchronized void sleep(int t) throws InterruptedException {
			this.wait(t);
		}
	}
}

