Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 from gateway_msgs.msg import ConnectionStatistics
00012 from rocon_gateway import gateway_hub
00013 from rocon_hub_client import hub_api
00014 from rocon_python_comms import WallRate
00015
00016
00017 import rocon_hub_client
00018 import rospy
00019 import sys
00020 import threading
00021
00022
00023
00024
00025
00026
00027 class WatcherThread(threading.Thread):
00028
00029 def __init__(self, ip, port):
00030 threading.Thread.__init__(self)
00031 self.daemon = True
00032 self.gateway_unavailable_timeout = \
00033 rospy.get_param('~gateway_unavailable_timeout', 30.0)
00034 self.gateway_dead_timeout = \
00035 rospy.get_param('~gateway_dead_timeout', 7200.0)
00036 self.watcher_thread_rate = rospy.get_param('~watcher_thread_rate', 0.2)
00037 try:
00038 self.hub = gateway_hub.GatewayHub(ip, port, [], [])
00039 except rocon_hub_client.HubError as e:
00040 rospy.logfatal("Hub Watcher: unable to connect to hub: %s" % str(e))
00041 sys.exit(-1)
00042 self.unavailable_gateways = []
00043
00044 def run(self):
00045 '''
00046 Run the hub watcher (sidekick) thread at the rate specified by the
00047 watcher_thread_rate parameter. The wathcer thread does the following:
00048 1. For all gateways available, see if we have a pinger available.
00049 2. Add and remove pingers as necessary
00050 3. Depending on pinger stats, update hub appropriately
00051 '''
00052 rate = WallRate(self.watcher_thread_rate)
00053 while True:
00054 remote_gateway_names = self.hub.list_remote_gateway_names()
00055
00056
00057 for name in remote_gateway_names:
00058
00059 gateway_key = hub_api.create_rocon_key(name)
00060
00061 ping_key = hub_api.create_rocon_gateway_key(name, ':ping')
00062 expiration_time = self.hub._redis_server.ttl(ping_key)
00063
00064 if expiration_time is None or expiration_time == -2:
00065
00066 continue
00067
00068 seconds_since_last_seen = \
00069 int(ConnectionStatistics.MAX_TTL - expiration_time)
00070
00071 if seconds_since_last_seen > self.gateway_unavailable_timeout:
00072 if name not in self.unavailable_gateways:
00073 rospy.logwarn("Hub Watcher: gateway " + name +
00074 " has been unavailable for " +
00075 str(self.gateway_unavailable_timeout) +
00076 " seconds! Marking as unavailable.")
00077 self.unavailable_gateways.append(name)
00078 self.hub.mark_named_gateway_available(gateway_key, False,
00079 seconds_since_last_seen)
00080 else:
00081 if name in self.unavailable_gateways:
00082 self.unavailable_gateways.remove(name)
00083 self.hub.mark_named_gateway_available(gateway_key, True,
00084 seconds_since_last_seen)
00085
00086
00087 if seconds_since_last_seen > self.gateway_dead_timeout:
00088 rospy.logwarn("Hub Watcher: gateway " + name +
00089 " has been unavailable for " +
00090 str(self.gateway_dead_timeout) +
00091 " seconds! Removing from hub.")
00092 self.hub.unregister_named_gateway(gateway_key)
00093 rate.sleep()