示例#1
0
    def get_socket_addresses(self, node_info, prv_port=None, pub_port=None):
        """ Change node info into tcp addresses. Adds a suggested address.
        :param Node node_info: node information
        :param prv_port: private port that should be used
        :param pub_port: public port that should be used
        :return:
        """
        prv_port = prv_port or node_info.p2p_prv_port
        pub_port = pub_port or node_info.p2p_pub_port

        socket_addresses = super().get_socket_addresses(node_info=node_info,
                                                        prv_port=prv_port,
                                                        pub_port=pub_port)

        address = self.suggested_address.get(node_info.key, None)
        if not address:
            return socket_addresses

        if self._is_address_valid(address, prv_port):
            socket_address = tcpnetwork.SocketAddress(address, prv_port)
            self._prepend_address(socket_addresses, socket_address)

        if self._is_address_valid(address, pub_port):
            socket_address = tcpnetwork.SocketAddress(address, pub_port)
            self._prepend_address(socket_addresses, socket_address)

        return socket_addresses[:MAX_CONNECT_SOCKET_ADDRESSES]
示例#2
0
    def change_config(self, config_desc):
        """ Change configuration descriptor.
        If node_name was changed, send hello to all peers to update node_name.
        If listening port is changed, than stop listening on old port and start
        listening on a new one. If seed address is changed, connect to a new
        seed.
        Change configuration for resource server.
        :param ClientConfigDescriptor config_desc: new config descriptor
        """
        tcpserver.TCPServer.change_config(self, config_desc)
        self.node_name = config_desc.node_name

        self.last_message_time_threshold = self.config_desc.p2p_session_timeout

        for peer in list(self.peers.values()):
            if (peer.port == self.config_desc.seed_port
                    and peer.address == self.config_desc.seed_host):
                return

        if self.config_desc.seed_host and self.config_desc.seed_port:
            try:
                socket_address = tcpnetwork.SocketAddress(
                    self.config_desc.seed_host, self.config_desc.seed_port)
                self.connect(socket_address)
            except ipaddress.AddressValueError as err:
                logger.error('Invalid seed address: ' + str(err))

        if self.resource_server:
            self.resource_server.change_config(config_desc)
示例#3
0
    def connect_to_seeds(self):
        self.last_time_tried_connect_with_seed = time.time()
        if not self.connect_to_known_hosts:
            return

        for ip_address, port in self.seeds:
            try:
                socket_address = tcpnetwork.SocketAddress(ip_address, port)
                self.connect(socket_address)
            except Exception as exc:
                logger.error("Cannot connect to seed {}:{}: {}".format(
                    ip_address, port, exc))
示例#4
0
    def connect_to_seeds(self):
        self.last_time_tried_connect_with_seed = time.time()
        if not self.connect_to_known_hosts:
            return

        for _ in range(len(self.seeds)):
            ip_address, port = self._get_next_random_seed()
            try:
                socket_address = tcpnetwork.SocketAddress(ip_address, port)
                self.connect(socket_address)
            except Exception as exc:
                logger.error("Cannot connect to seed %s:%s: %s", ip_address,
                             port, exc)
                continue
            break  # connected
示例#5
0
    def key_changed(self):
        """React to the fact that key id has been changed. Drop all
           connections with peer,
        restart peer keeper and connect to the network with new key id.
        """
        self.peer_keeper.restart(self.keys_auth.get_key_id())
        for p in self.peers.values():
            p.dropped()

        try:
            socket_address = tcpnetwork.SocketAddress(
                self.config_desc.seed_host, self.config_desc.seed_port)
            self.connect(socket_address)
        except AddressValueError, err:
            logger.error("Invalid seed address: " + err.message)
示例#6
0
    def connect_to_network(self):
        self.connect_to_seeds()
        if not self.connect_to_known_hosts:
            return

        for host in KnownHosts.select().where(
                KnownHosts.is_seed == False):  # noqa
            ip_address = host.ip_address
            port = host.port

            logger.debug("Connecting to {}:{}".format(ip_address, port))
            try:
                socket_address = tcpnetwork.SocketAddress(ip_address, port)
                self.connect(socket_address)
            except Exception as exc:
                logger.error("Cannot connect to host {}:{}: {}".format(
                    ip_address, port, exc))
示例#7
0
 def get_socket_addresses(self, node_info, port, key_id):
     """ Change node info into tcp addresses. Add suggested address
     :param Node node_info: node information
     :param int port: port that should be used
     :param key_id: node's public key
     :return:
     """
     socket_addresses = tcpserver.PendingConnectionsServer\
         .get_socket_addresses(
             self,
             node_info,
             port,
             key_id
         )
     addr = self.suggested_address.get(key_id, None)
     if addr:
         socket_addresses += [tcpnetwork.SocketAddress(addr, port)]
     return socket_addresses
示例#8
0
    def connect_to_network(self):
        # pylint: disable=singleton-comparison
        self.connect_to_seeds()
        if not self.connect_to_known_hosts:
            return

        for host in KnownHosts.select() \
                .where(KnownHosts.is_seed == False)\
                .limit(self.config_desc.opt_peer_num):  # noqa

            ip_address = host.ip_address
            port = host.port

            logger.debug("Connecting to {}:{}".format(ip_address, port))
            try:
                socket_address = tcpnetwork.SocketAddress(ip_address, port)
                self.connect(socket_address)
            except Exception as exc:
                logger.error("Cannot connect to host {}:{}: {}".format(
                    ip_address, port, exc))