message_listener_thread.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Willow Garage, Inc.
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 Willow Garage, Inc. 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 
35 from python_qt_binding.QtCore import QCoreApplication, QEvent
36 from python_qt_binding.QtCore import qWarning
37 
38 
39 class ListenerEvent(QEvent):
40 
41  def __init__(self, data):
42  super(ListenerEvent, self).__init__(QEvent.User)
43  self.data = data
44 
45 
46 class MessageListenerThread(threading.Thread):
47 
48  """
49  Waits for new messages loaded on the given topic, then calls the message listener.
50  One thread per listener, topic pair.
51  """
52 
53  def __init__(self, timeline, topic, listener):
54  threading.Thread.__init__(self)
55 
56  self.timeline = timeline
57  self.topic = topic
58  self.listener = listener
59  self.bag_msg_data = None
60  self._stop_flag = False
61  self.setDaemon(True)
62  self.start()
63 
64  def run(self):
65  """
66  Thread body. loops and notifies the listener of new messages
67  """
68  while not self._stop_flag:
69  # Wait for a new message
70  cv = self.timeline._messages_cvs[self.topic]
71  with cv:
72  while (self.topic not in self.timeline._messages) or (self.bag_msg_data == self.timeline._messages[self.topic]):
73  cv.wait()
74  if self._stop_flag:
75  return
76  bag_msg_data = self.timeline._messages[self.topic]
77  # View the message
78  self.bag_msg_data = bag_msg_data
79  try:
80  event = ListenerEvent(bag_msg_data)
81  QCoreApplication.postEvent(self.listener, event)
82  except Exception as ex:
83  qWarning('Error notifying listener %s: %s' % (type(self.listener), str(ex)))
84 
85  def stop(self):
86  self._stop_flag = True
87  cv = self.timeline._messages_cvs[self.topic]
88  with cv:
89  cv.notify_all()


rqt_bag
Author(s): Dirk Thomas , Aaron Blasdel , Austin Hendrix , Tim Field
autogenerated on Fri Feb 19 2021 03:14:14