Mercurial > hg > Members > kono > WifiBroadcast
view 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 source
package wifibroadcast; import java.io.IOException; import java.nio.ByteBuffer; public class WifiBroadcastTest { private static int DefaultPort = 8212; private static String MCASTADDR = "224.0.0.1"; static void main(String args[]) { int port = DefaultPort ; int count = 1024; long timeout = 1000; boolean multicast = false; 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) { wbr = new WifiMulticast(MCASTADDR,port); } else if (mchannel) { wbr = new WifiMulticastChannel(MCASTADDR,port); } else { wbr = new WifiBroadcast(port); } } catch (IOException e) { } sender(wbr,count); receiver(wbr,count, timeout); } public static int getOptInt(String[] args, int count, int i) { if (i<args.length) { count = Integer.parseInt(args[i]); } return count; } public static void sender(final WifiReceiver wbs, final int count) { Runnable sender = new Runnable() { @Override public void run() { ByteBuffer testData = getTestData(4096); try { for(int i = 0; i<count;i++) { testData.putInt(0, i); wbs.send(testData); } } catch (IOException e) { } } }; new Thread(sender).start(); } 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; running && i<count;i++) { wbr.recieve(testData,timeout); int seq = testData.getInt(); if (seq!=i) { bad++; i = seq; } } } catch (IOException e) { } System.out.println("get "+num+" packets, "+bad+" losts."); } }; new Thread(receiver).start(); new Thread(timeouter).start(); } public static ByteBuffer getTestData(int i) { ByteBuffer b = ByteBuffer.allocate(i); b.putInt(0); for(int j = 0; j<256; j++ ) { b.put((byte)j); if (! b.hasRemaining()) break; if (j == 255) j=0; } b.flip(); return b; } }