advertise_service.py
Go to the documentation of this file.
00001 from rosbridge_library.internal.ros_loader import get_service_class
00002 from rosbridge_library.internal import message_conversion
00003 from rosbridge_library.capability import Capability
00004 from rosbridge_library.util import string_types
00005 import fnmatch
00006 import rospy
00007 import time
00008 
00009 
00010 class AdvertisedServiceHandler():
00011 
00012     id_counter = 1
00013     responses = {}
00014 
00015     services_glob = None
00016 
00017     def __init__(self, service_name, service_type, protocol):
00018         self.service_name = service_name
00019         self.service_type = service_type
00020         self.protocol = protocol
00021         # setup the service
00022         self.service_handle = rospy.Service(service_name, get_service_class(service_type), self.handle_request)
00023 
00024     def next_id(self):
00025         id = self.id_counter
00026         self.id_counter += 1
00027         return id
00028 
00029     def handle_request(self, req):
00030         # generate a unique ID
00031         request_id = "service_request:" + self.service_name + ":" + str(self.next_id())
00032 
00033         # build a request to send to the external client
00034         request_message = {
00035             "op": "call_service",
00036             "id": request_id,
00037             "service": self.service_name,
00038             "args": message_conversion.extract_values(req)
00039         }
00040         self.protocol.send(request_message)
00041 
00042         # wait for a response
00043         while request_id not in self.responses.keys():
00044             time.sleep(0)
00045 
00046         resp = self.responses[request_id]
00047         del self.responses[request_id]
00048         return resp
00049 
00050 
00051 class AdvertiseService(Capability):
00052 
00053     advertise_service_msg_fields = [(True, "service", string_types), (True, "type", string_types)]
00054 
00055     def __init__(self, protocol):
00056         # Call superclass constructor
00057         Capability.__init__(self, protocol)
00058 
00059         # Register the operations that this capability provides
00060         protocol.register_operation("advertise_service", self.advertise_service)
00061 
00062     def advertise_service(self, message):
00063         # parse the incoming message
00064         service_name = message["service"]
00065 
00066         if AdvertiseService.services_glob is not None and AdvertiseService.services_glob:
00067             self.protocol.log("debug", "Service security glob enabled, checking service: " + service_name)
00068             match = False
00069             for glob in AdvertiseService.services_glob:
00070                 if (fnmatch.fnmatch(service_name, glob)):
00071                     self.protocol.log("debug", "Found match with glob " + glob + ", continuing service advertisement...")
00072                     match = True
00073                     break
00074             if not match:
00075                 self.protocol.log("warn", "No match found for service, cancelling service advertisement for: " + service_name)
00076                 return
00077         else:
00078             self.protocol.log("debug", "No service security glob, not checking service advertisement.")
00079 
00080         # check for an existing entry
00081         if service_name in self.protocol.external_service_list.keys():
00082             self.protocol.log("warn", "Duplicate service advertised. Overwriting %s." % service_name)
00083             self.protocol.external_service_list[service_name].service_handle.shutdown("Duplicate advertiser.")
00084             del self.protocol.external_service_list[service_name]
00085 
00086         # setup and store the service information
00087         service_type = message["type"]
00088         service_handler = AdvertisedServiceHandler(service_name, service_type, self.protocol)
00089         self.protocol.external_service_list[service_name] = service_handler
00090         self.protocol.log("info", "Advertised service %s." % service_name)


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Thu Jun 6 2019 21:51:43