00001
00002
00003 import roslib; roslib.load_manifest('multi_interface_roam')
00004 import rospy
00005 import config
00006 import dynamic_reconfigure.server
00007 import twisted.internet.reactor as reactor
00008 from multi_interface_roam.cfg import MultiInterfaceRoamConfig
00009 import mac_addr
00010 import interface_selector
00011 import sys
00012 from std_msgs.msg import Int32
00013 import sigblock
00014 import signal
00015
00016
00017 import asmach as smach
00018 smach.logdebug = lambda x: None
00019
00020 class Node:
00021 def __init__(self, *args, **kwargs):
00022 sigblock.save_mask()
00023 sigblock.block_signal(signal.SIGCHLD)
00024 rospy.init_node(*args, **kwargs)
00025 sigblock.restore_mask()
00026 rospy.core.add_shutdown_hook(self._shutdown_by_ros)
00027 reactor.addSystemEventTrigger('after', 'shutdown', self._shutdown_by_reactor)
00028
00029 def _shutdown_by_reactor(self):
00030 rospy.signal_shutdown("Reactor shutting down.")
00031
00032 def _shutdown_by_ros(self, why):
00033 reactor.fireSystemEvent('shutdown')
00034
00035 class RoamNode:
00036 def __init__(self):
00037 Node("multi_interface_roam")
00038 self.interface_selector = interface_selector.InterfaceSelector()
00039 self.reconfig_server = dynamic_reconfigure.server.Server(MultiInterfaceRoamConfig, self.reconfigure)
00040 self.iface_id_pub = rospy.Publisher('/current_iface_id', Int32, latch = True)
00041 self.interface_selector.update_event.subscribe_repeating(self._publish_status)
00042 self._publish_mapping_list = self.interface_selector.interfaces.values()
00043
00044 def _publish_status(self):
00045 ai = self.interface_selector.active_interfaces
00046 if not ai or ai[0] not in self._publish_mapping_list:
00047 index = -1
00048 else:
00049 index = self._publish_mapping_list.index(ai[0])
00050 self.iface_id_pub.publish(index)
00051
00052 def reconfigure(self, config, level):
00053 if config['interface'] not in self.interface_selector.interfaces:
00054 config['interface'] = ''
00055 interface = config['interface']
00056 ssid = config['ssid'] = config['ssid'][0:32]
00057 bssid = ""
00058 if not mac_addr.is_str(config['bssid']):
00059 config['bssid'] = ""
00060 else:
00061 bssid = mac_addr.str_to_packed(config['bssid'])
00062 use_tunnel = config['use_tunnel']
00063
00064 self.interface_selector.set_mode(ssid, bssid, interface, use_tunnel, config['band'])
00065
00066 return config
00067
00068 if __name__ == "__main__":
00069 def start():
00070 try:
00071 RoamNode()
00072 except:
00073 import traceback
00074 traceback.print_exc()
00075 print >> sys.stderr, "\nCaught exception during startup. Shutting down."
00076 reactor.fireSystemEvent('shutdown')
00077 reactor.addSystemEventTrigger('before', 'startup', start)
00078 reactor.run()