Package rocon_gateway :: Module utils
[frames] | no frames]

Source Code for Module rocon_gateway.utils

  1  #!/usr/bin/env python 
  2  # 
  3  # License: BSD 
  4  #   https://raw.github.com/robotics-in-concert/rocon_multimaster/hydro-devel/rocon_gateway/LICENSE 
  5  # 
  6   
  7  ############################################################################## 
  8  # Imports 
  9  ############################################################################## 
 10   
 11  import json 
 12  import collections 
 13  from gateway_msgs.msg import Rule, ConnectionType 
 14   
 15  ############################################################################## 
 16  # Constants 
 17  ############################################################################## 
 18   
 19  # for help in iterating over the set of connection constants 
 20  connection_types = frozenset([ConnectionType.PUBLISHER, ConnectionType.SUBSCRIBER, ConnectionType.SERVICE, ConnectionType.ACTION_CLIENT, ConnectionType.ACTION_SERVER]) 
 21  connection_types_list = [ConnectionType.PUBLISHER, ConnectionType.SUBSCRIBER, ConnectionType.SERVICE, ConnectionType.ACTION_CLIENT, ConnectionType.ACTION_SERVER] 
 22  connection_type_strings_list = ["publisher", "subscriber", "service", "action_client", "action_server"] 
 23  action_types = ['/goal', '/cancel', '/status', '/feedback', '/result'] 
 24   
 25  ############################################################################## 
 26  # Rule 
 27  ############################################################################## 
 28   
 29   
30 -class Connection():
31 ''' 32 An object that represents a connection containing all the gory details 33 about a connection, allowing a connection to be passed through to a 34 foreign gateway 35 36 - rule (gateway_msgs.msg.Rule) (containing type,name,node) 37 - type_info (msg type for pubsub or service api for services) 38 - xmlrpc_uri (the xmlrpc node uri for the connection) 39 '''
40 - def __init__(self, rule, type_info, xmlrpc_uri):
41 ''' 42 @param type_info : either topic_type (pubsub), service api (service) or ??? (action) 43 @type string 44 ''' 45 self.rule = rule 46 self.type_info = type_info 47 self.xmlrpc_uri = xmlrpc_uri
48
49 - def __eq__(self, other):
50 if isinstance(other, self.__class__): 51 return self.__dict__ == other.__dict__ 52 else: 53 return False
54
55 - def __ne__(self, other):
56 return not self.__eq__(other)
57
58 - def __str__(self):
59 if self.rule.type == ConnectionType.SERVICE: 60 return '{type: %s, name: %s, node: %s, uri: %s, service_api: %s}' % (self.rule.type, self.rule.name, self.rule.node, self.xmlrpc_uri, self.type_info) 61 else: 62 return '{type: %s, name: %s, node: %s, uri: %s, topic_type: %s}' % (self.rule.type, self.rule.name, self.rule.node, self.xmlrpc_uri, self.type_info)
63
64 - def __repr__(self):
65 return self.__str__()
66 67 68 ############################################################################## 69 # Registration 70 ############################################################################## 71
72 -class Registration():
73 ''' 74 An object that represents a connection registered with the local 75 master (or about to be registered). This has all the gory detail 76 for the connection. It includes the gateway name it originated 77 from as well as master registration information. 78 79 - connection (the remote connection information) 80 - remote_gateway (the remote gateway from where this connection originated) 81 - local_node (the local anonymously generated node name) 82 '''
83 - def __init__(self, connection, remote_gateway, local_node=None):
84 ''' 85 @param connection : string identifier storing the remote connection details (type, name, node) 86 @type gateway_msgs.msg.RemoteRule 87 88 @param remote_gateway : string identifier for where this registration game from 89 @type string 90 91 @param local_node : the local node that this registration is created under 92 @type string 93 ''' 94 self.connection = connection 95 self.remote_gateway = remote_gateway 96 self.local_node = local_node
97
98 - def __eq__(self, other):
99 if isinstance(other, self.__class__): 100 return self.__dict__ == other.__dict__ 101 else: 102 return False
103
104 - def __ne__(self, other):
105 return not self.__eq__(other)
106
107 - def __str__(self):
108 return '[%s]%s' % (self.remote_gateway, format_rule(self.connection.rule))
109
110 - def __repr__(self):
111 return self.__str__()
112 113 ########################################################################## 114 # serialization/deserialization Functions 115 ########################################################################## 116 117
118 -def convert(data):
119 ''' 120 Convert unicode to standard string (Not sure how necessary this is) 121 http://stackoverflow.com/questions/1254454/fastest-way-to-convert-a-dicts-keys-values-from-unicode-to-str 122 ''' 123 if isinstance(data, unicode): 124 return str(data) 125 elif isinstance(data, collections.Mapping): 126 return dict(map(convert, data.iteritems())) 127 elif isinstance(data, collections.Iterable): 128 return type(data)(map(convert, data)) 129 else: 130 return data
131 132
133 -def serialize(data):
134 return json.dumps(data)
135 136
137 -def deserialize(str_msg):
138 return convert(json.loads(str_msg))
139 140
141 -def serialize_connection(connection):
142 return serialize([connection.rule.type, 143 connection.rule.name, 144 connection.rule.node, 145 connection.type_info, 146 connection.xmlrpc_uri] 147 )
148 149
150 -def deserialize_connection(connection_str):
151 deserialized_list = deserialize(connection_str) 152 rule = Rule(deserialized_list[0], 153 deserialized_list[1], 154 deserialized_list[2] 155 ) 156 return Connection(rule, deserialized_list[3], deserialized_list[4])
157 158
159 -def serialize_connection_request(command, source, connection):
160 return serialize([command, source, 161 connection.rule.type, 162 connection.rule.name, 163 connection.rule.node, 164 connection.type_info, 165 connection.xmlrpc_uri] 166 )
167 168
169 -def serialize_rule_request(command, source, rule):
170 return serialize([command, source, rule.type, rule.name, rule.node])
171 172
173 -def deserialize_request(request_str):
174 deserialized_list = deserialize(request_str) 175 return deserialized_list[0], deserialized_list[1], deserialized_list[2:]
176 177
178 -def get_connection_from_list(connection_argument_list):
179 rule = Rule(connection_argument_list[0], connection_argument_list[1], connection_argument_list[2]) 180 return Connection(rule, connection_argument_list[3], connection_argument_list[4])
181 182
183 -def get_rule_from_list(rule_argument_list):
184 return Rule(rule_argument_list[0], rule_argument_list[1], rule_argument_list[2])
185 186 ########################################################################## 187 # Regex 188 ########################################################################## 189 190
191 -def is_all_pattern(pattern):
192 ''' 193 Convenience function for detecting the 'all' pattern. 194 195 @param pattern : the name rule string for the flip all concept 196 @type str 197 @return true if matching, false otherwise 198 @rtype Bool 199 ''' 200 if pattern == ".*": 201 return True 202 else: 203 return False
204 205 206 ########################################################################## 207 # Formatters 208 ########################################################################## 209
210 -def format_rule(rule):
211 return '[%s][%s][%s]' % (rule.type, rule.name, rule.node)
212 213 ########################################################################## 214 # Factories 215 ########################################################################## 216 217
218 -def create_empty_connection_type_dictionary():
219 ''' 220 Used to initialise a dictionary with rule type keys 221 and empty lists. 222 ''' 223 dic = {} 224 for connection_type in connection_types: 225 dic[connection_type] = [] 226 return dic
227