Exemple #1
0
 def setUp(self):
     pool = ConnectionPool(
         host='localhost', port=6379, db=9,
         encoding='utf-8', decode_responses=True,
         parser_class=HiredisParser)
     self.client = redis.Redis(connection_pool=pool)
     self.client.flushdb()
    def start(self):
        '''
          Start the server. Also connect, delete all rocon:xxx
          variables and reinitialise with specified values.

          Aborts the program if the connection fails.
        '''
        # Launch as a separate process group so we can control when it gets shut down.
        self._process = subprocess.Popen(["redis-server", self._files['redis_conf']], preexec_fn=os.setpgrp)
        pool = redis.ConnectionPool(host='localhost', port=int(self._parameters['port']), db=0)
        no_attempts = 5
        count = 0
        while count < no_attempts:
            try:
                self._server = redis.Redis(connection_pool=pool)
                rocon_keys = self._server.keys("rocon:*")
                pattern = re.compile("rocon:*")
                keys_to_delete = []
                for key in rocon_keys:
                    if pattern.match(key):
                        keys_to_delete.append(key)
                pipe = self._server.pipeline()
                if len(keys_to_delete) != 0:
                    pipe.delete(*keys_to_delete)  # * unpacks the list args - http://stackoverflow.com/questions/2921847/python-once-and-for-all-what-does-the-star-operator-mean-in-python
                pipe.set("rocon:hub:name", self._parameters['name'])
                pipe.execute()
                rospy.loginfo("Hub : reset hub variables on the redis server.")
                break
            except redis.ConnectionError:
                count += 1
                if count == no_attempts:
                    self.shutdown()
                    sys.exit(utils.logfatal("Hub : could not connect to the redis server - is it running?"))
                else:
                    rospy.rostime.wallsleep(0.1)
Exemple #3
0
def ping_hub(ip, port, timeout=5.0):
    '''
      Pings the hub for identification. This is currently used
      by the hub discovery module.

      @return Bool, Latency
    '''
    try:
        connection_pool = redis.ConnectionPool(host=ip, port=port,
                                               connection_class=HubConnection, socket_timeout=timeout)
        r = redis.Redis(connection_pool=connection_pool)
        name = r.get("rocon:hub:name")
    except redis.exceptions.ConnectionError as e:
        return False, str(e)
    if name is None:  # returns None if the server was there, but the key was not found.
        return False, "Redis Server is there but name key is not found"
    return (True, "")
Exemple #4
0
def ping_hub(ip, port):
    '''
      Pings the hub for identification. This is currently used
      by the hub discovery module.

      @return Bool, Latency
    '''
    try:
        connection_pool = redis.ConnectionPool(host=ip,
                                               port=port,
                                               connection_class=HubConnection)
        r = redis.Redis(connection_pool=connection_pool)
        name = r.get("rocon:hub:name")

    except redis.exceptions.ConnectionError:
        return False
    if name is None:  # returns None if the server was there, but the key was not found.
        return False
    return True
Exemple #5
0
 def setUp(self):
     self.client = redis.Redis(
         host='localhost', port=6379, db=9, charset='utf-8')
     self.client.flushdb()
Exemple #6
0
    def __init__(self, ip, port, whitelist=[], blacklist=[]):
        '''
          @param remote_gateway_request_callbacks : to handle redis responses
          @type list of function pointers (back to GatewaySync class

          @param ip : redis server ip
          @param port : redis server port

          @raise HubNameNotFoundError, HubNotFoundError
        '''
        # variables
        self.ip = ip
        self.port = port
        self.uri = str(ip) + ":" + str(port)
        self._redis_keys = {}
        self._redis_channels = {}

        # This is a temporary try-except block just to ping and see if the address we have here is
        # actually resolvable or it times out. Ideally we want to use socket_timeouts throughout,
        # but that will need modification of the way we handle the RedisListenerThread in
        # gateway_hub.py
        try:
            unused_ping = redis.Redis(host=ip, socket_timeout=5.0, port=port).ping()
            # should check ping result? Typically it just throws the timeout error
        except redis.exceptions.ConnectionError:
            self._redis_server = None
            raise HubNotFoundError("couldn't connect to the redis server")
        try:
            self.pool = redis.ConnectionPool(host=ip, port=port, db=0, socket_timeout=5.0)
            self._redis_server = redis.Redis(connection_pool=self.pool)
            self._redis_pubsub_server = self._redis_server.pubsub()
            hub_key_name = self._redis_server.get("rocon:hub:name")
            # Be careful, hub_name is None, it means the redis server is
            # found but hub_name not yet set or not set at all.

            # retrying for 5 seconds in case we started too fast
            retries = 0
            while self._redis_server and not hub_key_name and retries < 5:
                rospy.logwarn("couldn't resolve hub name on the redis server [%s:%s]. Retrying..." % (ip, port))
                retries += 1
                rospy.rostime.wallsleep(1.0)
                hub_key_name = self._redis_server.get("rocon:hub:name")

            if not hub_key_name:
                self._redis_server = None
                raise HubNameNotFoundError("couldn't resolve hub name on the redis server [%s:%s]" % (ip, port))
            else:
                self.name = hub_api.key_base_name(hub_key_name)  # perhaps should store all key names somewhere central
                rospy.logdebug("Gateway : resolved hub name [%s].", self.name)
        except redis.exceptions.ConnectionError:
            self._redis_server = None
            raise HubNotFoundError("couldn't connect to the redis server")

        # whitelists, blacklists - check against uri's hash names and non-uuid names
        uri_blacklist = [urlparse(x).hostname + ':' + str(urlparse(x).port) for x in blacklist if urlparse(x).hostname is not None]
        uri_whitelist = [urlparse(x).hostname + ':' + str(urlparse(x).port) for x in whitelist if urlparse(x).hostname is not None]
        nonuuid_blacklist = [rocon_gateway_utils.gateway_basename(x) for x in blacklist if urlparse(x) is None and rocon_gateway_utils.gateway_basename(x)]
        nonuuid_whitelist = [rocon_gateway_utils.gateway_basename(x) for x in whitelist if urlparse(x) is None and rocon_gateway_utils.gateway_basename(x)]
        if self.uri in uri_blacklist or self.name in blacklist or self.name in nonuuid_blacklist:
            raise HubConnectionBlacklistedError("ignoring blacklisted hub [%s]" % self.uri)
        if self.name in blacklist or self.name in nonuuid_whitelist:
            raise HubConnectionBlacklistedError("ignoring blacklisted hub [%s]" % self.uri)
        if not ((len(whitelist) == 0) or (self.uri in uri_whitelist) or (self.name in whitelist)):
            raise HubConnectionNotWhitelistedError("hub/ip not in non-empty whitelist [%s, %s][%s]" % (self.name, self.uri, whitelist))
Exemple #7
0
 def setUp(self):
     self.connection_pool = redis.ConnectionPool(port=6390)
     self.client = redis.Redis(connection_pool=self.connection_pool)
     self.pubsub = self.client.pubsub()