Mercurial > hg > Members > kono > WifiBroadcast
changeset 1:649b8573372c
cleanup
author | one |
---|---|
date | Sat, 28 Jul 2012 12:24:04 +0900 |
parents | df9d16620c08 |
children | 2a328333ba70 |
files | src/wifibroadcast/WifiBroadcast.java src/wifibroadcast/WifiBroadcastReciver.java src/wifibroadcast/WifiBroadcastSender.java src/wifibroadcast/WifiBroadcastTest.java src/wifibroadcast/WifiMulticast.java src/wifibroadcast/WifiMulticastChannel.java src/wifibroadcast/WifiReceiver.java src/wifibroadcast/WifiReciver.java src/wifibroadcast/WifiSender.java |
diffstat | 9 files changed, 92 insertions(+), 107 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/wifibroadcast/WifiBroadcast.java Sat Jul 28 12:24:04 2012 +0900 @@ -0,0 +1,50 @@ +package wifibroadcast; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.SocketException; +import java.net.StandardSocketOptions; +import java.nio.ByteBuffer; +import java.nio.channels.DatagramChannel; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.spi.SelectorProvider; +import java.util.Iterator; + +public class WifiBroadcast implements WifiReceiver,WifiSender { + + private DatagramChannel channel; + private Selector selector; + + public WifiBroadcast(int port) throws IOException { + selector = SelectorProvider.provider().openSelector(); + channel = SelectorProvider.provider().openDatagramChannel(); + channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); + try { + InetSocketAddress address = new InetSocketAddress("::", port); + channel.socket().bind(address); + } catch (SocketException e) { + // for some bad IPv6 implementation + channel.socket().bind(new InetSocketAddress(port)); + } + channel.register(selector, SelectionKey.OP_READ); + } + + public void recieve(ByteBuffer testData, long timeout) throws IOException { + if (selector.select(timeout)>0) { + for (Iterator<SelectionKey> it = selector.selectedKeys().iterator();it.hasNext(); ) { + SelectionKey s = it.next(); + it.remove(); + DatagramChannel ch = (DatagramChannel)s.channel(); + ch.read(testData); + testData.flip(); + return; // one at a time + } + } + } + + + public void send(ByteBuffer testData) throws IOException { + channel.write(testData); + } +}
--- a/src/wifibroadcast/WifiBroadcastReciver.java Sat Jul 28 12:16:49 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ -package wifibroadcast; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.SocketException; -import java.net.StandardSocketOptions; -import java.nio.ByteBuffer; -import java.nio.channels.ClosedChannelException; -import java.nio.channels.DatagramChannel; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.spi.SelectorProvider; -import java.util.Iterator; - -import fdl.TupleHandler; - -public class WifiBroadcastReciver implements WifiReciver { - - private DatagramChannel channel; - private Selector selector; - - public WifiBroadcastReciver(int port) throws IOException { - selector = SelectorProvider.provider().openSelector(); - channel = SelectorProvider.provider().openDatagramChannel(); - channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); - try { - InetSocketAddress address = new InetSocketAddress("::", port); - channel.socket().bind(address); - } catch (SocketException e) { - // for some bad IPv6 implementation - channel.socket().bind(new InetSocketAddress(port)); - } - channel.register(selector, SelectionKey.OP_READ); - } - - public void recieve(ByteBuffer testData, long timeout) throws IOException { - if (selector.select(timeout)>0) { - for (Iterator<SelectionKey> it = selector.selectedKeys().iterator();it.hasNext(); ) { - SelectionKey s = it.next(); - it.remove(); - DatagramChannel ch = (DatagramChannel)s.channel(); - ch.read(testData); - testData.flip(); - return; // one at a time - } - } - } - -}
--- a/src/wifibroadcast/WifiBroadcastSender.java Sat Jul 28 12:16:49 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -package wifibroadcast; - -import java.io.IOException; -import java.net.DatagramSocket; -import java.net.StandardSocketOptions; -import java.nio.ByteBuffer; -import java.nio.channels.DatagramChannel; - -public class WifiBroadcastSender implements WifiSender { - - private DatagramSocket socket; - private DatagramChannel channel; - - public WifiBroadcastSender(int port) throws IOException { - socket = new DatagramSocket(port); - channel = socket.getChannel(); - channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); - } - - public void send(ByteBuffer testData) throws IOException { - channel.write(testData); - } - -}
--- 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) {
--- a/src/wifibroadcast/WifiMulticast.java Sat Jul 28 12:16:49 2012 +0900 +++ b/src/wifibroadcast/WifiMulticast.java Sat Jul 28 12:24:04 2012 +0900 @@ -8,7 +8,7 @@ import java.net.InetAddress; import java.net.MulticastSocket; - public class WifiMulticast implements WifiReciver,WifiSender { + public class WifiMulticast implements WifiReceiver,WifiSender { private MulticastSocket soc; private InetAddress mAddr; private int port;
--- a/src/wifibroadcast/WifiMulticastChannel.java Sat Jul 28 12:16:49 2012 +0900 +++ b/src/wifibroadcast/WifiMulticastChannel.java Sat Jul 28 12:24:04 2012 +0900 @@ -10,7 +10,7 @@ import java.nio.ByteBuffer; import java.nio.channels.DatagramChannel; -public class WifiMulticastChannel implements WifiReciver,WifiSender { +public class WifiMulticastChannel implements WifiReceiver,WifiSender { private InetAddress mAddr; private DatagramChannel dc; private SocketAddress sAddr;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/wifibroadcast/WifiReceiver.java Sat Jul 28 12:24:04 2012 +0900 @@ -0,0 +1,13 @@ +package wifibroadcast; + +import java.io.IOException; +import java.nio.ByteBuffer; + +public interface WifiReceiver { + + void recieve(ByteBuffer testData, long timeout) throws IOException; + + void send(ByteBuffer testData) throws IOException; + + +}
--- a/src/wifibroadcast/WifiReciver.java Sat Jul 28 12:16:49 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -package wifibroadcast; - -import java.io.IOException; -import java.nio.ByteBuffer; - -public interface WifiReciver { - - void recieve(ByteBuffer testData, long timeout) throws IOException; - -}
--- a/src/wifibroadcast/WifiSender.java Sat Jul 28 12:16:49 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -package wifibroadcast; - -import java.io.IOException; -import java.nio.ByteBuffer; - -public interface WifiSender { - - void send(ByteBuffer testData) throws IOException; - -}