Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:binarization_protocol #53

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions hivemind_bus_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,17 @@ def on_message(self, *args):
if (isinstance(message, HiveMessage) and message.msg_type == HiveMessageType.BINARY):
self._handle_binary(message)
return
self.emitter.emit('message', message) # raw message
self._handle_hive_protocol(HiveMessage(**message))

if isinstance(message, HiveMessage):
self.emitter.emit('message', message.serialize()) # raw message
self._handle_hive_protocol(message)
elif isinstance(message, str):
self.emitter.emit('message', message) # raw message
self._handle_hive_protocol(HiveMessage(**json.loads(message)))
else:
assert isinstance(message, dict)
self.emitter.emit('message', json.dumps(message, ensure_ascii=False)) # raw message
self._handle_hive_protocol(HiveMessage(**message))
Comment on lines +304 to +307
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Validate dictionary content before constructing a HiveMessage.
Here, we assert that message is a dictionary and then immediately unpack it into a HiveMessage. Consider adding checks or error handling to ensure the dictionary has the correct structure and required fields (e.g., “msg_type”/“payload”) before converting it into a HiveMessage.

-assert isinstance(message, dict)
-self.emitter.emit('message', json.dumps(message, ensure_ascii=False))
-self._handle_hive_protocol(HiveMessage(**message))
+if isinstance(message, dict):
+    if "payload" not in message or "msg_type" not in message:
+        LOG.warning("Invalid message structure: missing required fields.")
+    else:
+        self.emitter.emit('message', json.dumps(message, ensure_ascii=False))
+        self._handle_hive_protocol(HiveMessage(**message))
+else:
+    LOG.warning("Expected a dictionary but got something else.")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
else:
assert isinstance(message, dict)
self.emitter.emit('message', json.dumps(message, ensure_ascii=False)) # raw message
self._handle_hive_protocol(HiveMessage(**message))
else:
if isinstance(message, dict):
if "payload" not in message or "msg_type" not in message:
LOG.warning("Invalid message structure: missing required fields.")
else:
self.emitter.emit('message', json.dumps(message, ensure_ascii=False))
self._handle_hive_protocol(HiveMessage(**message))
else:
LOG.warning("Expected a dictionary but got something else.")


def _handle_binary(self, message: HiveMessage):
assert message.msg_type == HiveMessageType.BINARY
Expand Down
Loading