changeset 25:a17e8b4ffe75

merge
author Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
date Wed, 07 Aug 2013 16:33:17 +0900
parents b801551b7d49 (diff) 4c3908c8b761 (current diff)
children 42ecbd9364fa
files
diffstat 2 files changed, 54 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/wifibroadcast/WifiBroadcast.java	Sat Sep 01 17:36:44 2012 +0900
+++ b/src/wifibroadcast/WifiBroadcast.java	Wed Aug 07 16:33:17 2013 +0900
@@ -27,8 +27,8 @@
 				System.out.println("Found broadcast "+address0);
 				mAddr = address0;
 				s = new DatagramSocket();
-				s.bind(new InetSocketAddress(address0,port+1+id));
-				s.setBroadcast(true);
+//				s.bind(new InetSocketAddress(address0,port+1+id));
+//				s.setBroadcast(true);
 			} catch (SocketException e) {
 			}
 		} else {
@@ -51,9 +51,24 @@
 
 
 	public void send(ByteBuffer testData) throws IOException {
-		DatagramPacket sendPacket = new DatagramPacket(testData.array(), testData.limit(),mAddr, port);
-		s.send(sendPacket);		
-		testData.position(testData.limit());
+		if(testData.limit() < 1500) {
+			DatagramPacket sendPacket = new DatagramPacket(testData.array(), testData.limit(),mAddr, port);
+			s.send(sendPacket);		
+			testData.position(testData.limit());
+		} else {
+			int temp = 1000;
+			for(int i = 0 ; i < testData.limit();) {
+				DatagramPacket sendPacket = new DatagramPacket(testData.array(),i, temp, mAddr, port);
+				s.send(sendPacket);		
+				testData.position(i);
+				i += 1000;
+				if(testData.limit() - i > 1000) {
+					temp = 1000;
+				} else {
+					temp = testData.limit() - i;
+				}
+			}
+		}
 		System.out.println("send");
 	}
 	
--- a/src/wifibroadcast/WifiBroadcastTest.java	Sat Sep 01 17:36:44 2012 +0900
+++ b/src/wifibroadcast/WifiBroadcastTest.java	Wed Aug 07 16:33:17 2013 +0900
@@ -1,7 +1,11 @@
 package wifibroadcast;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
 import java.util.LinkedList;
 
 public class WifiBroadcastTest {
@@ -188,7 +192,8 @@
 
 			@Override
 			public void run() {
-				ByteBuffer testData = getTestData(testSize);
+//				ByteBuffer testData = getTestData(testSize);
+				ByteBuffer testData = readTestData();
 				int i = 0;
 				try {
 					Thread.sleep(timeout);
@@ -233,7 +238,7 @@
 				ByteBuffer testData = ByteBuffer.allocate(4096);
 				int bad = 0, good = 0;
 				try {
-					for(int i = 0; running &&  i<count;i++) {
+					for(int i = 0; running && i<count;i++) {
 						testData.clear();
 						wbr.recieve(testData,timeout);
 						if (!testData.hasRemaining()) continue;
@@ -266,4 +271,31 @@
 		b.flip();
 		return b;
 	}
+	
+	public ByteBuffer readTestData() {
+		FileChannel srcChannel = null;
+		String path = "./testfile.txt";
+		ByteBuffer buffer = null;
+		try {
+			srcChannel = fileReader(path).getChannel();
+			buffer = ByteBuffer.allocate((int) srcChannel.size());
+			srcChannel.read(buffer);
+			buffer.clear();
+			return buffer;
+		} catch (IOException e) {
+			System.out.println("File not found.");
+		} finally {
+			try {
+				srcChannel.close();
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}
+		return buffer;
+	}
+	
+	private FileInputStream fileReader(String path)
+			throws FileNotFoundException {
+		return new FileInputStream(new File(path));
+	}
 }