diff rep/handler/Dispatcher.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
children bcdf5476b8e4
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/handler/Dispatcher.java	Mon Nov 10 22:07:45 2008 +0900
@@ -0,0 +1,77 @@
+package rep.handler;
+
+import java.io.IOException;
+
+import rep.REPCommand;
+import rep.Session;
+import rep.SessionManager;
+import rep.channel.REPSelectionKey;
+import rep.channel.REPSocketChannel;
+
+/**
+ * @author kono
+ *    Handle SessionManager incoming Channel
+ *    SessionManager Command and Multiplexed Editor Command come here.
+ *    Dispatch Editor command according to the session and the channel.
+ */
+public class Dispatcher extends Forwarder {
+
+	public Dispatcher(SessionManager manager) {
+		super(manager);
+	}
+
+	public void setQuit2(REPCommand cmd) {
+		send(cmd);
+	}
+
+	public boolean manage(REPCommand command) {
+		next.send(command);
+		return true;
+	}
+
+	public String toString(){
+		return ("Dispatcher:" + channel);
+	}
+	
+
+	public void handle(REPSelectionKey<REPCommand> key) throws IOException {
+		/*
+		 * SessionManagerから来たコマンドは、Editor関係のコマンドは、
+		 * sessionとeidを判定して、そのeditorにforwardしてやれば良い。
+		 * 残りは、manager.manage() で処理する。
+		 */
+		REPSocketChannel<REPCommand> channel = key.channel1();
+		REPCommand command = channel.read();
+		SessionManager.logger.writeLog("REPHandlerImpl.handle() : command = " + command);
+		if (manager.sessionManage(this, command)) return;
+		
+		distpatchToEditor(channel, command);
+	}
+
+	private void distpatchToEditor(REPSocketChannel<REPCommand> channel,
+			REPCommand command) throws IOException {
+		Session s = manager.getSession(command.sid);
+		if (s==null) throw new IOException();
+		REPNode f = s.getForwarder(channel);
+		if (f==null) throw new IOException();
+		if (!f.isDirect()) {
+			// another forwarder, pass it to the next session manager
+			f.send(command); 
+			return;
+		}
+		/*
+		 * local editor case. 
+		 */
+		Editor editor = (Editor)f;
+		editor.forwardedCommandManage(command, this);
+	}
+
+
+	public void setNext(REPNode f) {
+		next = f;
+	}
+
+	public boolean isMerging() {
+		return false;
+	}
+}