Esempio n. 1
0
    def get_peer(self, peer_id, tracker=None, boot_nodes=None, port=None):

        if isinstance(tracker, tuple):
            port = port or 8150
            protocol = PeerProtocol()
            reactor.listenUDP(port, protocol)

            d = defer.Deferred()

            def stop_listening_done(_, result):
                success, data = result
                if success and data:
                    d.callback(data)
                else:
                    d.callback(None)

            def get_peer_done(result):
                protocol.transport.stopListening().addCallback(
                    stop_listening_done, result)

            protocol.get_peer(peer_id, tracker).addCallback(get_peer_done)
            return d

        elif isinstance(boot_nodes, list):
            port = port or 8250

            peer = KadServer()

            d = defer.Deferred()

            def get_key_done(future):
                value = future.result()
                peer.stop()
                if value:
                    addr = derive_proxy_data(value)
                    d.callback(tuple(addr.split(',')))
                else:
                    d.callback(None)

            def bootstrap_done(_):
                asyncio.ensure_future(
                    peer.get(peer_id)).add_done_callback(get_key_done)

            def listen_done(_):
                asyncio.ensure_future(peer.bootstrap(
                    boot_nodes)).add_done_callback(bootstrap_done)

            peer.listen(port).addCallback(listen_done)

            return d

        else:
            logger.error("wrong tracker/boot nodes")
Esempio n. 2
0
    def join_centra_net(self, port=None, tracker=None):

        if tracker and not self.service_port:
            logger.error("proxy service not started")
            return

        port = port or config.proxy.server_peer_port
        protocol = PeerProtocol(peer_ip=self.ip,
                                peer_id=self.peer_id,
                                peer_info=self.service_port)
        reactor.listenUDP(port, protocol)

        def refresh():
            protocol.refresh_peers()

        if tracker:
            protocol.bootstrap(tracker)
        else:
            LoopingCall(refresh).start(5)
Esempio n. 3
0
    def pick_peer(self):
        port = config.proxy.server_slave_port + 2
        self.trans = reactor.listenUDP(port, self.protocol)

        def stop_listening(response):
            self.stop()
            return response

        d = self.protocol.pick_peer(self.tracker).addCallback(stop_listening)

        return d
Esempio n. 4
0
    def pick_peer(self, tracker, port=None, sysconf=None):
        port = port or 8150
        protocol = PeerProtocol()
        reactor.listenUDP(port, protocol)

        d = defer.Deferred()

        def stop_listening_done(_, result):
            success, data = result
            if success and data:
                d.callback(data)
            else:
                d.callback(None)

        def pick_peer_done(result):
            protocol.transport.stopListening().addCallback(
                stop_listening_done, result)

        protocol.pick_peer(tracker, sysconf).addCallback(pick_peer_done)
        return d
Esempio n. 5
0
 def run(self):
     port = config.proxy.server_slave_port
     self.trans = reactor.listenUDP(port, self.protocol)
     return self.protocol.bootstrap(get_proxy_id(), self.tracker)
Esempio n. 6
0
 def run(self):
     _, port = config.proxy.tracker.split(':')
     port = int(port)
     self.trans = reactor.listenUDP(port, self.protocol)
     self.refresh_loop = LoopingCall(self.protocol.refresh_peers).start(60)