From 303e92795e41b22d1ceae66dd76b084fc3a96c0d Mon Sep 17 00:00:00 2001 From: Adam Fast Date: Sat, 14 Sep 2013 19:07:32 -0500 Subject: [PATCH] Adds a private function for alerting when events happen inside the library. This function no-ops by default but can be overridden by subclassing IPSC and extending the method to perform whatever type of notification is desired. --- ipsc.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ipsc.py b/ipsc.py index 5d1f972..f2afadc 100644 --- a/ipsc.py +++ b/ipsc.py @@ -446,7 +446,17 @@ class IPSC(DatagramProtocol): peer['STATUS']['KEEP_ALIVES_SENT'] += 1 peer['STATUS']['KEEP_ALIVES_OUTSTANDING'] += 1 - + def _notify_event(self, network, event, info): + """ + Used internally whenever an event happens that may be useful to notify the outside world about. + Arguments: + network: string, network name to look up in config + event: string, basic description + info: dict, in the interest of accomplishing as much as possible without code changes. + The dict will typically contain a peer_id so the origin of the event is known. + """ + + pass #************************************************ # RECEIVED DATAGRAM - ACT IMMEDIATELY!!! @@ -481,6 +491,7 @@ class IPSC(DatagramProtocol): if not(valid_master(self._network, _peerid) == False or valid_peer(self._peer_list, _peerid) == False): logger.warning('(%s) PeerError: Peer not in peer-list: %s', self._network, _dec_peerid) return + self._notify_event(self._network, 'group_voice', {'peer_id': _dec_peerid}) group_voice(self._network, data) @@ -494,6 +505,7 @@ class IPSC(DatagramProtocol): # Generate a hashed paket from our template and send it. peer_alive_reply_packet = self.hashed_packet(self._local['AUTH_KEY'], self.PEER_ALIVE_REPLY_PKT) + self._notify_event(self._network, 'peer_keepalive', {'peer_id': _dec_peerid}) self.transport.write(peer_alive_reply_packet, (host, port)) elif (_packettype == MASTER_ALIVE_REPLY): @@ -531,9 +543,11 @@ class IPSC(DatagramProtocol): elif (_packettype == PEER_REG_REQ): # TO DO TO DO TO DO TO DO ***ADD CODE TO VALIDATE THE PEER IS IN OUR PEER-LIST HERE*** peer_reg_reply_packet = self.hashed_packet(self._local['AUTH_KEY'], self.PEER_REG_REPLY_PKT) + self._notify_event(self._network, 'peer_registration', {'peer_id': _dec_peerid}) self.transport.write(peer_reg_reply_packet, (host, port)) elif (_packettype == PEER_REG_REPLY): + self._notify_event(self._network, 'peer_registration_reply', {'peer_id': _dec_peerid}) for peer in self._config['PEERS']: if peer['RADIO_ID'] == _peerid: peer['STATUS']['CONNECTED'] = True