Package rocon_gateway :: Module watcher_thread
[frames] | no frames]

Source Code for Module rocon_gateway.watcher_thread

 1  #!/usr/bin/env python 
 2  # 
 3  # License: BSD 
 4  #   https://raw.github.com/robotics-in-concert/rocon_multimaster/hydro-devel/rocon_gateway/LICENSE 
 5  # 
 6  ############################################################################## 
 7  # Imports 
 8  ############################################################################## 
 9   
10  import rospy 
11  import httplib 
12   
13  ############################################################################## 
14  # Watcher 
15  ############################################################################## 
16   
17   
18 -class WatcherThread(object):
19 ''' 20 This used to be on a thread of its own, but now moved into 21 the gateway's main thread for running. 22 ''' 23
24 - def __init__(self, gateway, watch_loop_period):
25 self.trigger_update = False 26 self._trigger_shutdown = False 27 self._gateway = gateway 28 self._master = gateway.master 29 self._hub_manager = gateway.hub_manager 30 self._hubs = self._hub_manager.hubs 31 self._flipped_interface = gateway.flipped_interface 32 self._pulled_interface = gateway.pulled_interface 33 self._watch_loop_period = rospy.Duration(watch_loop_period) 34 self._last_loop_timestamp = rospy.Time.now() 35 self._internal_sleep_period = rospy.Duration(0, 200000000) # 200ms
36
37 - def start(self):
38 ''' 39 The watcher thread - monitors both the local master's system state (list of connections) 40 and the various rules to make sure rules and existing connections or flips are in sync. 41 ''' 42 while not rospy.is_shutdown(): 43 # don't waste time processing if we're not connnected to at least one hub 44 if self._gateway.is_connected(): 45 try: 46 connections = self._master.get_connection_state() 47 except httplib.ResponseNotReady: 48 rospy.logwarn("Gateway : received 'ResponseNotReady' from master api") 49 self._sleep() 50 continue 51 remote_gateway_hub_index = self._hub_manager.create_remote_gateway_hub_index() 52 self._gateway.update_flipped_interface(connections, remote_gateway_hub_index) 53 self._gateway.update_public_interface(connections) 54 self._gateway.update_pulled_interface(connections, remote_gateway_hub_index) 55 self._sleep()
56
57 - def _sleep(self):
58 ''' 59 Internal interruptible sleep loop to check for shutdown and update triggers. 60 This lets us set a really long watch_loop update if we wish. 61 ''' 62 while not rospy.is_shutdown() and not self.trigger_update and (rospy.Time.now() - self._last_loop_timestamp < self._watch_loop_period): 63 rospy.sleep(self._internal_sleep_period) 64 self.trigger_update = False 65 self._last_loop_timestamp = rospy.Time.now()
66