def __init__(self): ''' Creates a new instance. Find the topic of the master_discovery node using U{master_discovery_fkie.interface_finder.get_changes_topic() <http://docs.ros.org/api/master_discovery_fkie/html/modules.html#interface-finder-module>}. Also the parameter C{~ignore_hosts} will be analyzed to exclude hosts from sync. ''' self.masters = {} # the connection to the local service master self.materuri = masteruri_from_master() '''@ivar: the ROS master URI of the C{local} ROS master. ''' self.__lock = threading.RLock() # load interface self._load_interface() # subscribe to changes notifier topics topic_names = interface_finder.get_changes_topic(masteruri_from_master()) self.sub_changes = dict() '''@ivar: `dict` with topics {name: U{rospy.Subscriber<http://docs.ros.org/api/rospy/html/rospy.topics.Subscriber-class.html>}} publishes the changes of the discovered ROS masters.''' for topic_name in topic_names: rospy.loginfo("listen for updates on %s", topic_name) self.sub_changes[topic_name] = rospy.Subscriber(topic_name, MasterState, self._rosmsg_callback_master_state) self.__timestamp_local = None self.__own_state = None self.update_timer = None self.own_state_getter = None self._join_threads = dict() # threads waiting for stopping the sync thread # initialize the ROS services rospy.Service('~get_sync_info', GetSyncInfo, self._rosservice_get_sync_info) rospy.on_shutdown(self.finish) self.obtain_masters()
def __init__(self): ''' Creates a new instance. Find the topic of the master_discovery node using L{master_discovery_fkie.interface_finder.get_changes_topic()}. Also the parameter C{~ignore_hosts} will be analyzed to exclude hosts from sync. ''' self.masters = {} # the connection to the local service master self.materuri = masteruri_from_master() '''@ivar: the ROS master URI of the C{local} ROS master. ''' self.__lock = threading.RLock() # load interface self._load_interface() # subscribe to changes notifier topics topic_names = interface_finder.get_changes_topic( masteruri_from_master()) self.sub_changes = dict() '''@ivar: {dict} with topics C{(name: L{rospy.Subscriber})} publishes the changes of the discovered ROS masters.''' for topic_name in topic_names: rospy.loginfo("listen for updates on %s", topic_name) self.sub_changes[topic_name] = rospy.Subscriber( topic_name, MasterState, self._rosmsg_callback_master_state) self.__timestamp_local = None self.__own_state = None self.update_timer = None self.own_state_getter = None self._join_threads = dict( ) # threads waiting for stopping the sync thread # initialize the ROS services rospy.Service('~get_sync_info', GetSyncInfo, self._rosservice_get_sync_info) rospy.on_shutdown(self.finish) self.obtain_masters()
def obtain_masters(self): ''' This method use the service 'list_masters' of the master_discoverer to get the list of discovered ROS master. Based on this list the L{SyncThread} for synchronization will be created. @see: U{master_discovery_fkie.interface_finder.get_listmaster_service() <http://docs.ros.org/api/master_discovery_fkie/html/modules.html#interface-finder-module>} ''' if not rospy.is_shutdown(): service_names = interface_finder.get_listmaster_service(masteruri_from_master(), False) for service_name in service_names: try: with self.__lock: try: socket.setdefaulttimeout(5) discoverMasters = rospy.ServiceProxy(service_name, DiscoverMasters) resp = discoverMasters() masters = [] master_names = [m.name for m in resp.masters] rospy.loginfo("ROS masters obtained from '%s': %s", service_name, master_names) for m in resp.masters: if self._can_sync(m.name): # do not sync to the master, if it is in ignore list or not in filled sync list masters.append(m.name) self.update_master(m.name, m.uri, m.timestamp, m.timestamp_local, m.discoverer_name, m.monitoruri, m.online) for key in set(self.masters.keys()) - set(masters): self.remove_master(self.masters[key].name) except rospy.ServiceException, e: rospy.logwarn("ERROR Service call 'list_masters' failed: %s", str(e)) except: import traceback rospy.logwarn("ERROR while initial list masters: %s", traceback.format_exc()) finally: socket.setdefaulttimeout(None) self.update_timer = threading.Timer(self.UPDATE_INTERVALL, self.obtain_masters) self.update_timer.start()
def obtain_masters(self): ''' This method use the service 'list_masters' of the master_discoverer to get the list of discovered ROS master. Based on this list the L{SyncThread} for synchronization will be created. @see: U{master_discovery_fkie.interface_finder.get_listmaster_service() <http://docs.ros.org/api/master_discovery_fkie/html/modules.html#interface-finder-module>} ''' if not rospy.is_shutdown(): service_names = interface_finder.get_listmaster_service( masteruri_from_master(), False) for service_name in service_names: try: with self.__lock: try: socket.setdefaulttimeout(5) discoverMasters = rospy.ServiceProxy( service_name, DiscoverMasters) resp = discoverMasters() masters = [] master_names = [m.name for m in resp.masters] rospy.loginfo("ROS masters obtained from '%s': %s", service_name, master_names) for m in resp.masters: if not self._re_ignore_hosts.match( m.name ) or self._re_sync_hosts.match( m.name ): # do not sync to the master, if it is in ignore list masters.append(m.name) self.update_master(m.name, m.uri, m.timestamp, m.timestamp_local, m.discoverer_name, m.monitoruri, m.online) for key in set(self.masters.keys()) - set(masters): self.remove_master(self.masters[key].name) except rospy.ServiceException, e: rospy.logwarn( "ERROR Service call 'list_masters' failed: %s", str(e)) except: import traceback rospy.logwarn("ERROR while initial list masters: %s", traceback.format_exc()) finally: socket.setdefaulttimeout(None) self.update_timer = threading.Timer(self.UPDATE_INTERVALL, self.obtain_masters) self.update_timer.start()