def main_loop(self) -> None: select_timeout_sec = self.config['socket'].getfloat( 'select_interval_sec', fallback=5) # first, advertise ourselves for _ in range(2): self.advertise_hub() self.advertise_lan() while True: rs, ws, xs = select.select( [self.tun.fd, self.sock.fileno()], [], [], select_timeout_sec) for fd in rs: if fd == self.sock.fileno(): # network traffic packet = protocol.receive(self.sock) self.process_udp_packet(packet) elif fd == self.tun.fd: # tun traffic packet_bytes = self.tun.read() if self.is_tap: self.process_tap_packet(packet_bytes) else: self.process_tun_packet(packet_bytes) else: # generic timeout pass now = datetime.datetime.now() if (now - self.ts_last_maintenance ).total_seconds() > self.maintenance_interval_sec: self.ts_last_maintenance = now self.maintenance()
def _evaluate_observer(self, receive_socket): """ Evaluates a connection to an observer, adding it to the list of observers according to its subject. """ _, subject = protocol.receive(receive_socket) if subject in self.observers: self.observers[subject].append(receive_socket) else: self.observers[subject] = [receive_socket]
def __init__(self, subject, callback, deserializer, port, host): try: receive_socket = socket.socket() receive_socket.connect((host, port)) protocol.send(receive_socket, "", subject) message, _ = protocol.receive(receive_socket) except socket.error, msg: logger.warn( str(msg[1]) + " Did you Initialize() to start the core?") return
def _relay(self, receive_socket): """ Relays a signal, initiated by a notification, to the appropriate observers. """ message, notifier_subject = protocol.receive(receive_socket) for observer_subject in self.observers: if observer_subject == notifier_subject: for observer_sock in self.observers[observer_subject]: try: protocol.send(observer_sock, message, notifier_subject) # Remove observer socket since it's only created # for one-time use. self.observers[observer_subject].remove(observer_sock) except socket.error, msg: # Unprepared observer logger.warn("Socket error (core): " + msg[1])
def main_loop(args: Any, sock: socket.socket) -> None: # hosts map (addr,port) -> ts_last_packet hosts: Dict[protocol.Peer, datetime.datetime] = {} while True: try: packet = protocol.receive(sock) except protocol.MalformedPacket as e: log.debug('skipping malformed packet: %s' % e) continue if packet.magic is not protocol.Magic.C2H: log.debug('non-c2h packet, ignoring') continue hosts[packet.peer] = datetime.datetime.now() c2h = packet.payload assert c2h is not None assert isinstance(c2h, protocol.Packet_c2h) broadcast_peer(sock, packet.peer, c2h, hosts)
p.rx_vibrate_image = bool(receivedDict['vibrate_image']) p.rx_output = bool(receivedDict['output']) p.rx_string = receivedDict['string'] def send_to_hardware(message=p.rx_string): try: string = p.beautify(p.create(message)) for i in string: raw_go_hw = p.RX(data=i).raw() p.send(raw_go_hw) except: raw_go_hw = p.RX().raw() p.send(raw_go_hw) def recieve_from_software(message="ACK"): global receivedMessage receivedMessage = getMessage() sendMessage(encodeMessage(message)) if __name__ == "__main__": while True: state = p.receive() recieve_from_software() send_to_hardware() sendMessage(encodeMessage("ACK")) if state: p.send(p.RX(0,0,0,0,0).raw())