00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 import json
00012 import collections
00013 from gateway_msgs.msg import Rule, ConnectionType
00014
00015
00016
00017
00018
00019
00020 connection_types = frozenset([ConnectionType.PUBLISHER, ConnectionType.SUBSCRIBER, ConnectionType.SERVICE, ConnectionType.ACTION_CLIENT, ConnectionType.ACTION_SERVER])
00021 connection_types_list = [ConnectionType.PUBLISHER, ConnectionType.SUBSCRIBER, ConnectionType.SERVICE, ConnectionType.ACTION_CLIENT, ConnectionType.ACTION_SERVER]
00022 connection_type_strings_list = ["publisher","subscriber","service","action_client","action_server"]
00023 action_types = ['/goal', '/cancel', '/status', '/feedback', '/result']
00024
00025
00026
00027
00028
00029 class Connection():
00030 '''
00031 An object that represents a connection containing all the gory details
00032 about a connection, allowing a connection to be passed through to a
00033 foreign gateway
00034
00035 - rule (gateway_msgs.msg.Rule) (containing type,name,node)
00036 - type_info (msg type for pubsub or service api for services)
00037 - xmlrpc_uri (the xmlrpc node uri for the connection)
00038 '''
00039 def __init__(self, rule, type_info, xmlrpc_uri):
00040 '''
00041 @param type_info : either topic_type (pubsub), service api (service) or ??? (action)
00042 @type string
00043 '''
00044 self.rule = rule
00045 self.type_info = type_info
00046 self.xmlrpc_uri = xmlrpc_uri
00047
00048 def __eq__(self, other):
00049 if isinstance(other, self.__class__):
00050 return self.__dict__ == other.__dict__
00051 else:
00052 return False
00053
00054 def __ne__(self, other):
00055 return not self.__eq__(other)
00056
00057 def __str__(self):
00058 if self.rule.type == ConnectionType.SERVICE:
00059 return '{%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)
00060 else:
00061 return '{%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)
00062
00063 def __repr__(self):
00064 return self.__str__()
00065
00066
00067
00068
00069
00070
00071 class Registration():
00072 '''
00073 An object that represents a connection registered with the local
00074 master (or about to be registered). This has all the gory detail
00075 for the connection. It includes the gateway name it originated
00076 from as well as master registration information.
00077
00078 - connection (the remote connection information)
00079 - remote_gateway (the remote gateway from where this connection originated)
00080 - local_node (the local anonymously generated node name)
00081 '''
00082 def __init__(self, connection, remote_gateway, local_node = None):
00083 '''
00084 @param connection : string identifier storing the remote connection details (type, name, node)
00085 @type gateway_msgs.msg.RemoteRule
00086
00087 @param remote_gateway : string identifier for where this registration game from
00088 @type string
00089
00090 @param local_node : the local node that this registration is created under
00091 @type string
00092 '''
00093 self.connection = connection
00094 self.remote_gateway = remote_gateway
00095 self.local_node = local_node
00096
00097 def __eq__(self, other):
00098 if isinstance(other, self.__class__):
00099 return self.__dict__ == other.__dict__
00100 else:
00101 return False
00102
00103 def __ne__(self, other):
00104 return not self.__eq__(other)
00105
00106 def __str__(self):
00107 return '{gateway %s: %s}'%(self.remote_gateway,formatRule(self.connection.rule))
00108
00109 def __repr__(self):
00110 return self.__str__()
00111
00112
00113
00114
00115
00116 def convert(data):
00117 '''
00118 Convert unicode to standard string (Not sure how necessary this is)
00119 http://stackoverflow.com/questions/1254454/fastest-way-to-convert-a-dicts-keys-values-from-unicode-to-str
00120 '''
00121 if isinstance(data, unicode):
00122 return str(data)
00123 elif isinstance(data, collections.Mapping):
00124 return dict(map(convert, data.iteritems()))
00125 elif isinstance(data, collections.Iterable):
00126 return type(data)(map(convert, data))
00127 else:
00128 return data
00129
00130 def serialize(data):
00131 return json.dumps(data)
00132
00133 def deserialize(str_msg):
00134 return convert(json.loads(str_msg))
00135
00136 def serialize_connection(connection):
00137 return serialize([connection.rule.type,connection.rule.name,connection.rule.node,connection.type_info,connection.xmlrpc_uri])
00138
00139 def deserialize_connection(connection_str):
00140 list = deserialize(connection_str)
00141 rule = Rule(list[0],list[1],list[2])
00142 return Connection(rule, list[3], list[4])
00143
00144 def serialize_connection_request(command, source, connection):
00145 return serialize([command,source,connection.rule.type,connection.rule.name,connection.rule.node,connection.type_info,connection.xmlrpc_uri])
00146
00147 def serialize_rule_request(command,source,rule):
00148 return serialize([command,source,rule.type,rule.name,rule.node])
00149
00150 def deserialize_request(request_str):
00151 list = deserialize(request_str)
00152 return list[0], list[1], list[2:]
00153
00154 def get_connection_from_list(list):
00155 rule = Rule(list[0],list[1],list[2])
00156 return Connection(rule,list[3],list[4])
00157
00158 def get_rule_from_list(list):
00159 return Rule(list[0],list[1],list[2])
00160
00161
00162
00163
00164
00165
00166 def isAllPattern(pattern):
00167 '''
00168 Convenience function for detecting the flip all pattern.
00169
00170 @todo move to utils - should be shared with the public interface.
00171
00172 @param pattern : the name rule string for the flip all concept
00173 @type str
00174 @return true if matching, false otherwise
00175 @rtype Bool
00176 '''
00177 if pattern == ".*":
00178 return True
00179 else:
00180 return False
00181
00182
00183
00184
00185
00186
00187 def formatRule(rule):
00188 return '{type: %s, name/regex: %s, node-name/regex: %s}' % (rule.type, rule.name, rule.node)
00189
00190
00191
00192
00193
00194 def createEmptyConnectionTypeDictionary():
00195 '''
00196 Used to initialise a dictionary with rule type keys
00197 and empty lists.
00198 '''
00199 dic = {}
00200 for type in connection_types:
00201 dic[type] = []
00202 return dic