1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
#
# Copyright 2015 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
"""
Python Client classes for interfacing with the GNU Radio ControlPort interface
and for accessing Performance Counters.
While ControlPort and these client classes are designed to support multiple
Remote Procedure Call (RPC) transports, the Apache Thrift middle-ware RPC
is currently the only supported transport.
"""
"""
RPCMethods is a dictionary listing RPC transports currently supported
by this client.
Args:
function: the function whose parameter list will be examined
excluded_args: function arguments that are NOT to be added to the dictionary (sequence of strings)
options: result of command argument parsing (optparse.Values)
"""
RPCMethods = {'thrift': 'Apache Thrift',
#'ice': 'Zeroc ICE'
}
import exceptions
"""
Base class for RPC transport clients
Methods that all RPC clients should implement include:
def newConnection(host,port): Method for re-establishing a new client
connection to a different host / port
def properties([]): Given a list of ControlPort property names,
or an empty list to specify all currently registered properties,
this method returns a dictionary of metadata describing the
the specified properties. The dictionary key contains the name
of each returned properties.
def getKnobs([]): Given a list of ControlPort property names,
or an empty list to specify all currently registered properties,
this method returns a dictionary of the current value of
the specified properties.
def getRe([]): Given a list of regular expression strings,
this method returns a dictionary of the current value of
the all properties with names that match the specified
expressions.
def setKnobs({}): Given a dictionary of ControlPort property
key / value pairs, this method requests that ControlPort
attempt to set the specified named properties to the
value given. Success in setting each property to the
value specified requires that the property be registered
as a 'setable' ControlPort property, that the client have the
requisite privilege level to set the property, and
the underlying Block's implementation in handling
the set request.
Args:
method: name of the RPC transport
port: port number of the connection
host: hostname of the connection
"""
class RPCConnection(object):
def __init__(self, method, port, host=None):
(self.method, self.port) = (method, port)
if host is None: self.host = '127.0.0.1'
else: self.host = host
def __str__(self):
return "%s connection on %s:%s"%(self.getName(), self.getHost(), self.getPort())
def getName(self):
return RPCMethods[self.method]
def getHost(self):
return self.host
def getPort(self):
return self.port
def newConnection(self, host=None, port=None):
raise exceptions.NotImplementedError()
def properties(self, *args):
raise exceptions.NotImplementedError()
def getKnobs(self, *args):
raise exceptions.NotImplementedError()
def getRe(self,*args):
raise exceptions.NotImplementedError()
def setKnobs(self,*args):
raise exceptions.NotImplementedError()
def shutdown(self):
raise exceptions.NotImplementedError()
"""
RPC Client interface for the Apache Thrift middle-ware RPC transport.
Args:
port: port number of the connection
host: hostname of the connection
"""
class RPCConnectionThrift(RPCConnection):
def __init__(self, host=None, port=None):
if port is None: port = 9090
super(RPCConnectionThrift, self).__init__(method='thrift', port=port, host=host)
self.newConnection(host, port)
def newConnection(self, host=None, port=None):
from gnuradio.ctrlport.ThriftRadioClient import ThriftRadioClient
self.thriftclient = ThriftRadioClient(self.getHost(), self.getPort())
def properties(self, *args):
return self.thriftclient.radio.properties(*args)
def getKnobs(self, *args):
class Knob():
def __init__(self, key, value):
(self.key, self.value) = (key, value)
result = {}
for key, knob in self.thriftclient.radio.getKnobs(*args).iteritems():
if knob.type == 0: result[key] = Knob(key, knob.value.a_bool)
elif knob.type == 1: result[key] = Knob(key, knob.value.a_byte)
elif knob.type == 2: result[key] = Knob(key, knob.value.a_short)
elif knob.type == 3: result[key] = Knob(key, knob.value.a_int)
elif knob.type == 4: result[key] = Knob(key, knob.value.a_long)
elif knob.type == 5: result[key] = Knob(key, knob.value.a_double)
elif knob.type == 6: result[key] = Knob(key, knob.value.a_string)
elif knob.type == 7: result[key] = Knob(key, knob.value.a_complex)
elif knob.type == 8: result[key] = Knob(key, knob.value.a_f32vector)
elif knob.type == 9: result[key] = Knob(key, knob.value.a_f64vector)
elif knob.type == 10: result[key] = Knob(key, knob.value.a_s64vector)
elif knob.type == 11: result[key] = Knob(key, knob.value.a_s32vector)
elif knob.type == 12: result[key] = Knob(key, knob.value.a_s16vector)
elif knob.type == 13: result[key] = Knob(key, knob.value.a_s8vector)
elif knob.type == 14: result[key] = Knob(key, knob.value.a_s32vector)
elif knob.type == 15: result[key] = Knob(key, knob.value.byte)
else:
raise exceptions.ValueError
return result
def getRe(self,*args):
return self.thriftclient.radio.getRe(*args)
def setKnobs(self,*args):
self.thriftclient.radio.setKnobs(*args)
def shutdown(self):
self.thriftclient.radio.shutdown()
"""
GNURadioControlPortClient is the main class for creating a GNU Radio
ControlPort client application for all transports.
Two constructors are provided for creating a connection to ControlPort.
"""
class GNURadioControlPortClient():
"""
Constructor for creating a ControlPort connection to a specified host / port
Args:
host: hostname of the connection. Specifying None (default) will
select the loopback interface.
port: port number to use for the connection. Specifying None (default)
will select the specified RPC transport's default port number, if
the transport has a default.
rpcmethod: This string specifies the RPC transport to use for the
client connection. The default implementation currently uses
the Apache Thrift RPC transport. The value specified here must
be one of the transport keys listed in the RPCMethods dictionary
above
callback: This optional parameter is a callback function that will be passed
a reference to the Client implementation for the RPC transport specified
by rpcmethod. The callback will be executed after the client has been
constructed, but before __init__ returns.
blockingcallback: This optional parameter is a callback function with
no parameters that will be executed after callback() is executed,
but before __init__ returns. It is useful if your application
requires that a blocking function be called to start the application,
such as QtGui.QApplication.exec_
"""
def __init__(self, host = None, port = None, rpcmethod = 'thrift', callback = None, blockingcallback = None):
__init__([host, port], rpcmethod, callback, blockingcallback)
"""
Constructor for creating a ControlPort from a tuple of command line arguments (i.e. sys.argv)
Args:
argv: List of command line arguments. Future implementations may parse the argument list
for OptionParser style key / value pairs, however the current implementation
simply takes argv[1] and argv[2] as the connection hostname and port, respectively.
Example Usage:
In the following QT client example, the ControlPort host and port are specified to
the Client application as the first two command line arguments. The MAINWindow class is
of the type QtGui.QMainWindow, and is the main window for the QT application. MyApp
is a simple helper class for starting the application.
class MAINWindow(QtGui.QMainWindow):
... QT Application implementation ...
class MyApp(object):
def __init__(self, args):
from GNURadioControlPortClient import GNURadioControlPortClient
GNURadioControlPortClient(args, 'thrift', self.run, QtGui.QApplication(sys.argv).exec_)
def run(self, client):
MAINWindow(client).show()
MyApp(sys.argv)
"""
def __init__(self, argv = [], rpcmethod = 'thrift', callback = None, blockingcallback = None):
if len(argv) > 1: host = argv[1]
else: host = None
if len(argv) > 2: port = argv[2]
else: port = None
self.client = None
if RPCMethods.has_key(rpcmethod):
if rpcmethod == 'thrift':
# print("making RPCConnectionThrift")
self.client = RPCConnectionThrift(host, port)
# print("made %s" % self.client)
# print("making callback call")
if not callback is None: callback(self.client)
# print("making blockingcallback call")
if not blockingcallback is None: blockingcallback()
else:
print("Unsupported RPC method: ", rpcmethod)
raise exceptions.ValueError()
|