Mercurial > hg > Members > kono > WifiBroadcast
changeset 0:df9d16620c08
Wifi broad cast test program
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 28 Jul 2012 12:16:49 +0900 |
parents | |
children | 649b8573372c |
files | .classpath .project .settings/org.eclipse.jdt.core.prefs src/wifibroadcast/WifiBroadcastReciver.java src/wifibroadcast/WifiBroadcastSender.java src/wifibroadcast/WifiBroadcastTest.java src/wifibroadcast/WifiMulticast.java src/wifibroadcast/WifiMulticastChannel.java src/wifibroadcast/WifiReciver.java src/wifibroadcast/WifiSender.java |
diffstat | 10 files changed, 310 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.classpath Sat Jul 28 12:16:49 2012 +0900 @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> + <classpathentry kind="output" path="bin"/> +</classpath>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.project Sat Jul 28 12:16:49 2012 +0900 @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>WifiBroadcast</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.settings/org.eclipse.jdt.core.prefs Sat Jul 28 12:16:49 2012 +0900 @@ -0,0 +1,12 @@ +#Thu Jul 19 20:50:47 JST 2012 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/wifibroadcast/WifiBroadcastReciver.java Sat Jul 28 12:16:49 2012 +0900 @@ -0,0 +1,49 @@ +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 + } + } + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/wifibroadcast/WifiBroadcastSender.java Sat Jul 28 12:16:49 2012 +0900 @@ -0,0 +1,24 @@ +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); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/wifibroadcast/WifiBroadcastTest.java Sat Jul 28 12:16:49 2012 +0900 @@ -0,0 +1,96 @@ +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; + WifiSender wbs = null; + WifiReciver wbr = null; + for(int i=0;i<args.length;i++) { + if (args[i].equals("-m")) multicast = 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); + } else { + wbs = new WifiMulticastSender(MCASTADDR,port); + wbr = new WifiMulticastChannel(MCASTADDR,port); + } + } catch (IOException e) { + } + sender(wbs,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 WifiSender 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(); + } + + public static void receiver(final WifiReciver wbr, final int count, final long timeout) { + Runnable sender = new Runnable() { + @Override + public void run() { + ByteBuffer testData = ByteBuffer.allocate(4096); + int num = 0, bad = 0; + try { + for(int i = 0; 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(sender).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; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/wifibroadcast/WifiMulticast.java Sat Jul 28 12:16:49 2012 +0900 @@ -0,0 +1,42 @@ +package wifibroadcast; + +import java.io.IOException; +import java.nio.ByteBuffer; + + + import java.net.DatagramPacket; + import java.net.InetAddress; +import java.net.MulticastSocket; + + public class WifiMulticast implements WifiReciver,WifiSender { + private MulticastSocket soc; + private InetAddress mAddr; + private int port; + + public WifiMulticast(String mCASTADDR, int port) throws IOException { + this.port = port; + try { + mAddr = InetAddress.getByName(mCASTADDR); + soc = new MulticastSocket(port); + soc.joinGroup(mAddr); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void recieve(ByteBuffer testData, long timeout) throws IOException { + DatagramPacket packet = new DatagramPacket(testData.array(),testData.capacity()); + soc.receive(packet); + testData.limit(packet.getLength()); + testData.position(0); + } + + @Override + public void send(ByteBuffer testData) throws IOException { + DatagramPacket sendPacket = new DatagramPacket(testData.array(), testData.limit(),mAddr, port); + soc.send(sendPacket); + } + + } +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/wifibroadcast/WifiMulticastChannel.java Sat Jul 28 12:16:49 2012 +0900 @@ -0,0 +1,44 @@ +package wifibroadcast; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.NetworkInterface; +import java.net.SocketAddress; +import java.net.StandardProtocolFamily; +import java.net.StandardSocketOptions; +import java.nio.ByteBuffer; +import java.nio.channels.DatagramChannel; + +public class WifiMulticastChannel implements WifiReciver,WifiSender { + private InetAddress mAddr; + private DatagramChannel dc; + private SocketAddress sAddr; + + public WifiMulticastChannel(String mCASTADDR, int port) throws IOException { + // join multicast group on this interface, and also use this + // interface for outgoing multicast datagrams + NetworkInterface ni = NetworkInterface.getByName("en0"); + + dc = DatagramChannel.open(StandardProtocolFamily.INET) + .setOption(StandardSocketOptions.SO_REUSEADDR, true) + .bind(sAddr = new InetSocketAddress(port)) + .setOption(StandardSocketOptions.IP_MULTICAST_IF, ni); + + mAddr = InetAddress.getByName(mCASTADDR); + + dc.join(mAddr, ni); + + } + + @Override + public void recieve(ByteBuffer testData, long timeout) throws IOException { + dc.receive(testData); + } + + @Override + public void send(ByteBuffer testData) throws IOException { + dc.send(testData, sAddr); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/wifibroadcast/WifiReciver.java Sat Jul 28 12:16:49 2012 +0900 @@ -0,0 +1,10 @@ +package wifibroadcast; + +import java.io.IOException; +import java.nio.ByteBuffer; + +public interface WifiReciver { + + void recieve(ByteBuffer testData, long timeout) throws IOException; + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/wifibroadcast/WifiSender.java Sat Jul 28 12:16:49 2012 +0900 @@ -0,0 +1,10 @@ +package wifibroadcast; + +import java.io.IOException; +import java.nio.ByteBuffer; + +public interface WifiSender { + + void send(ByteBuffer testData) throws IOException; + +}