Mercurial > hg > Members > kono > WifiBroadcast
changeset 12:e1f43b669cdb
broadcast on DatagramChannel
author | one |
---|---|
date | Sun, 29 Jul 2012 14:37:12 +0900 |
parents | 7912fd3af027 |
children | 9bf4bf393e4e |
files | src/wifibroadcast/WifiBroadcast.java src/wifibroadcast/WifiBroadcastTest.java src/wifibroadcast/WifiMulticast.java src/wifibroadcast/WifiMulticastChannel.java |
diffstat | 4 files changed, 46 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/src/wifibroadcast/WifiBroadcast.java Sun Jul 29 12:10:17 2012 +0900 +++ b/src/wifibroadcast/WifiBroadcast.java Sun Jul 29 14:37:12 2012 +0900 @@ -61,7 +61,7 @@ return InetAddress.getByName("192.168.100.66"); } - public InetAddress getBroadcast() throws SocketException { + public static InetAddress getBroadcast() throws SocketException { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) {
--- a/src/wifibroadcast/WifiBroadcastTest.java Sun Jul 29 12:10:17 2012 +0900 +++ b/src/wifibroadcast/WifiBroadcastTest.java Sun Jul 29 14:37:12 2012 +0900 @@ -18,6 +18,7 @@ int sender_count = 1; WifiReceiver wbr[] = new WifiReceiver[receiver_count]; WifiReceiver wbs[] = new WifiReceiver[sender_count]; + private boolean bchannel; public static void main(String args[]) { new WifiBroadcastTest().test(args); @@ -33,7 +34,11 @@ } else if (mchannel) { for(int i=0;i<wbr.length;i++) wbr[i] = new WifiMulticastChannel(i,MCASTADDR,port,WifiReceiver.SocketType.Receiver); Thread.sleep(timeout/4); - for(int i=0;i<wbs.length;i++) wbs[i] = new WifiMulticastChannel(i,MCASTADDR,port,WifiReceiver.SocketType.Sender); + for(int i=0;i<wbs.length;i++) wbs[i] = new WifiMulticastChannel(i,MCASTADDR,port,WifiReceiver.SocketType.Sender); + } else if (bchannel) { + for(int i=0;i<wbr.length;i++) wbr[i] = new WifiBroadcastChannel(i,port,WifiReceiver.SocketType.Receiver); + Thread.sleep(timeout/4); + for(int i=0;i<wbs.length;i++) wbs[i] = new WifiBroadcastChannel(i,port,WifiReceiver.SocketType.Sender); } else { for(int i=0;i<wbr.length;i++) wbr[i] = new WifiBroadcast(i,port,WifiReceiver.SocketType.Receiver); Thread.sleep(timeout/4); @@ -50,14 +55,34 @@ 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("-T")) { i++; testSize = getOptInt(args, count, i);} - else if (args[i].equals("-p")) { i++; port = getOptInt(args, count, i);} + switch(args[i]) { + case "-m": multicast = true; break; + case "-channel": mchannel = true; break; + case "-bchannel": bchannel = 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 "-p": i++; port = getOptInt(args, port, i); break; + case "-a": i++; MCASTADDR = getOptString(args, MCASTADDR, 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"+ + "-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"+ + "-p N broadcast/multicast port\n"+ + "-a S broadcast/multicast address\n"+ + "-h print this\n"+ + "" + ); + } } } @@ -96,7 +121,10 @@ return count; } - + public String getOptString(String[] args, String str, int i) { + if (i<args.length) { str = args[i]; } + return str; + } public Thread sender(final WifiReceiver wbs, final int count) { Runnable sender = new Runnable() {
--- a/src/wifibroadcast/WifiMulticast.java Sun Jul 29 12:10:17 2012 +0900 +++ b/src/wifibroadcast/WifiMulticast.java Sun Jul 29 14:37:12 2012 +0900 @@ -2,7 +2,6 @@ import java.io.IOException; import java.nio.ByteBuffer; - import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket;
--- a/src/wifibroadcast/WifiMulticastChannel.java Sun Jul 29 12:10:17 2012 +0900 +++ b/src/wifibroadcast/WifiMulticastChannel.java Sun Jul 29 14:37:12 2012 +0900 @@ -14,12 +14,14 @@ import java.nio.channels.spi.SelectorProvider; public class WifiMulticastChannel implements WifiReceiver { - private InetAddress mAddr; - private DatagramChannel dc; - private SocketAddress sAddr; + protected InetAddress mAddr; + protected DatagramChannel dc; + protected SocketAddress sAddr; // select on DatagramChannel does not work now - private Selector selector; - private boolean selectMode = false; + protected Selector selector; + protected boolean selectMode = false; + + public WifiMulticastChannel() {} public WifiMulticastChannel(int id, String mCASTADDR, int port, SocketType sender) throws IOException { // join multicast group on this interface, and also use this @@ -39,7 +41,6 @@ dc = DatagramChannel.open(StandardProtocolFamily.INET) .setOption(StandardSocketOptions.SO_REUSEADDR, true) .setOption(StandardSocketOptions.IP_MULTICAST_IF, ni); - mAddr = InetAddress.getByName(mCASTADDR); sAddr = new InetSocketAddress(mAddr,port); dc.join(mAddr, ni);