Skip to content

Commit 9411a30

Browse files
committed
feat/target_site_id
adds target_site_id to HiveMessage extends slave protocol to inject those when site_id matches, this allows masters to target things like "kitchen" without knowing which, if any, satellite devices are there
1 parent e07d6c8 commit 9411a30

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

hivemind_bus_client/message.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class HiveMessageType(str, Enum):
2626

2727
class HiveMessage:
2828
def __init__(self, msg_type, payload=None, node=None, source_peer=None,
29-
route=None, target_peers=None, meta=None):
29+
route=None, target_peers=None, meta=None, target_site_id=None):
3030
# except for the hivemind node classes receiving the message and
3131
# creating the object nothing should be able to change these values
3232
# node classes might change them a runtime by the private attribute
@@ -50,12 +50,17 @@ def __init__(self, msg_type, payload=None, node=None, source_peer=None,
5050
payload = json.loads(payload)
5151
self._payload = payload or {}
5252

53+
self._site_id = target_site_id
5354
self._node = node # node semi-unique identifier
5455
self._source_peer = source_peer # peer_id
5556
self._route = route or [] # where did this message come from
5657
self._targets = target_peers or [] # where will it be sent
5758
self._meta = meta or {}
5859

60+
@property
61+
def target_site_id(self):
62+
return self._site_id
63+
5964
@property
6065
def msg_type(self):
6166
return self._msg_type
@@ -104,6 +109,7 @@ def as_dict(self):
104109
"payload": pload,
105110
"route": self.route,
106111
"node": self.node_id,
112+
"target_site_id": self.target_site_id,
107113
"source_peer": self.source_peer}
108114

109115
@property
@@ -120,19 +126,22 @@ def deserialize(payload):
120126

121127
if "msg_type" in payload:
122128
try:
123-
return HiveMessage(payload["msg_type"], payload["payload"])
129+
return HiveMessage(payload["msg_type"], payload["payload"],
130+
target_site_id=payload.get("target_site_id"))
124131
except:
125132
pass # not a hivemind message
126133

127134
if "type" in payload:
128135
try:
129136
# NOTE: technically could also be SHARED_BUS or THIRDPRTY
130137
return HiveMessage(HiveMessageType.BUS,
131-
Message.deserialize(payload))
138+
Message.deserialize(payload),
139+
target_site_id=payload.get("target_site_id"))
132140
except:
133141
pass # not a mycroft message
134142

135-
return HiveMessage(HiveMessageType.THIRDPRTY, payload)
143+
return HiveMessage(HiveMessageType.THIRDPRTY, payload,
144+
target_site_id=payload.get("target_site_id"))
136145

137146
def __getitem__(self, item):
138147
return self._payload.get(item)

hivemind_bus_client/protocol.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ def handle_send(self, message: Message):
3636
payload = message.data.get("payload")
3737
msg_type = message.data["msg_type"]
3838

39-
hmessage = HiveMessage(msg_type,
40-
payload=payload)
39+
hmessage = HiveMessage(msg_type, payload=payload)
4140

4241
if msg_type == HiveMessageType.BROADCAST:
4342
# only masters can broadcast, ignore silently
@@ -216,6 +215,16 @@ def handle_bus(self, message: HiveMessage):
216215

217216
def handle_broadcast(self, message: HiveMessage):
218217
LOG.info(f"BROADCAST: {message.payload}")
218+
219+
# if the message targets our site_id, send it to internal bus
220+
site = message.target_site_id
221+
if site and site == self.site_id:
222+
pload = message.payload
223+
# broadcast messages always come from a trusted source
224+
# only masters can emit them
225+
if isinstance(pload, MycroftMessage):
226+
self.handle_bus(message)
227+
219228
# if this device is also a hivemind server
220229
# forward to HiveMindListenerInternalProtocol
221230
data = message.serialize()
@@ -224,6 +233,17 @@ def handle_broadcast(self, message: HiveMessage):
224233

225234
def handle_propagate(self, message: HiveMessage):
226235
LOG.info(f"PROPAGATE: {message.payload}")
236+
237+
# if the message targets our site_id, send it to internal bus
238+
site = message.target_site_id
239+
if site and site == self.site_id:
240+
# might originate from untrusted
241+
# satellite anywhere in the hive
242+
# do not inject by default
243+
pload = message.payload
244+
#if isinstance(pload, MycroftMessage):
245+
# self.handle_bus(message)
246+
227247
# if this device is also a hivemind server
228248
# forward to HiveMindListenerInternalProtocol
229249
data = message.serialize()

0 commit comments

Comments
 (0)