Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 import threading
00034
00035 from python_qt_binding.QtCore import QCoreApplication, QEvent
00036 from python_qt_binding.QtCore import qWarning
00037
00038
00039 class ListenerEvent(QEvent):
00040 def __init__(self, data):
00041 super(ListenerEvent, self).__init__(1024)
00042 self.data = data
00043
00044
00045 class MessageListenerThread(threading.Thread):
00046 """
00047 Waits for new messages loaded on the given topic, then calls the message listener.
00048 One thread per listener, topic pair.
00049 """
00050 def __init__(self, timeline, topic, listener):
00051 threading.Thread.__init__(self)
00052
00053 self.timeline = timeline
00054 self.topic = topic
00055 self.listener = listener
00056 self.bag_msg_data = None
00057 self._stop_flag = False
00058 self.setDaemon(True)
00059 self.start()
00060
00061 def run(self):
00062 """
00063 Thread body. loops and notifies the listener of new messages
00064 """
00065 while not self._stop_flag:
00066
00067 cv = self.timeline._messages_cvs[self.topic]
00068 with cv:
00069 while (self.topic not in self.timeline._messages) or (self.bag_msg_data == self.timeline._messages[self.topic]):
00070 cv.wait()
00071 if self._stop_flag:
00072 return
00073 bag_msg_data = self.timeline._messages[self.topic]
00074
00075 self.bag_msg_data = bag_msg_data
00076 try:
00077 event = ListenerEvent(bag_msg_data)
00078 QCoreApplication.postEvent(self.listener, event)
00079 except Exception, ex:
00080 qWarning('Error notifying listener %s: %s' % (type(self.listener), str(ex)))
00081
00082 def stop(self):
00083 self._stop_flag = True
00084 cv = self.timeline._messages_cvs[self.topic]
00085 with cv:
00086 cv.notify_all()