Mercurial > hg > Members > kono > WifiBroadcast
view src/wifibroadcast/WifiBroadcastTest.java @ 18:e9d07562b6b7
network interface
author | one |
---|---|
date | Tue, 31 Jul 2012 16:32:03 +0900 |
parents | b5ab7003f6d1 |
children | bb2538b7eb8d |
line wrap: on
line source
package wifibroadcast; import java.io.IOException; import java.nio.ByteBuffer; import java.util.LinkedList; public class WifiBroadcastTest { private static int DefaultPort = 8412; private static String MCASTADDR = "224.0.0.1"; private static int testSize = 256; private static long timeout = 1000; LinkedList<Integer> ports = new LinkedList<Integer>(); LinkedList<String> addresses = new LinkedList<String>(); int count = 16; boolean multicast = false; boolean mchannel = false; int receiver_count = 1; int sender_count = 1; WifiReceiver wbr[] = new WifiReceiver[receiver_count]; WifiReceiver wbs[] = new WifiReceiver[sender_count]; private boolean bchannel; private boolean tcp; private String nis = "en1"; public WifiBroadcastTest() { ports.add(0,DefaultPort); addresses.add(0,MCASTADDR); } public static void main(String args[]) { new WifiBroadcastTest().test(args); } public void test(String args[]) { options(args); try { if (multicast) { for(int i=0;i<wbr.length;i++) wbr[i] = new WifiMulticast(i,getAddress(i),getPort(i),WifiReceiver.SocketType.Receiver); Thread.sleep(timeout/4); for(int i=0;i<wbs.length;i++) wbs[i] = new WifiMulticast(i,getAddress(i),getPort(i),WifiReceiver.SocketType.Sender); } else if (mchannel) { for(int i=0;i<wbr.length;i++) wbr[i] = new WifiMulticastChannel(i,getAddress(i),getPort(i),nis, WifiReceiver.SocketType.Receiver); Thread.sleep(timeout/4); for(int i=0;i<wbs.length;i++) wbs[i] = new WifiMulticastChannel(i,getAddress(i),getPort(i),nis, WifiReceiver.SocketType.Sender); } else if (tcp) { addresses.remove(); addresses.add("127.1"); for(int i=0;i<wbr.length;i++) wbr[i] = new WifiTCP(i,getAddress(i),getPort(i),WifiReceiver.SocketType.Receiver); Thread.sleep(timeout/4); for(int i=0;i<wbs.length;i++) wbs[i] = new WifiTCP(i,getAddress(i),getPort(i),WifiReceiver.SocketType.Sender); } else if (bchannel) { for(int i=0;i<wbr.length;i++) wbr[i] = new WifiBroadcastChannel(i,getPort(i),WifiReceiver.SocketType.Receiver); Thread.sleep(timeout/4); for(int i=0;i<wbs.length;i++) wbs[i] = new WifiBroadcastChannel(i,getPort(i),WifiReceiver.SocketType.Sender); } else { for(int i=0;i<wbr.length;i++) wbr[i] = new WifiBroadcast(i,getPort(i),WifiReceiver.SocketType.Receiver); Thread.sleep(timeout/4); for(int i=0;i<wbs.length;i++) wbs[i] = new WifiBroadcast(i,getPort(i),WifiReceiver.SocketType.Sender); } } catch (IOException e) { System.err.println("err "+e); } catch (InterruptedException e) { System.err.println("err "+e); } runTest(count, wbr, wbs); } private String getAddress(int i) { return addresses.get(i%addresses.size()); } private int getPort(int i) { return ports.get(i%ports.size()); } public void options(String[] args) { for(int i=0;i<args.length;i++) { switch(args[i]) { case "-m": multicast = true; break; case "-channel": mchannel = true; break; case "-bchannel": bchannel = true; break; case "-tcp": tcp = true; break; case "-r": i++; receiver_count = getOptInt(args, receiver_count, i); wbr = new WifiReceiver[receiver_count]; break; case "-s": i++; sender_count = getOptInt(args, sender_count, i); wbs = new WifiReceiver[sender_count]; break; case "-c": i++; count = getOptInt(args, count, i); break; case "-t": i++; timeout = getOptInt(args, (int) timeout, i); break; case "-T": i++; testSize = getOptInt(args, testSize, i); break; case "-n": i++; nis = getOptString(args, nis, i); break; case "-p": i++; setPort(args,i); break; case "-a": i++; setAddress(args, i); break; default: i++; System.out.println( "(default) Broadcast DatagramSocket\n"+ "-m Multicast DatagramSocket\n"+ "-cahnnel Multicast with DatagramChannel\n"+ "-bcahnnel Broadcase with DatagramChannel\n"+ "-tcp TCP stream\n"+ "-r N create N receivers\n"+ "-s N create N senders\n"+ "-c N send count packet\n"+ "-t N timeout N msec\n"+ "-T N send N byte packet\n"+ "-n en1 Network Interface name for multicast (default en1)\n"+ "-p N broadcast/multicast port\n"+ "-a S broadcast/multicast address\n"+ "-h print this\n"+ "" ); return; } } } public void runTest(int count, WifiReceiver[] wbr, WifiReceiver wbs[]) { Thread t = timeouter(); Thread send[] = new Thread[wbs.length]; int id = 0; for(WifiReceiver s:wbs) { send[id]= sender(s,count); id ++; } id = 0; Thread recv[] = new Thread[wbr.length]; for(WifiReceiver r: wbr) { recv[id] = receiver(r,id, count, timeout); id ++; } try { for(int i = 0;i<wbr.length;i++) { recv[i].join(); } for(int i = 0;i<wbs.length;i++) { send[i].join(); } t.join(); } catch (InterruptedException e) { } } public int getOptInt(String[] args, int count, int i) { if (i<args.length) { count = Integer.parseInt(args[i]); } return count; } public String getOptString(String[] args, String str, int i) { if (i<args.length) { str = args[i]; } return str; } private void setAddress(String[] args, int i) { String adr = getOptString(args,addresses.get(0),i); int p; if ((p=adr.indexOf(":"))>0) { String sp = adr.substring(p+1); adr = adr.substring(0,p-1); if (ports.size()>i) ports.remove(i); ports.add(i,Integer.parseInt(sp)); } if (addresses.size()>i) addresses.remove(i); addresses.add(i,adr); } private void setPort(String[] args, int i) { if (ports.size()>i) ports.remove(i); ports.add(i,Integer.parseInt(args[i])); } public Thread sender(final WifiReceiver wbs, final int count) { Runnable sender = new Runnable() { @Override public void run() { ByteBuffer testData = getTestData(testSize); int i = 0; try { Thread.sleep(timeout); for(i = 0; i<count;i++) { testData.putInt(0, i); wbs.send(testData); testData.flip(); } } catch (Exception e) { System.err.println("sender error at "+i+" "+e); } } }; Thread s = new Thread(sender); s.start(); return s; } private boolean running; public Thread timeouter() { running = true; Runnable timeouter = new Runnable() { @Override public void run() { try { Thread.sleep(30*1000); } catch (InterruptedException e) { } running = false; } }; Thread t = new Thread(timeouter); t.start(); return t; } public Thread receiver(final WifiReceiver wbr, final int id, final int count, final long timeout) { Runnable receiver = new Runnable() { @Override public void run() { ByteBuffer testData = ByteBuffer.allocate(4096); int bad = 0, good = 0; try { for(int i = 0; running && i<count;i++) { testData.clear(); wbr.recieve(testData,timeout); if (!testData.hasRemaining()) continue; System.out.println("receive id"+id+":"+testData.remaining()+" bytes."); int seq = testData.getInt(); if (seq!=i) { bad++; i = seq; } else { good++; } } } catch (Exception e) { System.err.println("receiver error "+e); } System.out.println("get "+good+" packets, "+bad+" losts."); } }; Thread r = new Thread(receiver); r.start(); return r; } public 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; } }