def _flip_connections(self, remote_name, connection_names, connection_type, cancel_flag=False): ''' (Un)Flip a service to a remote gateway. :param remote_name: the name of the remote gateway to flip to. :type remote_name: str :param connection_names: the topic/service/action_xxx names :type connetion_names: list of str :param connection_type: publisher, subscriber, service, action_client or action_server :type connection_type: gateway_msgs.ConnectionType :param cancel_flag: whether or not we are flipping (false) or unflipping (true) :type cancel_flag: bool ''' if len(connection_names) == 0: return req = gateway_srvs.RemoteRequest() req.cancel = cancel_flag req.remotes = [] for connection_name in connection_names: req.remotes.append(create_gateway_remote_rule(remote_name, create_gateway_rule(connection_name, connection_type))) try: resp = self._gateway_services['flip'](req) except rospy.service.ServiceException: # often disappears when the gateway shuts down just before the app manager, ignore silently. return if resp.result == 0: rospy.loginfo("Rapp Manager : successfully flipped %s" % str([os.path.basename(name) for name in connection_names])) else: if resp.result == gateway_msgs.ErrorCodes.NO_HUB_CONNECTION and cancel_flag: # can often happen if a concert goes down and brings the hub down as as well # so just suppress this warning if it's a request to cancel rospy.logwarn("Rapp Manager : failed to cancel flips (probably remote hub intentionally went down as well) [%s, %s]" % (resp.result, resp.error_message)) else: rospy.logerr("Rapp Manager : failed to flip [%s, %s]" % (resp.result, resp.error_message))
def _set_gateway_flip_rules(self, cancel_flag=False): """ Converts the concert whitelist into a set of remote gateway flip rules for the namespaces in which the rapp manager will drop 'to be shared' communications. """ req = gateway_srvs.RemoteRequest() req.cancel = cancel_flag flip_request_service = rospy.ServiceProxy('~flip', gateway_srvs.Remote) for connection_type in rocon_gateway.connection_types: concert_namespace_rule = rocon_gateway_utils.create_gateway_rule(name="/concert/.*", connection_type=connection_type) # self.parameters.application namespace always finishes with a trailing slash because of 'rosgraph.names.make_global_ns()' called on it. applications_namespace_rule = rocon_gateway_utils.create_gateway_rule(name=self.parameters.application_namespace + ".*", connection_type=connection_type) for concert_remote_gateway in self.concert_parameters.concert_whitelist: req.remotes.append(rocon_gateway_utils.create_gateway_remote_rule(concert_remote_gateway, concert_namespace_rule)) req.remotes.append(rocon_gateway_utils.create_gateway_remote_rule(concert_remote_gateway, applications_namespace_rule)) if not self.concert_parameters.concert_whitelist: req.remotes.append(rocon_gateway_utils.create_gateway_remote_rule(".*", concert_namespace_rule)) req.remotes.append(rocon_gateway_utils.create_gateway_remote_rule(".*", applications_namespace_rule)) warning_throttle_counter = 0 rate = rospy.Rate(10) # 10hz while not rospy.is_shutdown(): try: resp = flip_request_service(req) except rospy.service.ServiceException: raise GatewayNotFoundException() # often disappears when the gateway shuts down just before the app manager, ignore silently. if resp.result == gateway_msgs.ErrorCodes.SUCCESS: rospy.loginfo("Rapp Manager : set flip rules on the local gateway [remotes: %s]" % self.concert_parameters.concert_whitelist) break else: if resp.result == gateway_msgs.ErrorCodes.NO_HUB_CONNECTION and cancel_flag: # can often happen if a concert goes down and brings the hub down as as well # so just suppress this warning if it's a request to cancel rospy.logwarn("Rapp Manager : failed to cancel flips (probably remote hub intentionally went down as well) [%s, %s]" % (resp.result, resp.error_message)) raise GatewayNotFoundException() # often disappears when the gateway shuts down just before the app manager, ignore silently. else: warning_throttle_counter += 1 if warning_throttle_counter % 10 == 0: rospy.logerr("Rapp Manager : could not set flip rules on the local gateway") rate.sleep()
def _advertise_services(self, service_names): ''' Advertise rocon_app_manager services via the gateway, if it is available. :param service_names: rocon app manager service names :type service_names: strs ''' if self._gateway_name: req = gateway_srvs.AdvertiseRequest() req.cancel = False req.rules = [] for service_name in service_names: req.rules.append(create_gateway_rule(service_name, gateway_msgs.ConnectionType.SERVICE)) unused_resp = self._gateway_services['advertise'](req)
def _advertise_services(self, service_names): ''' Advertise rocon_app_manager services via the gateway, if it is available. :param service_names: rocon app manager service names :type service_names: strs ''' if self._gateway_name: req = gateway_srvs.AdvertiseRequest() req.cancel = False req.rules = [] for service_name in service_names: req.rules.append( create_gateway_rule(service_name, gateway_msgs.ConnectionType.SERVICE)) unused_resp = self._gateway_services['advertise'](req)
def _flip_connections(self, remote_name, connection_names, connection_type, cancel_flag=False): ''' (Un)Flip a service to a remote gateway. @param remote_name : the name of the remote gateway to flip to. @type str @param connection_names : the topic/service/action_xxx names @type list of str @param connection_type : publisher, subscriber, service, action_client or action_server @type gateway_msgs.ConnectionType @param cancel_flag : whether or not we are flipping (false) or unflipping (true) @type bool ''' if len(connection_names) == 0: return req = gateway_srvs.RemoteRequest() req.cancel = cancel_flag req.remotes = [] for connection_name in connection_names: req.remotes.append( create_gateway_remote_rule( remote_name, create_gateway_rule(connection_name, connection_type))) try: resp = self._gateway_services['flip'](req) except rospy.service.ServiceException: # often disappears when the gateway shuts down just before the app manager, ignore silently. return if resp.result == 0: rospy.loginfo( "App Manager : successfully flipped %s" % str([os.path.basename(name) for name in connection_names])) else: if resp.result == gateway_msgs.ErrorCodes.NO_HUB_CONNECTION and cancel_flag: # can often happen if a concert goes down and brings the hub down as as well # so just suppress this warning if it's a request to cancel rospy.logwarn( "App Manager : failed to cancel flips (probably remote hub intentionally went down as well) [%s, %s]" % (resp.result, resp.error_message)) else: rospy.logerr("App Manager : failed to flip [%s, %s]" % (resp.result, resp.error_message))
def _flip_all_connections(self, remote_name, connections, cancel_flag=False): ''' (Un)Flip connections to a remote gateway. :param remote_name: the name of the remote gateway to flip to. :type remote_name: str :param connections: the dict of connection types(topic/service/action_xxx) names :type connetions: dict :param cancel_flag: whether or not we are flipping (false) or unflipping (true) :type cancel_flag: bool :returns: success or not, message :rtype: bool, str ''' success = False message = "" req = gateway_srvs.RemoteRequest() req.cancel = cancel_flag req.remotes = [] # Create request for conn_type, conns in connections.items(): gateway_type = plurality_converter[conn_type] for conn_name in conns: remote_rule = create_gateway_remote_rule( remote_name, create_gateway_rule(conn_name, gateway_type)) req.remotes.append(remote_rule) # Send request attempts = 0 MAX_ATTEMPTS = 4 while not success and attempts < MAX_ATTEMPTS and not rospy.is_shutdown( ): try: resp = self._gateway_services['flip'](req) except rospy.service.ServiceException: # often disappears when the gateway shuts down just before the app manager, ignore silently. success = False message = "Flip service has disappeared" attempts = attempts + 1 continue if resp.result == gateway_msgs.ErrorCodes.SUCCESS: rospy.loginfo("Rapp Manager : successfully flipped %s" % str([(names) for names in connections.values()])) success = True else: if resp.result == gateway_msgs.ErrorCodes.NO_HUB_CONNECTION and cancel_flag: # can often happen if a concert goes down and brings the hub down as as well # so just suppress this warning if it's a request to cancel rospy.logwarn( "Rapp Manager : failed to cancel flips (probably remote hub intentionally went down as well) [%s, %s]" % (resp.result, resp.error_message)) else: message = "failed to flip [%s][%s, %s]" % ( str(connections), resp.result, resp.error_message) rospy.logerr("Rapp Manager : %s" % message) success = False attempts = attempts + 1 rospy.sleep(1.0) return success, message