capability.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 from rosbridge_library.internal.exceptions import InvalidArgumentException
34 from rosbridge_library.internal.exceptions import MissingArgumentException
35 
36 
37 class Capability:
38  """ Handles the operation-specific logic of a rosbridge message
39 
40  May define one or more opcodes to handle, for example 'publish' or
41  'call_service'
42 
43  Each connected client receives its own capability instance, which are
44  managed by the client's own protocol instance.
45 
46  Protocol.send() is available to send messages back to the client.
47 
48  """
49 
50  def __init__(self, protocol):
51  """ Abstract class constructor. All capabilities require a handle to
52  the containing protocol.
53 
54  Keyword arguments:
55  protocol -- the protocol instance for this capability instance
56 
57  """
58  self.protocol = protocol
59 
60  def handle_message(self, message):
61  """ Handle an incoming message.
62 
63  Called by the protocol after having already checked the message op code
64 
65  Keyword arguments:
66  message -- the incoming message, deserialized into a dictionary
67 
68  """
69  pass
70 
71  def finish(self):
72  """ Notify this capability that the client is finished and that it's
73  time to free up resources. """
74  pass
75 
76  def basic_type_check(self, msg, types_info):
77  """ Performs basic typechecking on fields in msg.
78 
79  Keyword arguments:
80  msg -- a message, deserialized into a dictoinary
81  types_info -- a list of tuples (mandatory, fieldname, fieldtype) where
82  mandatory - boolean, is the field mandatory
83  fieldname - the name of the field in the message
84  fieldtypes - the expected python type of the field or list of types
85 
86  Throws:
87  MissingArgumentException -- if a field is mandatory but not present in
88  the message
89  InvalidArgumentException -- if a field is present but not of the type
90  specified by fieldtype
91 
92  """
93  for mandatory, fieldname, fieldtypes in types_info:
94  if mandatory and fieldname not in msg:
95  raise MissingArgumentException("Expected a %s field but none was found." % fieldname)
96  elif fieldname in msg:
97  if not isinstance(fieldtypes, tuple):
98  fieldtypes = (fieldtypes, )
99  valid = False
100  for typ in fieldtypes:
101  if isinstance(msg[fieldname], typ):
102  valid = True
103  if not valid:
104  raise InvalidArgumentException("Expected field %s to be one of %s. Invalid value: %s" % (fieldname, fieldtypes, msg[fieldname]))
105 
def basic_type_check(self, msg, types_info)
Definition: capability.py:76


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Fri May 10 2019 02:17:02