Mercurial > hg > Members > kono > WifiBroadcast
diff src/wifibroadcast/WifiBroadcastTest.java @ 1:649b8573372c
cleanup
author | one |
---|---|
date | Sat, 28 Jul 2012 12:24:04 +0900 |
parents | df9d16620c08 |
children | 2a328333ba70 |
line wrap: on
line diff
--- a/src/wifibroadcast/WifiBroadcastTest.java Sat Jul 28 12:16:49 2012 +0900 +++ b/src/wifibroadcast/WifiBroadcastTest.java Sat Jul 28 12:24:04 2012 +0900 @@ -12,25 +12,26 @@ int count = 1024; long timeout = 1000; boolean multicast = false; - WifiSender wbs = null; - WifiReciver wbr = null; + boolean mchannel = false; + WifiReceiver wbr = null; for(int i=0;i<args.length;i++) { if (args[i].equals("-m")) multicast = true; + else if (args[i].equals("-channel")) mchannel = true; else if (args[i].equals("-c")) { i++; count = getOptInt(args, count, i);} else if (args[i].equals("-t")) { i++; timeout = getOptInt(args, count, i);} else if (args[i].equals("-p")) { i++; port = getOptInt(args, count, i);} } try { if (multicast) { - wbs = new WifiBroadcastSender(port); - wbr = new WifiBroadcastReciver(port); + wbr = new WifiMulticast(MCASTADDR,port); + } else if (mchannel) { + wbr = new WifiMulticastChannel(MCASTADDR,port); } else { - wbs = new WifiMulticastSender(MCASTADDR,port); - wbr = new WifiMulticastChannel(MCASTADDR,port); + wbr = new WifiBroadcast(port); } } catch (IOException e) { } - sender(wbs,count); + sender(wbr,count); receiver(wbr,count, timeout); } @@ -43,7 +44,7 @@ - public static void sender(final WifiSender wbs, final int count) { + public static void sender(final WifiReceiver wbs, final int count) { Runnable sender = new Runnable() { @Override public void run() { @@ -60,14 +61,27 @@ new Thread(sender).start(); } - public static void receiver(final WifiReciver wbr, final int count, final long timeout) { - Runnable sender = new Runnable() { + private static boolean running; + + public static void receiver(final WifiReceiver wbr, final int count, final long timeout) { + running = true; + Runnable timeouter = new Runnable() { + @Override + public void run() { + try { + Thread.sleep(30*1000); + } catch (InterruptedException e) { + } + running = false; + } + }; + Runnable receiver = new Runnable() { @Override public void run() { ByteBuffer testData = ByteBuffer.allocate(4096); int num = 0, bad = 0; try { - for(int i = 0; i<count;i++) { + for(int i = 0; running && i<count;i++) { wbr.recieve(testData,timeout); int seq = testData.getInt(); if (seq!=i) { @@ -79,7 +93,8 @@ System.out.println("get "+num+" packets, "+bad+" losts."); } }; - new Thread(sender).start(); + new Thread(receiver).start(); + new Thread(timeouter).start(); } public static ByteBuffer getTestData(int i) {