Skip to content

Commit

Permalink
Bug fixed: Sending message using python data objects is possible now
Browse files Browse the repository at this point in the history
  • Loading branch information
macsnoeren committed Sep 21, 2018
1 parent c330f17 commit f56d2c9
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions TcpServerNode.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import pprint
import random
import hashlib
import re

#######################################################################################################################
# TCPServer Class #####################################################################################################
Expand All @@ -29,8 +30,6 @@
# Implements a node that is able to connect to other nodes and is able to accept connections from other nodes.
# After instantiation, the node creates a TCP/IP server with the given port.
#


class Node(threading.Thread):

# Python class constructor
Expand Down Expand Up @@ -95,7 +94,7 @@ def delete_closed_connections(self):
del self.nodesOut[self.nodesIn.index(n)]

# Send a message to all the nodes that are connected with this node.
# data is a python variabele which is converted to JSON that is send over to the other node.
# data is a python variable which is converted to JSON that is send over to the other node.
# exclude list gives all the nodes to which this data should not be sent.
def send_to_nodes(self, data, exclude = []):
for n in self.nodesIn:
Expand Down Expand Up @@ -215,6 +214,9 @@ def __init__(self, nodeServer, sock, clientAddress, callback):
self.callback = callback
self.terminate_flag = threading.Event()

# Variable for parsing the incoming json messages
self.buffer = ""

id = hashlib.md5()
t = self.host + str(self.port) + str(random.randint(1, 99999999))
id.update(t.encode('ascii'))
Expand All @@ -226,7 +228,9 @@ def __init__(self, nodeServer, sock, clientAddress, callback):
# This data is converted into json and send.
def send(self, data):
try:
self.sock.sendall(json.dumps(data, separators=(',', ':')).encode('ascii'))
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])
Expand All @@ -245,7 +249,7 @@ def run(self):
while not self.terminate_flag.is_set(): # Check whether the thread needs to be closed
line = ""
try:
line = self.sock.recv(4096)
line = self.sock.recv(4096) # the line ends with -TSN\n

except socket.timeout:
pass
Expand All @@ -255,18 +259,29 @@ def run(self):
print("NodeConnection: Socket has been terminated (%s)" % line)

if line != "":
try:
obj = json.loads(line)
self.callback("NODEMESSAGE", self.nodeServer, self, obj)
except:
print("NodeConnection: Data could not be parsed (%s)" % line)
self.buffer += str(line.decode('utf-8'))

# Get the messages
index = self.buffer.find("-TSN")
while ( index > 0 ):
message = self.buffer[0:index]
self.buffer = self.buffer[index+4::]
index = self.buffer.find("-TSN")
try:
obj = json.loads(message)
self.callback("NODEMESSAGE", self.nodeServer, self, obj)
except:
print("NodeConnection: Data could not be parsed (%s)" % line)

time.sleep(0.01)

self.sock.settimeout(None)
self.sock.close()
print("NodeConnection: Stopped")

def get_message(self):
print("TESTING")

#######################################################################################################################
# Example usage of Node ###############################################################################################
#######################################################################################################################
Expand Down

0 comments on commit f56d2c9

Please sign in to comment.