diff rep/SessionManager.java @ 361:65c6d12a5835

*** empty log message ***
author kono
date Sun, 19 Oct 2008 22:50:20 +0900
parents b25f832f875d
children f0bd158dace6
line wrap: on
line diff
--- a/rep/SessionManager.java	Sun Oct 19 20:30:52 2008 +0900
+++ b/rep/SessionManager.java	Sun Oct 19 22:50:20 2008 +0900
@@ -323,6 +323,11 @@
 		
 	}
 	
+	/* 
+	 * Select Session from Manager button
+	 *    selected editor is joined editor directly connected to this session
+	 *    manager.
+	 */
 	public void selectSession(SelectButtonEvent event) throws IOException {
 		int sid = event.getSID();
 		Session session = sessionList.get(sid);
@@ -337,29 +342,43 @@
 		selectSession(sid, session, editor.getEID(), editor);
 	}
 
-
+	/*
+	 * Select Session Protocol handler
+	 */
 	private void selectSession(int sid, Session session, int eid, Forwarder editor) {
 		if(session.hasOwner()){
-			session.addForwarder(editor);
 			REPCommand sendCommand = new REPCommand();
 			if (editor.isDirect()&&editor.getEID()==eid) {
+				session.addForwarder(editor);
 				sendUpdate(session.getSID());
 				sendCommand.setCMD(REP.SMCMD_JOIN_ACK);
 			} else {
 				// SELECT_ACK is sent to the session ring to
 				// find out joined editor
 				sendCommand.setCMD(REP.SMCMD_SELECT_ACK);
+				// Do not directly addForwarder(forwarder). It may be
+				// shared among sessions.
+				Forwarder f = new Editor(this, false, makeID(editorList.newEid()));
+				f.setChannel(editor.channel); // incoming channel
+				f.setHost(myHost);
+				f.setSID(sid);
+				session.addForwarder(f);
 			}
 			sendCommand.setEID(editor.getEID());
 			sendCommand.setSID(sid);
-			sendCommand.string = "";
+			sendCommand.string = session.getName();
 			editor.send(sendCommand);
 		}else {
-			session.addForwarder(editor);
-			editor.setHost(myHost);
-			editor.setSID(sid);
+			// session searching 
 			Forwarder next = routingTable.toSession(sid);
 			
+			Forwarder f = new Editor(this, false, makeID(editorList.newEid()));
+			f.setChannel(editor.channel); // incoming channel
+			f.setNext(next);
+			f.setHost(myHost);
+			f.setSID(sid);
+			session.setFirstForwarder(f);
+			
 			REPCommand command = new REPCommand();
 			command.setCMD(REP.SMCMD_SELECT);
 			command.setSID(sid);
@@ -489,8 +508,8 @@
 	
 		break;
 	
+		case SMCMD_PUT_ACK:
 		case SMCMD_JOIN_ACK:
-		case SMCMD_PUT_ACK:
 			registEditor(forwarder,command);
 			updateGUI();
 			break;
@@ -508,6 +527,15 @@
 			} else {
 				routingTable.add(forwarder,getSMID(command.eid),command.sid);
 				smList.sendToMaster(command);
+				// registEditor will be done by SMCMD_PUT_ACK
+			}
+			if (forwarder.isDirect()) {
+				// send put_ack to the editor now.
+				command.setCMD(REP.SMCMD_PUT_ACK);
+				command.string = command.string;
+				command.setEID(command.eid);
+				command.setSID(command.sid);
+				forwarder.send(command);
 			}
 			updateGUI();
 	
@@ -515,10 +543,20 @@
 		break;
 
 		case SMCMD_SELECT:
+		{
+			Session session = sessionList.get(command.sid);
+			if (session==null) {
+				sessionList.put(command.sid,
+						new Session(command.sid, command.string,null));
+			}
+			selectSession(command.sid, session, command.eid, forwarder);
+		}
+			break;
 		case SMCMD_SELECT_ACK:
 		{
 			Session session = sessionList.get(command.sid);
-			selectSession(command.sid, session, command.eid, forwarder);
+			selectSession(command.sid, session, command.eid, 
+					session.getFirstForwarder());
 		}
 			break;
 			
@@ -610,21 +648,23 @@
 		return newid+smList.sessionManagerID()*MAXID;
 	}
 
-	private int getSessionID(int id) {
-		return id%MAXID;
-	}
 	
 	private int getSMID(int id) {
 		return id/MAXID;
 	}
 
 
+	/**
+	 * Register Editor to our editorList. No connection is made.
+	 * @param forwarder     Editor to be add
+	 * @param command
+	 */
 	private void registEditor(Forwarder forwarder,REPCommand command) {
 		// make ack for PUT/JOIN. Do not send this to the editor,
 		// before select. After select, ack is sent to the editor. 
 		routingTable.add(forwarder,getSMID(command.eid),command.sid);
 		Editor editor;
-		if (getSessionID(command.sid)==smList.sessionManagerID()
+		if (getSMID(command.sid)==smList.sessionManagerID()
 				&& forwarder.isDirect()) {
 			// direct link だった
 			editor = (Editor)forwarder;