message_loader_thread.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 import threading
00034 
00035 
00036 class MessageLoaderThread(threading.Thread):
00037     """
00038     Waits for a new playhead position on the given topic, then loads the message at that position and notifies the view threads.
00039 
00040     One thread per topic.  Maintains a cache of recently loaded messages.
00041     """
00042     def __init__(self, timeline, topic):
00043         threading.Thread.__init__(self)
00044 
00045         self.timeline = timeline
00046         self.topic = topic
00047 
00048         self.bag_playhead_position = None
00049 
00050         self._message_cache_capacity = 50
00051         self._message_cache = {}
00052         self._message_cache_keys = []
00053 
00054         self._stop_flag = False
00055 
00056         self.setDaemon(True)
00057         self.start()
00058 
00059     def reset(self):
00060         self.bag_playhead_position = None
00061 
00062     def run(self):
00063         while not self._stop_flag:
00064             # Wait for a new entry
00065             cv = self.timeline._playhead_positions_cvs[self.topic]
00066             with cv:
00067                 while (self.topic not in self.timeline._playhead_positions) or (self.bag_playhead_position == self.timeline._playhead_positions[self.topic]):
00068                     cv.wait()
00069                     if self._stop_flag:
00070                         return
00071                 bag, playhead_position = self.timeline._playhead_positions[self.topic]
00072 
00073             self.bag_playhead_position = (bag, playhead_position)
00074 
00075             # Don't bother loading the message if there are no listeners
00076             if not self.timeline.has_listeners(self.topic):
00077                 continue
00078 
00079             # Load the message
00080             if playhead_position is None:
00081                 msg_data = None
00082             else:
00083                 msg_data = self._get_message(bag, playhead_position)
00084 
00085             # Inform the views
00086             messages_cv = self.timeline._messages_cvs[self.topic]
00087             with messages_cv:
00088                 self.timeline._messages[self.topic] = (bag, msg_data)
00089                 messages_cv.notify_all()      # notify all views that a message is loaded
00090 
00091     def _get_message(self, bag, position):
00092         key = '%s%s' % (bag.filename, str(position))
00093         if key in self._message_cache:
00094             return self._message_cache[key]
00095 
00096         msg_data = self.timeline.read_message(bag, position)
00097 
00098         self._message_cache[key] = msg_data
00099         self._message_cache_keys.append(key)
00100 
00101         if len(self._message_cache) > self._message_cache_capacity:
00102             oldest_key = self._message_cache_keys[0]
00103             del self._message_cache[oldest_key]
00104             self._message_cache_keys.remove(oldest_key)
00105 
00106         return msg_data
00107 
00108     def stop(self):
00109         self._stop_flag = True
00110         cv = self.timeline._playhead_positions_cvs[self.topic]
00111         with cv:
00112             cv.notify_all()


rqt_bag
Author(s): Aaron Blasdel, Tim Field
autogenerated on Wed Sep 16 2015 06:58:10