def __init__(self, gateway_info, concert_alias, is_local_client=False): ''' Initialise, finally ending with a call on the service to the client's platform info service. :param gateway_msgs.RemoteGateway gateway_info: information from this client's remote gateway :param str concert_alias: a string (+ index) human consumable form of the name :param bool is_local_client: is on the same ip as the concert (if we care) ''' self.msg = concert_msgs.ConcertClient() """The publishable data structure describing a concert client.""" self.msg.name = concert_alias self.msg.gateway_name = gateway_info.name self.msg.state = ConcertClient.State.PENDING self.msg.ip = gateway_info.ip self.msg.is_local_client = is_local_client self.gateway_info = gateway_info """Information about this client's gateway used for flipping, pulling and collecting connectivity statistics.""" # This will get assigned when a msg comes in and set back to none once it is processed. # We only ever do processing on self.msg in one place to avoid threading problems # (the transition handlers) so that is why we store a cached copy here. self._remote_controller = "" self._lock = threading.Lock() # timestamps self._timestamps = {} self._timestamps['last_seen'] = rospy.get_rostime() self._timestamps['last_state_change'] = rospy.get_rostime() platform_info_publisher_name = '/concert/clients/' + self.msg.gateway_name.lower( ).replace(' ', '_') + '/' + 'platform_info' self.platform_info_proxy = rocon_python_comms.SubscriberProxy( platform_info_publisher_name, rocon_std_msgs.MasterInfo)
def __init__(self): ''' Creates the polling topics necessary for updating statistics about the running gateway-hub network. ''' self._last_update = 0 self._gateway_namespace = None self._local_gateway = None self._remote_gateways = None self.gateway_nodes = [] # Gateway nodes self.flipped_nodes = [] # Flip connection nodes (i.e. topic name) self.pulled_nodes = [] self.pulled_edges = [] # Gateway-Topic edges self.gateway_edges = [] # Gateway-Gateway edges self.flipped_edges = [] # All unconnected node-topics or topic-nodes # Rubbish to clear out once rocon_gateway_graph is integrated self.bad_nodes = [] try: self._gateway_namespace = rocon_gateway_utils.resolve_local_gateway( timeout=rospy.rostime.Duration(2.0)) self._gateway_info = rocon_python_comms.SubscriberProxy( self._gateway_namespace + '/gateway_info', gateway_msgs.GatewayInfo) self._remote_gateway_info = rospy.ServiceProxy( self._gateway_namespace + '/remote_gateway_info', gateway_srvs.RemoteGatewayInfo) except rocon_python_comms.NotFoundException as e: rospy.logerr("Gateway Graph: %s" % str(e)) self._gateway_namespace = None
def initialise(self): """ Loops around (indefinitely) until it makes a connection with the rapp manager and retrieves the rapp list. """ # get the rapp list - just loop around until catch it once - it is not dynamically changing rospy.loginfo( "Interactions : calling the rapp manager to get the rapp list.") get_rapp_list = rocon_python_comms.SubscriberProxy( '~rapp_list', rocon_app_manager_msgs.RappList) while not rospy.is_shutdown(): # msg is rocon_app_manager_msgs/RappList # this returns almost immediately with None if rospy gets shutdown, so a long duration is ok msg = get_rapp_list(rospy.Duration(30.0)) if msg is None: rospy.logwarn( "Interactions : unable to connect with the rapp manager : {0} not found, will keep trying." .format(rospy.resolve_name('~rapp_list'))) else: self._available_rapps = rapp_list_msg_to_dict( msg.available_rapps) rospy.loginfo( "Interactions : discovered rapp support for pairing modes:" ) for rapp in msg.available_rapps: rospy.loginfo("Interactions : '%s'" % rapp.name) break get_rapp_list.unregister()
def console_only_main(node_name='concert_service_info', title='Concert Service Information'): rospy.init_node(node_name) try: topic_name = rocon_python_comms.find_topic( 'concert_msgs/Services', timeout=rospy.rostime.Duration(5.0), unique=True) except rocon_python_comms.NotFoundException as e: print( rocon_console.red + "failed to find unique topic of type 'concert_msgs/Services' [%s]" % str(e) + rocon_console.reset) sys.exit(1) service_info_proxy = rocon_python_comms.SubscriberProxy( topic_name, concert_msgs.Services) try: service_info_proxy.wait_for_publishers() except rospy.exceptions.ROSInterruptException: rospy.logwarn( 'Concert Service Info : ros shut down before concert info could be found.' ) trial = 0 MAX_TRIAL = 5 while not rospy.is_shutdown(): result = service_info_proxy(rospy.Duration(0.2)) if result: service_info = result break rospy.rostime.wallsleep(1.0) # human time trial = trial + 1 if trial > MAX_TRIAL: rospy.logerr( 'Concert Service info : concert is not found within ' + str(MAX_TRIAL) + ' trials') sys.exit(1) rocon_console.pretty_println('Concert Service Information', rocon_console.bold) for s in service_info.services: print_info(' Resource : ', s.resource_name) print_info(' Name : ', s.name) print_info(' Description : ', s.description) print_info(' Author : ', s.author) print_info(' Priority : ', s.priority) print_info(' Launcher Type : ', s.launcher_type) print_info(' Status : ', s.status) print_info(' Enabled : ', s.enabled) print ''
def local_gateway_name(): gateway_name = None gateway_info_service = rocon_python_comms.SubscriberProxy( '~gateway_info', gateway_msgs.GatewayInfo) while not rospy.is_shutdown(): gateway_info = gateway_info_service(timeout=rospy.Duration(0.2)) if gateway_info: if gateway_info.connected: gateway_name = gateway_info.name break rospy.rostime.wallsleep(1.0) return gateway_name
def get_master_info(timeout=1.0): ''' Tries to gather the rocon master info but if not available, return with a rocon_std_msgs.MasterInfo_ object filled with appropriate information ("Unknown Master" ...). :param double timeout: how long to blather around looking for the master info topic. :returns: the master information :rtype: rocon_std_msgs.MasterInfo_ .. include:: weblinks.rst ''' # default values master_info = rocon_std_msgs.MasterInfo() master_info.name = "Unknown Master" master_info.description = "Unknown" master_info.version = rocon_std_msgs.Strings.ROCON_VERSION master_info.icon = rocon_python_utils.ros.icon_resource_to_msg( 'rocon_icons/unknown.png') try: topic_name = rocon_python_comms.find_topic( 'rocon_std_msgs/MasterInfo', timeout=rospy.rostime.Duration(timeout), unique=True) except rocon_python_comms.NotFoundException as e: print( rocon_console.red + "failed to find unique topic of type 'rocon_std_msgs/MasterInfo' [%s]" % str(e) + rocon_console.reset) master_info.description = "Is it rocon enabled? See http://wiki.ros.org/rocon_master_info" return master_info master_info_proxy = rocon_python_comms.SubscriberProxy( topic_name, rocon_std_msgs.MasterInfo) try: master_info_proxy.wait_for_publishers() except rospy.exceptions.ROSInterruptException: rospy.logwarn( "Concert Info : ros shut down before rocon master info could be retrieved." ) master_info.description = "Unkonwn" return master_info result = master_info_proxy(rospy.Duration(0.2)) if result: master_info = result # rocon_std_msgs.MasterInfo return master_info
def wait_for_gateway(ns=_gateway_namespace, timeout=rospy.Duration(5.0)): ''' Slowly loop (and block) until the gateway is connected to a hub. ''' gateway_info_service = rocon_python_comms.SubscriberProxy( ns + '/gateway_info', gateway_msgs.GatewayInfo) start_time = rospy.Time.now() while not rospy.is_shutdown(): gateway_info = gateway_info_service() if gateway_info.connected: break if rospy.Time.now() - start_time > timeout: raise GatewaySampleRuntimeError( "timed out waiting for the gateway to connect to a hub") rospy.sleep(0.5)
def _init_gateway_services(self): self._gateway_services = {} self._gateway_services[ 'gateway_info'] = rocon_python_comms.SubscriberProxy( '~gateway_info', gateway_msgs.GatewayInfo) self._gateway_services['remote_gateway_info'] = rospy.ServiceProxy( '~remote_gateway_info', gateway_srvs.RemoteGatewayInfo) self._gateway_services['flip'] = rospy.ServiceProxy( '~flip', gateway_srvs.Remote) self._gateway_services['advertise'] = rospy.ServiceProxy( '~advertise', gateway_srvs.Advertise) self._gateway_services['pull'] = rospy.ServiceProxy( '~pull', gateway_srvs.Remote) self._gateway_publishers = {} self._gateway_publishers['force_update'] = rospy.Publisher( "~force_update", std_msgs.Empty)
def resolve_gateway_info(gateway_namespace=None): ''' @param the local topic namespace to prepend to the 'gateway_info' identifier. Uses resolve_local_gateway if none is specified. @type str @return the local gateway info in all its gory detail. @rtype gateway_msgs.GatewayInfo @raise rocon_gateway.GatewayError: if no remote gateways or no matching gateways available. ''' if gateway_namespace == None: gateway_namespace = resolve_local_gateway() gateway_info = rocon_python_comms.SubscriberProxy( gateway_namespace + '/gateway_info', gateway_msgs.GatewayInfo)() return gateway_info
def _match_robot_name_to_gateway_name(self): gateway_info_service = rocon_python_comms.SubscriberProxy('~gateway_info', gateway_msgs.GatewayInfo) rate = rospy.Rate(10) # 10hz warning_throttle_counter = 0 while not rospy.is_shutdown(): gateway_info = gateway_info_service(timeout=rospy.Duration(0.3)) if gateway_info: if self.parameters.robot_name != gateway_info.name.lower().replace(' ', '_'): self.parameters.robot_name = gateway_info.name.lower().replace(' ', '_') rospy.loginfo("Rapp Manager : matching robot name with gateway name '%s'" % self.parameters.robot_name) break try: rate.sleep() warning_throttle_counter += 1 if warning_throttle_counter % 10 == 0: rospy.logwarn("Rapp Manager : unable to find the local gateway, will keep trying") except rospy.exceptions.ROSInterruptException: rospy.loginfo("Rapp Manager : breaking out of gateway search loop [most likely just ros shutting down]") raise GatewayNotFoundException()
def main(node_name='master_info', title='Master Information'): ''' Establishes a connection and prints master information to the console. ''' rospy.init_node(node_name) try: topic_name = rocon_python_comms.find_topic('rocon_std_msgs/MasterInfo', timeout=rospy.rostime.Duration(5.0), unique=True) except rocon_python_comms.NotFoundException as e: print(console.red + "failed to find unique topic of type 'rocon_std_msgs/MasterInfo' [%s]" % str(e) + console.reset) sys.exit(1) master_info_proxy = rocon_python_comms.SubscriberProxy(topic_name, rocon_std_msgs.MasterInfo) try: master_info_proxy.wait_for_publishers() except rospy.exceptions.ROSInterruptException: rospy.logwarn("Concert Info : ros shut down before concert info could be found.") master_info = rocon_std_msgs.MasterInfo() while not rospy.is_shutdown(): result = master_info_proxy(rospy.Duration(0.2)) if result: master_info = result break rospy.rostime.wallsleep(1.0) # human time console.pretty_println(title, console.bold) print(console.cyan + " Name : " + console.yellow + master_info.name + console.reset) print(console.cyan + " Description: " + console.yellow + master_info.description + console.reset) print(console.cyan + " Icon : " + console.yellow + master_info.icon.resource_name + console.reset) print(console.cyan + " Version : " + console.yellow + master_info.version + console.reset) if qt_available: icon = rocon_python_utils.ros.find_resource_from_string(master_info.icon.resource_name) signal.signal(signal.SIGINT, signal.SIG_DFL) # make sure this comes after the rospy call, otherwise it will handle signals. app = QtGui.QApplication(sys.argv) window = Window(master_info.name, master_info.description, master_info.version, icon) window.show() sys.exit(app.exec_())
def _get_gateway_info(self): gateway_info_proxy = rocon_python_comms.SubscriberProxy( "~gateway_info", gateway_msgs.GatewayInfo) # This needs to be in a loop, since it must not only check for a response, but that the gateway # is connected to the hub. If it isn't connected, it needs to try again. start_time = rospy.get_rostime() while not rospy.is_shutdown(): gateway_info = gateway_info_proxy(rospy.Duration(0.1)) if gateway_info: if gateway_info.connected: name = gateway_info.name ip = gateway_info.ip break else: rospy.loginfo( "Conductor : no hub yet available, spinning...") rospy.rostime.wallsleep(0.1) if rospy.get_rostime() - start_time > rospy.Duration(15.0): raise rocon_python_comms.NotFoundException( "couldn't retrieve gateway information") gateway_info_proxy.unregister() return (name, ip)
def get_services_info(): ''' Used to get concert_service_info resultdict stores key as resource_name and value as tuple of services info keyList stores list of resoure_names to be used as key's for resultdict :returns: the concert service information as a tuple (keyList, resultdict) :rtype: (list, dictionary) ''' resultdict = {} keyList = [] try: topic_name = rocon_python_comms.find_topic( 'concert_msgs/Services', timeout=rospy.rostime.Duration(5.0), unique=True) except rocon_python_comms.NotFoundException as e: print( rocon_console.red + "failed to find unique topic of type 'concert_msgs/Services' [%s]" % str(e) + rocon_console.reset) sys.exit(1) service_info_proxy = rocon_python_comms.SubscriberProxy( topic_name, concert_msgs.Services) try: service_info_proxy.wait_for_publishers() except rospy.exceptions.ROSInterruptException: rospy.logwarn( 'Concert Service Info : ros shut down before concert info could be found.' ) trial = 0 MAX_TRIAL = 5 while not rospy.is_shutdown(): result = service_info_proxy(rospy.Duration(0.2)) if result: service_info = result break rospy.rostime.wallsleep(1.0) # human time trial = trial + 1 if trial > MAX_TRIAL: rospy.logerr( 'Concert Service info : concert is not found within ' + str(MAX_TRIAL) + ' trials') sys.exit(1) for s in service_info.services: objResult = Object() objResult.resource_name = s.resource_name objResult.name = s.name objResult.description = s.description objResult.author = s.author objResult.priority = s.priority objResult.launcher_type = s.launcher_type objResult.status = s.status objResult.enabled = s.enabled objResult.icon = s.icon resultdict[objResult.resource_name] = (objResult) keyList.append(objResult.resource_name) return (keyList, resultdict)
def test_subscriber_proxy(self): talker_data = rocon_python_comms.SubscriberProxy('chatter', String)() self.assertEquals("dude", talker_data.data)
def configure(self): self._gateway_info = rocon_python_comms.SubscriberProxy( self.gateway_namespace + '/gateway_info', gateway_msgs.GatewayInfo) self._remote_gateway_info = rospy.ServiceProxy( self.gateway_namespace + '/remote_gateway_info', gateway_srvs.RemoteGatewayInfo)