annotate src/wifibroadcast/WifiBroadcastTest.java @ 18:e9d07562b6b7

network interface
author one
date Tue, 31 Jul 2012 16:32:03 +0900
parents b5ab7003f6d1
children bb2538b7eb8d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 package wifibroadcast;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 import java.io.IOException;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 import java.nio.ByteBuffer;
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
5 import java.util.LinkedList;
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 public class WifiBroadcastTest {
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
8 private static int DefaultPort = 8412;
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 private static String MCASTADDR = "224.0.0.1";
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
10 private static int testSize = 256;
4
404778ac3f55 worked. Broadcast, Multicast, Multicast channel.
one
parents: 3
diff changeset
11 private static long timeout = 1000;
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12
9
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
13
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
14 LinkedList<Integer> ports = new LinkedList<Integer>();
cce76e5eb29b with tcp
one
parents: 14
diff changeset
15 LinkedList<String> addresses = new LinkedList<String>();
9
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
16 int count = 16;
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
17 boolean multicast = false;
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
18 boolean mchannel = false;
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
19 int receiver_count = 1;
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
20 int sender_count = 1;
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
21 WifiReceiver wbr[] = new WifiReceiver[receiver_count];
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
22 WifiReceiver wbs[] = new WifiReceiver[sender_count];
12
e1f43b669cdb broadcast on DatagramChannel
one
parents: 10
diff changeset
23 private boolean bchannel;
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
24 private boolean tcp;
18
e9d07562b6b7 network interface
one
parents: 16
diff changeset
25 private String nis = "en1";
9
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
26
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
27 public WifiBroadcastTest() {
cce76e5eb29b with tcp
one
parents: 14
diff changeset
28 ports.add(0,DefaultPort);
cce76e5eb29b with tcp
one
parents: 14
diff changeset
29 addresses.add(0,MCASTADDR);
cce76e5eb29b with tcp
one
parents: 14
diff changeset
30 }
cce76e5eb29b with tcp
one
parents: 14
diff changeset
31
2
2a328333ba70 no compile errors
one
parents: 1
diff changeset
32 public static void main(String args[]) {
9
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
33 new WifiBroadcastTest().test(args);
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
34 }
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
35
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
36 public void test(String args[]) {
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
37 options(args);
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 try {
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 if (multicast) {
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
40 for(int i=0;i<wbr.length;i++) wbr[i] = new WifiMulticast(i,getAddress(i),getPort(i),WifiReceiver.SocketType.Receiver);
10
fc180f38257e multiple sender
one
parents: 9
diff changeset
41 Thread.sleep(timeout/4);
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
42 for(int i=0;i<wbs.length;i++) wbs[i] = new WifiMulticast(i,getAddress(i),getPort(i),WifiReceiver.SocketType.Sender);
1
649b8573372c cleanup
one
parents: 0
diff changeset
43 } else if (mchannel) {
18
e9d07562b6b7 network interface
one
parents: 16
diff changeset
44 for(int i=0;i<wbr.length;i++) wbr[i] = new WifiMulticastChannel(i,getAddress(i),getPort(i),nis, WifiReceiver.SocketType.Receiver);
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
45 Thread.sleep(timeout/4);
18
e9d07562b6b7 network interface
one
parents: 16
diff changeset
46 for(int i=0;i<wbs.length;i++) wbs[i] = new WifiMulticastChannel(i,getAddress(i),getPort(i),nis, WifiReceiver.SocketType.Sender);
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
47 } else if (tcp) {
16
b5ab7003f6d1 tcp running
one
parents: 15
diff changeset
48 addresses.remove(); addresses.add("127.1");
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
49 for(int i=0;i<wbr.length;i++) wbr[i] = new WifiTCP(i,getAddress(i),getPort(i),WifiReceiver.SocketType.Receiver);
cce76e5eb29b with tcp
one
parents: 14
diff changeset
50 Thread.sleep(timeout/4);
cce76e5eb29b with tcp
one
parents: 14
diff changeset
51 for(int i=0;i<wbs.length;i++) wbs[i] = new WifiTCP(i,getAddress(i),getPort(i),WifiReceiver.SocketType.Sender);
12
e1f43b669cdb broadcast on DatagramChannel
one
parents: 10
diff changeset
52 } else if (bchannel) {
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
53 for(int i=0;i<wbr.length;i++) wbr[i] = new WifiBroadcastChannel(i,getPort(i),WifiReceiver.SocketType.Receiver);
cce76e5eb29b with tcp
one
parents: 14
diff changeset
54 Thread.sleep(timeout/4);
cce76e5eb29b with tcp
one
parents: 14
diff changeset
55 for(int i=0;i<wbs.length;i++) wbs[i] = new WifiBroadcastChannel(i,getPort(i),WifiReceiver.SocketType.Sender);
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 } else {
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
57 for(int i=0;i<wbr.length;i++) wbr[i] = new WifiBroadcast(i,getPort(i),WifiReceiver.SocketType.Receiver);
10
fc180f38257e multiple sender
one
parents: 9
diff changeset
58 Thread.sleep(timeout/4);
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
59 for(int i=0;i<wbs.length;i++) wbs[i] = new WifiBroadcast(i,getPort(i),WifiReceiver.SocketType.Sender);
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 }
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 } catch (IOException e) {
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
62 System.err.println("err "+e);
9c99e2193277 find broadcast
one
parents: 2
diff changeset
63 } catch (InterruptedException e) {
9c99e2193277 find broadcast
one
parents: 2
diff changeset
64 System.err.println("err "+e);
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 }
9
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
66 runTest(count, wbr, wbs);
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
67
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
68 }
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
69
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
70 private String getAddress(int i) {
cce76e5eb29b with tcp
one
parents: 14
diff changeset
71 return addresses.get(i%addresses.size());
cce76e5eb29b with tcp
one
parents: 14
diff changeset
72 }
cce76e5eb29b with tcp
one
parents: 14
diff changeset
73
cce76e5eb29b with tcp
one
parents: 14
diff changeset
74 private int getPort(int i) {
cce76e5eb29b with tcp
one
parents: 14
diff changeset
75 return ports.get(i%ports.size());
cce76e5eb29b with tcp
one
parents: 14
diff changeset
76 }
cce76e5eb29b with tcp
one
parents: 14
diff changeset
77
cce76e5eb29b with tcp
one
parents: 14
diff changeset
78 public void options(String[] args) {
cce76e5eb29b with tcp
one
parents: 14
diff changeset
79 for(int i=0;i<args.length;i++) {
cce76e5eb29b with tcp
one
parents: 14
diff changeset
80 switch(args[i]) {
cce76e5eb29b with tcp
one
parents: 14
diff changeset
81 case "-m": multicast = true; break;
cce76e5eb29b with tcp
one
parents: 14
diff changeset
82 case "-channel": mchannel = true; break;
cce76e5eb29b with tcp
one
parents: 14
diff changeset
83 case "-bchannel": bchannel = true; break;
cce76e5eb29b with tcp
one
parents: 14
diff changeset
84 case "-tcp": tcp = true; break;
cce76e5eb29b with tcp
one
parents: 14
diff changeset
85 case "-r": i++; receiver_count = getOptInt(args, receiver_count, i); wbr = new WifiReceiver[receiver_count]; break;
cce76e5eb29b with tcp
one
parents: 14
diff changeset
86 case "-s": i++; sender_count = getOptInt(args, sender_count, i); wbs = new WifiReceiver[sender_count]; break;
cce76e5eb29b with tcp
one
parents: 14
diff changeset
87 case "-c": i++; count = getOptInt(args, count, i); break;
cce76e5eb29b with tcp
one
parents: 14
diff changeset
88 case "-t": i++; timeout = getOptInt(args, (int) timeout, i); break;
cce76e5eb29b with tcp
one
parents: 14
diff changeset
89 case "-T": i++; testSize = getOptInt(args, testSize, i); break;
18
e9d07562b6b7 network interface
one
parents: 16
diff changeset
90 case "-n": i++; nis = getOptString(args, nis, i); break;
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
91 case "-p": i++; setPort(args,i); break;
cce76e5eb29b with tcp
one
parents: 14
diff changeset
92 case "-a": i++; setAddress(args, i); break;
cce76e5eb29b with tcp
one
parents: 14
diff changeset
93 default: i++;
cce76e5eb29b with tcp
one
parents: 14
diff changeset
94 System.out.println(
cce76e5eb29b with tcp
one
parents: 14
diff changeset
95 "(default) Broadcast DatagramSocket\n"+
cce76e5eb29b with tcp
one
parents: 14
diff changeset
96 "-m Multicast DatagramSocket\n"+
cce76e5eb29b with tcp
one
parents: 14
diff changeset
97 "-cahnnel Multicast with DatagramChannel\n"+
cce76e5eb29b with tcp
one
parents: 14
diff changeset
98 "-bcahnnel Broadcase with DatagramChannel\n"+
cce76e5eb29b with tcp
one
parents: 14
diff changeset
99 "-tcp TCP stream\n"+
cce76e5eb29b with tcp
one
parents: 14
diff changeset
100 "-r N create N receivers\n"+
cce76e5eb29b with tcp
one
parents: 14
diff changeset
101 "-s N create N senders\n"+
cce76e5eb29b with tcp
one
parents: 14
diff changeset
102 "-c N send count packet\n"+
cce76e5eb29b with tcp
one
parents: 14
diff changeset
103 "-t N timeout N msec\n"+
cce76e5eb29b with tcp
one
parents: 14
diff changeset
104 "-T N send N byte packet\n"+
18
e9d07562b6b7 network interface
one
parents: 16
diff changeset
105 "-n en1 Network Interface name for multicast (default en1)\n"+
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
106 "-p N broadcast/multicast port\n"+
cce76e5eb29b with tcp
one
parents: 14
diff changeset
107 "-a S broadcast/multicast address\n"+
cce76e5eb29b with tcp
one
parents: 14
diff changeset
108 "-h print this\n"+
cce76e5eb29b with tcp
one
parents: 14
diff changeset
109 ""
cce76e5eb29b with tcp
one
parents: 14
diff changeset
110 );
cce76e5eb29b with tcp
one
parents: 14
diff changeset
111 return;
cce76e5eb29b with tcp
one
parents: 14
diff changeset
112 }
cce76e5eb29b with tcp
one
parents: 14
diff changeset
113 }
cce76e5eb29b with tcp
one
parents: 14
diff changeset
114 }
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
118
cce76e5eb29b with tcp
one
parents: 14
diff changeset
119 public void runTest(int count, WifiReceiver[] wbr, WifiReceiver wbs[]) {
9
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
120 Thread t = timeouter();
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
121 Thread send[] = new Thread[wbs.length];
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
122 int id = 0;
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
123 for(WifiReceiver s:wbs) {
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
124 send[id]= sender(s,count);
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
125 id ++;
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
126 }
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
127 id = 0;
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
128 Thread recv[] = new Thread[wbr.length];
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
129 for(WifiReceiver r: wbr) {
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
130 recv[id] = receiver(r,id, count, timeout);
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
131 id ++;
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
132 }
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
133 try {
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
134 for(int i = 0;i<wbr.length;i++) {
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
135 recv[i].join();
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
136 }
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
137 for(int i = 0;i<wbs.length;i++) {
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
138 send[i].join();
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
139 }
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
140 t.join();
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
141 } catch (InterruptedException e) {
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
142 }
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
143 }
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
144
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
145
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
146
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
147 public int getOptInt(String[] args, int count, int i) {
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 if (i<args.length) { count = Integer.parseInt(args[i]); }
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 return count;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 }
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
151
12
e1f43b669cdb broadcast on DatagramChannel
one
parents: 10
diff changeset
152 public String getOptString(String[] args, String str, int i) {
e1f43b669cdb broadcast on DatagramChannel
one
parents: 10
diff changeset
153 if (i<args.length) { str = args[i]; }
e1f43b669cdb broadcast on DatagramChannel
one
parents: 10
diff changeset
154 return str;
e1f43b669cdb broadcast on DatagramChannel
one
parents: 10
diff changeset
155 }
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
156
15
cce76e5eb29b with tcp
one
parents: 14
diff changeset
157 private void setAddress(String[] args, int i) {
cce76e5eb29b with tcp
one
parents: 14
diff changeset
158 String adr = getOptString(args,addresses.get(0),i);
cce76e5eb29b with tcp
one
parents: 14
diff changeset
159 int p;
cce76e5eb29b with tcp
one
parents: 14
diff changeset
160 if ((p=adr.indexOf(":"))>0) {
cce76e5eb29b with tcp
one
parents: 14
diff changeset
161 String sp = adr.substring(p+1);
cce76e5eb29b with tcp
one
parents: 14
diff changeset
162 adr = adr.substring(0,p-1);
cce76e5eb29b with tcp
one
parents: 14
diff changeset
163 if (ports.size()>i) ports.remove(i);
cce76e5eb29b with tcp
one
parents: 14
diff changeset
164 ports.add(i,Integer.parseInt(sp));
cce76e5eb29b with tcp
one
parents: 14
diff changeset
165 }
cce76e5eb29b with tcp
one
parents: 14
diff changeset
166 if (addresses.size()>i) addresses.remove(i);
cce76e5eb29b with tcp
one
parents: 14
diff changeset
167 addresses.add(i,adr);
cce76e5eb29b with tcp
one
parents: 14
diff changeset
168 }
cce76e5eb29b with tcp
one
parents: 14
diff changeset
169
cce76e5eb29b with tcp
one
parents: 14
diff changeset
170 private void setPort(String[] args, int i) {
cce76e5eb29b with tcp
one
parents: 14
diff changeset
171 if (ports.size()>i) ports.remove(i);
cce76e5eb29b with tcp
one
parents: 14
diff changeset
172 ports.add(i,Integer.parseInt(args[i]));
cce76e5eb29b with tcp
one
parents: 14
diff changeset
173 }
cce76e5eb29b with tcp
one
parents: 14
diff changeset
174
cce76e5eb29b with tcp
one
parents: 14
diff changeset
175 public Thread sender(final WifiReceiver wbs, final int count) {
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 Runnable sender = new Runnable() {
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
177
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
178 @Override
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 public void run() {
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
180 ByteBuffer testData = getTestData(testSize);
9c99e2193277 find broadcast
one
parents: 2
diff changeset
181 int i = 0;
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 try {
4
404778ac3f55 worked. Broadcast, Multicast, Multicast channel.
one
parents: 3
diff changeset
183 Thread.sleep(timeout);
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
184 for(i = 0; i<count;i++) {
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 testData.putInt(0, i);
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 wbs.send(testData);
2
2a328333ba70 no compile errors
one
parents: 1
diff changeset
187 testData.flip();
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 }
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
189 } catch (Exception e) {
9c99e2193277 find broadcast
one
parents: 2
diff changeset
190 System.err.println("sender error at "+i+" "+e);
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 }
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 }
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 };
2
2a328333ba70 no compile errors
one
parents: 1
diff changeset
194 Thread s = new Thread(sender);
2a328333ba70 no compile errors
one
parents: 1
diff changeset
195 s.start();
9
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
196 return s;
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 }
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198
9
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
199 private boolean running;
1
649b8573372c cleanup
one
parents: 0
diff changeset
200
9
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
201 public Thread timeouter() {
1
649b8573372c cleanup
one
parents: 0
diff changeset
202 running = true;
649b8573372c cleanup
one
parents: 0
diff changeset
203 Runnable timeouter = new Runnable() {
649b8573372c cleanup
one
parents: 0
diff changeset
204 @Override
649b8573372c cleanup
one
parents: 0
diff changeset
205 public void run() {
649b8573372c cleanup
one
parents: 0
diff changeset
206 try {
649b8573372c cleanup
one
parents: 0
diff changeset
207 Thread.sleep(30*1000);
649b8573372c cleanup
one
parents: 0
diff changeset
208 } catch (InterruptedException e) {
649b8573372c cleanup
one
parents: 0
diff changeset
209 }
649b8573372c cleanup
one
parents: 0
diff changeset
210 running = false;
649b8573372c cleanup
one
parents: 0
diff changeset
211 }
649b8573372c cleanup
one
parents: 0
diff changeset
212 };
9
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
213 Thread t = new Thread(timeouter); t.start();
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
214 return t;
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
215 }
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
216
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
217 public Thread receiver(final WifiReceiver wbr, final int id, final int count, final long timeout) {
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
218
1
649b8573372c cleanup
one
parents: 0
diff changeset
219 Runnable receiver = new Runnable() {
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 @Override
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 public void run() {
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 ByteBuffer testData = ByteBuffer.allocate(4096);
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
223 int bad = 0, good = 0;
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 try {
1
649b8573372c cleanup
one
parents: 0
diff changeset
225 for(int i = 0; running && i<count;i++) {
2
2a328333ba70 no compile errors
one
parents: 1
diff changeset
226 testData.clear();
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 wbr.recieve(testData,timeout);
2
2a328333ba70 no compile errors
one
parents: 1
diff changeset
228 if (!testData.hasRemaining()) continue;
5
7679d10bdae9 multiple receivers
one
parents: 4
diff changeset
229 System.out.println("receive id"+id+":"+testData.remaining()+" bytes.");
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 int seq = testData.getInt();
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 if (seq!=i) {
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
232 bad++; i = seq;
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
233 } else {
9c99e2193277 find broadcast
one
parents: 2
diff changeset
234 good++;
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 }
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 }
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
237 } catch (Exception e) {
9c99e2193277 find broadcast
one
parents: 2
diff changeset
238 System.err.println("receiver error "+e);
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
239 }
3
9c99e2193277 find broadcast
one
parents: 2
diff changeset
240 System.out.println("get "+good+" packets, "+bad+" losts.");
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
241 }
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 };
2
2a328333ba70 no compile errors
one
parents: 1
diff changeset
243 Thread r = new Thread(receiver); r.start();
9
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
244 return r;
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
245 }
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
246
9
95d58cc5bb0b instance for test, multiple senders
one
parents: 5
diff changeset
247 public ByteBuffer getTestData(int i) {
0
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
248 ByteBuffer b = ByteBuffer.allocate(i);
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
249 b.putInt(0);
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 for(int j = 0; j<256; j++ ) {
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
251 b.put((byte)j);
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
252 if (! b.hasRemaining()) break;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
253 if (j == 255) j=0;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
254 }
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
255 b.flip();
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
256 return b;
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 }
df9d16620c08 Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
258 }