changeset 389:4b535bef903a

23
author one@firefly.cr.ie.u-ryukyu.ac.jp
date Mon, 10 Nov 2008 22:24:21 +0900
parents 4ec3b70f8f09
children aa07134fea32
files rep/SessionManager.java rep/handler/Forwarder.java rep/handler/REPNode.java test/ServerSample.java
diffstat 4 files changed, 52 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/rep/SessionManager.java	Mon Nov 10 22:23:02 2008 +0900
+++ b/rep/SessionManager.java	Mon Nov 10 22:24:21 2008 +0900
@@ -170,12 +170,17 @@
 	}
 
 	private void selectSession0(int sid, Session session, int eid, REPNode editor) {
+		logger.writeLog("Select sid="+sid+" and "+editor);
 		if (editor.isDirect()&&editor.getEID()==eid) {
 			REPCommand command = new REPCommand();
 			command.setSID(sid);
 			command.setEID(eid);
 			command.setString(session.getName());
-			editor.selectSession(command, session);
+			if(session.hasOwner()){
+				editor.selectSession(command,session);
+			}else {
+				forwardSelect(sid, session, eid, editor);
+			}
 		} else {
 			// we don't have this editor, search the editor first.
 			REPNode next = routingTable.toSessionManager(getSMID(eid));
@@ -386,7 +391,11 @@
 			// shared among sessions.
 			REPNode f = createSessionForwarder(command.sid, forwarder);
 			session.addForwarder(f); // f.next is set up here.
-			forwarder.selectSession(command,session);
+			if(session.hasOwner()){
+				forwarder.selectSession(command,session);
+			}else {
+				forwardSelect(command.sid, session, command.eid, forwarder);
+			}
 		}
 			break;
 		case SMCMD_SELECT_ACK:
@@ -484,14 +493,7 @@
 		for(;editor.isDirect();editor = editor.getNextForwarder()) {
 			if (editor.getEID()==command.eid) {
 				// select したeditor を見つけた
-				command.cmd=REP.SMCMD_JOIN_ACK;
-				editor.send(command);
-				sendUpdate(command.sid);
-				if (sync) {
-					REPCommand sync = new REPCommand(command);
-					sync.setCMD(REP.SMCMD_SYNC);
-					editor.sendSync(sync);
-				}
+				editor.joinAck(command,command.sid);
 				return;
 			}
 		}
@@ -600,4 +602,5 @@
 	public void addWaitingSessionManager(REPNode fw, REPCommand command) {
 		smList.addWaitingSessionManager(fw, command);
 	}
+
 }
--- a/rep/handler/Forwarder.java	Mon Nov 10 22:23:02 2008 +0900
+++ b/rep/handler/Forwarder.java	Mon Nov 10 22:24:21 2008 +0900
@@ -100,30 +100,36 @@
 		return false;
 	}
 
+	/* 
+	 * Connect to the session which has a active connection (forwarder or
+	 * editor) 
+	 */
 	public void selectSession(REPCommand sendCommand,Session session) {
-		if(session.hasOwner()){
-			// we have a selected session.
-			if (isDirect()&&getEID()==sendCommand.eid) {
-				// Found directly connected joined editor. Send join_ack().
-				// we have one more point to send JOIN_ACK to the editor.
-				session.addForwarder(this);
-				manager.sendUpdate(session.getSID());
-				sendCommand.setCMD(REP.SMCMD_JOIN_ACK);
-				if (manager.sync) {
-					REPCommand sync = new REPCommand(sendCommand);
-					sync.setCMD(REP.SMCMD_SYNC);
-					sendSync(sync);
-				}
-			} else {
-				// We have a session, but joined editor is on the other sm.
-				// SELECT_ACK is sent to the session ring to
-				// find out the joined editor.
-				sendCommand.setCMD(REP.SMCMD_SELECT_ACK);
-			}
-			sendCommand.string = session.getName();
-			send(sendCommand);
-		}else {
-			manager.forwardSelect(sid, session, eid, this);
+		// we have a selected session.
+		if (isDirect()&&getEID()==sendCommand.eid) {
+			// Found directly connected joined editor. Send join_ack().
+			// we have one more point to send JOIN_ACK to the editor.
+			session.addForwarder(this);
+			joinAck(sendCommand, session.getSID());
+		} else {
+			// We have a session, but joined editor is on the other sm.
+			// SELECT_ACK is sent to the session ring to
+			// find out the joined editor to send JOIN_ACK.
+			sendCommand.setCMD(REP.SMCMD_SELECT_ACK);
+		}
+		sendCommand.string = session.getName();
+		send(sendCommand);
+
+	}
+
+	@Override
+	public void joinAck(REPCommand sendCommand, int sid) {
+		manager.sendUpdate(sid);
+		sendCommand.setCMD(REP.SMCMD_JOIN_ACK);
+		if (manager.sync) {
+			REPCommand sync = new REPCommand(sendCommand);
+			sync.setCMD(REP.SMCMD_SYNC);
+			sendSync(sync);
 		}
 	}
 
--- a/rep/handler/REPNode.java	Mon Nov 10 22:23:02 2008 +0900
+++ b/rep/handler/REPNode.java	Mon Nov 10 22:24:21 2008 +0900
@@ -153,5 +153,8 @@
 	public void sendSync(REPCommand sync) {
 		next.sendWithSeq(sync);
 	}
+
+	public abstract void joinAck(REPCommand sendCommand, int sid) ;
+
 	
 }
--- a/test/ServerSample.java	Mon Nov 10 22:23:02 2008 +0900
+++ b/test/ServerSample.java	Mon Nov 10 22:24:21 2008 +0900
@@ -27,11 +27,13 @@
 		// (非ブロックモードに設定:重要)
 		REPServerSocketChannel<REPCommand> serverSocketChannel = REPServerSocketChannel.<REPCommand>open(pack);
 		serverSocketChannel.configureBlocking(false);
-		// この方法だと、IPv6 (Dual stack) 対応にならない..
-		// このホストの全てのアドレスを取得して、それ全部に対して、socketを開けて、すべてに対して、
-		// select する必要がある。
-		//serverSocketChannel.socket().bind(new InetSocketAddress(5100));
-		serverSocketChannel.socket().bind(new InetSocketAddress("::",5100));
+		serverSocketChannel.socket().setReuseAddress(true);
+		try {
+			serverSocketChannel.socket().bind(new InetSocketAddress("::",5100));
+		} catch (SocketException e) {
+			// some system does not support "::"
+			serverSocketChannel.socket().bind(new InetSocketAddress(5100));
+		}
 
 		// セレクタにサーバソケットチャンネルを登録。サーバへの受付を監視
 		serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);