Package rocon_hub_client :: Module hub_client
[frames] | no frames]

Source Code for Module rocon_hub_client.hub_client

 1  #!/usr/bin/env python 
 2  # 
 3  # License: BSD 
 4  #   https://raw.github.com/robotics-in-concert/rocon_multimaster/master/rocon_hub_client/LICENSE 
 5  # 
 6  ############################################################################### 
 7  # Imports 
 8  ############################################################################### 
 9   
10  import redis 
11  import rospy 
12  from urlparse import urlparse 
13  import rocon_utilities 
14   
15  # local imports 
16  import hub_api 
17  from .exceptions import HubNameNotFoundError, HubNotFoundError, \ 
18                          HubConnectionBlacklistedError, HubConnectionNotWhitelistedError 
19   
20  ############################################################################## 
21  # Hub 
22  ############################################################################## 
23   
24   
25 -class Hub(object):
26
27 - def __init__(self, ip, port, whitelist=[], blacklist=[]):
28 ''' 29 @param remote_gateway_request_callbacks : to handle redis responses 30 @type list of function pointers (back to GatewaySync class 31 32 @param ip : redis server ip 33 @param port : redis server port 34 35 @raise HubNameNotFoundError, HubNotFoundError 36 ''' 37 # variables 38 self.uri = str(ip) + ":" + str(port) 39 self._redis_keys = {} 40 self._redis_channels = {} 41 42 # redis server connection 43 try: 44 self.pool = redis.ConnectionPool(host=ip, port=port, db=0) 45 self._redis_server = redis.Redis(connection_pool=self.pool) 46 self._redis_pubsub_server = self._redis_server.pubsub() 47 hub_key_name = self._redis_server.get("rocon:hub:name") 48 # Be careful, hub_name is None, it means the redis server is 49 # found but hub_name not yet set or not set at all. 50 if not hub_key_name: 51 self._redis_server = None 52 raise HubNameNotFoundError("couldn't resolve hub name on the redis server [%s:%s]" % (ip, port)) 53 else: 54 self.name = hub_api.key_base_name(hub_key_name) # perhaps should store all key names somewhere central 55 rospy.logdebug("Gateway : resolved hub name [%s].", self.name) 56 except redis.exceptions.ConnectionError: 57 self._redis_server = None 58 raise HubNotFoundError("couldn't connect to the redis server") 59 60 # whitelists, blacklists - check against uri's hash names and non-uuid names 61 uri_blacklist = [urlparse(x).hostname + ':' + str(urlparse(x).port) for x in blacklist if urlparse(x).hostname is not None] 62 uri_whitelist = [urlparse(x).hostname + ':' + str(urlparse(x).port) for x in whitelist if urlparse(x).hostname is not None] 63 nonuuid_blacklist = [rocon_utilities.gateway_basename(x) for x in blacklist if urlparse(x) is None and rocon_utilities.gateway_basename(x)] 64 nonuuid_whitelist = [rocon_utilities.gateway_basename(x) for x in whitelist if urlparse(x) is None and rocon_utilities.gateway_basename(x)] 65 if self.uri in uri_blacklist or self.name in blacklist or self.name in nonuuid_blacklist: 66 raise HubConnectionBlacklistedError("ignoring blacklisted hub [%s]" % self.uri) 67 if self.name in blacklist or self.name in nonuuid_whitelist: 68 raise HubConnectionBlacklistedError("ignoring blacklisted hub [%s]" % self.uri) 69 if not ((len(whitelist) == 0) or (self.uri in uri_whitelist) or (self.name in whitelist)): 70 raise HubConnectionNotWhitelistedError("hub/ip not in non-empty whitelist [%s][%s]%s" % (self.name, self.uri, whitelist))
71