Ejemplo n.º 1
0
    def send_ping(self):
        request_bytes = CommHeader(msgtype="ping_request").get_bytes()

        # UDP requests expect a node ID
        for _ in range(4):
            request_bytes.append(0x00)

        self.sock.sendto(request_bytes, ('255.255.255.255', self.port))
Ejemplo n.º 2
0
    def send_request(self, node_id, request):
        # just drop request for unknown clients for now
        if node_id in self.nodes.keys():
            request_bytes = CommHeader(msgtype=request).get_bytes()
            request_bytes.extend(node_id.to_bytes(4, byteorder="big"))

            self.sock.sendto(
                request_bytes,
                (self.nodes[node_id].address, self.nodes[node_id].port))
Ejemplo n.º 3
0
    def process_received_packets(self):
        for packet in self._packet_handler.available_packets:
            if packet["msgtype"] == "name_request":
                print("SocketClient: name request received")
                payload = NameReplyPayload(name=self.name)
                header = CommHeader(msgtype="name_reply",
                                    payload_len=len(payload.get_bytes()))

                self.send_bytes(header.get_bytes() + payload.get_bytes())

                self._packet_handler.available_packets.remove(packet)
Ejemplo n.º 4
0
    def send_state_reply(self):
        button_state = self.window.get_button_state()
        slider_state = self.window.get_slider_state()
        self.window.clear_button_state()

        payload = StateReplyPayload(button_state=button_state, slider_state=slider_state).get_bytes()
        header = CommHeader(msgtype="state_reply", payload_len=len(payload)).get_bytes()

        self.socket.send_bytes(header + payload)
Ejemplo n.º 5
0
    def handle_incoming(self):
        readable, writeable, errored = select.select([self.sock], [self.sock],
                                                     [self.sock], self.timeout)

        # only handle incoming if there is incoming to handle
        if self.sock not in readable:
            return

        incoming, addr = self.sock.recvfrom(4096)

        if len(incoming) == 0:
            return

        remote_ip = addr[0]
        remote_port = addr[1]

        if remote_ip == self.ip:
            # skip broadcast messages from ourself
            return

        hdr = CommHeader(bytes=incoming[0:CommHeader.header_len])
        hdr_dict = hdr.get_dict()
        incoming = incoming[CommHeader.header_len:]

        node_id = int.from_bytes(incoming[0:4], byteorder='big', signed=False)
        node_str = '{}'.format(f'0x{node_id:08x}')
        incoming = incoming[4:]

        if node_id in self.nodes:
            self.nodes[node_id].last_seen = time.time()

        if hdr_dict["msgtype"] == "ping_reply":
            if node_id not in self.nodes.keys(
            ) or self.nodes[node_id].address != remote_ip:
                print(f"Node {node_str} discovered at ip {remote_ip}")
                self.nodes[node_id] = NodeInfo(remote_ip, remote_port)

        else:
            return
            print(f"{remote_ip} : {self.ip}")
            print(f"received {len(incoming)} bytes: {incoming}")
            self.nodes[node_id].inbound_packet_buffer.append(incoming)
Ejemplo n.º 6
0
    def send(self, node_id, byte_values):
        byte_values = [max(0, min(255, x)) for x in byte_values]

        packet = CommHeader(payload_len=len(byte_values),
                            msgtype="frame_update").get_bytes()
        packet.extend(node_id.to_bytes(4, byteorder="big"))
        packet.extend(byte_values)

        self.udp_handler.send_bytes(node_id, packet)
Ejemplo n.º 7
0
 def send_request(self, client_name, request):
     # just drop request for unknown clients for now
     for client in self._client_dict.values():
         if client.name == client_name:
             client.send_bytes(CommHeader(msgtype=request).get_bytes())
Ejemplo n.º 8
0
 def send_request(self, request_name):
     bytes = CommHeader(msgtype=request_name).get_bytes()
     self.send_bytes(bytes)