changeset 171:690182302c05

*** empty log message ***
author kent
date Thu, 28 Aug 2008 18:56:59 +0900
parents 30cf7747d134
children a776ec9ed848
files rep/REPPacketReceive.java rep/channel/REPSocketChannel.java rep/channel/REPUnpack.java test/channeltest/StringPacker.java test/channeltest/testNetworkSimulator.java test/channeltest/testSeMa.java
diffstat 6 files changed, 93 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/rep/REPPacketReceive.java	Thu Aug 28 18:56:46 2008 +0900
+++ b/rep/REPPacketReceive.java	Thu Aug 28 18:56:59 2008 +0900
@@ -3,7 +3,7 @@
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
-import java.nio.channels.SelectionKey;
+import java.nio.channels.SocketChannel;
 import java.nio.charset.CharacterCodingException;
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetDecoder;
@@ -22,8 +22,8 @@
 		socketchannel = sc;
 	}
 	
-
-	public REPCommand unpackUConv(REPSocketChannel<REPCommand> sc) throws IOException {
+	// このクラスはシミュレーションの時には生成されないので、SocketChannelでいいんだよ REPSocketChannelにしない
+	public REPCommand unpackUConv(SocketChannel sc) throws IOException {
 		ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
 		long len = 0;
 		header.clear();
--- a/rep/channel/REPSocketChannel.java	Thu Aug 28 18:56:46 2008 +0900
+++ b/rep/channel/REPSocketChannel.java	Thu Aug 28 18:56:59 2008 +0900
@@ -96,7 +96,7 @@
 	}
 	
 	public P read() throws IOException{
-		return unpack.unpackUConv(this);
+		return unpack.unpackUConv(sc);
 	}
 	public boolean write(P p){
 		ByteBuffer bb = pack.packUConv(p);
--- a/rep/channel/REPUnpack.java	Thu Aug 28 18:56:46 2008 +0900
+++ b/rep/channel/REPUnpack.java	Thu Aug 28 18:56:59 2008 +0900
@@ -1,9 +1,10 @@
 package rep.channel;
 
 import java.io.IOException;
+import java.nio.channels.SocketChannel;
 
 public interface REPUnpack<P> {
 
-	public P unpackUConv(REPSocketChannel<P> sc) throws IOException;
+	public P unpackUConv(SocketChannel sc) throws IOException;
 	
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/channeltest/StringPacker.java	Thu Aug 28 18:56:59 2008 +0900
@@ -0,0 +1,82 @@
+package test.channeltest;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.channels.SocketChannel;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+
+import rep.channel.REPPack;
+import rep.channel.REPSocketChannel;
+import rep.channel.REPUnpack;
+
+public class StringPacker implements REPPack<String>, REPUnpack<String> {
+
+	public ByteBuffer packUConv(String log) {
+		int size;
+		ByteBuffer blog = ByteBuffer.allocate(log.length()*3);
+
+		/* ヘッダ あとでもう一回書き直す */
+		blog.putInt(0);
+		
+		/* 文字列を追加  */
+		CharBuffer cb = blog.asCharBuffer();
+		cb.put(log);
+
+		/* ヘッダに書き込む情報  */
+		size = blog.position();
+		blog.rewind();
+		/* ヘッダ 文字列の長さ */
+		blog.putInt(size);
+		blog.rewind();
+
+		return blog;
+		/*
+		for(int i=0;i<log.length();i++) {
+			blog.putChar(log.charAt(i));
+		}
+		blog.flip();
+		return blog;
+		*/
+	}
+
+	public void send(String log) {
+	}
+
+	public String unpackUConv(SocketChannel sc) throws IOException {
+		ByteBuffer bb = ByteBuffer.allocate(10);
+
+		// ヘッダの読み込み 4Byteのハズ...? 
+		bb.limit(4);
+		sc.read(bb);
+		int size = bb.getInt();
+
+		// Stringの読み込み 
+		bb = ByteBuffer.allocate(size*2);
+		bb.limit(size);
+		sc.read(bb);
+
+		//  Stringに変換して返す
+		return bb.asCharBuffer().toString();
+	}
+
+	/*
+	public String unpack(ByteBuffer data){
+		data.getInt();
+        //Decode UTF-8 to System Encoding(UTF-16) 
+		Charset charset = Charset.forName("UTF-8");
+		CharsetDecoder decoder = charset.newDecoder();
+		CharBuffer cb = null;
+        try {
+                cb = decoder.decode(data);
+        } catch (CharacterCodingException e) {
+        }
+        cb.rewind();
+        data.rewind();
+
+        return cb.toString();
+	}*/
+
+}
--- a/test/channeltest/testNetworkSimulator.java	Thu Aug 28 18:56:46 2008 +0900
+++ b/test/channeltest/testNetworkSimulator.java	Thu Aug 28 18:56:59 2008 +0900
@@ -15,8 +15,9 @@
 	static public REPLogger ns = new REPLogger();
 
 	public static void main(String[] args){
-		REPServerSocketChannel.isSimulation = false;
-		testNetworkSimulator testns = new testNetworkSimulator(3, 10, 50);
+		REPServerSocketChannel.isSimulation = true;
+		testNetworkSimulator testns = new testNetworkSimulator(1, 0, 2);
+
 		
 		testns.startTest();
 	}
--- a/test/channeltest/testSeMa.java	Thu Aug 28 18:56:46 2008 +0900
+++ b/test/channeltest/testSeMa.java	Thu Aug 28 18:56:59 2008 +0900
@@ -30,7 +30,8 @@
 
 	@SuppressWarnings("unchecked")
 	public void run() {
-		REPSelector selector;
+		REPSelector selector=null;
+
 		REPServerSocketChannel<String> scs;
 		try {
 			selector = REPSelector.create();