More work on notification
This commit is contained in:
parent
d2a0bd4d65
commit
2fdedc751e
@ -223,14 +223,14 @@ class GroupChat(Chat):
|
|||||||
self._group.join()
|
self._group.join()
|
||||||
|
|
||||||
name = self._group.get_owner().get_service_name()
|
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 = StreamReader(self._group, service)
|
||||||
self._buddy_reader.set_listener(self._buddy_recv_message)
|
self._buddy_reader.set_listener(self._buddy_recv_message)
|
||||||
service.register(self._group)
|
service.register(self._group)
|
||||||
|
|
||||||
service = Service(name, GROUP_CHAT_SERVICE_TYPE,
|
service = Service(name, GROUP_CHAT_SERVICE_TYPE,
|
||||||
GROUP_CHAT_SERVICE_ADDRESS,
|
GROUP_CHAT_SERVICE_PORT,
|
||||||
GROUP_CHAT_SERVICE_PORT, True)
|
GROUP_CHAT_SERVICE_ADDRESS)
|
||||||
self._group.add_service(service)
|
self._group.add_service(service)
|
||||||
|
|
||||||
self._buddy_reader = StreamReader(self._group, service)
|
self._buddy_reader = StreamReader(self._group, service)
|
||||||
|
@ -26,7 +26,7 @@ class Owner(Buddy):
|
|||||||
nick = "n00b"
|
nick = "n00b"
|
||||||
|
|
||||||
service = Service(nick, PRESENCE_SERVICE_TYPE,
|
service = Service(nick, PRESENCE_SERVICE_TYPE,
|
||||||
'', PRESENCE_SERVICE_PORT)
|
PRESENCE_SERVICE_PORT)
|
||||||
|
|
||||||
Buddy.__init__(self, service, nick)
|
Buddy.__init__(self, service, nick)
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import avahi
|
||||||
|
|
||||||
from Buddy import Buddy
|
from Buddy import Buddy
|
||||||
from Buddy import Owner
|
from Buddy import Owner
|
||||||
from Buddy import PRESENCE_SERVICE_TYPE
|
from Buddy import PRESENCE_SERVICE_TYPE
|
||||||
@ -103,7 +105,13 @@ class LocalGroup(Group):
|
|||||||
|
|
||||||
def _on_service_resolved(self, interface, protocol, name, stype, domain,
|
def _on_service_resolved(self, interface, protocol, name, stype, domain,
|
||||||
host, aprotocol, address, port, txt, flags):
|
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:
|
if stype == PRESENCE_SERVICE_TYPE:
|
||||||
self._add_buddy(Buddy(service, name))
|
self._add_buddy(Buddy(service, name))
|
||||||
elif stype.startswith("_olpc"):
|
elif stype.startswith("_olpc"):
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
from Service import Service
|
|
||||||
from sugar.p2p.Notifier import Notifier
|
from sugar.p2p.Notifier import Notifier
|
||||||
import network
|
import network
|
||||||
|
|
||||||
class NotificationListener:
|
class NotificationListener:
|
||||||
def __init__(self, group, name):
|
def __init__(self, group, name):
|
||||||
service = group.get_service(name, Notifier.TYPE)
|
service = group.get_service(name, Notifier.TYPE)
|
||||||
print service.get_address()
|
|
||||||
print service.get_port()
|
|
||||||
server = network.GroupServer(service.get_address(),
|
server = network.GroupServer(service.get_address(),
|
||||||
service.get_port(),
|
service.get_port(),
|
||||||
self._recv_multicast)
|
self._recv_multicast)
|
||||||
@ -18,6 +15,5 @@ class NotificationListener:
|
|||||||
self._listeners.add(listener)
|
self._listeners.add(listener)
|
||||||
|
|
||||||
def _recv_multicast(self, msg):
|
def _recv_multicast(self, msg):
|
||||||
print 'Got message ' + msg
|
|
||||||
for listener in self._listeners:
|
for listener in self._listeners:
|
||||||
listener(msg)
|
listener(msg)
|
||||||
|
@ -7,11 +7,10 @@ class Notifier:
|
|||||||
PORT = 6300
|
PORT = 6300
|
||||||
|
|
||||||
def __init__(self, group, name):
|
def __init__(self, group, name):
|
||||||
service = Service(name, Notifier.TYPE, Notifier.ADDRESS,
|
service = Service(name, Notifier.TYPE, Notifier.PORT, Notifier.ADDRESS)
|
||||||
Notifier.PORT, True)
|
|
||||||
service.register(group)
|
service.register(group)
|
||||||
|
|
||||||
address = service.get_address()
|
address = service.get_multicast_group()
|
||||||
port = service.get_port()
|
port = service.get_port()
|
||||||
self._client = network.GroupClient(address, port)
|
self._client = network.GroupClient(address, port)
|
||||||
|
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
import presence
|
import presence
|
||||||
|
|
||||||
class Service(object):
|
class Service(object):
|
||||||
def __init__(self, name, stype, address, port, multicast=False):
|
def __init__(self, name, stype, port, mgroup=None):
|
||||||
self._name = name
|
self._name = name
|
||||||
self._stype = stype
|
self._stype = stype
|
||||||
self._address = str(address)
|
|
||||||
self._port = int(port)
|
self._port = int(port)
|
||||||
self._multicast = multicast
|
self._mgroup = mgroup
|
||||||
|
|
||||||
def get_name(self):
|
def get_name(self):
|
||||||
return self._name
|
return self._name
|
||||||
@ -23,10 +22,16 @@ class Service(object):
|
|||||||
def set_port(self, port):
|
def set_port(self, port):
|
||||||
self._port = port
|
self._port = port
|
||||||
|
|
||||||
|
def get_multicast_group(self):
|
||||||
|
return self._mgroup
|
||||||
|
|
||||||
def is_multicast(self):
|
def is_multicast(self):
|
||||||
return self._multicast
|
return self._mgroup != None
|
||||||
|
|
||||||
def register(self, group):
|
def register(self, group):
|
||||||
pannounce = presence.PresenceAnnounce()
|
pannounce = presence.PresenceAnnounce()
|
||||||
pannounce.register_service(self._address, self._name,
|
if self._mgroup:
|
||||||
self._port, self._stype)
|
pannounce.register_service(self._name, self._port, self._stype,
|
||||||
|
multicast = self._mgroup)
|
||||||
|
else:
|
||||||
|
pannounce.register_service(self._name, self._port, self._stype)
|
||||||
|
@ -37,7 +37,7 @@ class LocalModel(AbstractModel):
|
|||||||
self._notifier.notify(key)
|
self._notifier.notify(key)
|
||||||
|
|
||||||
def _setup_service(self):
|
def _setup_service(self):
|
||||||
service = Service(self._model_id, LocalModel.SERVICE_TYPE, '',
|
service = Service(self._model_id, LocalModel.SERVICE_TYPE,
|
||||||
LocalModel.SERVICE_PORT)
|
LocalModel.SERVICE_PORT)
|
||||||
self._setup_server(service)
|
self._setup_server(service)
|
||||||
service.register(self._group)
|
service.register(self._group)
|
||||||
|
@ -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.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
|
||||||
self._hostname = None
|
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)
|
g = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP)
|
||||||
if rs_name is None:
|
if rs_name is None:
|
||||||
if self._hostname is None:
|
if self._hostname is None:
|
||||||
|
Loading…
Reference in New Issue
Block a user