Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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