changeset 310:511376c066db

*** empty log message ***
author kono
date Sun, 05 Oct 2008 11:01:58 +0900
parents f27c8551e877
children 7107faaf3feb
files rep/channel/REPSocketChannel.java test/ServerSample.java test/sematest/TestSessionManager.java
diffstat 3 files changed, 37 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/rep/channel/REPSocketChannel.java	Sat Oct 04 22:22:07 2008 +0900
+++ b/rep/channel/REPSocketChannel.java	Sun Oct 05 11:01:58 2008 +0900
@@ -97,7 +97,8 @@
 		close1();
 	}
 
-	public long read(ByteBuffer header) throws IOException {
+
+	public int read(ByteBuffer header) throws IOException {
 		return sc.read(header);
 	}
 
--- a/test/ServerSample.java	Sat Oct 04 22:22:07 2008 +0900
+++ b/test/ServerSample.java	Sun Oct 05 11:01:58 2008 +0900
@@ -1,7 +1,6 @@
 package test;
-import java.nio.*;
+import java.io.IOException;
 import java.nio.channels.*;
-import java.nio.charset.*;
 import java.net.*;
 
 import rep.REPCommand;
@@ -10,12 +9,16 @@
 import rep.channel.REPSelectionKey;
 import rep.channel.REPSelector;
 import rep.channel.REPServerSocketChannel;
+import rep.channel.REPSocketChannel;
 
 public class ServerSample
 {
+	// client も書いて、standalone example として動くべき
 	public static void main(String[] argv)
 	throws Exception
 	{
+		// Thread base のSimulationか、実際のSocketかの選択
+		REPServerSocketChannel.isSimulation = false;
 		// セレクタの用意
 		REPSelector<REPCommand> selector = REPSelector.create();
 
@@ -36,6 +39,7 @@
 		while (true) {
 
 			// セレクタにイベントが発生するまでブロック
+			// select のreturn valueは信用しない。selectedKeys()を使う。
 			selector.select();
 
 			// 獲得したイベントごとに処理を実行
@@ -56,7 +60,9 @@
 
 					// サーバソケットチャンネルからソケットチャンネルを獲得
 					// ソケットチャンネルを経由してクライアントと通信できる
-					SocketChannel socketChannel = serverSocketChannel.accept();
+					//SocketChannel socketChannel = serverSocketChannel.accept();
+					REPSocketChannel<REPCommand> socketChannel;
+					socketChannel = selectionKey.accept(pack);
 
 					// 接続先がなくてもここに処理が飛ぶことがある。対象が
 					// nullの場合は処理を抜ける
@@ -76,35 +82,35 @@
 				else if (selectionKey.isReadable()) {
 
 					// 登録されているソケットチャンネルを取得
-					SocketChannel socketChannel = 
-						(SocketChannel)selectionKey.channel();
+					REPSocketChannel<REPCommand> socketChannel = 
+						selectionKey.channel1();
 
-					Charset charset = Charset.forName("US-ASCII");
-					ByteBuffer byteBuffer = ByteBuffer.allocate(8192);
-
+					REPCommand cmd = null;
 					// クライアントからメッセージの受信
-					switch (socketChannel.read(byteBuffer)) {
-					case -1:
+					try {
+						cmd = socketChannel.read();
+					} catch (IOException e) {
 						// クライアント側が接続を切断していた場合は、サーバも
 						// 接続を切断。セレクタから登録を削除
 						selectionKey.cancel(); // これは必要だと思う
-						socketChannel.close(); // 既にcloseされているはず
-						break;
-					case 0:
+						socketChannel.close(); // たぶん不要
+					}
+					if (cmd==null) {
 						// 読み込むべきメッセージは届いていないので処理を飛ばす
+						// こういう場合もある
 						continue;
-					default:
-						// クライアントからメッセージを取得し、標準出力へ
-						byteBuffer.flip();
-					System.out.print("EEE: " + charset.decode(byteBuffer));
+					}
+					// クライアントからメッセージを取得し、標準出力へ
+					System.out.print("EEE: " + cmd);
 
 					// クライアントへメッセージを送信
-					socketChannel.write(charset.encode("Good bye!\r\n"));
+					cmd = new REPCommand(cmd);
+					cmd.setString("This is the answer.");
+					socketChannel.write(cmd);
 
 					// クライアントとの接続を切断。セレクタから登録を削除
-					//socketChannel.close();
-					break;
-					}
+					socketChannel.close();
+					//break;
 				}
 				System.out.println(selectionKey.toString());
 			}
--- a/test/sematest/TestSessionManager.java	Sat Oct 04 22:22:07 2008 +0900
+++ b/test/sematest/TestSessionManager.java	Sun Oct 05 11:01:58 2008 +0900
@@ -91,6 +91,14 @@
 		return i+1;
 	}
 
+
+	private void startTest() {
+		int i = 0;
+		for(SessionManager master:sessionManagers) {
+			i = startSessionManager(master,i, masterPort + i);
+		}
+	}
+
 	public static void main(String[] args){
 		/*
 		 * set simulation mode
@@ -104,12 +112,4 @@
 	}
 
 
-	private void startTest() {
-		int i = 0;
-		for(SessionManager master:sessionManagers) {
-			i = startSessionManager(master,i, masterPort + i);
-		}
-	}
-
-
 }