316
|
1 package rep;
|
|
2
|
349
|
3 import java.io.IOException;
|
|
4
|
353
|
5
|
316
|
6 import rep.channel.REPLogger;
|
349
|
7 import rep.channel.REPSelectionKey;
|
316
|
8 import rep.channel.REPSocketChannel;
|
317
|
9 import rep.handler.PacketSet;
|
349
|
10 import rep.handler.REPHandler;
|
316
|
11
|
349
|
12 public class Forwarder extends EditorPlus implements REPHandler {
|
319
|
13 int seq = 0;
|
|
14 Forwarder next;
|
316
|
15 // REPCommands we sent to the next editor
|
|
16 final int limit=100;
|
|
17 REPLogger ns = REPLogger.singleton();
|
317
|
18 SessionManager manager;
|
355
|
19 public REP mode = null;
|
316
|
20
|
317
|
21 public Forwarder(SessionManager manager) {
|
|
22 this.manager = manager;
|
|
23 }
|
|
24
|
319
|
25 public int seq() {
|
|
26 return seq++;
|
316
|
27 }
|
|
28
|
|
29 public void send(REPCommand command) {
|
318
|
30 assert(command!=null);
|
327
|
31 REPCommand c = new REPCommand(command);
|
328
|
32 manager.addWriteQueue(new PacketSet(channel,null, c));
|
316
|
33 }
|
|
34
|
|
35 public REPSocketChannel<REPCommand> getChannel() {
|
319
|
36 return channel;
|
316
|
37 }
|
|
38
|
|
39 public void setChannel(REPSocketChannel<REPCommand> channel) {
|
319
|
40 this.channel = channel;
|
316
|
41 }
|
|
42
|
318
|
43 public void setQuit2(REPCommand cmd) {
|
|
44 send(cmd);
|
|
45 }
|
|
46
|
319
|
47 public void setNext(Forwarder next) {
|
|
48 this.next = next;
|
|
49 }
|
|
50
|
|
51 public Forwarder getNextForwarder() {
|
|
52 return next;
|
|
53 }
|
|
54
|
|
55 public boolean manage(REPCommand command) {
|
353
|
56 next.send(command);
|
319
|
57 return true;
|
|
58 }
|
|
59
|
349
|
60 public String toString(){
|
|
61 return ("Forwarder:" + channel);
|
|
62 }
|
|
63
|
|
64 public String getLocalHostName() {
|
|
65 return channel.getLocalHostName();
|
|
66 }
|
|
67
|
|
68 public void cancel(REPSocketChannel<REPCommand> socketChannel) {
|
|
69 manager.remove(socketChannel);
|
|
70 }
|
|
71
|
|
72 public void handle(REPSelectionKey<REPCommand> key) throws Exception {
|
|
73 /*
|
|
74 * SessionManagerから来たコマンドは、Editor関係のコマンドは、
|
|
75 * sessionとeidを判定して、そのeditorにforwardしてやれば良い。
|
|
76 * 残りは、manager.manage() で処理する。
|
|
77 */
|
|
78 REPSocketChannel<REPCommand> channel = key.channel1();
|
|
79 REPCommand command = channel.read();
|
|
80 SessionManager.logger.writeLog("REPHandlerImpl.handle() : command = " + command);
|
353
|
81 if (manager.sessionManage(this, command)) return;
|
349
|
82 Session s = manager.getSession(command.sid);
|
|
83 Forwarder editor = s.getFirstForwarder();
|
|
84 if (editor!=null) {
|
|
85 editor.manage(command);
|
|
86 } else throw new IOException();
|
|
87 }
|
|
88
|
355
|
89 public void setMode(REP cmd) {
|
|
90 mode = cmd;
|
|
91 }
|
|
92
|
|
93 public boolean isEditor() {
|
|
94 return mode==REP.SMCMD_JOIN||mode==REP.SMCMD_PUT;
|
|
95 }
|
|
96
|
|
97 public boolean isForwarder() {
|
|
98 return mode==REP.SMCMD_SM_JOIN||mode==REP.SMCMD_SM_JOIN_ACK;
|
|
99 }
|
|
100
|
|
101
|
316
|
102 } |