Rework UDP source and sink, with incompatible API changes
[debian/gnuradio] / gnuradio-examples / python / network / vector_sink.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2006,2010 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
10 # any later version.
11
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21
22
23 from gnuradio import gr
24 from gnuradio.eng_option import eng_option
25 from optparse import OptionParser
26
27 class vector_sink(gr.top_block):
28     def __init__(self, host, port, pkt_size, eof, wait):
29         gr.top_block.__init__(self, "vector_sink")
30
31         udp = gr.udp_source(gr.sizeof_float, host, port, pkt_size,
32                             eof=eof, wait=wait)
33         sink = gr.file_sink(gr.sizeof_float, "received.dat")
34         self.connect(udp, sink)
35
36 if __name__ == "__main__":
37     parser = OptionParser(option_class=eng_option)
38     parser.add_option("", "--host", type="string", default="0.0.0.0",
39                       help="local host name (domain name or IP address)")
40     parser.add_option("", "--port", type="int", default=65500,
41                       help="port value to listen to for connection")
42     parser.add_option("", "--packet-size", type="int", default=1471,
43                       help="packet size.")
44     parser.add_option("", "--no-eof", action="store_true", default=False,
45                       help="don't send EOF on disconnect")
46     parser.add_option("", "--no-wait", action="store_true", default=False,
47                       help="don't wait for source")
48     (options, args) = parser.parse_args()
49     if len(args) != 0:
50         parser.print_help()
51         raise SystemExit, 1
52     
53     # Create an instance of a hierarchical block
54     top_block = vector_sink(options.host, options.port,
55                             options.packet_size,
56                             not options.no_eof, not options.no_wait)
57     
58     try:    
59         # Run forever
60         top_block.run()
61     except KeyboardInterrupt:
62         # Ctrl-C exits
63         pass
64