1
2
3
4
5
6
7
8
9
10
11 import json
12 import collections
13 from gateway_msgs.msg import Rule, ConnectionType
14
15
16
17
18
19
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
27
28
29
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
50 if isinstance(other, self.__class__):
51 return self.__dict__ == other.__dict__
52 else:
53 return False
54
56 return not self.__eq__(other)
57
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
66
67
68
69
70
71
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
99 if isinstance(other, self.__class__):
100 return self.__dict__ == other.__dict__
101 else:
102 return False
103
105 return not self.__eq__(other)
106
108 return '[%s]%s' % (self.remote_gateway, format_rule(self.connection.rule))
109
112
113
114
115
116
117
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
134 return json.dumps(data)
135
136
138 return convert(json.loads(str_msg))
139
140
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
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
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
170 return serialize([command, source, rule.type, rule.name, rule.node])
171
172
174 deserialized_list = deserialize(request_str)
175 return deserialized_list[0], deserialized_list[1], deserialized_list[2:]
176
177
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
184 return Rule(rule_argument_list[0], rule_argument_list[1], rule_argument_list[2])
185
186
187
188
189
190
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
208
209
212
213
214
215
216
217
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