Esempio n. 1
0
 def get_local_server(self, remote_ip):
     """return local server bound to owner of protocol"""
     if self.local_servers.has_key(remote_ip):
         # is server running
         return self.local_servers[remote_ip]
     else:
         # if not, spawn it
         local_port = get_free_port()
         new_server = PeerServerFactory(self.manager, remote_ip, local_port)
         self.local_servers[remote_ip] = new_server
         new_server.start_listening()
         return new_server
    def start_listening(self, conf_file=None):
        """launch well-known server of profiles"""
        self.local_port = get_free_port()
        # define callbacks on public_ip discovery
        def _stun_succeed(address):
            """Discovery succeeded"""
            self.public_ip, self.public_port = address
            display_status(_("STUN discovery found address %s:%d" % (self.public_ip, self.public_port)))
            self.listener = reactor.listenTCP(self.local_port, self)
            return True

        def _stun_fail(failure):
            self.public_ip, self.public_port = self.local_ip, self.local_port
            display_warning(_("STUN failed:", failure.getErrorMessage()), title=_("File server Error"))
            self.listener = reactor.listenTCP(self.local_port, self)
            return False

        # get public ip
        deferred = stun.DiscoverAddress(self.local_port, reactor, build_params(conf_file))
        deferred.addCallback(_stun_succeed)
        deferred.addErrback(_stun_fail)
        return deferred