diff --git a/TcpServerNode.py b/TcpServerNode.py index b73353b..c0752a4 100644 --- a/TcpServerNode.py +++ b/TcpServerNode.py @@ -216,6 +216,7 @@ def __init__(self, nodeServer, sock, clientAddress, callback): # Variable for parsing the incoming json messages self.buffer = "" + self.message_count = 0; id = hashlib.md5() t = self.host + str(self.port) + str(random.randint(1, 99999999)) @@ -227,15 +228,19 @@ def __init__(self, nodeServer, sock, clientAddress, callback): # Send data to the node. The data should be a python variabele # This data is converted into json and send. def send(self, data): + self.message_count = self.message_count + 1 + data['_mc'] = self.message_count try: message = json.dumps(data, separators=(',', ':')) + "-TSN"; - #self.sock.sendall(json.dumps(data, separators=(',', ':')).encode('ascii')) self.sock.sendall(message.encode('utf-8')) except: print("NodeConnection.send: Unexpected error:", sys.exc_info()[0]) self.terminate_flag.set() + def get_message_count(self): + return self.message_count + # Stop the node client. Please make sure you join the thread. def stop(self): self.terminate_flag.set() diff --git a/test.py b/test.py index ef94b99..afb670e 100644 --- a/test.py +++ b/test.py @@ -14,30 +14,42 @@ node1 = None node2 = None +def callback_node_event(event, node, other, data): + if ( event == "NODEINBOUNDCLOSED" ): + print("NODE: " + event + "\n") -def callback_node_event1(event, node, other, data): - print("Event Node 1 (" + node.id + "): %s: %s" % (event, data)) - node.send_to_nodes({"thank": "you"}) + elif ( event == "NODEOUTBOUNDCLOSED" ): + print("NODE: " + event + "\n") + elif ( event == "CONNECTEDWITHNODE" ): + print("NODE: " + event + "\n") -def callback_node_event2(event, node, other, data): - print("Event Node 2 (" + node.id + "): %s: %s" % (event, data)) - node.send_to_nodes({"thank": "you"}) + elif ( event == "NODECONNECTED" ): + print("NODE: " + event + "\n") + elif ( event == "NODEMESSAGE" ): + print("NODE: " + event + ": " + str(data) + "\n") -node1 = Node('localhost', 10000, callback_node_event1) -node2 = Node('localhost', 20000, callback_node_event2) + else: + print("NODE: Event is not known: " + event) + +node1 = Node('localhost', 10000, callback_node_event) +node2 = Node('localhost', 20000, callback_node_event) node1.start() node2.start() node1.connect_with_node('localhost', 20000) +time.sleep(2) + #node1.terminate_flag.set() # Stopping the thread node1.send_to_nodes({"test": "ha", "ni": "hao"}) while True: + node1.send_to_nodes({"node": "Node 1", "test": "ha", "ni": "hao"}) + node2.send_to_nodes({"node": "Node 2", "test": "ha", "ni": "hao"}) time.sleep(1) node1.stop()