comparison src/wifibroadcast/WifiBroadcastTest.java @ 1:649b8573372c

cleanup
author one
date Sat, 28 Jul 2012 12:24:04 +0900
parents df9d16620c08
children 2a328333ba70
comparison
equal deleted inserted replaced
0:df9d16620c08 1:649b8573372c
10 static void main(String args[]) { 10 static void main(String args[]) {
11 int port = DefaultPort ; 11 int port = DefaultPort ;
12 int count = 1024; 12 int count = 1024;
13 long timeout = 1000; 13 long timeout = 1000;
14 boolean multicast = false; 14 boolean multicast = false;
15 WifiSender wbs = null; 15 boolean mchannel = false;
16 WifiReciver wbr = null; 16 WifiReceiver wbr = null;
17 for(int i=0;i<args.length;i++) { 17 for(int i=0;i<args.length;i++) {
18 if (args[i].equals("-m")) multicast = true; 18 if (args[i].equals("-m")) multicast = true;
19 else if (args[i].equals("-channel")) mchannel = true;
19 else if (args[i].equals("-c")) { i++; count = getOptInt(args, count, i);} 20 else if (args[i].equals("-c")) { i++; count = getOptInt(args, count, i);}
20 else if (args[i].equals("-t")) { i++; timeout = getOptInt(args, count, i);} 21 else if (args[i].equals("-t")) { i++; timeout = getOptInt(args, count, i);}
21 else if (args[i].equals("-p")) { i++; port = getOptInt(args, count, i);} 22 else if (args[i].equals("-p")) { i++; port = getOptInt(args, count, i);}
22 } 23 }
23 try { 24 try {
24 if (multicast) { 25 if (multicast) {
25 wbs = new WifiBroadcastSender(port); 26 wbr = new WifiMulticast(MCASTADDR,port);
26 wbr = new WifiBroadcastReciver(port); 27 } else if (mchannel) {
28 wbr = new WifiMulticastChannel(MCASTADDR,port);
27 } else { 29 } else {
28 wbs = new WifiMulticastSender(MCASTADDR,port); 30 wbr = new WifiBroadcast(port);
29 wbr = new WifiMulticastChannel(MCASTADDR,port);
30 } 31 }
31 } catch (IOException e) { 32 } catch (IOException e) {
32 } 33 }
33 sender(wbs,count); 34 sender(wbr,count);
34 receiver(wbr,count, timeout); 35 receiver(wbr,count, timeout);
35 } 36 }
36 37
37 38
38 39
41 return count; 42 return count;
42 } 43 }
43 44
44 45
45 46
46 public static void sender(final WifiSender wbs, final int count) { 47 public static void sender(final WifiReceiver wbs, final int count) {
47 Runnable sender = new Runnable() { 48 Runnable sender = new Runnable() {
48 @Override 49 @Override
49 public void run() { 50 public void run() {
50 ByteBuffer testData = getTestData(4096); 51 ByteBuffer testData = getTestData(4096);
51 try { 52 try {
58 } 59 }
59 }; 60 };
60 new Thread(sender).start(); 61 new Thread(sender).start();
61 } 62 }
62 63
63 public static void receiver(final WifiReciver wbr, final int count, final long timeout) { 64 private static boolean running;
64 Runnable sender = new Runnable() { 65
66 public static void receiver(final WifiReceiver wbr, final int count, final long timeout) {
67 running = true;
68 Runnable timeouter = new Runnable() {
69 @Override
70 public void run() {
71 try {
72 Thread.sleep(30*1000);
73 } catch (InterruptedException e) {
74 }
75 running = false;
76 }
77 };
78 Runnable receiver = new Runnable() {
65 @Override 79 @Override
66 public void run() { 80 public void run() {
67 ByteBuffer testData = ByteBuffer.allocate(4096); 81 ByteBuffer testData = ByteBuffer.allocate(4096);
68 int num = 0, bad = 0; 82 int num = 0, bad = 0;
69 try { 83 try {
70 for(int i = 0; i<count;i++) { 84 for(int i = 0; running && i<count;i++) {
71 wbr.recieve(testData,timeout); 85 wbr.recieve(testData,timeout);
72 int seq = testData.getInt(); 86 int seq = testData.getInt();
73 if (seq!=i) { 87 if (seq!=i) {
74 bad++; i = seq; 88 bad++; i = seq;
75 } 89 }
77 } catch (IOException e) { 91 } catch (IOException e) {
78 } 92 }
79 System.out.println("get "+num+" packets, "+bad+" losts."); 93 System.out.println("get "+num+" packets, "+bad+" losts.");
80 } 94 }
81 }; 95 };
82 new Thread(sender).start(); 96 new Thread(receiver).start();
97 new Thread(timeouter).start();
83 } 98 }
84 99
85 public static ByteBuffer getTestData(int i) { 100 public static ByteBuffer getTestData(int i) {
86 ByteBuffer b = ByteBuffer.allocate(i); 101 ByteBuffer b = ByteBuffer.allocate(i);
87 b.putInt(0); 102 b.putInt(0);