def client(msg): # Set up port and protocol port = "24124" binding = "tcp://localhost:%s" % port binding_ipc = "ipc:///tmp/zmqtest" ipc_envelope_tag = "client_ipc" # Set up 0MQ as Request/Response topology context = zmq.Context() client_socket = context.socket(zmq.REQ) # Name the envelope/frameID so the receiver knows who sent it client_socket.setsockopt(zmq.IDENTITY, 'ipc_envelope_tag') client_socket.connect(binding_ipc) time.sleep(0.1) # Send one message and exit print("Sending Message of type \"%d\"" % msg.metadata.message_type) client_socket.send(msg.SerializeToString()) # Wait for response message = client_socket.recv() # De-Serialize response ack = gmessages.Message() ack.ParseFromString(message) print("Received reply [%s]" % (ack.control.cmd))
def compose_ack(ack="ACK"): """ Compose a standard acknowlege message """ # Add the metadata msg = gmessages.Message() msg.metadata.message_type = 0 msg.metadata.device_id = 0 msg.metadata.network_id = 0 msg.metadata.application_id = 0 msg.metadata.tx_time = int(time.time()) msg.metadata.sequence = 1 msg.metadata.periph_count = 0 msg.metadata.device_name = "Server" # Add an ACK command msg.control.key = 0 msg.control.is_indexed = False msg.control.cmd = ack return msg
def server(): # Set up port and protocol port = "24124" binding = "tcp://*:%s" % port binding_ipc = "ipc:///tmp/zmqtest" # Set up 0MQ as Request/Response topology context = zmq.Context() server_socket = context.socket(zmq.ROUTER) # Bind to the socket ##server_socket.setsockopt(zmq.IDENTITY, 'reader') server_socket.bind(binding_ipc) time.sleep(0.1) while True: # Receive the mesasage id_frame, empty_frame, message = server_socket.recv_multipart() try: # De-Serialize it msg = gmessages.Message() msg.ParseFromString(message) # Report it print(("Message of type \"%d\"received from \"%s\" " "with %d peripherals") % (msg.metadata.message_type, msg.metadata.device_name, msg.metadata.periph_count)) for p in msg.peripheral: print(("Peripheral \"%s\" with id: \"%d\"" " contains payload of \"%s\"") % (p.peripheral_name, p.peripheral_id, repr( p.payload)[1:-1])) print("") ack = compose_ack("ACK") except Exception: ack = compose_ack("NACK") # Respond to client server_socket.send_multipart( [id_frame, empty_frame, ack.SerializeToString()])
def server(): # Set up port and protocol port = "24124" binding = "tcp://*:%s" % port # Set up 0MQ as Request/Response topology context = zmq.Context() server_socket = context.socket(zmq.REP) # Bind to the socket server_socket.bind(binding) while True: # Receive the mesasage message = server_socket.recv() try: # De-Serialize it msg = gmessages.Message() msg.ParseFromString(message) # Report it print(("Message of type \"%d\"received from \"%s\" " "with %d peripherals") % (msg.metadata.message_type, msg.metadata.device_name, msg.metadata.periph_count)) for p in msg.peripheral: print(("Peripheral \"%s\" with id: \"%d\"" " contains payload of \"%s\"") % (p.peripheral_name, p.peripheral_id, repr( p.payload)[1:-1])) print("") ack = compose_ack("ACK") except Exception: ack = compose_ack("NACK") # Respond to client server_socket.send(ack.SerializeToString())
def client(msg): # Set up port and protocol port = "24124" binding = "tcp://localhost:%s" % port # Set up 0MQ as Request/Response topology context = zmq.Context() client_socket = context.socket(zmq.REQ) client_socket.connect(binding) # Send one message and exit print("Sending Message of type \"%d\"" % msg.metadata.message_type) client_socket.send(msg.SerializeToString()) # Wait for response message = client_socket.recv() # De-Serialize response ack = gmessages.Message() ack.ParseFromString(message) print("Received reply [%s]" % (ack.control.cmd))
print "Msg time:", msg.metadata.tx_time print "Sequence:", msg.metadata.sequence print "Peripheral Count:", msg.metadata.periph_count print "Device Name:", msg.metadata.device_name for p in msg.peripheral: print "Peripheral ID", p.peripheral_id print "Payload ID", p.payload_id print "Peripheral Name", p.peripheral_name print "Payload Name", p.payload_name print "Payload Attached", p.payload_attached print "payload", repr(p.payload)[1:-1] print "payload crc", p.payload_crc if __name__ == "__main__": if len(sys.argv) != 2: print "Usage:", sys.argv[0], "INPUT_FILE" sys.exit(1) msg = gmessages.Message() try: with open(sys.argv[1], "rb") as f: msg.ParseFromString(f.read()) except IOError: print sys.argv[1] + ": File not found." sys.exit(1) printMessage(msg)