Skip to content
Snippets Groups Projects
Select Git revision
  • 6de6a6b8e1ba3f5b8f2d76ed079f922b4d91d6fd
  • master default
  • labcomm2014_tc31
  • labcomm2014
  • js
  • java_dyn_msg_dec
  • anders.blomdell
  • typeref
  • pragma
  • compiler-refactoring
  • labcomm2013
  • v2014.1
  • v2014.0
  • v2013.0
14 results

TestClient.java

Blame
  • Forked from Anders Blomdell / LabComm
    402 commits behind the upstream repository.
    TestClient.java 1.67 KiB
    package labcommTCPtest.client;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    import se.lth.control.labcomm.LabCommDecoderChannel;
    import se.lth.control.labcomm.LabCommEncoderChannel;
    import labcommTCPtest.gen.FooSample;
    import labcommTCPtest.gen.FooSample.Handler;
    
    public class TestClient implements Handler {
    
    
    	private OutputStream out;
    	private InputStream in;
    
    	public TestClient(Socket server) throws IOException {
    		out = server.getOutputStream();
    		in = server.getInputStream();
    	}
    
    	public void test() {
    		
    		try {
    			LabCommEncoderChannel e = new LabCommEncoderChannel(out );
    			FooSample.register(e);
    			FooSample sample = new FooSample();
    			sample.x = 17;
    			sample.y = 42;
    			sample.t = 1717;
    			sample.d = 0.42;
    			printSample("Client sending", sample);
    			FooSample.encode(e, sample);
    
    			LabCommDecoderChannel c = new LabCommDecoderChannel(in);
    			FooSample.register(c,this);
    			c.runOne();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	public static void main(String... args) {
    		String server = "localhost";
    		int port = 9999;
    		try {
    			Socket s = new Socket(server, port);
    			TestClient c = new TestClient(s);
    			c.test();
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	private void printSample(String header, FooSample sample2) throws Exception {
    		System.out.format("[TestClient] %s: (%d, %d, %d, %f)\n", header, sample2.x, sample2.y, sample2.t, sample2.d);
    	}
    
    	public void handle_FooSample(FooSample sample2) throws Exception {
    		printSample("TestClient.handle_FooSample", sample2);
    
    	}
    }