Threading fixes for ZMQ work. Also moved radio optparse options into radio.py.
This commit is contained in:
+22
-48
@@ -29,19 +29,24 @@ from air_modes.exceptions import *
|
||||
import zmq
|
||||
|
||||
class screen_printer(threading.Thread):
|
||||
def __init__(self, position, context):
|
||||
def __init__(self, position, context, addr=None):
|
||||
threading.Thread.__init__(self)
|
||||
self._subscriber = context.socket(zmq.SUB)
|
||||
self._subscriber.connect("inproc://modes-radio-pub")
|
||||
if addr is not None:
|
||||
self._subscriber.connect("tcp://%s" % addr)
|
||||
else:
|
||||
self._subscriber.connect("inproc://modes-radio-pub")
|
||||
self._subscriber.setsockopt(zmq.SUBSCRIBE, "dl_data")
|
||||
|
||||
self._printer = air_modes.output_print(position)
|
||||
self.done = False
|
||||
self.done = threading.Event()
|
||||
self.finished = threading.Event()
|
||||
self.setDaemon(True)
|
||||
self.start()
|
||||
|
||||
def run(self):
|
||||
while not self.done:
|
||||
queue_empty = False
|
||||
while not self.done.is_set():
|
||||
[address, msg] = self._subscriber.recv_multipart() #blocking
|
||||
try:
|
||||
self._printer.output(msg)
|
||||
@@ -49,28 +54,14 @@ class screen_printer(threading.Thread):
|
||||
pass
|
||||
|
||||
self._subscriber.close()
|
||||
self.finished.set()
|
||||
|
||||
|
||||
def main():
|
||||
my_position = None
|
||||
usage = "%prog: [options] output filename"
|
||||
usage = "%prog: [options]"
|
||||
parser = OptionParser(option_class=eng_option, usage=usage)
|
||||
parser.add_option("-R", "--subdev", type="string",
|
||||
help="select USRP Rx side A or B", metavar="SUBDEV")
|
||||
parser.add_option("-A", "--antenna", type="string",
|
||||
help="select which antenna to use on daughterboard")
|
||||
parser.add_option("-D", "--args", type="string",
|
||||
help="arguments to pass to radio constructor", default="")
|
||||
parser.add_option("-f", "--freq", type="eng_float", default=1090e6,
|
||||
help="set receive frequency in Hz [default=%default]", metavar="FREQ")
|
||||
parser.add_option("-g", "--gain", type="int", default=None,
|
||||
help="set RF gain", metavar="dB")
|
||||
parser.add_option("-r", "--rate", type="eng_float", default=4000000,
|
||||
help="set ADC sample rate [default=%default]")
|
||||
parser.add_option("-T", "--threshold", type="eng_float", default=5.0,
|
||||
help="set pulse detection threshold above noise in dB [default=%default]")
|
||||
parser.add_option("-F","--filename", type="string", default=None,
|
||||
help="read data from file instead of USRP")
|
||||
air_modes.modes_radio.add_radio_options(parser)
|
||||
parser.add_option("-K","--kml", type="string", default=None,
|
||||
help="filename for Google Earth KML output")
|
||||
parser.add_option("-P","--sbs1", action="store_true", default=False,
|
||||
@@ -85,37 +76,21 @@ def main():
|
||||
help="Use UDP source on specified port")
|
||||
parser.add_option("-m","--multiplayer", type="string", default=None,
|
||||
help="FlightGear server to send aircraft data, in format host:port")
|
||||
parser.add_option("-o","--osmocom", action="store_true", default=False,
|
||||
help="Use gr-osmocom source (RTLSDR or HackRF) instead of UHD source")
|
||||
parser.add_option("-p","--pmf", action="store_true", default=False,
|
||||
help="Use pulse matched filtering")
|
||||
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
#construct the radio
|
||||
context = zmq.Context(1)
|
||||
tb = air_modes.modes_radio(options, context)
|
||||
tb.start()
|
||||
#tb will publish via ZMQ on the "radio-pub" feed
|
||||
#clients connect to tb socket and do things.
|
||||
#the radio-pub feed is channelized into "dl_data" and (not yet)
|
||||
#"ul_data" which correspond to downlink and uplink data
|
||||
|
||||
if options.location is not None:
|
||||
reader = csv.reader([options.location], quoting=csv.QUOTE_NONNUMERIC)
|
||||
my_position = reader.next()
|
||||
|
||||
if options.kml is not None:
|
||||
#we spawn a thread to run every 30 seconds (or whatever) to generate KML
|
||||
dbname = 'adsb.db'
|
||||
sqldb = air_modes.output_sql(my_position, dbname, context) #input into the db
|
||||
#kmlgen = air_modes.output_kml(options.kml, dbname, my_position, lock) #create a KML generating thread to read from the db
|
||||
#tb.subscribe('dl_data', sqldb.output)
|
||||
|
||||
# if options.sbs1 is True:
|
||||
# sbs1port = air_modes.output_sbs1(my_position, 30003)
|
||||
# tb.subscribe('dl_data', sbs1port.output)
|
||||
#updates.append(sbs1port.add_pending_conns)
|
||||
|
||||
printer = None
|
||||
if options.no_print is not True:
|
||||
@@ -126,23 +101,22 @@ def main():
|
||||
# fgout = air_modes.output_flightgear(my_position, fghost, int(fgport))
|
||||
# tb.subscribe('dl_data', fgout.output)
|
||||
|
||||
#start an updater thread to handle adding/removing TCP connections
|
||||
#TODO this can be removed when we start using 0MQ
|
||||
# updater = threading.Thread(target=timed_callback, args=(updates,))
|
||||
# updater.setDaemon(True)
|
||||
# updater.start()
|
||||
|
||||
tb.wait()
|
||||
# if options.sbs1 is True:
|
||||
# sbs1port = air_modes.output_sbs1(my_position, 30003)
|
||||
# tb.subscribe('dl_data', sbs1port.output)
|
||||
#updates.append(sbs1port.add_pending_conns)
|
||||
|
||||
tb.run()
|
||||
tb._pubsub.done.set()
|
||||
tb._pubsub.finished.wait(0.2)
|
||||
|
||||
if printer is not None:
|
||||
printer.done = True
|
||||
#printer.join()
|
||||
printer.done.set()
|
||||
printer.finished.wait(0.2)
|
||||
if options.kml is not None:
|
||||
sqldb.done = True
|
||||
#sqldb.join()
|
||||
|
||||
time.sleep(0.1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user