view src/wifibroadcast/WifiBroadcastTest.java @ 2:2a328333ba70

no compile errors
author one
date Sat, 28 Jul 2012 13:06:57 +0900
parents 649b8573372c
children 9c99e2193277
line wrap: on
line source

package wifibroadcast;

import java.io.IOException;
import java.nio.ByteBuffer;

import javax.sql.rowset.Joinable;

public class WifiBroadcastTest {
	private static int DefaultPort = 8212;
	private static String MCASTADDR = "224.0.0.1";

	public static void main(String args[]) {
		int port = DefaultPort ; 
		int count = 1024;
		long timeout = 1000;
		boolean multicast = false;
		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) {
				wbr = new WifiMulticast(MCASTADDR,port);
			} else if (mchannel) {
				wbr = new WifiMulticastChannel(MCASTADDR,port);
			} else {
				wbr = new WifiBroadcast(port);
			}
		} catch (IOException e) {
		}
		sender(wbr,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 WifiReceiver 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);
						testData.flip();
					}
				} catch (IOException e) {
				}
		}
		};
		Thread s = new Thread(sender);
		s.start();
		try {
			s.join();
		} catch (InterruptedException e) {
		}
	}

	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; running &&  i<count;i++) {
						testData.clear();
						wbr.recieve(testData,timeout);
						if (!testData.hasRemaining()) continue;
						int seq = testData.getInt();
						if (seq!=i) {
							bad++; i = seq;
						}
					}
				} catch (IOException e) {
				}
				System.out.println("get "+num+" packets, "+bad+" losts.");
		}
		};
		Thread r = new Thread(receiver); r.start();
		Thread t = new Thread(timeouter); t.start();
		try {
			r.join(); 
			t.join();
		} catch (InterruptedException e) {
		}
	}
	
	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;
	}
}