Skip to content

Commit

Permalink
Created a test.py file according to the new implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
macsnoeren committed Sep 21, 2018
1 parent f56d2c9 commit fc610df
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
7 changes: 6 additions & 1 deletion TcpServerNode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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()
Expand Down
28 changes: 20 additions & 8 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit fc610df

Please sign in to comment.