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 from functools import partial
00034 from rosbridge_library.capability import Capability
00035 from rosbridge_library.internal.services import ServiceCaller
00036 
00037 
00038 class CallService(Capability):
00039 
00040     call_service_msg_fields = [(True, "service", (str, unicode)),
00041            (False, "fragment_size", (int, type(None))), (False, "compression", (str, unicode))]
00042 
00043     def __init__(self, protocol):
00044         # Call superclas constructor
00045         Capability.__init__(self, protocol)
00046 
00047         # Register the operations that this capability provides
00048         protocol.register_operation("call_service", self.call_service)
00049 
00050     def call_service(self, message):
00051         # Pull out the ID
00052         cid = message.get("id", None)
00053         
00054         # Typecheck the args
00055         self.basic_type_check(message, self.call_service_msg_fields)
00056 
00057         # Extract the args
00058         service = message["service"]
00059         fragment_size = message.get("fragment_size", None)
00060         compression = message.get("compression", "none")
00061         args = message.get("args", [])
00062         
00063         # Check for deprecated service ID, eg. /rosbridge/topics#33
00064         cid = extract_id(service, cid)
00065 
00066         # Create the callbacks
00067         s_cb = partial(self._success, cid, service, fragment_size, compression)
00068         e_cb = partial(self._failure, cid, service)
00069 
00070         # Kick off the service caller thread
00071         ServiceCaller(trim_servicename(service), args, s_cb, e_cb).start()
00072 
00073     def _success(self, cid, service, fragment_size, compression, message):
00074         outgoing_message = {
00075             "op": "service_response",
00076             "service": service,
00077             "values": message,
00078             "result": True
00079         }
00080         if cid is not None:
00081             outgoing_message["id"] = cid
00082         # TODO: fragmentation, compression
00083         self.protocol.send(outgoing_message)
00084 
00085     def _failure(self, cid, service, exc):
00086         self.protocol.log("error", "call_service %s: %s" %
00087             (type(exc).__name__, str(exc)), cid)
00088         # send response with result: false
00089         outgoing_message = {
00090             "op": "service_response",
00091             "service": service,
00092             "values": str(exc),
00093             "result": False
00094         }
00095         if cid is not None:
00096             outgoing_message["id"] = cid
00097         self.protocol.send(outgoing_message)
00098 
00099 
00100 def trim_servicename(service):
00101     if '#' in service:
00102         return service[:service.find('#')]
00103     return service
00104 
00105 
00106 def extract_id(service, cid):
00107     if cid is not None:
00108         return cid
00109     elif '#' in service:
00110         return service[service.find('#') + 1:]


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Mon Oct 6 2014 06:58:09