Mercurial > hg > Members > kono > WifiBroadcast
changeset 9:95d58cc5bb0b
instance for test, multiple senders
author | one |
---|---|
date | Sun, 29 Jul 2012 00:03:29 +0900 |
parents | 058b77fe8472 |
children | fc180f38257e |
files | .classpath src/wifibroadcast/WifiBroadcast.java src/wifibroadcast/WifiBroadcastTest.java src/wifibroadcast/WifiDatagram.java src/wifibroadcast/WifiMulticast.java src/wifibroadcast/WifiMulticastChannel.java |
diffstat | 6 files changed, 81 insertions(+), 50 deletions(-) [+] |
line wrap: on
line diff
--- a/.classpath Sat Jul 28 22:06:42 2012 +0900 +++ b/.classpath Sun Jul 29 00:03:29 2012 +0900 @@ -6,15 +6,11 @@ <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"> - <attributes> - <attribute name="maven.pomderived" value="true"/> - </attributes> - </classpathentry> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk7u6"/> <classpathentry kind="output" path="target/classes"/> </classpath>
--- a/src/wifibroadcast/WifiBroadcast.java Sat Jul 28 22:06:42 2012 +0900 +++ b/src/wifibroadcast/WifiBroadcast.java Sun Jul 29 00:03:29 2012 +0900 @@ -18,7 +18,7 @@ private int port; private InetAddress mAddr; - public WifiBroadcast(int port, SocketType sender) throws IOException { + public WifiBroadcast(int id,int port, SocketType sender) throws IOException { this.port = port; if (sender == SocketType.Sender) { @@ -27,7 +27,7 @@ System.out.println("Found broadcast "+address0); mAddr = address0; s = new DatagramSocket(); - s.bind(new InetSocketAddress(address0,port+1)); + s.bind(new InetSocketAddress(address0,port+1+id)); s.setBroadcast(true); } catch (SocketException e) { }
--- a/src/wifibroadcast/WifiBroadcastTest.java Sat Jul 28 22:06:42 2012 +0900 +++ b/src/wifibroadcast/WifiBroadcastTest.java Sun Jul 29 00:03:29 2012 +0900 @@ -9,58 +9,95 @@ private static int testSize = 256; private static long timeout = 1000; + + int port = DefaultPort ; + 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]; + public static void main(String args[]) { - int port = DefaultPort ; - int count = 16; - boolean multicast = false; - boolean mchannel = false; - int receiver_count = 1; - WifiReceiver wbr[] = new WifiReceiver[receiver_count]; - WifiReceiver wbs = 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("-r")) { i++; receiver_count = getOptInt(args, count, i); wbr = new WifiReceiver[receiver_count]; } - 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);} - } + 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(MCASTADDR,port,WifiReceiver.SocketType.Receiver); + for(int i=0;i<wbr.length;i++) wbr[i] = new WifiMulticast(i,MCASTADDR,port,WifiReceiver.SocketType.Receiver); Thread.sleep(timeout); - wbs = new WifiMulticast(MCASTADDR,port,WifiReceiver.SocketType.Receiver); + for(int i=0;i<wbs.length;i++) wbs[i] = new WifiMulticast(i,MCASTADDR,port,WifiReceiver.SocketType.Sender); } else if (mchannel) { - for(int i=0;i<wbr.length;i++) wbr[i]= new WifiMulticastChannel(MCASTADDR,port,WifiReceiver.SocketType.Receiver); + for(int i=0;i<wbr.length;i++) wbr[i]= new WifiMulticastChannel(i,MCASTADDR,port,WifiReceiver.SocketType.Receiver); Thread.sleep(timeout); - wbs = new WifiMulticastChannel(MCASTADDR,port,WifiReceiver.SocketType.Sender); + for(int i=0;i<wbs.length;i++) wbs[i] = new WifiMulticastChannel(i,MCASTADDR,port,WifiReceiver.SocketType.Sender); } else { - for(int i=0;i<wbr.length;i++) wbr[i]= new WifiBroadcast(port,WifiReceiver.SocketType.Receiver); + for(int i=0;i<wbr.length;i++) wbr[i]= new WifiBroadcast(i,port,WifiReceiver.SocketType.Receiver); Thread.sleep(timeout); - wbs = new WifiBroadcast(port,WifiReceiver.SocketType.Sender); + for(int i=0;i<wbs.length;i++) wbs[i] = new WifiBroadcast(i,port,WifiReceiver.SocketType.Sender); } } catch (IOException e) { System.err.println("err "+e); } catch (InterruptedException e) { System.err.println("err "+e); } - sender(wbs,count); - int id = 0; - for(WifiReceiver r: wbr) { - receiver(r,id++, count, timeout); + runTest(count, wbr, wbs); + + } + + public void options(String[] args) { + 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("-r")) { i++; receiver_count = getOptInt(args, count, i); wbr = new WifiReceiver[receiver_count]; } + else if (args[i].equals("-s")) { i++; sender_count = getOptInt(args, count, i); wbs = new WifiReceiver[sender_count]; } + 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);} } } - public static int getOptInt(String[] args, int count, int i) { + 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 static void sender(final WifiReceiver wbs, final int count) { + public Thread sender(final WifiReceiver wbs, final int count) { Runnable sender = new Runnable() { @Override @@ -81,15 +118,12 @@ }; Thread s = new Thread(sender); s.start(); - try { - s.join(); - } catch (InterruptedException e) { - } + return s; } - private static boolean running; + private boolean running; - public static void receiver(final WifiReceiver wbr, final int id, final int count, final long timeout) { + public Thread timeouter() { running = true; Runnable timeouter = new Runnable() { @Override @@ -101,6 +135,12 @@ 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() { @@ -126,15 +166,10 @@ } }; Thread r = new Thread(receiver); r.start(); - Thread t = new Thread(timeouter); t.start(); - try { - r.join(); - t.join(); - } catch (InterruptedException e) { - } + return r; } - public static ByteBuffer getTestData(int i) { + public ByteBuffer getTestData(int i) { ByteBuffer b = ByteBuffer.allocate(i); b.putInt(0); for(int j = 0; j<256; j++ ) {
--- a/src/wifibroadcast/WifiDatagram.java Sat Jul 28 22:06:42 2012 +0900 +++ b/src/wifibroadcast/WifiDatagram.java Sun Jul 29 00:03:29 2012 +0900 @@ -15,7 +15,7 @@ private int port; private InetAddress mAddr; - public WifiDatagram(int port, SocketType sender) throws IOException { + public WifiDatagram(int id, int port, SocketType sender) throws IOException { this.port = port; if (sender == SocketType.Sender) {
--- a/src/wifibroadcast/WifiMulticast.java Sat Jul 28 22:06:42 2012 +0900 +++ b/src/wifibroadcast/WifiMulticast.java Sun Jul 29 00:03:29 2012 +0900 @@ -12,7 +12,7 @@ private InetAddress mAddr; private int port; - public WifiMulticast(String mCASTADDR, int port, SocketType sender) throws IOException { + public WifiMulticast(int id, String mCASTADDR, int port, SocketType sender) throws IOException { this.port = port; try { mAddr = InetAddress.getByName(mCASTADDR);
--- a/src/wifibroadcast/WifiMulticastChannel.java Sat Jul 28 22:06:42 2012 +0900 +++ b/src/wifibroadcast/WifiMulticastChannel.java Sun Jul 29 00:03:29 2012 +0900 @@ -19,7 +19,7 @@ private SocketAddress sAddr; private Selector selector; - public WifiMulticastChannel(String mCASTADDR, int port, SocketType sender) throws IOException { + public WifiMulticastChannel(int id, String mCASTADDR, int port, SocketType sender) throws IOException { // join multicast group on this interface, and also use this // interface for outgoing multicast datagrams selector = SelectorProvider.provider().openSelector();