11 import cPickle
as pickle
17 from Crypto.PublicKey
import RSA
18 import Crypto.Util.number
as CUN
20 import gateway_msgs.msg
as gateway_msgs
27 connection_types = frozenset([gateway_msgs.ConnectionType.PUBLISHER, gateway_msgs.ConnectionType.SUBSCRIBER,
28 gateway_msgs.ConnectionType.SERVICE, gateway_msgs.ConnectionType.ACTION_CLIENT, gateway_msgs.ConnectionType.ACTION_SERVER])
29 connection_types_list = [gateway_msgs.ConnectionType.PUBLISHER, gateway_msgs.ConnectionType.SUBSCRIBER,
30 gateway_msgs.ConnectionType.SERVICE, gateway_msgs.ConnectionType.ACTION_CLIENT, gateway_msgs.ConnectionType.ACTION_SERVER]
31 connection_type_strings_list = [
"publisher",
"subscriber",
"service",
"action_client",
"action_server"]
32 action_types = [
'/goal',
'/cancel',
'/status',
'/feedback',
'/result']
42 An object that represents a connection containing all the gory details 43 about a connection, allowing a connection to be passed through to a 46 - rule (gateway_msgs.msg.Rule) (containing type,name,node) 47 - type_info (msg type for pubsub or service api for services) 48 - xmlrpc_uri (the xmlrpc node uri for the connection) 51 def __init__(self, rule, type_msg, type_info, xmlrpc_uri):
53 @param type_msg : message type of the topic or service 54 @param type_info : either topic_type (pubsub), service api (service) or ??? (action) 63 if isinstance(other, self.__class__):
69 return not self.
__eq__(other)
75 if self.rule.type == gateway_msgs.ConnectionType.SERVICE:
76 return '{type: %s, name: %s, node: %s, uri: %s, service_api: %s, service_type: %s}' % (
79 return '{type: %s, name: %s, node: %s, uri: %s, topic_type: %s}' % (
87 Checks to see if this connection has the same rule 88 as an item in the given connection_list 90 @param connection_list : connection list to trawl. 91 @type utils.Connection[] 92 @return true if this connection rule matches a connection rule in the list 95 for connection
in connection_list:
102 Checks for equivalency regardless of type_info and xmlrpc_uri. 104 @param connection : connection to compare with 105 @type utils.Connection 106 @return true if equivalent, false otherwise 109 return (self.rule.name == connection.rule.name
and 110 self.rule.type == connection.rule.type
and 111 self.rule.node == connection.rule.node)
121 An object that represents a connection registered with the local 122 master (or about to be registered). This has all the gory detail 123 for the connection. It includes the gateway name it originated 124 from as well as master registration information. 126 - connection (the remote connection information) 127 - remote_gateway (the remote gateway from where this connection originated) 128 - local_node (the local anonymously generated node name) 131 def __init__(self, connection, remote_gateway, local_node=None):
133 @param connection : string identifier storing the remote connection details (type, name, node) 134 @type gateway_msgs.msg.RemoteRule 136 @param remote_gateway : string identifier for where this registration game from 139 @param local_node : the local node that this registration is created under 147 if isinstance(other, self.__class__):
153 return not self.
__eq__(other)
183 return pickle.dumps(data)
189 deserialized_data = pickle.loads(str_msg)
190 except ValueError
as e:
191 rospy.logwarn(
"Gateway : error in deserialization [%s]" % e)
193 print(traceback.format_exc())
194 print(
"Data : %s" % str_msg)
195 return deserialized_data
201 connection.rule.name,
202 connection.rule.node,
204 connection.type_info,
205 connection.xmlrpc_uri]
211 rule = gateway_msgs.Rule(deserialized_list[0],
212 deserialized_list[1],
215 return Connection(rule, deserialized_list[3], deserialized_list[4], deserialized_list[5])
220 connection.rule.type,
221 connection.rule.name,
222 connection.rule.node,
224 connection.type_info,
225 connection.xmlrpc_uri]
230 return serialize([command, source, rule.type, rule.name, rule.node])
235 return deserialized_list[0], deserialized_list[1], deserialized_list[2:]
239 rule = gateway_msgs.Rule(connection_argument_list[0], connection_argument_list[1], connection_argument_list[2])
240 return Connection(rule, connection_argument_list[3], connection_argument_list[4], connection_argument_list[5])
244 return gateway_msgs.Rule(rule_argument_list[0], rule_argument_list[1], rule_argument_list[2])
250 MAX_PLAINTEXT_LENGTH = 256
254 key = RSA.generate(8 * MAX_PLAINTEXT_LENGTH)
255 public_key = key.publickey()
256 return key, public_key
260 return RSA.importKey(serialized_key)
264 return key.exportKey()
268 if len(plaintext) > MAX_PLAINTEXT_LENGTH:
270 raise ValueError(
'Trying to encrypt text longer than ' + MAX_PLAINTEXT_LENGTH +
' bytes!')
271 K = CUN.getRandomNumber(128, os.urandom)
272 ciphertext = public_key.encrypt(plaintext, K)
278 return key.decrypt(ciphertext)
283 decrypted_connection = copy.deepcopy(connection)
284 decrypted_connection.type_info =
decrypt(connection.type_info, key)
285 decrypted_connection.xmlrpc_uri =
decrypt(connection.xmlrpc_uri, key)
286 return decrypted_connection
290 encrypted_connection = copy.deepcopy(connection)
291 encrypted_connection.type_info =
encrypt(connection.type_info, key)
292 encrypted_connection.xmlrpc_uri =
encrypt(connection.xmlrpc_uri, key)
293 return encrypted_connection
302 Convenience function for detecting the 'all' pattern. 304 @param pattern : the name rule string for the flip all concept 306 @return true if matching, false otherwise 320 return '[%s][%s][%s]' % (rule.type, rule.name, rule.node)
329 Used to initialise a dictionary with rule type keys 330 and empty lists or sets (or whatever collection_type passed as argument). 333 for connection_type
in connection_types:
334 dic[connection_type] = collection_type()
337 difflist =
lambda l1, l2: [x
for x
in l1
if x
not in l2]
347 for name, service
in ccproxy_channel_dict.iteritems():
348 service_name = service.name
349 service_type = service.type
350 service_uri = service.xmlrpc_uri
351 nodes = service.nodes
353 connection =
Connection(gateway_msgs.Rule(connection_type, service_name, node[0]), service_type, service_uri, node[1])
354 connections.add(connection)
360 for name, topic
in ccproxy_channel_dict.iteritems():
361 topic_name = topic.name
362 topic_type = topic.type
365 connection =
Connection(gateway_msgs.Rule(connection_type, topic_name, node[0]), topic_type, topic_type, node[1])
366 connections.add(connection)
372 for name, action
in ccproxy_channel_dict.iteritems():
373 action_name = action.name
374 goal_topic_type = action.type
377 connection =
Connection(gateway_msgs.Rule(connection_type, action_name, node[0]), goal_topic_type, goal_topic_type, node[1])
378 connections.add(connection)
def decrypt(ciphertext, key)
def is_all_pattern(pattern)
Regex.
def create_empty_connection_type_dictionary(collection_type=list)
Factories.
def decrypt_connection(connection, key)
def _get_connections_from_action_chan_dict(ccproxy_channel_dict, connection_type)
def __init__(self, connection, remote_gateway, local_node=None)
def hasSameRule(self, connection)
def get_rule_from_list(rule_argument_list)
def __init__(self, rule, type_msg, type_info, xmlrpc_uri)
def generate_private_public_key()
def serialize_connection_request(command, source, connection)
def serialize_connection(connection)
def format_rule(rule)
Formatters.
def deserialize_request(request_str)
def get_connection_from_list(connection_argument_list)
def _get_connections_from_pub_sub_chan_dict(ccproxy_channel_dict, connection_type)
def encrypt_connection(connection, key)
def serialize_rule_request(command, source, rule)
def inConnectionList(self, connection_list)
def deserialize_key(serialized_key)
def encrypt(plaintext, public_key)
def deserialize_connection(connection_str)
def _get_connections_from_service_chan_dict(ccproxy_channel_dict, connection_type)
Conversion from Connection Cache Proxy channels (as passed in callback) to Gateway connections...