From eb5e9f49cc3d1016184e8db2cc5a87c2c8c029e0 Mon Sep 17 00:00:00 2001 From: imdano <> Date: Tue, 18 Mar 2008 09:12:05 +0000 Subject: [PATCH] Added checks to auto-reconnection code to keep it from constantly trying to reconnect when it isn't working. Added a ShouldAutoReconnect method to the daemon, to simply the call needed in monitor.py's auto_reconnect method. --- daemon.py | 13 +++++++++---- monitor.py | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/daemon.py b/daemon.py index f0bb328..d9756e9 100644 --- a/daemon.py +++ b/daemon.py @@ -390,10 +390,6 @@ class ConnectionWizard(dbus.service.Object): self.auto_connecting = True time.sleep(1.5) gobject.timeout_add(3000, self._monitor_wired_autoconnect) - - @dbus.service.method('org.wicd.daemon') - def GetAutoConnecting(self): - return self.auto_connecting def _wireless_autoconnect(self): """ Attempts to autoconnect to a wireless network. """ @@ -496,6 +492,15 @@ class ConnectionWizard(dbus.service.Object): """ self.need_profile_chooser = misc.to_bool(val) + + @dbus.service.method('org.wicd.daemon') + def ShouldAutoReconnect(self): + """ Returns True if it's the right time to try autoreconnecting. """ + if self.GetAutoReconnect() and not self.CheckIfConnecting() and \ + not self.GetForcedDisconnect() and not self.auto_connecting: + return True + else: + return False @dbus.service.method('org.wicd.daemon') def GetForcedDisconnect(self): diff --git a/monitor.py b/monitor.py index d01ddcf..5bf27d5 100755 --- a/monitor.py +++ b/monitor.py @@ -21,6 +21,7 @@ import dbus import gobject import os import sys +import time from dbus.mainloop.glib import DBusGMainLoop import wpath @@ -56,6 +57,8 @@ class ConnectionStatus(): self.connection_lost_counter = 0 self.last_state = misc.NOT_CONNECTED self.reconnecting = False + self.reconnect_tries = 0 + self.last_reconnect_time = time.time() self.signal_changed = False def check_for_wired_connection(self, wired_ip): @@ -184,10 +187,12 @@ class ConnectionStatus(): else: info = ["wireless", wireless.GetCurrentNetwork(self.iwconfig)] elif state == misc.WIRELESS: + self.reconnect_tries = 0 info = [wifi_ip, wireless.GetCurrentNetwork(self.iwconfig), str(wireless.GetPrintableSignalStrength(self.iwconfig)), str(wireless.GetCurrentNetworkID(self.iwconfig))] elif state == misc.WIRED: + self.reconnect_tries = 0 info = [wired_ip] else: print 'ERROR: Invalid state!' @@ -212,13 +217,19 @@ class ConnectionStatus(): if self.reconnecting: return + # Some checks to keep reconnect retries from going crazy. + if self.reconnect_tries > 2 and \ + time.time() - self.last_reconnect_time < 30: + return + self.reconnecting = True daemon.SetCurrentInterface('') print 'autoreconnect' - if daemon.GetAutoReconnect() and not daemon.CheckIfConnecting() and \ - not daemon.GetForcedDisconnect() and not daemon.GetAutoConnecting(): + if daemon.ShouldAutoReconnect(): print 'Starting automatic reconnect process' + self.last_reconnect_time = time.time() + self.reconnect_tries += 1 # If we just lost a wireless connection, try to connect to that # network again. Otherwise just call Autoconnect.