def stop(self): """ Stop the nodes connection and stop asyncore.loop thread """ LOGGER.info("Stopping") self._running = False for node in self.values(): node.close() self.thread.join()
def handle_close(self): """ Close the connection and clear buffer """ self.data_buff = '' LOGGER.error('%s;Connection closed', self.hostname) self.close() # remove itself from aggregator self.aggregator.pop(self.hostname, None)
def start(self): """ Connect all nodes and run asyncore.loop in a thread """ self._running = True for node in self.values(): node.start() self.thread.start() LOGGER.info("Aggregator started")
def _send(self, hostname, message): """ Safe send message to node """ try: self[hostname].send(message) except KeyError: LOGGER.warning("Node not managed: %s", hostname) except socket.error: LOGGER.warning("Send failed: %s", hostname)
def send_nodes(self, nodes_list, message): """ Send the `message` to `nodes_list` nodes If nodes_list is None, send to all nodes """ if nodes_list is None: LOGGER.debug("Broadcast: %r", message) self.broadcast(message) else: LOGGER.debug("Send: %r to %r", message, nodes_list) for node in nodes_list: self._send(node, message)
def main(args=None): """ Aggregate all nodes radio sniffer """ args = args or sys.argv[1:] opts = SnifferAggregator.parser.parse_args(args) try: # Parse arguments nodes_list = SnifferAggregator.select_nodes(opts) if opts.debug: LOGGER.setLevel(logging.DEBUG) # Run the aggregator with SnifferAggregator(nodes_list, opts.outfd, opts.raw) as aggregator: aggregator.run() LOGGER.info('%u packets captured', aggregator.rx_packets) except (ValueError, RuntimeError) as err: sys.stderr.write("%s\n" % err) exit(1)
def main(args=None): """ Aggregate all nodes radio sniffer """ args = args or sys.argv[1:] opts = SnifferAggregator.parser.parse_args(args) try: # Parse arguments nodes_list = SnifferAggregator.select_nodes(opts) if opts.debug: LOGGER.setLevel(logging.DEBUG) # Run the aggregator with SnifferAggregator(nodes_list, opts.outfd, opts.raw) as aggregator: aggregator.run() LOGGER.info('%u packets captured', aggregator.rx_packets) except (ValueError, RuntimeError) as err: sys.stderr.write("{}\n".format(err)) exit(1)
def handle_data(self, data): """ Print the data received line by line """ while True: data = self._strip_until_pkt_start(data) if not data.startswith('EX\2') or len(data) < self.ZEP_HDR_LEN: break # length = header length + data['len_byte'] full_len = self.ZEP_HDR_LEN + ord(data[self.ZEP_HDR_LEN - 1]) if len(data) < full_len: break # Extract packet pkt, data = data[:full_len], data[full_len:] LOGGER.debug('%s;Packet received len: %d', self.hostname, full_len) self.pkt_handler(pkt) self.aggregator.rx_packets += 1 return data
def handle_error(self): """ Connection failed """ LOGGER.error('%s;%r', self.hostname, sys.exc_info())
def handle_data(self, data): """ Dummy handle data """ LOGGER.info("%s received %u bytes", self.hostname, len(data)) return '' # Remaining unprocessed data
def _loop(self): """ Run asyncore loop send SIGINT at the end to stop main process """ asyncore.loop(timeout=1, use_poll=True) if self._running: # Don't send signal if we are stopping LOGGER.info("Loop finished, all connection closed") os.kill(os.getpid(), signal.SIGINT)