gnuradio-core: update copyrights
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / qa_udp_sink_source.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2008,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, gr_unittest
24 from threading import Timer
25
26 class test_sink_source(gr_unittest.TestCase):
27
28     def setUp(self):
29         self.tb_snd = gr.top_block()
30         self.tb_rcv = gr.top_block()
31
32     def tearDown(self):
33         self.tb_rcv = None
34         self.tb_snd = None
35         
36     def test_001(self):
37         port = 65500
38
39         n_data = 16
40         src_data = [float(x) for x in range(n_data)]
41         expected_result = tuple(src_data)
42         src = gr.vector_source_f(src_data)
43         udp_snd = gr.udp_sink( gr.sizeof_float, 'localhost', port )
44         self.tb_snd.connect( src, udp_snd )
45
46         udp_rcv = gr.udp_source( gr.sizeof_float, 'localhost', port )
47         dst = gr.vector_sink_f()
48         self.tb_rcv.connect( udp_rcv, dst )
49
50         self.tb_rcv.start()
51         self.tb_snd.run()
52         udp_snd.disconnect()
53         self.timeout = False
54         q = Timer(3.0,self.stop_rcv)
55         q.start()
56         self.tb_rcv.wait()
57         q.cancel()
58
59         result_data = dst.data()
60         self.assertEqual(expected_result, result_data)
61         self.assert_(not self.timeout)
62         
63     def test_002(self):
64         udp_rcv = gr.udp_source( gr.sizeof_float, '0.0.0.0', 0, eof=False )
65         rcv_port = udp_rcv.get_port()
66
67         udp_snd = gr.udp_sink( gr.sizeof_float, '127.0.0.1', 65500 )
68         udp_snd.connect( 'localhost', rcv_port )
69
70         n_data = 16
71         src_data = [float(x) for x in range(n_data)]
72         expected_result = tuple(src_data)
73         src = gr.vector_source_f(src_data)
74         dst = gr.vector_sink_f()
75
76         self.tb_snd.connect( src, udp_snd )
77         self.tb_rcv.connect( udp_rcv, dst )
78
79         self.tb_rcv.start()
80         self.tb_snd.run()
81         udp_snd.disconnect()
82         self.timeout = False
83         q = Timer(3.0,self.stop_rcv)
84         q.start()
85         self.tb_rcv.wait()
86         q.cancel()
87
88         result_data = dst.data()
89         self.assertEqual(expected_result, result_data)
90         self.assert_(self.timeout)  # source ignores EOF?
91
92     def stop_rcv(self):
93         self.timeout = True
94         self.tb_rcv.stop()
95         #print "tb_rcv stopped by Timer"
96         
97 if __name__ == '__main__':
98     gr_unittest.main ()
99