Refactor msgq thread classes to use gru.msgq_runner
authorjcorgan <jcorgan@221aa14e-8319-0410-a670-987f0aec2ac5>
Fri, 10 Jul 2009 01:26:37 +0000 (01:26 +0000)
committerjcorgan <jcorgan@221aa14e-8319-0410-a670-987f0aec2ac5>
Fri, 10 Jul 2009 01:26:37 +0000 (01:26 +0000)
git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@11407 221aa14e-8319-0410-a670-987f0aec2ac5

gr-wxgui/src/python/common.py
gr-wxgui/src/python/fftsink_nongl.py
gr-wxgui/src/python/scopesink_nongl.py
gr-wxgui/src/python/waterfallsink_nongl.py

index c6b9509b293a5cbf66a2a310569a3511ee310526..d555a1f055a9c3b824e2b71cb0c793c0ec562fcb 100644 (file)
@@ -44,31 +44,26 @@ def register_access_methods(destination, controller):
 ##################################################
 # Input Watcher Thread
 ##################################################
-import threading
+from gnuradio import gru
 
-class input_watcher(threading.Thread):
+class input_watcher(gru.msgq_runner):
        """
        Input watcher thread runs forever.
        Read messages from the message queue.
        Forward messages to the message handler.
        """
        def __init__ (self, msgq, controller, msg_key, arg1_key='', arg2_key=''):
-               threading.Thread.__init__(self)
-               self.setDaemon(1)
-               self.msgq = msgq
                self._controller = controller
                self._msg_key = msg_key
                self._arg1_key = arg1_key
                self._arg2_key = arg2_key
-               self.keep_running = True
-               self.start()
+               gru.msgq_runner.__init__(self, msgq, self.handle_msg)
+
+       def handle_msg(self, msg):
+               if self._arg1_key: self._controller[self._arg1_key] = msg.arg1()
+               if self._arg2_key: self._controller[self._arg2_key] = msg.arg2()
+               self._controller[self._msg_key] = msg.to_string()
 
-       def run(self):
-               while self.keep_running:
-                       msg = self.msgq.delete_head()
-                       if self._arg1_key: self._controller[self._arg1_key] = msg.arg1()
-                       if self._arg2_key: self._controller[self._arg2_key] = msg.arg2()
-                       self._controller[self._msg_key] = msg.to_string()
 
 ##################################################
 # Shared Functions
index a1033e8182db4052753aa956052d87dd66391bc5..ca5e91fdbea3d708a7927281505c8a133ad105bf 100644 (file)
@@ -25,7 +25,6 @@ from gnuradio.wxgui import stdgui2
 import wx
 import plot
 import numpy
-import threading
 import math    
 
 DIV_LEVELS = (1, 2, 5, 10, 20)
@@ -193,34 +192,28 @@ class DataEvent(wx.PyEvent):
         self.__class__ (self.GetId())
 
 
-class input_watcher (threading.Thread):
+class input_watcher (gru.msgq_runner):
     def __init__ (self, msgq, fft_size, event_receiver, **kwds):
-        threading.Thread.__init__ (self, **kwds)
-        self.setDaemon (1)
-        self.msgq = msgq
         self.fft_size = fft_size
         self.event_receiver = event_receiver
-        self.keep_running = True
-        self.start ()
-
-    def run (self):
-        while (self.keep_running):
-            msg = self.msgq.delete_head()  # blocking read of message queue
-            itemsize = int(msg.arg1())
-            nitems = int(msg.arg2())
-
-            s = msg.to_string()            # get the body of the msg as a string
-
-            # There may be more than one FFT frame in the message.
-            # If so, we take only the last one
-            if nitems > 1:
-                start = itemsize * (nitems - 1)
-                s = s[start:start+itemsize]
-
-            complex_data = numpy.fromstring (s, numpy.float32)
-            de = DataEvent (complex_data)
-            wx.PostEvent (self.event_receiver, de)
-            del de
+        gru.msgq_runner.__init__(self, msgq, self.handle_msg)
+
+    def handle_msg(self, msg):
+        itemsize = int(msg.arg1())
+        nitems = int(msg.arg2())
+
+        s = msg.to_string() # get the body of the msg as a string
+
+        # There may be more than one FFT frame in the message.
+        # If so, we take only the last one
+        if nitems > 1:
+            start = itemsize * (nitems - 1)
+            s = s[start:start+itemsize]
+
+        complex_data = numpy.fromstring (s, numpy.float32)
+        de = DataEvent (complex_data)
+        wx.PostEvent (self.event_receiver, de)
+        del de
 
 class control_panel(wx.Panel):
     
index bd31879923be94de07cf1f7e066dfe14b30f4470..5c1379ee643d9992e2446296b51183492b447ba0 100644 (file)
@@ -25,7 +25,6 @@ from gnuradio.wxgui import stdgui2
 import wx
 import gnuradio.wxgui.plot as plot
 import numpy
-import threading
 import struct
 
 default_scopesink_size = (640, 240)
@@ -193,48 +192,40 @@ class win_info (object):
         return self.marker
 
 
-class input_watcher (threading.Thread):
+class input_watcher (gru.msgq_runner):
     def __init__ (self, msgq, event_receiver, frame_decim, **kwds):
-        threading.Thread.__init__ (self, **kwds)
-        self.setDaemon (1)
-        self.msgq = msgq
         self.event_receiver = event_receiver
         self.frame_decim = frame_decim
         self.iscan = 0
-        self.keep_running = True
-        self.start ()
-
-    def run (self):
-        # print "input_watcher: pid = ", os.getpid ()
-        while (self.keep_running):
-            msg = self.msgq.delete_head()   # blocking read of message queue
-            if self.iscan == 0:            # only display at frame_decim
-                self.iscan = self.frame_decim
-                                
-                nchan = int(msg.arg1())    # number of channels of data in msg
-                nsamples = int(msg.arg2()) # number of samples in each channel
-
-                s = msg.to_string()      # get the body of the msg as a string
-
-                bytes_per_chan = nsamples * gr.sizeof_float
-
-                records = []
-                for ch in range (nchan):
-
-                    start = ch * bytes_per_chan
-                    chan_data = s[start:start+bytes_per_chan]
-                    rec = numpy.fromstring (chan_data, numpy.float32)
-                    records.append (rec)
+        gru.msgq_runner.__init__(self, msgq, self.handle_msg)
 
+    def handle_msg(self, msg):
+        if self.iscan == 0:            # only display at frame_decim
+            self.iscan = self.frame_decim
+            
+            nchan = int(msg.arg1())    # number of channels of data in msg
+            nsamples = int(msg.arg2()) # number of samples in each channel
+            
+            s = msg.to_string()      # get the body of the msg as a string
+            
+            bytes_per_chan = nsamples * gr.sizeof_float
+            
+            records = []
+            for ch in range (nchan):
+                
+                start = ch * bytes_per_chan
+                chan_data = s[start:start+bytes_per_chan]
+                rec = numpy.fromstring (chan_data, numpy.float32)
+                records.append (rec)
+                
                 # print "nrecords = %d, reclen = %d" % (len (records),nsamples)
-
+                
                 de = DataEvent (records)
                 wx.PostEvent (self.event_receiver, de)
                 records = []
                 del de
 
-            # end if iscan == 0
-            self.iscan -= 1
+        self.iscan -= 1
     
 
 class scope_window (wx.Panel):
index 9d97c4e3c5c1021bed1c5464848a2dcbef2b0a7f..bb478c7cf541adacf6f2f9aea405b55b0e510570 100644 (file)
@@ -26,7 +26,6 @@ import wx
 import gnuradio.wxgui.plot as plot
 import numpy
 import os
-import threading
 import math    
 
 default_fftsink_size = (640,240)
@@ -148,37 +147,29 @@ class DataEvent(wx.PyEvent):
 
     def Clone (self): 
         self.__class__ (self.GetId())
-
-
-class input_watcher (threading.Thread):
+    
+class input_watcher (gru.msgq_runner):
     def __init__ (self, msgq, fft_size, event_receiver, **kwds):
-        threading.Thread.__init__ (self, **kwds)
-        self.setDaemon (1)
-        self.msgq = msgq
         self.fft_size = fft_size
         self.event_receiver = event_receiver
-        self.keep_running = True
-        self.start ()
-
-    def run (self):
-        while (self.keep_running):
-            msg = self.msgq.delete_head()  # blocking read of message queue
-            itemsize = int(msg.arg1())
-            nitems = int(msg.arg2())
-
-            s = msg.to_string()            # get the body of the msg as a string
-
-            # There may be more than one FFT frame in the message.
-            # If so, we take only the last one
-            if nitems > 1:
-                start = itemsize * (nitems - 1)
-                s = s[start:start+itemsize]
-
-            complex_data = numpy.fromstring (s, numpy.float32)
-            de = DataEvent (complex_data)
-            wx.PostEvent (self.event_receiver, de)
-            del de
-    
+        gru.msgq_runner.__init__(self, msgq, self.handle_msg)
+
+    def handle_msg(self, msg):
+        itemsize = int(msg.arg1())
+        nitems = int(msg.arg2())
+
+        s = msg.to_string() # get the body of the msg as a string
+
+        # There may be more than one FFT frame in the message.
+        # If so, we take only the last one
+        if nitems > 1:
+            start = itemsize * (nitems - 1)
+            s = s[start:start+itemsize]
+
+        complex_data = numpy.fromstring (s, numpy.float32)
+        de = DataEvent (complex_data)
+        wx.PostEvent (self.event_receiver, de)
+        del de
 
 class waterfall_window (wx.Panel):
     def __init__ (self, fftsink, parent, id = -1,