Esempio n. 1
0
    def monitor_connections(self):
        reactor.callLater(config.user.monitor_connections_interval,
                          self.monitor_connections)

        if len(self._peer_connections) == 0:
            logger.warning('No Connected Peer Found')
            known_peers = self._xrd_node.peer_manager.load_known_peers()
            self._peer_q.extend(known_peers)

        connected_peers_set = set()
        for conn_protocol in self._peer_connections:
            connected_peers_set.add(conn_protocol.peer.full_address)

        for peer_item in config.user.peer_list:
            peer_metadata = IPMetadata.from_full_address(peer_item)
            if peer_metadata.full_address in self._peer_q:
                self._peer_q.remove(peer_metadata.full_address)
            if peer_metadata.full_address not in connected_peers_set:
                self.connect_peer([peer_metadata.full_address])

        if len(self._peer_connections) >= config.user.max_peers_limit:
            return

        if len(self._peer_q) == 0:
            return

        peer_address_list = []
        max_length = min(10, config.user.max_peers_limit)
        while len(self._peer_q) > 0 and len(peer_address_list) != max_length:
            peer_address_list.append(self._peer_q.pop(0))

        self.connect_peer(peer_address_list)
Esempio n. 2
0
    def connect_peer(self, full_address_list):
        for full_address in full_address_list:
            try:
                addr = IPMetadata.from_full_address(full_address)

                connected_peers = self.get_connected_peer_addrs()
                should_connect = addr.full_address not in connected_peers

                if should_connect:
                    reactor.connectTCP(addr.ip, addr.port, self)

            except Exception as e:
                logger.warning("Could not connect to %s - %s", full_address,
                               str(e))
Esempio n. 3
0
 def test_global_2(self):
     addr = IPMetadata.from_full_address('123.123.123.1:9000',
                                         check_global=True)
     self.assertEquals('123.123.123.1', addr.ip)
     self.assertEquals(9000, addr.port)
Esempio n. 4
0
 def test_global_1(self):
     with self.assertRaisesRegexp(ValueError,
                                  'Local Peer IP Found 192.168.0.1:9000'):
         IPMetadata.from_full_address('192.168.0.1:9000', check_global=True)
Esempio n. 5
0
 def test_wrong_port_3(self):
     with self.assertRaisesRegexp(ValueError,
                                  'Invalid Peer Port 192.168.0.1:-1'):
         IPMetadata.from_full_address('192.168.0.1:-1')
Esempio n. 6
0
 def test_invalid_2(self):
     with self.assertRaisesRegexp(AddressValueError,
                                  'Expected 4 octets in \'abc\''):
         IPMetadata.from_full_address('abc')
Esempio n. 7
0
 def test_invalid_1(self):
     with self.assertRaisesRegexp(AddressValueError,
                                  'Address cannot be empty'):
         IPMetadata.from_full_address('')
Esempio n. 8
0
 def test_basic_3(self):
     addr = IPMetadata.from_full_address('192.168.0.1')
     self.assertEquals('192.168.0.1', addr.ip)
     self.assertEquals(config.user.p2p_local_port, addr.port)
Esempio n. 9
0
 def test_basic_2(self):
     addr = IPMetadata.from_full_address('192.168.0.1:1234')
     self.assertEquals('192.168.0.1', addr.ip)
     self.assertEquals(1234, addr.port)