Commit 163cfef3 authored by dkirov's avatar dkirov

disconnect when avahi-daemon stop responding

fix reference in connection_handlers
parent b7ceb9dd
...@@ -470,11 +470,9 @@ class ClientZeroconf: ...@@ -470,11 +470,9 @@ class ClientZeroconf:
self.ip_to_hash = {} self.ip_to_hash = {}
def test_avahi(self): def test_avahi(self):
#~ self.avahi_error = False
try: try:
import avahi import avahi
except ImportError: except ImportError:
#~ self.avahi_error = True
return False return False
return True return True
...@@ -489,21 +487,24 @@ class ClientZeroconf: ...@@ -489,21 +487,24 @@ class ClientZeroconf:
self.roster = roster_zeroconf.Roster(self.zeroconf) self.roster = roster_zeroconf.Roster(self.zeroconf)
def remove_announce(self): def remove_announce(self):
return self.zeroconf.remove_announce() if self.zeroconf:
return self.zeroconf.remove_announce()
def announce(self): def announce(self):
return self.zeroconf.announce() if self.zeroconf:
return self.zeroconf.announce()
def set_show_msg(self, show, msg): def set_show_msg(self, show, msg):
self.zeroconf.txt['msg'] = msg if self.zeroconf:
self.last_msg = msg self.zeroconf.txt['msg'] = msg
return self.zeroconf.update_txt(show) self.last_msg = msg
return self.zeroconf.update_txt(show)
def resolve_all(self): def resolve_all(self):
self.zeroconf.resolve_all() if self.zeroconf:
self.zeroconf.resolve_all()
def reannounce(self, txt): def reannounce(self, txt):
#~ if self.zeroconf:
self.remove_announce() self.remove_announce()
self.zeroconf.txt = txt self.zeroconf.txt = txt
self.zeroconf.port = self.port self.zeroconf.port = self.port
...@@ -538,8 +539,6 @@ class ClientZeroconf: ...@@ -538,8 +539,6 @@ class ClientZeroconf:
self.roster.zeroconf = None self.roster.zeroconf = None
self.roster._data = None self.roster._data = None
self.roster = None self.roster = None
#~ self.caller.show = 'offline'
#~ self.caller.dispatch('STATUS', 'offline')
def kill_all_connections(self): def kill_all_connections(self):
for connection in self.connections.values(): for connection in self.connections.values():
...@@ -575,7 +574,9 @@ class ClientZeroconf: ...@@ -575,7 +574,9 @@ class ClientZeroconf:
return False return False
def getRoster(self): def getRoster(self):
return self.roster.getRoster() if self.roster:
return self.roster.getRoster()
return {}
def send(self, msg_iq): def send(self, msg_iq):
msg_iq.setFrom(self.roster.zeroconf.name) msg_iq.setFrom(self.roster.zeroconf.name)
......
...@@ -712,8 +712,8 @@ class ConnectionHandlersZeroconf(ConnectionVcard, ConnectionBytestream): ...@@ -712,8 +712,8 @@ class ConnectionHandlersZeroconf(ConnectionVcard, ConnectionBytestream):
tim = time.localtime(timegm(tim)) tim = time.localtime(timegm(tim))
frm = msg.getFrom() frm = msg.getFrom()
if frm == None: if frm == None:
for key in self.zeroconf.contacts: for key in self.connection.zeroconf.contacts:
if ip == self.zeroconf.contacts[key][zeroconf.C_ADDRESS]: if ip == self.connection.zeroconf.contacts[key][zeroconf.C_ADDRESS]:
frm = key frm = key
frm = str(frm) frm = str(frm)
jid = frm jid = frm
......
...@@ -203,8 +203,9 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): ...@@ -203,8 +203,9 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
(_('Connection with account "%s" has been lost') % self.name, (_('Connection with account "%s" has been lost') % self.name,
_('To continue sending and receiving messages, you will need to reconnect.'))) _('To continue sending and receiving messages, you will need to reconnect.')))
self.status = 'offline' self.status = 'offline'
self.disconnect()
def connect(self, data = None, show = 'online', msg = ''): def connect(self, show = 'online', msg = ''):
self.get_config_values_or_default() self.get_config_values_or_default()
if not self.connection: if not self.connection:
self.connection = client_zeroconf.ClientZeroconf(self) self.connection = client_zeroconf.ClientZeroconf(self)
...@@ -214,13 +215,16 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): ...@@ -214,13 +215,16 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.dispatch('CONNECTION_LOST', self.dispatch('CONNECTION_LOST',
(_('Could not connect to "%s"') % self.name, (_('Could not connect to "%s"') % self.name,
_('Please check if Avahi is installed.'))) _('Please check if Avahi is installed.')))
self.disconnect()
return return
self.connection.connect(show, msg) self.connection.connect(show, msg)
if not self.connection.listener: if not self.connection.listener:
self.dispatch('STATUS', 'offline')
self.status = 'offline' self.status = 'offline'
self.dispatch('CONNECTION_LOST', self.dispatch('CONNECTION_LOST',
(_('Could not start local service') % self.name, (_('Could not start local service'),
_('Unable to bind to port "%d".' % self.port))) _('Please check if avahi-daemon is running.')))
self.disconnect()
return return
else: else:
self.connection.announce() self.connection.announce()
...@@ -247,8 +251,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): ...@@ -247,8 +251,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.connection = None self.connection = None
# stop calling the timeout # stop calling the timeout
self.call_resolve_timeout = False self.call_resolve_timeout = False
def reannounce(self): def reannounce(self):
if self.connected: if self.connected:
txt = {} txt = {}
...@@ -266,7 +269,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): ...@@ -266,7 +269,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.port = port self.port = port
last_msg = self.connection.last_msg last_msg = self.connection.last_msg
self.disconnect() self.disconnect()
if not self.connect(show = self.status, msg = last_msg): if not self.connect(self.status, last_msg):
return return
if self.status != 'invisible': if self.status != 'invisible':
self.connection.announce() self.connection.announce()
...@@ -281,7 +284,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf): ...@@ -281,7 +284,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
check = True #to check for errors from zeroconf check = True #to check for errors from zeroconf
# 'connect' # 'connect'
if show != 'offline' and not self.connected: if show != 'offline' and not self.connected:
if not self.connect(None, show, msg): if not self.connect(show, msg):
return return
if show != 'invisible': if show != 'invisible':
check = self.connection.announce() check = self.connection.announce()
......
...@@ -65,6 +65,9 @@ class Zeroconf: ...@@ -65,6 +65,9 @@ class Zeroconf:
# left for eventual later use # left for eventual later use
pass pass
def error_callback1(self, err):
gajim.log.debug('RR' + str(err))
def error_callback(self, err): def error_callback(self, err):
gajim.log.debug(str(err)) gajim.log.debug(str(err))
# timeouts are non-critical # timeouts are non-critical
...@@ -80,7 +83,7 @@ class Zeroconf: ...@@ -80,7 +83,7 @@ class Zeroconf:
# synchronous resolving # synchronous resolving
self.server.ResolveService( int(interface), int(protocol), name, stype, \ self.server.ResolveService( int(interface), int(protocol), name, stype, \
domain, avahi.PROTO_UNSPEC, dbus.UInt32(0), \ domain, avahi.PROTO_UNSPEC, dbus.UInt32(0), \
reply_handler=self.service_resolved_callback, error_handler=self.error_callback) reply_handler=self.service_resolved_callback, error_handler=self.error_callback1)
def remove_service_callback(self, interface, protocol, name, stype, domain, flags): def remove_service_callback(self, interface, protocol, name, stype, domain, flags):
gajim.log.debug('Service %s in domain %s on %i.%i disappeared.' % (name, domain, interface, protocol)) gajim.log.debug('Service %s in domain %s on %i.%i disappeared.' % (name, domain, interface, protocol))
...@@ -215,9 +218,7 @@ class Zeroconf: ...@@ -215,9 +218,7 @@ class Zeroconf:
else: else:
txt['status'] = 'avail' txt['status'] = 'avail'
self.txt = txt self.txt = txt
gajim.log.debug('Publishing service %s of type %s' % (self.name, self.stype)) gajim.log.debug('Publishing service %s of type %s' % (self.name, self.stype))
self.entrygroup.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0), self.name, self.stype, '', '', self.port, avahi.dict_to_txt_array(self.txt), reply_handler=self.service_added_callback, error_handler=self.service_add_fail_callback) self.entrygroup.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0), self.name, self.stype, '', '', self.port, avahi.dict_to_txt_array(self.txt), reply_handler=self.service_added_callback, error_handler=self.service_add_fail_callback)
self.entrygroup.Commit(reply_handler=self.service_committed_callback, self.entrygroup.Commit(reply_handler=self.service_committed_callback,
...@@ -257,18 +258,34 @@ class Zeroconf: ...@@ -257,18 +258,34 @@ class Zeroconf:
def browse_domain(self, interface, protocol, domain): def browse_domain(self, interface, protocol, domain):
self.new_service_type(interface, protocol, self.stype, domain, '') self.new_service_type(interface, protocol, self.stype, domain, '')
def avahi_dbus_connect_cb(self, a, connect, disconnect):
if connect != "":
gajim.log.debug('Lost connection to avahi-daemon')
try:
self.connected = False
self.disconnect()
self.disconnected_CB()
except Exception, e:
print e
else:
gajim.log.debug('We are connected to avahi-daemon')
# connect to dbus # connect to dbus
def connect_dbus(self): def connect_dbus(self):
if self.server: if self.server:
return True return True
try: try:
self.bus = dbus.SystemBus() self.bus = dbus.SystemBus()
# is there any way to check if a dbus name exists? self.bus.add_signal_receiver(self.avahi_dbus_connect_cb,
# that might make the Introspect Error go away... "NameOwnerChanged", "org.freedesktop.DBus",
arg0="org.freedesktop.Avahi")
self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, \ self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, \
avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER) avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
self.server.connect_to_signal('StateChanged', self.server_state_changed_callback) self.server.connect_to_signal('StateChanged',
except dbus.dbus_bindings.DBusException, e: self.server_state_changed_callback)
except Exception, e:
# Avahi service is not present # Avahi service is not present
self.server = None self.server = None
gajim.log.debug(str(e)) gajim.log.debug(str(e))
...@@ -280,8 +297,8 @@ class Zeroconf: ...@@ -280,8 +297,8 @@ class Zeroconf:
self.name = self.username + '@' + self.host # service name self.name = self.username + '@' + self.host # service name
if not self.connect_dbus(): if not self.connect_dbus():
return False return False
self.connected = True
self.connected = True
# start browsing # start browsing
if self.domain is None: if self.domain is None:
# Explicitly browse .local # Explicitly browse .local
...@@ -304,11 +321,11 @@ class Zeroconf: ...@@ -304,11 +321,11 @@ class Zeroconf:
self.connected = False self.connected = False
if self.service_browser: if self.service_browser:
self.service_browser.Free() self.service_browser.Free()
self.service_browser = None
if self.domain_browser: if self.domain_browser:
self.domain_browser.Free() self.domain_browser.Free()
self.domain_browser = None
self.remove_announce() self.remove_announce()
self.service_browser = None
self.domain_browser = None
self.server = None self.server = None
# refresh txt data of all contacts manually (no callback available) # refresh txt data of all contacts manually (no callback available)
......
...@@ -1413,7 +1413,6 @@ class Interface: ...@@ -1413,7 +1413,6 @@ class Interface:
response = dlg.get_response() response = dlg.get_response()
if response == gtk.RESPONSE_OK: if response == gtk.RESPONSE_OK:
new_name = dlg.input_entry.get_text() new_name = dlg.input_entry.get_text()
print 'account, data', account, data, new_name
gajim.config.set_per('accounts', account, 'name', new_name) gajim.config.set_per('accounts', account, 'name', new_name)
status = gajim.connections[account].status status = gajim.connections[account].status
gajim.connections[account].username = new_name gajim.connections[account].username = new_name
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment