Skip to content

Commit c2230d4

Browse files
committed
Misc comments
1 parent 0b88490 commit c2230d4

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

RpiCluster/ConnectionHandler.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@
55
_MESSAGE_SEPARATOR = "\r"
66

77

8-
class ConnectionHandler():
8+
class ConnectionHandler:
9+
"""Class to handle sending and recieving messages from a socket
10+
11+
Attributes:
12+
sock: socket object representing the connection
13+
_buffered_string: A string buffer that holds all characters recieved from the socket not yet converted to messages
14+
_buffered_messages: An array of messages recieved and ready to be consumed
15+
"""
916

1017
def __init__(self, sock):
1118
self.sock = sock
1219
self._buffered_string = ""
1320
self._buffered_messages = []
1421

1522
def _check_buffer_for_messages(self):
23+
"""Private method to parse the current string buffer for messages and store them if found"""
1624
split_buffered_data = self._buffered_string.split(_MESSAGE_SEPARATOR)
1725
if len(split_buffered_data) > 1: # If we find more than one item, there is a message
1826
messages_to_process = split_buffered_data[0:-1]
@@ -22,12 +30,14 @@ def _check_buffer_for_messages(self):
2230
self._buffered_string = split_buffered_data[-1]
2331

2432
def _get_message_in_buffer(self):
33+
"""Looks in the buffer to find messages, if it finds one it pops it off otherwise it returns None"""
2534
if len(self._buffered_messages) > 0:
2635
return json.loads(self._buffered_messages.pop(0))
2736
else:
2837
return None
2938

3039
def get_message(self):
40+
"""Gets a single message from the socket, blocks if no messages available"""
3141
message_in_buffer = self._get_message_in_buffer()
3242
if message_in_buffer:
3343
return message_in_buffer
@@ -39,7 +49,7 @@ def get_message(self):
3949
raise DisconnectionException("Failed to receive messages, client has disconnected")
4050

4151
data_len = len(data)
42-
if data_len > 0: # Do something if we got data
52+
if data_len > 0: # Do something if we get data
4353
self._buffered_string += data.decode("utf-8") # Keep track of our buffered stored data
4454

4555
self._check_buffer_for_messages()
@@ -50,6 +60,7 @@ def get_message(self):
5060
raise DisconnectionException("No further messages received, client has disconnected")
5161

5262
def send_message(self, payload, payload_type="message"):
63+
"""Prepares and sends a payload to the along the socket"""
5364
payload = json.dumps({"type": payload_type, "payload": payload}) + _MESSAGE_SEPARATOR
5465

5566
try:

0 commit comments

Comments
 (0)