Package node_manager_fkie :: Module update_handler
[frames] | no frames]

Source Code for Module node_manager_fkie.update_handler

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2012, Fraunhofer FKIE/US, Alexander Tiderko 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Fraunhofer nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32   
 33  import threading 
 34  from python_qt_binding import QtCore 
 35   
 36  from master_discovery_fkie.master_info import MasterInfo 
 37  from update_thread import UpdateThread 
 38   
39 -class UpdateHandler(QtCore.QObject):
40 ''' 41 A class to retrieve the state about ROS master from remote discovery node and 42 publish it be sending a QT signal. To retrieve the state a new thread will be 43 created. 44 ''' 45 master_info_signal = QtCore.Signal(MasterInfo) 46 ''' 47 @ivar: master_info_signal is a signal, which is emitted, if a new 48 L{aster_discovery_fkie.MasterInfo} is retrieved. 49 ''' 50 error_signal = QtCore.Signal(str, str) 51 ''' 52 @ivar: error_signal is a signal (masteruri, error message), which is emitted, 53 if an error while retrieving a master info was occurred. 54 ''' 55
56 - def __init__(self):
57 QtCore.QObject.__init__(self) 58 self.__updateThreads = {} 59 self.__requestedUpdates = {} 60 self._lock = threading.RLock()
61
62 - def stop(self):
63 print " Shutdown update threads..." 64 self.__requestedUpdates.clear() 65 for key, thread in self.__updateThreads.iteritems(): 66 thread.join(3) 67 print " Update threads are off!"
68
69 - def requestMasterInfo(self, masteruri, monitoruri, delayed_exec=0.0):
70 ''' 71 This method starts a thread to get the informations about the ROS master by 72 the given RCP uri of the master_discovery node. If all informations are 73 retrieved, a C{master_info_signal} of this class will be emitted. If for given 74 masteruri a thread is already running, it will be inserted to the requested 75 updates. For the same masteruri only one requested update can be stored. 76 On update error the requested update will be ignored. 77 This method is thread safe. 78 79 @param masteruri: the URI of the remote ROS master 80 @type masteruri: C{str} 81 @param monitoruri: the URI of the monitor RPC interface of the master_discovery node 82 @type monitoruri: C{str} 83 @param delayed_exec: Delay the execution of the request for given seconds. 84 @type delayed_exec: C{float} 85 ''' 86 with self._lock: 87 try: 88 if (self.__updateThreads.has_key(masteruri)): 89 self.__requestedUpdates[masteruri] = (monitoruri, delayed_exec) 90 else: 91 self.__create_update_thread(monitoruri, masteruri, delayed_exec) 92 # from urlparse import urlparse 93 # om = urlparse(masteruri) 94 except: 95 pass
96
97 - def _on_master_info(self, minfo):
98 self.master_info_signal.emit(minfo) 99 self.__handle_requests(minfo.masteruri)
100
101 - def _on_error(self, masteruri, error):
102 self.error_signal.emit(masteruri, error) 103 self.__handle_requests(masteruri)
104
105 - def __handle_requests(self, masteruri):
106 with self._lock: 107 try: 108 thread = self.__updateThreads.pop(masteruri) 109 del thread 110 monitoruri, delayed_exec = self.__requestedUpdates.pop(masteruri) 111 self.__create_update_thread(monitoruri, masteruri, delayed_exec) 112 except KeyError: 113 # import traceback 114 # print traceback.format_exc() 115 pass 116 except: 117 import traceback 118 print traceback.format_exc()
119
120 - def __create_update_thread(self, monitoruri, masteruri, delayed_exec):
121 upthread = UpdateThread(monitoruri, masteruri, delayed_exec) 122 self.__updateThreads[masteruri] = upthread 123 upthread.update_signal.connect(self._on_master_info) 124 upthread.error_signal.connect(self._on_error) 125 upthread.start()
126