1
0
mirror of https://github.com/gryf/wicd.git synced 2025-12-19 20:38:00 +01:00

Improved automatic reconnection behavior.

Improved debug mode behavior.
Improved the way networking.py interfaces passes attributes on to wnettools.py interfaces.
Fixed crash in __printReturn when a parameter to return wasn't of type 'str'.
This commit is contained in:
imdano
2008-03-17 07:50:51 +00:00
parent 7f3c2b08fb
commit cff1336d32
4 changed files with 82 additions and 68 deletions

View File

@@ -292,6 +292,8 @@ class ConnectionWizard(dbus.service.Object):
configfile = open(self.app_conf, "w")
config.write(configfile)
self.debug_mode = misc.to_bool(debug)
self.wifi.debug = self.debug_mode
self.wired.debug = self.debug_mode
@dbus.service.method('org.wicd.daemon')
def GetDebugMode(self):
@@ -750,7 +752,7 @@ class ConnectionWizard(dbus.service.Object):
self.wifi.disconnect_script = self.GetWirelessProperty(id,
'disconnectscript')
print 'Connecting to wireless network', self.LastScan[id]['essid']
return self.wifi.Connect(self.LastScan[id])
return self.wifi.Connect(self.LastScan[id], debug=self.debug_mode)
@dbus.service.method('org.wicd.daemon.wireless')
def CheckIfWirelessConnecting(self):
@@ -901,7 +903,7 @@ class ConnectionWizard(dbus.service.Object):
self.wired.before_script = self.GetWiredProperty("beforescript")
self.wired.after_script = self.GetWiredProperty("afterscript")
self.wired.disconnect_script = self.GetWiredProperty("disconnectscript")
self.wired.Connect(self.WiredNetwork)
self.wired.Connect(self.WiredNetwork, debug=self.debug_mode)
########## LOG FILE STUFF
#################################
@@ -1193,7 +1195,7 @@ class ConnectionWizard(dbus.service.Object):
def __printReturn(self, text, value):
"""prints the specified text and value, then returns the value"""
if self.debug_mode:
print text + " " + value
print ''.join([text, " ", str(value)])
return value
def get_option(self, section, option, default=None):

View File

@@ -163,7 +163,11 @@ class ConnectionStatus():
state = misc.CONNECTING
else: # No connection at all.
state = misc.NOT_CONNECTED
self.auto_reconnect()
if self.last_state == misc.WIRELESS:
from_wireless = True
else:
from_wireless = False
self.auto_reconnect(from_wireless)
self.update_state(state)
return True
@@ -197,7 +201,7 @@ class ConnectionStatus():
self.last_state = state
return True
def auto_reconnect(self):
def auto_reconnect(self, from_wireless=None):
""" Automatically reconnects to a network if needed.
If automatic reconnection is turned on, this method will
@@ -215,27 +219,14 @@ class ConnectionStatus():
if daemon.GetAutoReconnect() and not daemon.CheckIfConnecting() and \
not daemon.GetForcedDisconnect() and not daemon.GetAutoConnecting():
print 'Starting automatic reconnect process'
# First try connecting through ethernet
if wired.CheckPluggedIn():
print "Wired connection available, trying to connect..."
daemon.AutoConnect(False, reply_handler=reply_handle,
error_handler=err_handle)
self.reconnecting = False
return
# Next try the last wireless network we were connected to
# If we just lost a wireless connection, try to connect to that
# network again. Otherwise just call Autoconnect.
cur_net_id = wireless.GetCurrentNetworkID(self.iwconfig)
if cur_net_id > -1: # Needs to be a valid network
if not self.tried_reconnect:
print 'Trying to reconnect to last used wireless \
network'
wireless.ConnectWireless(cur_net_id)
self.tried_reconnect = True
elif not wireless.CheckIfWirelessConnecting():
print "Couldn't reconnect to last used network, \
scanning for an autoconnect network..."
daemon.AutoConnect(True, reply_handler=reply_handle,
error_handler=err_handle)
if from_wireless and cur_net_id > -1: # Needs to be a valid network
print 'Trying to reconnect to last used wireless ' + \
'network'
wireless.ConnectWireless(cur_net_id)
else:
daemon.AutoConnect(True, reply_handler=reply_handle,
error_handler=err_handle)

View File

@@ -56,8 +56,6 @@ if __name__ == '__main__':
class Controller(object):
""" Parent class for the different interface types. """
wireless_interface = None
wired_interface = None
connecting_thread = None
before_script = None
after_script = None
@@ -71,24 +69,39 @@ class Controller(object):
self.global_dns_1 = None
self.global_dns_2 = None
self.global_dns_3 = None
self._wired_interface = None
self._wireless_interface = None
self._debug = None
def __setattr__(self, attr, value):
""" Provided custom self.variable assignments.
def set_wireless_iface(self, value):
self._wireless_interface = value
if self.wiface:
self.wiface.SetInterface(value)
Calls the appropriate methods if self.wireless_interface or
self.wired_interface is set.
def get_wireless_iface(self):
return self._wireless_interface
"""
if attr == 'wireless_interface':
object.__setattr__(self, attr, value)
if self.wiface:
self.SetWiface(value)
elif attr == 'wired_interface':
object.__setattr__(self, attr, value)
if self.liface:
self.SetLiface(value)
else:
object.__setattr__(self, attr, value)
def set_wired_iface(self, value):
self._wired_interface = value
if self.liface:
self.liface.SetInterface(value)
def get_wired_iface(self):
return self._wired_interface
def set_debug(self, value):
self._debug = value
if self.wiface:
self.wiface.SetDebugMode(value)
if self.liface:
self.liface.SetDebugMode(value)
def get_debug(self):
return self._debug
wireless_interface = property(get_wireless_iface, set_wireless_iface)
wired_interface = property(get_wired_iface, set_wired_iface)
debug = property(get_debug, set_debug)
def SetWiface(self, iface):
""" Sets the wireless interface for the associated wnettools class. """
@@ -303,22 +316,24 @@ class Wireless(Controller):
def __init__(self):
""" Initialize the class. """
Controller.__init__(self)
self.wpa_driver = None
self._wpa_driver = None
self.wiface = wnettools.WirelessInterface(self.wireless_interface,
self.wpa_driver)
def __setattr__(self, attr, value):
if attr == 'wpa_driver':
self.__dict__[attr] = value
if self.wiface:
self.SetWPADriver(value)
else:
object.__setattr__(self, attr, value)
def set_wpa_driver(self, value):
self._wpa_driver = value
if self.wiface:
self.SetWPADriver(value)
def get_wpa_driver(self): return self._wpa_driver
wpa_driver = property(get_wpa_driver, set_wpa_driver)
def LoadInterfaces(self):
""" Load the wnettools controls for the wired/wireless interfaces. """
self.wiface = wnettools.WirelessInterface(self.wireless_interface,
self.wpa_driver)
self.debug, self.wpa_driver)
def Scan(self, essid=None):
""" Scan for available wireless networks.
@@ -347,7 +362,7 @@ class Wireless(Controller):
aps.sort(key=lambda x: x['strength'])
return aps
def Connect(self, network):
def Connect(self, network, debug=False):
""" Spawn a connection thread to connect to the network.
Keyword arguments:
@@ -358,7 +373,7 @@ class Wireless(Controller):
self.wireless_interface, self.wired_interface,
self.wpa_driver, self.before_script, self.after_script,
self.disconnect_script, self.global_dns_1,
self.global_dns_2, self.global_dns_3)
self.global_dns_2, self.global_dns_3, debug)
self.connecting_thread.setDaemon(True)
self.connecting_thread.start()
return True
@@ -657,14 +672,14 @@ class Wired(Controller):
""" Initialise the class. """
Controller.__init__(self)
self.wpa_driver = None
self.liface = wnettools.WiredInterface(self.wired_interface)
self.liface = wnettools.WiredInterface(self.wired_interface, self.debug)
def __setattr__(self, attr, val):
object.__setattr__(self, attr, val)
def LoadInterfaces(self):
""" Load the wnettools controls for the wired/wireless interfaces. """
self.liface = wnettools.WiredInterface(self.wired_interface)
self.liface = wnettools.WiredInterface(self.wired_interface, self.debug)
def CheckPluggedIn(self):
""" Check whether the wired connection is plugged in.
@@ -675,7 +690,7 @@ class Wired(Controller):
"""
return self.liface.GetPluggedIn()
def Connect(self, network):
def Connect(self, network, debug=False):
""" Spawn a connection thread to connect to the network.
Keyword arguments:
@@ -686,7 +701,7 @@ class Wired(Controller):
self.wireless_interface, self.wired_interface,
self.before_script, self.after_script,
self.disconnect_script, self.global_dns_1,
self.global_dns_2, self.global_dns_3)
self.global_dns_2, self.global_dns_3, debug)
self.connecting_thread.setDaemon(True)
self.connecting_thread.start()
return True

View File

@@ -131,6 +131,10 @@ class Interface(object):
self.IP_FOUND = False
self.Check()
def SetDebugMode(self, value):
""" If True, verbose output is enabled. """
self.verbose = value
def SetInterface(self, iface):
""" Sets the interface.
@@ -204,7 +208,7 @@ class Interface(object):
"""
cmd = 'ifconfig ' + self.iface + ' up'
#if self.verbose: print cmd
if self.verbose: print cmd
misc.Run(cmd)
return True
@@ -390,7 +394,7 @@ class Interface(object):
"""
cmd = 'ifconfig ' + self.iface
#if self.verbose: print cmd
if self.verbose: print cmd
output = misc.Run(cmd)
return misc.RunRegex(ip_pattern, output)
@@ -514,7 +518,7 @@ class WirelessInterface(Interface):
essid -- essid to set the interface to
"""
cmd = 'iwconfig ' + self.iface + ' essid "' + essid + '"'
cmd = ''.join(['iwconfig ', self.iface, ' essid "', essid, '"'])
if self.verbose: print cmd
misc.Run(cmd)
@@ -573,7 +577,7 @@ class WirelessInterface(Interface):
access_points = []
for cell in networks:
# Only use sections where there is an ESSID.
if cell.count('ESSID:') > 0:
if 'ESSID:' in cell:
# Add this network to the list of networks
entry = self._ParseAccessPoint(cell, ralink_info)
if entry is not None:
@@ -891,9 +895,11 @@ class WirelessInterface(Interface):
if len(info) < 5:
break
if info[2] == network.get('essid'):
if info[5] == 'WEP' or (info[5] == 'OPEN' and info[4] == 'WEP'):
if info[5] == 'WEP' or (info[5] == 'OPEN' and \
info[4] == 'WEP'):
print 'Setting up WEP'
cmd = 'iwconfig ' + self.iface + ' key ' + network.get('key')
cmd = ''.join(['iwconfig ', self.iface, ' key ',
network.get('key')])
if self.verbose: print cmd
misc.Run(cmd)
else:
@@ -939,7 +945,7 @@ class WirelessInterface(Interface):
"""
if not iwconfig:
cmd = 'iwconfig ' + self.iface
# if self.verbose: print cmd
if self.verbose: print cmd
output = misc.Run(cmd)
else:
output = iwconfig
@@ -968,7 +974,7 @@ class WirelessInterface(Interface):
"""
if iwconfig:
cmd = 'iwconfig ' + self.iface
# if self.verbose: print cmd
if self.verbose: print cmd
output = misc.Run(cmd)
else:
output = iwconfig
@@ -985,7 +991,7 @@ class WirelessInterface(Interface):
"""
if not iwconfig:
cmd = 'iwconfig ' + self.iface
# if self.verbose: print cmd
if self.verbose: print cmd
output = misc.Run(cmd)
else:
output = iwconfig