diff --git a/sugar/chat/chat.py b/sugar/chat/chat.py index fef2c838..4d595f71 100755 --- a/sugar/chat/chat.py +++ b/sugar/chat/chat.py @@ -223,14 +223,14 @@ class GroupChat(Chat): self._group.join() name = self._group.get_owner().get_service_name() - service = Service(name, CHAT_SERVICE_TYPE, '', CHAT_SERVICE_PORT) + service = Service(name, CHAT_SERVICE_TYPE, CHAT_SERVICE_PORT) self._buddy_reader = StreamReader(self._group, service) self._buddy_reader.set_listener(self._buddy_recv_message) service.register(self._group) service = Service(name, GROUP_CHAT_SERVICE_TYPE, - GROUP_CHAT_SERVICE_ADDRESS, - GROUP_CHAT_SERVICE_PORT, True) + GROUP_CHAT_SERVICE_PORT, + GROUP_CHAT_SERVICE_ADDRESS) self._group.add_service(service) self._buddy_reader = StreamReader(self._group, service) diff --git a/sugar/p2p/Buddy.py b/sugar/p2p/Buddy.py index 8263a8e3..ad6b8aab 100644 --- a/sugar/p2p/Buddy.py +++ b/sugar/p2p/Buddy.py @@ -26,7 +26,7 @@ class Owner(Buddy): nick = "n00b" service = Service(nick, PRESENCE_SERVICE_TYPE, - '', PRESENCE_SERVICE_PORT) + PRESENCE_SERVICE_PORT) Buddy.__init__(self, service, nick) diff --git a/sugar/p2p/Group.py b/sugar/p2p/Group.py index faa6041c..4ac2518f 100644 --- a/sugar/p2p/Group.py +++ b/sugar/p2p/Group.py @@ -1,3 +1,5 @@ +import avahi + from Buddy import Buddy from Buddy import Owner from Buddy import PRESENCE_SERVICE_TYPE @@ -103,7 +105,13 @@ class LocalGroup(Group): def _on_service_resolved(self, interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags): - service = Service(name, stype, address, port) + multicast = None + for prop in avahi.txt_array_to_string_array(txt): + (key, value) = prop.split('=') + if key == 'multicast': + multicast = value + + service = Service(name, stype, port, multicast) if stype == PRESENCE_SERVICE_TYPE: self._add_buddy(Buddy(service, name)) elif stype.startswith("_olpc"): diff --git a/sugar/p2p/NotificationListener.py b/sugar/p2p/NotificationListener.py index 13ee4736..dd48acac 100644 --- a/sugar/p2p/NotificationListener.py +++ b/sugar/p2p/NotificationListener.py @@ -1,12 +1,9 @@ -from Service import Service from sugar.p2p.Notifier import Notifier import network class NotificationListener: def __init__(self, group, name): service = group.get_service(name, Notifier.TYPE) - print service.get_address() - print service.get_port() server = network.GroupServer(service.get_address(), service.get_port(), self._recv_multicast) @@ -18,6 +15,5 @@ class NotificationListener: self._listeners.add(listener) def _recv_multicast(self, msg): - print 'Got message ' + msg for listener in self._listeners: listener(msg) diff --git a/sugar/p2p/Notifier.py b/sugar/p2p/Notifier.py index e4d3847c..50d96b4b 100644 --- a/sugar/p2p/Notifier.py +++ b/sugar/p2p/Notifier.py @@ -7,11 +7,10 @@ class Notifier: PORT = 6300 def __init__(self, group, name): - service = Service(name, Notifier.TYPE, Notifier.ADDRESS, - Notifier.PORT, True) + service = Service(name, Notifier.TYPE, Notifier.PORT, Notifier.ADDRESS) service.register(group) - address = service.get_address() + address = service.get_multicast_group() port = service.get_port() self._client = network.GroupClient(address, port) diff --git a/sugar/p2p/Service.py b/sugar/p2p/Service.py index 9371b05e..7c864dff 100644 --- a/sugar/p2p/Service.py +++ b/sugar/p2p/Service.py @@ -1,12 +1,11 @@ import presence class Service(object): - def __init__(self, name, stype, address, port, multicast=False): + def __init__(self, name, stype, port, mgroup=None): self._name = name self._stype = stype - self._address = str(address) self._port = int(port) - self._multicast = multicast + self._mgroup = mgroup def get_name(self): return self._name @@ -23,10 +22,16 @@ class Service(object): def set_port(self, port): self._port = port + def get_multicast_group(self): + return self._mgroup + def is_multicast(self): - return self._multicast + return self._mgroup != None def register(self, group): pannounce = presence.PresenceAnnounce() - pannounce.register_service(self._address, self._name, - self._port, self._stype) + if self._mgroup: + pannounce.register_service(self._name, self._port, self._stype, + multicast = self._mgroup) + else: + pannounce.register_service(self._name, self._port, self._stype) diff --git a/sugar/p2p/model/LocalModel.py b/sugar/p2p/model/LocalModel.py index 8a2b3308..59ed8708 100644 --- a/sugar/p2p/model/LocalModel.py +++ b/sugar/p2p/model/LocalModel.py @@ -37,7 +37,7 @@ class LocalModel(AbstractModel): self._notifier.notify(key) def _setup_service(self): - service = Service(self._model_id, LocalModel.SERVICE_TYPE, '', + service = Service(self._model_id, LocalModel.SERVICE_TYPE, LocalModel.SERVICE_PORT) self._setup_server(service) service.register(self._group) diff --git a/sugar/p2p/presence.py b/sugar/p2p/presence.py index 4edfc38c..4c5c086f 100644 --- a/sugar/p2p/presence.py +++ b/sugar/p2p/presence.py @@ -77,7 +77,7 @@ class PresenceAnnounce(object): self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER) self._hostname = None - def register_service(self, rs_address, rs_name, rs_port, rs_service, **kwargs): + def register_service(self, rs_name, rs_port, rs_service, **kwargs): g = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP) if rs_name is None: if self._hostname is None: