10 from urlparse
import urlparse
13 import rocon_python_redis
as redis
14 import rocon_gateway_utils
17 from .exceptions
import HubNameNotFoundError, HubNotFoundError, \
18 HubConnectionBlacklistedError, HubConnectionNotWhitelistedError
28 This might be useful if doing connections with socket timeouts and we 29 need special functionality. Not currently using though. Pass in to 30 the redis server constructor as connection_class=HubConnection 32 def __init__(self, host='localhost', port=6379, db=0, password=None,
33 socket_timeout=1.0, encoding=
'utf-8',
34 encoding_errors=
'strict', decode_responses=
False):
35 super(HubConnection, self).
__init__(host, port, db, password,
36 socket_timeout, encoding,
37 encoding_errors, decode_responses)
47 Pings the hub for identification. This is currently used 48 by the hub discovery module. 53 connection_pool = redis.ConnectionPool(host=ip, port=port,
54 connection_class=HubConnection, socket_timeout=timeout)
55 r = redis.Redis(connection_pool=connection_pool)
56 name = r.get(
"rocon:hub:name")
57 except redis.exceptions.ConnectionError
as e:
60 return False,
"Redis Server is there but name key is not found" 70 def __init__(self, ip, port, whitelist=[], blacklist=[]):
72 @param remote_gateway_request_callbacks : to handle redis responses 73 @type list of function pointers (back to GatewaySync class 75 @param ip : redis server ip 76 @param port : redis server port 78 @raise HubNameNotFoundError, HubNotFoundError 83 self.
uri = str(ip) +
":" + str(port)
92 unused_ping = redis.Redis(host=ip, socket_timeout=5.0, port=port).ping()
94 except redis.exceptions.ConnectionError:
96 raise HubNotFoundError(
"couldn't connect to the redis server")
98 self.
pool = redis.ConnectionPool(host=ip, port=port, db=0, socket_timeout=5.0)
101 hub_key_name = self._redis_server.get(
"rocon:hub:name")
107 while self.
_redis_server and not hub_key_name
and retries < 5:
108 rospy.logwarn(
"couldn't resolve hub name on the redis server [%s:%s]. Retrying..." % (ip, port))
110 rospy.rostime.wallsleep(1.0)
111 hub_key_name = self._redis_server.get(
"rocon:hub:name")
115 raise HubNameNotFoundError(
"couldn't resolve hub name on the redis server [%s:%s]" % (ip, port))
117 self.
name = hub_api.key_base_name(hub_key_name)
118 rospy.logdebug(
"Gateway : resolved hub name [%s].", self.
name)
119 except redis.exceptions.ConnectionError:
121 raise HubNotFoundError(
"couldn't connect to the redis server")
124 uri_blacklist = [urlparse(x).hostname +
':' + str(urlparse(x).port)
for x
in blacklist
if urlparse(x).hostname
is not None]
125 uri_whitelist = [urlparse(x).hostname +
':' + str(urlparse(x).port)
for x
in whitelist
if urlparse(x).hostname
is not None]
126 nonuuid_blacklist = [rocon_gateway_utils.gateway_basename(x)
for x
in blacklist
if urlparse(x)
is None and rocon_gateway_utils.gateway_basename(x)]
127 nonuuid_whitelist = [rocon_gateway_utils.gateway_basename(x)
for x
in whitelist
if urlparse(x)
is None and rocon_gateway_utils.gateway_basename(x)]
128 if self.
uri in uri_blacklist
or self.
name in blacklist
or self.
name in nonuuid_blacklist:
129 raise HubConnectionBlacklistedError(
"ignoring blacklisted hub [%s]" % self.
uri)
130 if self.
name in blacklist
or self.
name in nonuuid_whitelist:
131 raise HubConnectionBlacklistedError(
"ignoring blacklisted hub [%s]" % self.
uri)
132 if not ((len(whitelist) == 0)
or (self.
uri in uri_whitelist)
or (self.
name in whitelist)):
133 raise HubConnectionNotWhitelistedError(
"hub/ip not in non-empty whitelist [%s, %s][%s]" % (self.
name, self.
uri, whitelist))
137 Kills any open socket connections to the redis server. This is 138 necessary if the server is out of range, as the py-redis client 139 will hang all open connections indefinitely 142 self._redis_server.connection_pool.disconnect()
143 except AttributeError:
150 return self.
uri == other.uri
153 return not self.
__eq__(other)
def ping_hub(ip, port, timeout=5.0)
def __init__(self, ip, port, whitelist=[], blacklist=[])
def __init__(self, host='localhost', port=6379, db=0, password=None, socket_timeout=1.0, encoding='utf-8', encoding_errors='strict', decode_responses=False)