interface_upper.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import system
00004 from netlink_monitor import netlink_monitor, IFSTATE
00005 from state_publisher import CompositeStatePublisher
00006 from twisted.internet import reactor
00007 from twisted.internet.defer import inlineCallbacks, DeferredLock
00008 
00009 # TODO Add tests.
00010 
00011 class InterfaceUpper:
00012     def __init__(self, iface):
00013         self.iface = iface
00014         self._lock = DeferredLock()
00015         CompositeStatePublisher(lambda x: x, [
00016             netlink_monitor.get_state_publisher(iface, IFSTATE.PLUGGED),
00017             netlink_monitor.get_state_publisher(iface, IFSTATE.UP),
00018         ]).subscribe(self._cb)
00019         self._is_shutdown = False
00020         self.state = None
00021         reactor.addSystemEventTrigger('before', 'shutdown', self._shutdown)
00022    
00023     @inlineCallbacks
00024     def restart(self):
00025         yield self._lock.acquire()
00026         try:
00027             yield system.system('ifconfig', self.iface, '0.0.0.0')
00028             yield system.system('ifconfig', self.iface, 'down')
00029         finally:
00030             self._lock.release()
00031 
00032     @inlineCallbacks
00033     def _cb(self, old_state, new_state):
00034         plugged, up = new_state
00035         self.state = new_state
00036         if plugged and not up and not self._is_shutdown:
00037             yield self._lock.acquire()
00038             try:
00039                 yield system.system('ifconfig', self.iface, 'up')
00040             finally:
00041                 self._lock.release()
00042 
00043     @inlineCallbacks
00044     def _shutdown(self):
00045         self._is_shutdown = True
00046         if self.state:
00047             yield self.restart()


multi_interface_roam
Author(s): Blaise Gassend
autogenerated on Thu Apr 24 2014 15:34:18