diff rep/SessionManager.java @ 382:4b87f89b3afd

REP Session Manager (Java version) new structure
author one@firefly.cr.ie.u-ryukyu.ac.jp
date Mon, 10 Nov 2008 22:07:45 +0900
parents c78569ab5fce
children bcdf5476b8e4
line wrap: on
line diff
--- a/rep/SessionManager.java	Mon Nov 10 01:12:46 2008 +0000
+++ b/rep/SessionManager.java	Mon Nov 10 22:07:45 2008 +0900
@@ -18,8 +18,18 @@
 import rep.channel.REPLogger;
 import rep.channel.REPServerSocketChannel;
 import rep.channel.REPSocketChannel;
-import rep.handler.PacketSet;
-import rep.handler.REPHandler;
+import rep.gui.CloseButtonEvent;
+import rep.gui.DoGUIUpdate;
+import rep.gui.SelectButtonEvent;
+import rep.gui.SessionManagerEvent;
+import rep.gui.SessionManagerEventListener;
+import rep.gui.SessionManagerGUI;
+import rep.gui.SessionManagerGUIimpl;
+import rep.handler.Dispatcher;
+import rep.handler.Editor;
+import rep.handler.REPNode;
+import rep.handler.FirstConnector;
+import rep.handler.Forwarder;
 import rep.channel.REPSelector;
 import rep.xml.SessionXMLDecoder;
 import rep.xml.SessionXMLEncoder;
@@ -61,7 +71,7 @@
 	private BlockingQueue<SessionManagerEvent> waitingEventQueue 
 		= new LinkedBlockingQueue<SessionManagerEvent>();;
 	// host name of this server. One of connecting SocketChannel's hostname
-	String myHost;
+	public String myHost;
 	// Single threaded write queueu. To avoid dead lock with too many writes.
 	private LinkedList<PacketSet> writeQueue = new LinkedList<PacketSet>();
 	private int receive_port;
@@ -75,7 +85,7 @@
 	SessionXMLDecoder decoder = new SessionXMLDecoder();
 	SessionXMLEncoder encoder = new SessionXMLEncoder();
 	// SocketChannel for our parent. At most one parent is allowed.
-	private Forwarder sm_join_channel;
+	private REPNode sm_join_channel;
 	// Routing table for session and session manager.
 	private RoutingTable routingTable = new RoutingTable(this);
 	private SessionManagerEvent execAfterConnect = null;;
@@ -118,7 +128,7 @@
 		//getAllByNameで取れた全てのアドレスに対してbindする
 		ssc.socket().bind(new InetSocketAddress(port));
 		ssc.register(selector, SelectionKey.OP_ACCEPT, 
-				new Forwarder(this));
+				new Dispatcher(this));  // FirstConnector?
 
 		sessionList = new SessionList();
 		smList = new SessionManagerList();
@@ -183,7 +193,7 @@
 		List<PacketSet> w = waitingCommandInMerge;
 		waitingCommandInMerge = new LinkedList<PacketSet>();
 		for(PacketSet p: w) {
-			Editor e = p.getEditor();
+			REPNode e = p.getEditor();
 			if(e.isMerging()) { // still merging do nothing
 				waitingCommandInMerge.add(p);
 			} else {
@@ -219,7 +229,7 @@
 	 */
 	private void close(REPSocketChannel<REPCommand> channel) {
 		REPSelectionKey<REPCommand>key = channel.keyFor1(selector);
-		REPHandler handler = (REPHandler)key.attachment();
+		REPNode handler = (REPNode)key.attachment();
 		key.cancel();
 		handler.cancel(channel);
 		// we have to remove session/enditor
@@ -250,7 +260,7 @@
 				 * A hadler throw IOException() in case of a trouble to
 				 * close the channel.
 				 */
-				REPHandler handler = (REPHandler)(key.attachment());
+				REPNode handler = (REPNode)key.attachment();
 				try {
 					handler.handle(key);
 				} catch (IOException e) {
@@ -261,7 +271,7 @@
 		}
 	}
 	
-	void registerChannel(REPSocketChannel<REPCommand> channel,Forwarder handler) throws IOException {
+	public void registerChannel(REPSocketChannel<REPCommand> channel,REPNode handler) throws IOException {
 		if(channel == null) {
 			return;
 		}
@@ -280,7 +290,7 @@
 	}
 
 
-	private void removeChannel(Forwarder channel) {
+	private void removeChannel(REPNode channel) {
 		REPSelectionKey<REPCommand> key = channel.channel.keyFor1(selector);
 		key.cancel();
 		try {
@@ -293,17 +303,17 @@
 	void updateGUI() {
 		//リストのコピーをGUIに渡す
 		LinkedList<Session> sList = new LinkedList<Session>(sessionList.values());
-		LinkedList<Editor> eList;
+		LinkedList<REPNode> eList;
 		if (false) { 
 			// local editor only 
-			eList = new LinkedList<Editor>();
-			for(Editor e:editorList.values()) {
+			eList = new LinkedList<REPNode>();
+			for(REPNode e:editorList.values()) {
 				if (getSMID(e.eid)==smList.sessionManagerID()) {
 					eList.add(e);
 				}
 			}
 		} else {
-			eList = new LinkedList<Editor>(editorList.values());
+			eList = new LinkedList<REPNode>(editorList.values());
 		}
 		//GUIに反映
 		Runnable doRun = new DoGUIUpdate(sList, eList, gui);
@@ -312,13 +322,13 @@
 
 
 
-	void setMyHostName(String localHostName) {
+	public void setMyHostName(String localHostName) {
 		myHost = localHostName + receive_port;
 		setHostToEditor(myHost);
 	}
 
 	private void setHostToEditor(String myHost2) {
-		for(Editor editor : editorList.values()){
+		for(REPNode editor : editorList.values()){
 			if (editor.channel!=null)
 				editor.setHost(myHost2);
 		}
@@ -344,7 +354,7 @@
 			REPSocketChannel<REPCommand> sessionchannel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
 			sessionchannel.connect(addr);
 			while(!sessionchannel.finishConnect());
-			Forwarder sm = new FirstConnector(this);
+			REPNode sm = new FirstConnector(this);
 			registerChannel(sessionchannel, sm);
 			sm_join(sm);
 		}catch (IOException e) {
@@ -359,7 +369,7 @@
 	 * channel に SMCMD_SM_JOIN command を送る。
 	 * @param channel
 	 */
-	private void sm_join(Forwarder channel){
+	private void sm_join(REPNode channel){
 		sm_join_channel = channel;
 		//SM_JOINコマンドを生成。
 		REPCommand command = new REPCommand();
@@ -387,7 +397,7 @@
 		int sid = event.getSID();
 		Session session = sessionList.get(sid);
 		if (session==null) throw new IOException();
-		Editor editor = (Editor)event.getEditor();
+		REPNode editor = event.getEditor();
 		if (editor.hasSession()) return;
 		// assert(getSMID(editor.eid)==smList.sessionManagerID());
 		// assert(editor.channel!=null);
@@ -395,12 +405,12 @@
 		selectSession0(sid, session, editor.getEID(), editor);
 	}
 
-	private void selectSession0(int sid, Session session, int eid, Editor editor) {
+	private void selectSession0(int sid, Session session, int eid, REPNode editor) {
 		if (editor.isDirect()&&editor.getEID()==eid) {
 			selectSession(sid, session, editor.getEID(), editor);
 		} else {
 			// we don't have this editor, search the editor first.
-			Forwarder next = routingTable.toSessionManager(getSMID(eid));
+			REPNode next = routingTable.toSessionManager(getSMID(eid));
 			// pass the select command to the next path.
 			REPCommand command = new REPCommand();
 			command.setCMD(REP.SMCMD_SELECT0);
@@ -415,7 +425,7 @@
 	 * Select Session Protocol handler
 	 *    called from GUI or incoming SMCMD_SELECT command.
 	 */
-	private void selectSession(int sid, Session session, int eid, Forwarder editor) {
+	private void selectSession(int sid, Session session, int eid, REPNode editor) {
 		if(session.hasOwner()){
 			// we have selected session.
 			REPCommand sendCommand = new REPCommand();
@@ -440,12 +450,12 @@
 	}
 
 	private void forwardSelect(int sid, Session session, int eid,
-			Forwarder editor) {
-		Forwarder next;
+			REPNode editor) {
+		REPNode next;
 		// session searching continue...
 		next = routingTable.toSessionManager(getSMID(sid));
 		// make a forwarding channel here
-		Forwarder f = createSessionDispatcher(sid, next);
+		REPNode f = createSessionForwarder(sid, next);
 		session.setFirstForwarder(f);
 		session.addForwarder(editor);
 		// pass the select command to the next path.
@@ -457,8 +467,9 @@
 		next.send(command);
 	}
 
-	private Forwarder createSessionDispatcher(int sid, Forwarder editor) {
-		Forwarder f = new Editor(this, false, makeID(editorList.newEid()));
+	private REPNode createSessionForwarder(int sid, REPNode editor) {
+		REPNode f = new Forwarder(this);
+		f.setEID(makeID(editorList.newEid()));
 		f.setChannel(editor.channel); // incoming channel
 		f.setHost(myHost);
 		f.setSID(sid);
@@ -479,9 +490,9 @@
 	 * Create new editor in this sessin manager. A dummy editor
 	 * is created also.
 	 */
-	public Editor newEditor(REPSocketChannel<REPCommand> channel) {
+	public REPNode newEditor(REPSocketChannel<REPCommand> channel) {
 		int eid =  makeID(editorList.newEid());
-		Editor editor = new Editor(this, eid, channel);
+		REPNode editor = new Editor(this, eid, channel);
 		editorList.add(editor);
 		return editor;
 	}
@@ -489,7 +500,7 @@
 	/*
 	 * Create new session.
 	 */
-	public Session newSession(Forwarder master) {
+	public Session newSession(REPNode master) {
 		int sid= makeID(sessionList.newSessionID());
 		Session session = new Session(sid, master);
 		sessionList.put(sid, session);
@@ -584,7 +595,7 @@
 	}
 
 
-	boolean sessionManage(Forwarder forwarder, REPCommand command) throws ClosedChannelException,
+	public boolean sessionManage(REPNode forwarder, REPCommand command) throws ClosedChannelException,
 			IOException {
 		switch(command.cmd){
 	
@@ -652,9 +663,9 @@
 			/*
 			 * finding joining editor, do not make the path.
 			 */
-			Forwarder editor  = editorList.get(command.eid);
+			REPNode editor  = editorList.get(command.eid);
 			if (editor==null|| !editor.isDirect()) {
-				Forwarder next = routingTable.toSessionManager(getSMID(command.eid));
+				REPNode next = routingTable.toSessionManager(getSMID(command.eid));
 				next.send(command);
 				break;
 			}
@@ -671,7 +682,7 @@
 			}
 			// Do not directly addForwarder(forwarder). It may be
 			// shared among sessions.
-			Forwarder f = createSessionDispatcher(command.sid, forwarder);
+			REPNode f = createSessionForwarder(command.sid, forwarder);
 			session.addForwarder(f); // f.next is set up here.
 			selectSession(command.sid, session, command.eid, forwarder);
 		}
@@ -731,8 +742,8 @@
 		return true;
 	}
 
-	private void registSessionManager(Forwarder forwarder, REPCommand command) {
-		Forwarder sm;
+	private void registSessionManager(REPNode forwarder, REPCommand command) {
+		REPNode sm;
 		int psid = command.eid;
 		if (forwarder.sid!=-1) {
 		// すでに channelはSessionManager Idを持っていて、
@@ -770,7 +781,7 @@
 	 * 指定されたeditorがlocalにあるかどうかを調べる。なければ、他に送る。戻って何回も探すことが
 	 * あり得るので、よろしくない。
 	 */
-	private void searchSelectedEditor(REPCommand command, Forwarder editor) {
+	private void searchSelectedEditor(REPCommand command, REPNode editor) {
 		for(;editor.isDirect();editor = editor.getNextForwarder()) {
 			if (editor.getEID()==command.eid) {
 				// select したeditor を見つけた
@@ -821,10 +832,10 @@
 	 * @param forwarder     Editor to be add
 	 * @param command
 	 */
-	public void registEditor(Forwarder forwarder,REPCommand command) {
+	public void registEditor(REPNode 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. 
-		Editor editor;
+		REPNode editor;
 		if (getSMID(command.eid)==smList.sessionManagerID()) {
 			if (forwarder.isDirect()) {
 				editor = (Editor)forwarder;
@@ -880,7 +891,7 @@
 		execAfterConnect = null;
 	}
 
-	public void setParent(Forwarder fw) {
+	public void setParent(REPNode fw) {
 		smList.setParent(fw);
 	}
 	
@@ -890,5 +901,9 @@
 		return "rep.SessionManager-"+myId+"@"+myHost+":"+receive_port; 
 	}
 
+	public void addWaitingSessionManager(REPNode fw, REPCommand command) {
+		smList.addWaitingSessionManager(fw, command) ;
+	}
+
 
 }