Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 import socket
00038 import struct
00039 import yaml
00040 import sys
00041
00042 def go(config_file):
00043 data = yaml.load(open(config_file))
00044 if 'device' not in data:
00045 raise Exception('no device specified')
00046 dev = data['device']
00047 if type(dev) != dict or not dev.has_key('type'):
00048 raise Exception('parse error on device')
00049 typ = dev['type']
00050 if typ == 'generic_io_socket_server':
00051 if not dev.has_key('host') or not dev.has_key('port'):
00052 raise Exception('parse error on device')
00053 host = dev['host']
00054 port = dev['port']
00055 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
00056 sock.bind((host, port))
00057 sock.listen(1)
00058 while True:
00059 print 'Listening for connection'
00060 conn, addr = sock.accept()
00061 print 'Accepted new connection'
00062 while True:
00063 length = 1*4+3*4+4*4;
00064 fmt_str = '<IIIIIIII'
00065 data = ''
00066 while len(data) < length:
00067 new_data = conn.recv(length-len(data))
00068 if not new_data:
00069 break
00070 data += new_data
00071 if len(data) != length:
00072 break
00073 values = struct.unpack(fmt_str, data)
00074 print 'Received:'
00075 print values
00076 else:
00077 raise Exception('unsupported device type')
00078
00079
00080 USAGE = 'Usage: mock_server.py <config.yaml>'
00081
00082 if __name__ == '__main__':
00083 if len(sys.argv) != 2:
00084 print(USAGE)
00085 sys.exit(1)
00086 go(sys.argv[1])
00087