call_service.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 import fnmatch
00034 from functools import partial
00035 from rosbridge_library.capability import Capability
00036 from rosbridge_library.internal.services import ServiceCaller
00037 from rosbridge_library.util import string_types
00038 
00039 
00040 class CallService(Capability):
00041 
00042     call_service_msg_fields = [(True, "service", string_types),
00043                                (False, "fragment_size", (int, type(None))),
00044                                (False, "compression", string_types)]
00045 
00046     services_glob = None
00047 
00048     def __init__(self, protocol):
00049         # Call superclas constructor
00050         Capability.__init__(self, protocol)
00051 
00052         # Register the operations that this capability provides
00053         protocol.register_operation("call_service", self.call_service)
00054 
00055     def call_service(self, message):
00056         # Pull out the ID
00057         cid = message.get("id", None)
00058 
00059         # Typecheck the args
00060         self.basic_type_check(message, self.call_service_msg_fields)
00061 
00062         # Extract the args
00063         service = message["service"]
00064         fragment_size = message.get("fragment_size", None)
00065         compression = message.get("compression", "none")
00066         args = message.get("args", [])
00067 
00068         if CallService.services_glob is not None and CallService.services_glob:
00069             self.protocol.log("debug", "Service security glob enabled, checking service: " + service)
00070             match = False
00071             for glob in CallService.services_glob:
00072                 if (fnmatch.fnmatch(service, glob)):
00073                     self.protocol.log("debug", "Found match with glob " + glob + ", continuing service call...")
00074                     match = True
00075                     break
00076             if not match:
00077                 self.protocol.log("warn", "No match found for service, cancelling service call for: " + service)
00078                 return
00079         else:
00080             self.protocol.log("debug", "No service security glob, not checking service call.")
00081 
00082         # Check for deprecated service ID, eg. /rosbridge/topics#33
00083         cid = extract_id(service, cid)
00084 
00085         # Create the callbacks
00086         s_cb = partial(self._success, cid, service, fragment_size, compression)
00087         e_cb = partial(self._failure, cid, service)
00088 
00089         # Kick off the service caller thread
00090         ServiceCaller(trim_servicename(service), args, s_cb, e_cb).start()
00091 
00092     def _success(self, cid, service, fragment_size, compression, message):
00093         outgoing_message = {
00094             "op": "service_response",
00095             "service": service,
00096             "values": message,
00097             "result": True
00098         }
00099         if cid is not None:
00100             outgoing_message["id"] = cid
00101         # TODO: fragmentation, compression
00102         self.protocol.send(outgoing_message)
00103 
00104     def _failure(self, cid, service, exc):
00105         self.protocol.log("error", "call_service %s: %s" %
00106                           (type(exc).__name__, str(exc)), cid)
00107         # send response with result: false
00108         outgoing_message = {
00109             "op": "service_response",
00110             "service": service,
00111             "values": str(exc),
00112             "result": False
00113         }
00114         if cid is not None:
00115             outgoing_message["id"] = cid
00116         self.protocol.send(outgoing_message)
00117 
00118 
00119 def trim_servicename(service):
00120     if '#' in service:
00121         return service[:service.find('#')]
00122     return service
00123 
00124 
00125 def extract_id(service, cid):
00126     if cid is not None:
00127         return cid
00128     elif '#' in service:
00129         return service[service.find('#') + 1:]


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