more methods in roster_zeroconf, on_new_service when service is resolved

This commit is contained in:
Stefan Bethge 2006-06-02 18:46:52 +00:00
parent cf5ad66259
commit 40fc5202a5
3 changed files with 106 additions and 62 deletions

View File

@ -190,12 +190,12 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
def _on_new_service(self,jid):
self.roster.setItem(jid)
self.dispatch('ROSTER', self.roster)
#self.dispatch('ROSTER', self.roster)
self.dispatch('NOTIFY', (jid, self.roster.getStatus(jid), '', 'Gajim', 0, None, 0))
def _on_remove_service(self,jid):
# roster.delItem(jid)
# self.dispatch('ROSTER', roster)
pass
self.roster.delItem(jid)
#self.dispatch('NOTIFY', self, )
def connect(self, data = None, show = 'online'):
if self.connection:
@ -204,6 +204,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.zeroconf.connect()
self.connection = client_zeroconf.ClientZeroconf(self.zeroconf)
self.roster = self.connection.getRoster()
self.dispatch('ROSTER', self.roster)
self.connected = STATUS_LIST.index(show)
def connect_and_init(self, show, msg, signed):
@ -254,25 +255,9 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.zeroconf.update_txt(txt)
self.dispatch('STATUS', show)
'''
def _on_disconnected(self):
self.dispatch('STATUS', 'offline')
self.disconnect()
'''
def get_status(self):
return STATUS_LIST[self.connected]
def send_motd(self, jid, subject = '', msg = ''):
'''
if not self.connection:
return
msg_iq = common.xmpp.Message(to = jid, body = msg, subject = subject)
self.connection.send(msg_iq)
'''
pass
def send_message(self, jid, msg, keyID, type = 'chat', subject='',
chatstate = None, msg_id = None, composing_jep = None, resource = None):
'''
@ -456,12 +441,9 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
'''
def update_contact(self, jid, name, groups):
'''
# update roster item on jabber server
if self.connection:
self.connection.getRoster().setItem(jid = jid, name = name,
groups = groups)
'''
def new_account(self, name, config, sync = False):
'''
@ -567,9 +549,8 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
'''
pass
'''
def get_metacontacts(self):
'''
# Get metacontacts list from storage as described in JEP 0049
if not self.connection:
return
@ -578,6 +559,8 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
iq2.addChild(name='storage', namespace='storage:metacontacts')
self.connection.send(iq)
'''
pass
'''
def store_metacontacts(self, tags_list):
# Send meta contacts to the storage namespace

View File

@ -15,15 +15,48 @@ class Roster:
if self._data.has_key(jid):
return self._data[jid]
def setItem(self, item):
print 'setItem in Roster: jid: %s' % item
# data is maybe not already resolved
# what data is expected here?
self._data[item] = self.zeroconf.get_contact(item)
def setItem(self, jid, name = '', groups = ''):
print 'setItem %s in Roster' % jid
(service_jid, domain, interface, protocol, host, address, port, txt) \
= self.zeroconf.get_contact(jid)
def __getitem__(self,item):
self._data[jid]={}
self._data[jid]['name']=jid[:jid.find('@')]
self._data[jid]['ask'] = 'no' #?
self._data[jid]['subscription'] = 'both'
self._data[jid]['groups'] = []
self._data[jid]['resources'] = {}
self._data[jid]['address'] = address
self._data[jid]['host'] = host
self._data[jid]['port'] = port
self._data[jid]['txt'] = txt
txt_dict = self.zeroconf.txt_array_to_dict(txt)
if txt_dict.has_key('status'):
status = txt_dict['status']
else:
status = ''
if status == 'avail': status = 'online'
self._data[jid]['status'] = status
self._data[jid]['show'] = status
print self._data[jid]
def delItem(self, jid):
print 'delItem %s in Roster' % jid
if self._data.has_key(jid):
del self._data[jid]
def __getitem__(self,jid):
print '__getitem__ in Roster'
return self._data[item]
return self._data[jid]
def getItems(self):
print 'getItems in Roster'
# Return list of all [bare] JIDs that the roster is currently tracks.
return self._data.keys()
def keys(self):
print 'keys in Roster'
return self._data.keys()
def getRaw(self):
print 'getRaw in Roster'
@ -31,11 +64,42 @@ class Roster:
def getResources(self, jid):
print 'getResources(%s) in Roster' % jid
# return self
return {}
'''
delItem(jid)
getStatus(jid)
getPriority(jid)
getShow(jid)
'''
def getStatus(self, jid):
print 'getStatus %s in Roster' % jid
txt = self._data[jid]['txt']
txt_dict = self.zeroconf.txt_array_to_dict(txt)
if txt_dict.has_key('status'):
status = txt_dict['status']
else:
status = ''
if status == 'avail' or status == '':
return 'online'
else:
return status
def getShow(self, jid):
print 'getShow in Roster'
return getStatus(jid)
def getPriority(jid):
return 5
def getSubscription(self,jid):
print 'getSubscription in Roster'
return 'both'
def Subscribe(self,jid):
pass
def Unsubscribe(self,jid):
pass
def Authorize(self,jid):
pass
def Unauthorize(self,jid):
pass

View File

@ -41,7 +41,6 @@ class Zeroconf:
self.server.ResolveService( int(interface), int(protocol), name, stype, \
domain, avahi.PROTO_UNSPEC, dbus.UInt32(0), \
reply_handler=self.service_resolved_callback, error_handler=self.print_error_callback)
self.new_serviceCB(name)
def remove_service_callback(self, interface, protocol, name, stype, domain, flags):
print "Service '%s' in domain '%s' on %i.%i disappeared." % (name, domain, interface, protocol)
@ -77,20 +76,15 @@ class Zeroconf:
return l
def service_resolved_callback(self, interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags):
self.contacts[name] = (name, domain, interface, protocol, host, address, port, txt)
self.new_serviceCB(name)
# different handler when resolving all contacts
def service_resolved_all_callback(self, interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags):
print "Service data for service '%s' in domain '%s' on %i.%i:" % (name, domain, interface, protocol)
print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(avahi.txt_array_to_string_array(txt)))
'''
# add domain to stay unique
if domain != 'local':
add_domain = '.'+domain
else:
add_domain = ''
self.contacts[name'@'+host+add_domain] = (name, stype, domain, interface, protocol, host, address, port, txt)
'''
self.contacts[name] = (name, stype, domain, interface, protocol, host, address, port, txt)
self.contacts[name] = (name, domain, interface, protocol, host, address, port, txt)
def service_added_callback(self):
print 'Service successfully added'
@ -150,6 +144,9 @@ class Zeroconf:
self.entrygroup.Commit(reply_handler=self.service_committed_callback, error_handler=self.print_error_callback)
def announce(self):
#for testing
#self.contacts['stefan@munin'] = ('stefan@munin', 'local', '8', '0', 'munin', '192.168.1.29', '5121',avahi.string_array_to_txt_array(['status','avail']))
#self.new_serviceCB('stefan@munin')
state = self.server.GetState()
if state == avahi.SERVER_RUNNING:
@ -193,10 +190,10 @@ class Zeroconf:
# refresh data manually - really ok or too much traffic?
def resolve_all(self):
for val in self.contacts.values():
#val:(name, stype, domain, interface, protocol, host, address, port, txt)
self.server.ResolveService( int(val[3]), int(val[4]), val[0], \
self.stype, val[2], avahi.PROTO_UNSPEC, dbus.UInt32(0),\
reply_handler=self.service_resolved_callback, error_handler=self.print_error_callback)
#val:(name, domain, interface, protocol, host, address, port, txt)
self.server.ResolveService( int(val[2]), int(val[3]), val[0], \
self.stype, val[1], avahi.PROTO_UNSPEC, dbus.UInt32(0),\
reply_handler=self.service_resolved_all_callback, error_handler=self.print_error_callback)
def get_contacts(self):
self.resolve_all()