watcher.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # License: BSD
4 # https://raw.github.com/robotics-in-concert/rocon_multimaster/license/LICENSE
5 #
6 
7 ##############################################################################
8 # Imports
9 ##############################################################################
10 
11 from gateway_msgs.msg import ConnectionStatistics
12 from rocon_gateway import gateway_hub
13 from rocon_hub_client import hub_api
14 from rocon_python_comms import WallRate
15 
16 
17 import rocon_hub_client
18 import rospy
19 import sys
20 import threading
21 
22 ##############################################################################
23 # Main watcher thread
24 ##############################################################################
25 
26 
27 class WatcherThread(threading.Thread):
28 
29  def __init__(self, ip, port):
30  threading.Thread.__init__(self)
31  self.daemon = True
32  self.gateway_gone_timeout = rospy.get_param('~gateway_gone_timeout', 300.0)
33  self.watcher_thread_rate = rospy.get_param('~watcher_thread_rate', 0.2)
34  try:
35  self.hub = gateway_hub.GatewayHub(ip, port, [], [])
36  except rocon_hub_client.HubError as e:
37  rospy.logfatal("Hub Watcher: unable to connect to hub: %s" % str(e))
38  sys.exit(-1)
40 
41  def run(self):
42  '''
43  Run the hub watcher (sidekick) thread at the rate specified by the
44  watcher_thread_rate parameter. The watcher thread does the following:
45  1. For all gateways available, see if we have a pinger available.
46  2. Add and remove pingers as necessary
47  3. Depending on pinger stats, update hub appropriately
48  '''
49  rate = WallRate(self.watcher_thread_rate)
50  unavailable_set = set()
51  while True:
52  remote_gateway_names = self.hub.list_remote_gateway_names()
53 
54  # Check all pingers
55  for name in remote_gateway_names:
56 
57  gateway_key = hub_api.create_rocon_key(name)
58  # Get time for this gateway when hub was last seen
59  ping_key = hub_api.create_rocon_gateway_key(name, ':ping')
60  expiration_time = self.hub._redis_server.ttl(ping_key)
61  # rospy.logwarn("<= {0} TTL {1}".format(ping_key, expiration_time))
62 
63  if expiration_time is None or expiration_time == -2:
64  # Probably in the process of starting up, ignore for now
65  continue
66 
67  seconds_since_last_seen = int(ConnectionStatistics.MAX_TTL - expiration_time)
68 
69  # rospy.logwarn("<= Not seen since {0} secs...".format(seconds_since_last_seen))
70 
71  # if it has been gone for more than one loop
72  if seconds_since_last_seen > rate.period:
73  rospy.logwarn("Hub Watcher: gateway " + name +
74  " has been unavailable for " + str(seconds_since_last_seen) +
75  " seconds.")
76  unavailable_set.add(gateway_key)
77  self.hub.mark_named_gateway_available(gateway_key, False, seconds_since_last_seen)
78 
79  # Check if gateway gone
80  if seconds_since_last_seen > self.gateway_gone_timeout:
81  rospy.logwarn("Hub Watcher: gateway " + name +
82  " has been unavailable for " +
83  str(self.gateway_gone_timeout) +
84  " seconds! Removing from hub.")
85 
86  # Gone for too long => unregister
87  self.hub.unregister_named_gateway(gateway_key)
88 
89  # Mark gateway as available
90  else:
91  if gateway_key in unavailable_set:
92  rospy.logwarn("Hub Watcher: gateway " + name +
93  " detected available again !")
94  unavailable_set.remove(gateway_key)
95  self.hub.mark_named_gateway_available(gateway_key, True, seconds_since_last_seen)
96 
97  rate.sleep()
def __init__(self, ip, port)
Definition: watcher.py:29
Main watcher thread.
Definition: watcher.py:27


rocon_hub
Author(s): Daniel Stonier , Jihoon Lee , Piyush Khandelwal
autogenerated on Mon Jun 10 2019 14:40:13