<div name="mailplane_signature">Hi,</div><div class="gmail_quote"><div><div><br></div><div>I’m building an Python application that has to connect and disconnect from Wifi on linux box. It's my very first experience with d-bus and gobject.</div>
<div><br></div><div>I’m using NetworkManager layer, through the nice networkmanager lib found in cnetworkmanager (<a href="http://vidner.net/martin/software/cnetworkmanager/" target="_blank">http://vidner.net/martin/software/cnetworkmanager/</a> thanx to Martin Vidner), in my daemon (named stationd).</div>
<div><br></div><div>This daemon runs a gobject.MainLoop. Once a timeout_add_seconds awake (triggers by an action of user in GUI), I have to disconnect current running Wifi and connect to a new one:</div><div><br></div><div>
from dbus.mainloop.glib import DBusGMainLoop</div><div> DBusGMainLoop(set_as_default=True)</div><div><br></div><div> from networkmanager import NetworkManager</div><div> import networkmanager.applet.settings as settings</div>
<div> from networkmanager.applet import USER_SERVICE </div><div> from networkmanager.applet.service import NetworkManagerUserSettings, NetworkManagerSettings</div><div> import time</div><div> import memcache</div>
<div><br></div><div> import gobject</div><div> loop = gobject.MainLoop()</div><div><br></div><div> nm = NetworkManager()</div><div><br></div><div> dummy_handler = lambda *args: None</div><div><br></div><div> cache = memcache.Client(['<a href="http://127.0.0.1:11211" target="_blank">127.0.0.1:11211</a>',] )</div>
<div><br></div><div><br></div><div> def get_device(dev_spec, hint):</div><div> candidates = []</div><div> devs = NetworkManager().GetDevices()</div><div> for dev in devs:</div><div> if dev._settings_type() == hint:</div>
<div> candidates.append(dev)</div><div> if len(candidates) == 1:</div><div> return candidates[0]</div><div> for dev in devs:</div><div> if dev["Interface"] == dev_spec:</div>
<div> return dev</div><div> print "Device '%s' not found" % dev_spec</div><div> return None</div><div><br></div><div> def kill_allconnections():</div><div> connections=nm['ActiveConnections']</div>
<div> for c in connections:</div><div> print c.object_path</div><div> nm.DeactivateConnection(c.object_path)</div><div><br></div><div> class Wifi(object):</div><div> </div><div> def connect(self, ssid, security="open", password=None):</div>
<div> "Connects to given Wifi network"</div><div> c=None # connection settings</div><div> us = NetworkManagerUserSettings([])</div><div> if security=="open":</div>
<div> c = settings.WiFi(ssid)</div><div> elif security=="wep":</div><div> c = settings.Wep(ssid, password)</div><div> elif security=="wpa":</div><div>
c = settings.WpaPsk(ssid, password)</div><div> else:</div><div> raise AttributeError("invalid security model '%s'"%security)</div><div> svc = USER_SERVICE</div>
<div> svc_conn = us.addCon(c.conmap)</div><div> hint = svc_conn.settings["connection"]["type"]</div><div> dev = get_device("", hint)</div><div> appath = "/"</div>
<div> nm.ActivateConnection(svc, svc_conn, dev, appath, reply_handler=dummy_handler, error_handler=dummy_handler)</div><div><br></div><div><br></div><div> def change_network_settings():</div><div> key="station:network:change"</div>
<div> change=cache.get(key)</div><div> if change is not None:</div><div> print "DISCONNECT"</div><div> kill_allconnections()</div><div> print "CHANGE SETTINGS"</div>
<div> wifi=cache.get(key+':wifi')</div><div> if wifi is not None:</div><div> ssid=cache.get(key+':wifi:ssid')</div><div> security=cache.get(key+':wifi:security')</div>
<div> password=cache.get(key+':wifi:password')</div><div> print "SWITCHING TO %s"%ssid</div><div> Wifi().connect(ssid, security, password)</div><div> cache.delete(key)</div>
<div> return True </div><div><br></div><div> def mainloop():</div><div> gobject.timeout_add_seconds(1, change_network_settings)</div><div> try:</div><div> loop.run()</div><div> except KeyboardInterrupt:</div>
<div> loop.quit()</div><div><br></div><div> if __name__=="__main__":</div><div> mainloop()</div><div><br></div><div>This runs perfectly for a first connection (read : the box is not connected, daemon is ran and box connects flawlessly to Wifi). Issue is when I try to connect to another Wifi : kill_allconnections() is ran silently, and connect method raises an exception on nm.ActivateConnection:</div>
<div><br></div><div> Traceback (most recent call last):</div><div> File "stationd.py", line 40, in change_network_settings</div><div> Wifi().connect(ssid, security, password)</div><div> File "/home/biopredictive/station/lib/network.py", line 88, in connect</div>
<div> us = NetworkManagerUserSettings([])</div><div> File "/home/biopredictive/station/lib/networkmanager/applet/service/__init__.py", line 71, in __init__</div><div> super(NetworkManagerUserSettings, self).__init__(conmaps, USER_SERVICE)</div>
<div> File "/home/biopredictive/station/lib/networkmanager/applet/service/__init__.py", line 33, in __init__</div><div> dbus.service.Object.__init__(self, bus, opath, bus_name)</div><div> File "/usr/lib/pymodules/python2.6/dbus/service.py", line 480, in __init__</div>
<div> self.add_to_connection(conn, object_path)</div><div> File "/usr/lib/pymodules/python2.6/dbus/service.py", line 571, in add_to_connection</div><div> self._fallback)</div><div> KeyError: "Can't register the object-path handler for '/org/freedesktop/NetworkManagerSettings': there is already a handler"</div>
<div><br></div><div>It looks like my former connection didn’t release all its resources ? </div><div><br></div><div>Would you please help ?</div></div><div><br></div>(I've asked this question on StackOverflow, too : <a href="http://stackoverflow.com/q/8938509/532219" target="_blank">http://stackoverflow.com/q/8938509/532219</a> )<span class="HOEnZb"><font color="#888888"><br>
<div name="mailplane_signature"><br></div><div name="mailplane_signature">--
<p>Olivier Deckmyn</p></div><br>
</font></span></div><br>