comparison 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
comparison
equal deleted inserted replaced
381:65fdb3dc1885 382:4b87f89b3afd
1 package rep.handler;
2
3 import java.io.IOException;
4
5 import rep.REPCommand;
6 import rep.Session;
7 import rep.SessionManager;
8 import rep.channel.REPSelectionKey;
9 import rep.channel.REPSocketChannel;
10
11 /**
12 * @author kono
13 * Handle SessionManager incoming Channel
14 * SessionManager Command and Multiplexed Editor Command come here.
15 * Dispatch Editor command according to the session and the channel.
16 */
17 public class Dispatcher extends Forwarder {
18
19 public Dispatcher(SessionManager manager) {
20 super(manager);
21 }
22
23 public void setQuit2(REPCommand cmd) {
24 send(cmd);
25 }
26
27 public boolean manage(REPCommand command) {
28 next.send(command);
29 return true;
30 }
31
32 public String toString(){
33 return ("Dispatcher:" + channel);
34 }
35
36
37 public void handle(REPSelectionKey<REPCommand> key) throws IOException {
38 /*
39 * SessionManagerから来たコマンドは、Editor関係のコマンドは、
40 * sessionとeidを判定して、そのeditorにforwardしてやれば良い。
41 * 残りは、manager.manage() で処理する。
42 */
43 REPSocketChannel<REPCommand> channel = key.channel1();
44 REPCommand command = channel.read();
45 SessionManager.logger.writeLog("REPHandlerImpl.handle() : command = " + command);
46 if (manager.sessionManage(this, command)) return;
47
48 distpatchToEditor(channel, command);
49 }
50
51 private void distpatchToEditor(REPSocketChannel<REPCommand> channel,
52 REPCommand command) throws IOException {
53 Session s = manager.getSession(command.sid);
54 if (s==null) throw new IOException();
55 REPNode f = s.getForwarder(channel);
56 if (f==null) throw new IOException();
57 if (!f.isDirect()) {
58 // another forwarder, pass it to the next session manager
59 f.send(command);
60 return;
61 }
62 /*
63 * local editor case.
64 */
65 Editor editor = (Editor)f;
66 editor.forwardedCommandManage(command, this);
67 }
68
69
70 public void setNext(REPNode f) {
71 next = f;
72 }
73
74 public boolean isMerging() {
75 return false;
76 }
77 }