Commit 2e0fa68f authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

added python test decoder and a server that only encodes

parent 4b757e87
#!/usr/bin/python
import os
import sys
import socket
import rwsocket
if not any('labcomm' in p for p in sys.path):
sys.path.append('../../lib/python')
import labcomm
if __name__ == "__main__":
print "Trying to connect..."
host = sys.argv[1] #'localhost'
port = sys.argv[2] #'8081'
addr = (host,int(port))
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(addr)
print "Connected!"
d = labcomm.Decoder(labcomm.StreamReader(sock))
while True:
try:
data,decl = d.decode()
if data:
print data
except KeyError as e:
print 'KeyError : "%s"' % str(e)
break
except:
print 'exception...', sys.exc_info()[0]
break
package labcommTCPtest.server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.ServerSocket;
import se.lth.control.labcomm.LabCommDecoderChannel;
import se.lth.control.labcomm.LabCommEncoderChannel;
import labcommTCPtest.gen.FooSample;
public class OneShotServer {//implements Handler {
private OutputStream out;
public OneShotServer(Socket s) throws IOException {
out = s.getOutputStream();
}
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("Server sending", sample);
FooSample.encode(e, sample);
Thread.sleep(1);
sample.x++;
sample.y--;
printSample("Server sending", sample);
FooSample.encode(e, sample);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String... args) {
String server = "localhost";
int port = 9999;
try {
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
OneShotServer c = new OneShotServer(s);
c.test();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private void printSample(String header, FooSample sample2) throws Exception {
System.out.println(header);
System.out.format("TestClient.invoke(%d, %d, %d, %f)\n", sample2.x, sample2.y, sample2.t, sample2.d);
}
}
#!/bin/sh
PYTHONPATH=../../lib/python ./example_tcp_client_decoder.py
import socket
import types
def flush(self):
pass
socket.socket.write = socket.socket.send
socket.socket.read = socket.socket.recv
socket.socket.flush = flush
# class rwsocket(socket.socket):
# write=socket.socket.send
# read=socket.socket.recv
# def flush(self):
# pass
# def accept(self):
# sock, addr = super(rwsocket, self).accept()
# # sock.write = types.MethodType(self.write, sock, sock.__class__)
# # sock.read = types.MethodType(self.write, sock, sock.__class__)
# sock.__class__ = rwsocket
# return (sock, addr)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment