capability.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 rosbridge_library.internal.exceptions import InvalidArgumentException
00034 from rosbridge_library.internal.exceptions import MissingArgumentException
00035 
00036 
00037 class Capability:
00038     """ Handles the operation-specific logic of a rosbridge message
00039 
00040     May define one or more opcodes to handle, for example 'publish' or
00041     'call_service'
00042 
00043     Each connected client receives its own capability instance, which are
00044     managed by the client's own protocol instance.
00045 
00046     Protocol.send() is available to send messages back to the client.
00047 
00048     """
00049 
00050     def __init__(self, protocol):
00051         """ Abstract class constructor.  All capabilities require a handle to
00052         the containing protocol.
00053 
00054         Keyword arguments:
00055         protocol -- the protocol instance for this capability instance
00056 
00057         """
00058         self.protocol = protocol
00059 
00060     def handle_message(self, message):
00061         """ Handle an incoming message.
00062 
00063         Called by the protocol after having already checked the message op code
00064 
00065         Keyword arguments:
00066         message -- the incoming message, deserialized into a dictionary
00067 
00068         """
00069         pass
00070 
00071     def finish(self):
00072         """ Notify this capability that the client is finished and that it's
00073         time to free up resources. """
00074         pass
00075 
00076     def basic_type_check(self, msg, types_info):
00077         """ Performs basic typechecking on fields in msg.
00078 
00079         Keyword arguments:
00080         msg        -- a message, deserialized into a dictoinary
00081         types_info -- a list of tuples (mandatory, fieldname, fieldtype) where
00082                 mandatory - boolean, is the field mandatory
00083                 fieldname - the name of the field in the message
00084                 fieldtypes - the expected python type of the field or list of types
00085 
00086         Throws:
00087         MissingArgumentException -- if a field is mandatory but not present in
00088         the message
00089         InvalidArgumentException -- if a field is present but not of the type
00090         specified by fieldtype
00091 
00092         """
00093         for mandatory, fieldname, fieldtypes in types_info:
00094             if mandatory and fieldname not in msg:
00095                 raise MissingArgumentException("Expected a %s field but none was found." % fieldname)
00096             elif fieldname in msg:
00097                 if not isinstance(fieldtypes, tuple):
00098                     fieldtypes = (fieldtypes, )
00099                 valid = False
00100                 for typ in fieldtypes:
00101                     if isinstance(msg[fieldname], typ):
00102                         valid = True
00103                 if not valid:
00104                     raise InvalidArgumentException("Expected field %s to be one of %s. Invalid value: %s" % (fieldname, fieldtypes, msg[fieldname]))
00105 


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Wed Sep 13 2017 03:18:07