Merged features/mp-sched -r8915:9335 into the trunk. The trunk now
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / top_block.py
1 #
2 # Copyright 2007 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
20
21
22 from gnuradio_swig_python import top_block_swig, \
23     top_block_wait_unlocked, top_block_run_unlocked
24
25 #import gnuradio.gr.gr_threading as _threading
26 import gr_threading as _threading
27
28
29 #
30 # There is no problem that can't be solved with an additional
31 # level of indirection...
32 #
33 # This kludge allows ^C to interrupt top_block.run and top_block.wait
34 #
35 class _top_block_waiter(_threading.Thread):
36     def __init__(self, tb):
37         _threading.Thread.__init__(self)
38         self.setDaemon(1)
39         self.tb = tb
40         self.event = _threading.Event()
41         self.start()
42
43     def run(self):
44         top_block_wait_unlocked(self.tb)
45         self.event.set()
46
47     def wait(self):
48         while not self.event.isSet():
49             self.event.wait(0.100)
50
51
52 #
53 # This hack forces a 'has-a' relationship to look like an 'is-a' one.
54 #
55 # It allows Python classes to subclass this one, while passing through
56 # method calls to the C++ class shared pointer from SWIG.
57 #
58 # It also allows us to intercept method calls if needed.
59 #
60 # This allows the 'run_locked' methods, which are defined in gr_top_block.i,
61 # to release the Python global interpreter lock before calling the actual
62 # method in gr_top_block
63 #
64 class top_block(object):
65     def __init__(self, name="top_block"):
66         self._tb = top_block_swig(name)
67
68     def __getattr__(self, name):
69         return getattr(self._tb, name)
70
71     def start(self):
72         self._tb.start()
73         
74     def stop(self):
75         self._tb.stop()
76
77     def run(self):
78         self.start()
79         self.wait()
80
81     def wait(self):
82         _top_block_waiter(self._tb).wait()
83
84
85     # FIXME: these are duplicated from hier_block2.py; they should really be implemented
86     # in the original C++ class (gr_hier_block2), then they would all be inherited here
87
88     def connect(self, *points):
89         '''connect requires one or more arguments that can be coerced to endpoints.
90         If more than two arguments are provided, they are connected together successively.
91         '''
92         if len (points) < 1:
93             raise ValueError, ("connect requires at least one endpoint; %d provided." % (len (points),))
94         else:
95             if len(points) == 1:
96                 self._tb.connect(points[0].basic_block())
97             else:
98                 for i in range (1, len (points)):
99                     self._connect(points[i-1], points[i])
100
101     def _connect(self, src, dst):
102         (src_block, src_port) = self._coerce_endpoint(src)
103         (dst_block, dst_port) = self._coerce_endpoint(dst)
104         self._tb.connect(src_block.basic_block(), src_port,
105                          dst_block.basic_block(), dst_port)
106
107     def _coerce_endpoint(self, endp):
108         if hasattr(endp, 'basic_block'):
109             return (endp, 0)
110         else:
111             if hasattr(endp, "__getitem__") and len(endp) == 2:
112                 return endp # Assume user put (block, port)
113             else:
114                 raise ValueError("unable to coerce endpoint")
115
116     def disconnect(self, *points):
117         '''connect requires one or more arguments that can be coerced to endpoints.
118         If more than two arguments are provided, they are disconnected successively.
119         '''
120         if len (points) < 1:
121             raise ValueError, ("disconnect requires at least two endpoints; %d provided." % (len (points),))
122         else:
123             if len(points) == 1:
124                 self._tb.disconnect(points[0].basic_block())
125             else:
126                 for i in range (1, len (points)):
127                     self._disconnect(points[i-1], points[i])
128
129     def _disconnect(self, src, dst):
130         (src_block, src_port) = self._coerce_endpoint(src)
131         (dst_block, dst_port) = self._coerce_endpoint(dst)
132         self._tb.disconnect(src_block.basic_block(), src_port,
133                             dst_block.basic_block(), dst_port)
134