Exemplo n.º 1
0
    def start_receiving(self):
        """
        This function starts by broadcasting a BPDU, then runs the main loop
        for a Bridge that receives and sends messages and BPDUs to specific
        ports, and takes care of broadcasting BPDUs and messages to all ports
        """
        start_time = time.time()
        self._broadcast_BPDU()

        while True:
            self._initial_port_checks()

            # is it time to send a new BPDU?
            if int(round((time.time() - start_time) * 1000)) > 500:
                self._broadcast_BPDU()
                start_time = time.time()

            ready, ignore, ignore2 = select.select([port.socket for port in self.ports], [], [], 0.1)
            for x in ready:
                port = self.ports[self.sockets[x]]

                if not port.BPDU_list:
                    # if port not designated, print out designated
                    if not port.designated:
                        self._print_designated_port(port.port_id)
                        self.forwarding_table = ForwardingTable()
                    port.designated = True

                self._enable_or_disable(port)

                message = port.socket.recv(RECEIVE_SIZE)
                message_json = json.loads(message)

                if message_json['type'] == 'bpdu':
                    bpdu_in = BPDU(message_json['source'], message_json['dest'], message_json['message']['id'], message_json['message']['root'], message_json['message']['cost'])# + 1)
                    self._received_bpdu_logic(bpdu_in, port)

                elif message_json['type'] == 'data':
                    data_in = create_DataMessage_from_json(message)
                    self._received_data_logic(data_in, port, message)
Exemplo n.º 2
0
 def test_create_DataMessage_from_json(self):
     testDataMessage = create_DataMessage_from_json(self.json_Data)
     self.assertEquals(testDataMessage.source, '02a1')
     self.assertEquals(testDataMessage.dest, '9xa2')
     self.assertEquals(testDataMessage.type, 'data')
     self.assertEquals(testDataMessage.id, '92b4')