Commit 78f439c6 authored by Philipp Hörist's avatar Philipp Hörist

Refactor disconnect code

Fixes #9467
parent 59855a6c
This diff is collapsed.
......@@ -71,7 +71,7 @@ class Ping:
# We haven't got the pong in time, disco and reconnect
log.warning('No reply received for keepalive ping. Reconnecting...')
self._con.disconnectedReconnCB()
self._con.disconnect(immediately=True)
def send_ping(self, contact: ContactsT) -> None:
if not app.account_is_connected(self._account):
......
......@@ -676,7 +676,7 @@ class ClientZeroconf:
def zeroconf_init(self, show, msg):
self.zeroconf = zeroconf.Zeroconf(self.caller._on_new_service,
self.caller._on_remove_service, self.caller._on_name_conflictCB,
self.caller._on_disconnected, self.caller._on_error,
self.caller._on_disconnect, self.caller._on_error,
self.caller.username, self.caller.host, self.port)
self.zeroconf.txt['msg'] = msg
self.zeroconf.txt['status'] = show
......
......@@ -127,7 +127,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
def _on_resolve_timeout(self):
if self.connected:
if not self.connection.resolve_all():
self._on_disconnected()
self.disconnect()
return False
diffs = self.roster.getDiffs()
for key in diffs:
......@@ -162,22 +162,6 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
app.nec.push_incoming_event(ZeroconfPresenceReceivedEvent(
None, conn=self, fjid=jid, show='offline', status=''))
def disconnectedReconnCB(self):
"""
Called when we are disconnected. Comes from network manager for example
we don't try to reconnect, network manager will tell us when we can
"""
if app.account_is_connected(self.name):
# we cannot change our status to offline or connecting
# after we auth to server
self.old_show = STATUS_LIST[self.connected]
self.connected = 0
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
show='offline'))
# random number to show we wait network manager to send us a reconenct
self.time_to_reconnect = 5
self.on_purpose = False
def _on_name_conflictCB(self, alt_name):
self.disconnect()
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
......@@ -241,14 +225,33 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
GLib.timeout_add_seconds(5, self._on_resolve_timeout)
return True
def disconnect(self, on_purpose=False):
def disconnect(self, reconnect=True, immediately=True):
log.info('Start disconnecting zeroconf')
if reconnect:
if app.account_is_connected(self.name):
# we cannot change our status to offline or connecting
# after we auth to server
self.old_show = STATUS_LIST[self.connected]
# random number to show we wait network manager to send
# us a reconenct
self.time_to_reconnect = 5
else:
self.time_to_reconnect = None
self.connected = 0
self.time_to_reconnect = None
if self.connection:
self.connection.disconnect()
self.connection = None
# stop calling the timeout
self.call_resolve_timeout = False
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
show='offline'))
def _on_disconnect(self):
self.connected = 0
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
show='offline'))
def reannounce(self):
if self.connected:
......
......@@ -185,7 +185,7 @@ class RemoveAccountWindow:
# Close all opened windows
app.interface.roster.close_all(self.account, force=True)
if self.account in app.connections:
app.connections[self.account].disconnect(on_purpose=True)
app.connections[self.account].disconnect(reconnect=False)
app.connections[self.account].cleanup()
del app.connections[self.account]
app.logger.remove_roster(app.get_jid_from_account(self.account))
......
......@@ -1341,7 +1341,7 @@ class Interface:
def on_cancel():
del self.instances[account]['online_dialog']['ssl_error']
obj.conn.disconnect(on_purpose=True)
obj.conn.disconnect(reconnect=False)
app.nec.push_incoming_event(OurShowEvent(None, conn=obj.conn,
show='offline'))
......@@ -1404,7 +1404,7 @@ class Interface:
def on_cancel():
del self.instances[obj.conn.name]['online_dialog']\
['plain_connection']
obj.conn.disconnect(on_purpose=True)
obj.conn.disconnect(reconnect=False)
app.nec.push_incoming_event(OurShowEvent(None, conn=obj.conn,
show='offline'))
......@@ -1436,7 +1436,7 @@ class Interface:
def on_cancel():
del self.instances[obj.conn.name]['online_dialog']['insecure_ssl']
obj.conn.disconnect(on_purpose=True)
obj.conn.disconnect(reconnect=False)
app.nec.push_incoming_event(OurShowEvent(None, conn=obj.conn,
show='offline'))
......@@ -1478,7 +1478,7 @@ class Interface:
def on_cancel():
del self.instances[obj.conn.name]['online_dialog']\
['insecure_password']
obj.conn.disconnect(on_purpose=True)
obj.conn.disconnect(reconnect=False)
app.nec.push_incoming_event(OurShowEvent(None, conn=obj.conn,
show='offline'))
......@@ -2458,12 +2458,12 @@ class Interface:
for connection in app.connections.values():
if connection.connected <= 0 and connection.time_to_reconnect:
log.info('Connect %s', connection.name)
connection.reconnect()
GLib.timeout_add_seconds(2, connection.reconnect)
else:
for connection in app.connections.values():
if connection.connected > 1:
log.info('Disconnect %s', connection.name)
connection.disconnectedReconnCB()
connection.disconnect(immediately=True)
def create_zeroconf_default_config(self):
if app.config.get_per('accounts', app.ZEROCONF_ACC_NAME, '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