def Main(): rct.init(sys.argv) try: serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.bind((rct.host, 8899)) serversocket.listen(5) # become a server socket, maximum 5 connections except: rct.errlog('unable to connect to', rct.host, 'port', rct.port) sys.exit(0) rct.dbglog('waiting for client connection') while True: connection, address = serversocket.accept() rct.dbglog('connection accepted', connection, address) # Start a new thread and return its identifier start_new_thread(socket_thread, (connection, address)) #socket_thread(connection, address) serversocket.close()
def sim_response(connection, frame): if frame.command == rct.cmd_read: frame.command = rct.cmd_response frame.prepare(rct.cmd_long_response, frame.id, frame.address, frame.id_obj.sim_data) frame.encode() rct.dbglog( 'Read : #{:3} 0x{:08X} {:50} ->'.format(frame.id_obj.idx, frame.id_obj.id, frame.id_obj.name), binascii.hexlify(frame.stream)) connection.send(frame.stream) elif frame.command == rct.cmd_write: rct.dbglog( 'Write : #{:3} 0x{:08X} {:50} ->'.format(frame.id_obj.idx, frame.id_obj.id, frame.id_obj.name), frame.value) frame.id_obj.sim_data = rct.encode_by_type(frame.id_obj.data_type, frame.id_obj.sim_data) elif frame.command == rct.cmd_long_write: rct.dbglog( 'Write L: #{:3} 0x{:08X} {:50} ->'.format(frame.id_obj.idx, frame.id_obj.id, frame.id_obj.name), frame.value) frame.id_obj.sim_data = rct.encode_by_type(frame.id_obj.data_type, frame.id_obj.sim_data)
def socket_thread(connection, address): frame = rct.Frame() while True: try: ready = select.select([ connection, ], [], [], 1000.0) except select.error: break if ready[0]: # read up to 256 bytes in one chunk buf = connection.recv(256) if len(buf) > 0: consumed = 0 while consumed < len(buf): i = frame.consume(buf[consumed:]) consumed += i if frame.FrameComplete == True: if frame.CRCOk == True: frame = sim_response(connection, frame) else: rct.errlog('Discard frame with wrong CRC:', binascii.hexlify(frame.stream)) # waiting for new frame frame = rct.Frame() else: # seems the connection is closed -> exit thread break # connection.shutdown(0) # 0 = done receiving, 1 = done sending, 2 = both connection.close() # connection error event here, maybe reconnect rct.dbglog('thread: close connection ', connection)
def main(): rct.init(sys.argv) clientsocket = rct.connect_to_server() if clientsocket is not None: fmt = '#0x{:08X} {:'+str(rct.param_len)+'}'# {:'+str(rct.desc_len)+'}:' for obj in rct.id_tab: if rct.search_id > 0 and obj.id != rct.search_id: continue if rct.search_name is not None and fnmatch.fnmatch(obj.name, rct.search_name) == False: continue value = rct.read(clientsocket, obj.id) if rct.dbglog(fmt.format(obj.id, obj.name), value) == False: print(value) rct.close(clientsocket) sys.exit(0)
def main(): rct.init(sys.argv) clientsocket = rct.connect_to_server() if clientsocket is not None: for obj in rct.id_tab: if rct.search_id > 0 and obj.id != rct.search_id: continue if rct.search_name is not None and fnmatch.fnmatch( obj.name, rct.search_name) == False: continue value = rct.read(clientsocket, obj.id) if rct.dbglog( '#{:3} 0x{:08X} {:45} {:75}:'.format( obj.idx, obj.id, obj.name, obj.desc), value, obj.unit) == False: print value rct.close(clientsocket) sys.exit(0)