From 6f3c1ac74063ec82bae6bbe34e643519077085cb Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Thu, 11 May 2006 22:31:02 -0400 Subject: [PATCH] Make StreamReader search for a free port --- p2p/Service.py | 3 +++ p2p/StreamReader.py | 14 ++++++++++++-- p2p/presence.py | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/p2p/Service.py b/p2p/Service.py index 87df42b7..50bbf86c 100644 --- a/p2p/Service.py +++ b/p2p/Service.py @@ -19,6 +19,9 @@ class Service(object): def get_port(self): return self._port + + def set_port(self, port): + self._port = port def is_multicast(self): return self._multicast diff --git a/p2p/StreamReader.py b/p2p/StreamReader.py index f8a45cb9..125aa70c 100644 --- a/p2p/StreamReader.py +++ b/p2p/StreamReader.py @@ -29,8 +29,18 @@ class StreamReader: server.start() def _setup_unicast(self): - p2p_server = GlibXMLRPCServer(("", self._service.get_port())) - p2p_server.register_instance(StreamReaderRequestHandler(self)) + started = False + tries = 10 + port = self._service.get_port() + while not started and tries > 0: + try: + p2p_server = GlibXMLRPCServer(("", port)) + p2p_server.register_instance(StreamReaderRequestHandler(self)) + started = True + except: + port = port + 1 + tries = tries - 1 + self._service.set_port(port) def _recv_multicast(self, msg): [ nick_name, data ] = msg['data'].split(" |**| ", 2) diff --git a/p2p/presence.py b/p2p/presence.py index 58057d9b..e16fc928 100644 --- a/p2p/presence.py +++ b/p2p/presence.py @@ -31,7 +31,7 @@ class PresenceDiscovery(object): self.server.ResolveService(int(interface), int(protocol), name, stype, domain, avahi.PROTO_UNSPEC, dbus.UInt32(0), reply_handler=reply_handler, error_handler=error_handler) def new_service(self, interface, protocol, name, stype, domain, flags): - print "Found service '%s' (%d) of type '%s' in domain '%s' on %i.%i." % (name, flags, stype, domain, interface, protocol) +# print "Found service '%s' (%d) of type '%s' in domain '%s' on %i.%i." % (name, flags, stype, domain, interface, protocol) for listener in self._service_listeners: listener(ACTION_SERVICE_NEW, interface, protocol, name, stype, domain, flags)