diff rep/SessionManager.java @ 386:bba62c4ac323

sync-option
author one@firefly.cr.ie.u-ryukyu.ac.jp
date Mon, 10 Nov 2008 22:19:34 +0900
parents 1fca50ce3508
children 6f356d160e58
line wrap: on
line diff
--- a/rep/SessionManager.java	Mon Nov 10 22:18:14 2008 +0900
+++ b/rep/SessionManager.java	Mon Nov 10 22:19:34 2008 +0900
@@ -61,6 +61,8 @@
 	private REPNode sm_join_channel;
 	// Routing table for session and session manager.
 	private RoutingTable routingTable = new RoutingTable(this);
+	// sync option
+	public boolean sync = true;
 	
 	static public REPLogger logger = REPLogger.singleton();
 	
@@ -169,7 +171,11 @@
 
 	private void selectSession0(int sid, Session session, int eid, REPNode editor) {
 		if (editor.isDirect()&&editor.getEID()==eid) {
-			selectSession(sid, session, editor.getEID(), editor);
+			REPCommand command = new REPCommand();
+			command.setSID(sid);
+			command.setEID(eid);
+			command.setString(session.getName());
+			editor.selectSession(command, session);
 		} else {
 			// we don't have this editor, search the editor first.
 			REPNode next = routingTable.toSessionManager(getSMID(eid));
@@ -183,35 +189,7 @@
 		}
 	}
 
-	/*
-	 * Select Session Protocol handler
-	 *    called from GUI or incoming SMCMD_SELECT command.
-	 */
-	private void selectSession(int sid, Session session, int eid, REPNode editor) {
-		if(session.hasOwner()){
-			// we have selected session.
-			REPCommand sendCommand = new REPCommand();
-			if (editor.isDirect()&&editor.getEID()==eid) {
-				// Found directly connected joined editor. Send join_ack().
-				session.addForwarder(editor);
-				sendUpdate(session.getSID());
-				sendCommand.setCMD(REP.SMCMD_JOIN_ACK);
-			} 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.setSID(sid);
-			sendCommand.string = session.getName();
-			sendCommand.setEID(eid);
-			editor.send(sendCommand);
-		}else {
-			forwardSelect(sid, session, eid, editor);
-		}
-	}
-
-	private void forwardSelect(int sid, Session session, int eid,
+	public void forwardSelect(int sid, Session session, int eid,
 			REPNode editor) {
 		REPNode next;
 		// session searching continue...
@@ -241,11 +219,16 @@
 	/*
 	 * Create and send UPDATE command.
 	 */
-	private void sendUpdate(int sid) {
+	public void sendUpdate(int sid) {
 		REPCommand command = makeREPCommandWithSessionList(REP.SMCMD_UPDATE);
 		command.setSID(sid);
 		command.setEID(REP.SM_EID.id);
-		smList.sendToMaster(command);
+		if (isMaster()) {
+			command.setCMD(REP.SMCMD_UPDATE_ACK);
+			smList.sendToSlaves(command);
+		} else {
+			smList.sendToMaster(command);
+		}
 	}
 
 	/*
@@ -403,7 +386,7 @@
 			// shared among sessions.
 			REPNode f = createSessionForwarder(command.sid, forwarder);
 			session.addForwarder(f); // f.next is set up here.
-			selectSession(command.sid, session, command.eid, forwarder);
+			forwarder.selectSession(command,session);
 		}
 			break;
 		case SMCMD_SELECT_ACK:
@@ -435,20 +418,17 @@
 			}
 		}
 		break;
-	
+
+		case SMCMD_SYNC_ACK:
+			break;
+			
 		case SMCMD_SM_JOIN_ACK:				
 			send_sm_join_ack(command.eid, command.sid, command);
 			break;
 	
 		case SMCMD_UPDATE:
-			if (!isMaster()) {
-				command.setString(mergeUpdate(command));
-				// 上に知らせる
-				smList.sendToMaster(command);
-				break;
-			}
-			// fall thru
-			command.setCMD(REP.SMCMD_UPDATE_ACK);
+			sendUpdate(command.sid);
+			break;
 		case SMCMD_UPDATE_ACK:
 			command.setString(mergeUpdate(command));
 			// 下に知らせる
@@ -506,6 +486,12 @@
 				// 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);
+				}
 				return;
 			}
 		}