concert_master.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # License: BSD
00004 #   https://raw.github.com/robotics-in-concert/rocon_orchestration/concert_master/LICENSE
00005 #
00006 ##############################################################################
00007 # Imports
00008 ##############################################################################
00009 
00010 import roslib
00011 roslib.load_manifest('concert_master')
00012 import rospy
00013 import sys
00014 import traceback
00015 
00016 from concert_msgs.srv import *
00017 from gateway_msgs.srv import GatewayInfo
00018 from rocon_hub_client.hub_client import HubClient
00019 
00020 
00021 class ConcertMaster(object):
00022     concertmaster_key = "concertmasterlist"
00023     is_connected = False
00024     hub_client = None
00025 
00026     def __init__(self):
00027         self.is_connected = False
00028 
00029         self.name = rospy.get_name()
00030         self.param = self._setup_ros_parameters()
00031 
00032         self.hub_client = HubClient(whitelist=self.param['hub_whitelist'],
00033                                     blacklist=self.param['hub_blacklist'],
00034                                     is_zeroconf=False,
00035                                     namespace='rocon',
00036                                     name=self.name,
00037                                     callbacks=None)
00038 
00039         self.service = {}
00040         try:
00041             self.service['gateway_info'] = rospy.ServiceProxy("~gateway_info", GatewayInfo)
00042             self.service['gateway_info'].wait_for_service()
00043         except rospy.exceptions.ROSInterruptException:
00044             rospy.logwarn("Concert Master : ros shut down before services could be found.")
00045 
00046     def spin(self):
00047         self._connect_to_hub()
00048         rospy.loginfo("Concert Master : connected to hub [%s]" % self.hub_uri)
00049         self._register_concert_master()
00050         rospy.loginfo("Concert Master : registered on the hub [%s]" % self.name)
00051 
00052         rospy.spin()
00053         self._shutdown()
00054 
00055     def _connect_to_hub(self):
00056         while not rospy.is_shutdown() and not self.is_connected:
00057             gateway_info = self.service['gateway_info']()
00058             if gateway_info.connected == True:
00059                 hub_uri = gateway_info.hub_uri
00060                 if self.hub_client.connect(hub_uri):
00061                     self.is_connected = True
00062                     self.name = gateway_info.name
00063                     self.hub_uri = hub_uri
00064             else:
00065                 rospy.loginfo("Concert Master : no hub yet available, spinning...")
00066             rospy.sleep(1.0)
00067 
00068     def _setup_ros_parameters(self):
00069         param = {}
00070         param['hub_uri'] = rospy.get_param('~hub_uri', '')
00071         param['hub_whitelist'] = rospy.get_param('~hub_whitelist', '')
00072         param['hub_blacklist'] = rospy.get_param('~hub_blacklist', '')
00073         return param
00074 
00075     def _register_concert_master(self):
00076         try:
00077             self.hub_client.registerKey(self.concertmaster_key, self.name)
00078         except Exception as e:
00079             rospy.logerr("Concert Master : %s" % str(e))
00080             traceback.print_exc(file=sys.stdout)
00081 
00082     def _unregister_concert_master(self):
00083         try:
00084             self.hub_client.unregisterKey(self.concertmaster_key, self.name)
00085         except HubClient.ConnectionError:
00086             # usually just shut down before we did...die quietly
00087             #rospy.logwarn("Concert Master : lost connection to the hub (probably shut down before we did)")
00088             pass
00089 
00090     def _shutdown(self):
00091         self._unregister_concert_master()
00092         rospy.loginfo("Concert Master : shutting down")
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


concert_master
Author(s): Jihoon
autogenerated on Tue Jan 22 2013 13:08:39