Mercurial > hg > RemoteEditor > REPSessionManager
annotate rep/Forwarder.java @ 373:ab4405cd3351 current-release **INVALID**
looks like we are done. Wait for further test.
author | kono |
---|---|
date | Wed, 22 Oct 2008 02:53:12 +0900 |
parents | e16b6326fdac |
children | c4ffdac26132 |
rev | line source |
---|---|
316 | 1 package rep; |
2 | |
349 | 3 import java.io.IOException; |
316 | 4 import rep.channel.REPLogger; |
349 | 5 import rep.channel.REPSelectionKey; |
316 | 6 import rep.channel.REPSocketChannel; |
317 | 7 import rep.handler.PacketSet; |
349 | 8 import rep.handler.REPHandler; |
316 | 9 |
349 | 10 public class Forwarder extends EditorPlus implements REPHandler { |
319 | 11 int seq = 0; |
12 Forwarder next; | |
316 | 13 // REPCommands we sent to the next editor |
14 final int limit=100; | |
15 REPLogger ns = REPLogger.singleton(); | |
317 | 16 SessionManager manager; |
355 | 17 public REP mode = null; |
316 | 18 |
317 | 19 public Forwarder(SessionManager manager) { |
20 this.manager = manager; | |
21 } | |
22 | |
319 | 23 public int seq() { |
24 return seq++; | |
316 | 25 } |
26 | |
27 public void send(REPCommand command) { | |
318 | 28 assert(command!=null); |
365
c432755c3555
distributed session debug continue... SELECT/SELECT_ACK loop
kono
parents:
363
diff
changeset
|
29 assert(channel!=null); |
327 | 30 REPCommand c = new REPCommand(command); |
328 | 31 manager.addWriteQueue(new PacketSet(channel,null, c)); |
316 | 32 } |
33 | |
34 public REPSocketChannel<REPCommand> getChannel() { | |
319 | 35 return channel; |
316 | 36 } |
37 | |
38 public void setChannel(REPSocketChannel<REPCommand> channel) { | |
319 | 39 this.channel = channel; |
316 | 40 } |
41 | |
318 | 42 public void setQuit2(REPCommand cmd) { |
43 send(cmd); | |
44 } | |
45 | |
319 | 46 public void setNext(Forwarder next) { |
47 this.next = next; | |
48 } | |
49 | |
50 public Forwarder getNextForwarder() { | |
51 return next; | |
52 } | |
53 | |
54 public boolean manage(REPCommand command) { | |
353 | 55 next.send(command); |
319 | 56 return true; |
57 } | |
58 | |
349 | 59 public String toString(){ |
60 return ("Forwarder:" + channel); | |
61 } | |
62 | |
63 public String getLocalHostName() { | |
64 return channel.getLocalHostName(); | |
65 } | |
66 | |
67 public void cancel(REPSocketChannel<REPCommand> socketChannel) { | |
68 manager.remove(socketChannel); | |
69 } | |
70 | |
358 | 71 public void handle(REPSelectionKey<REPCommand> key) throws IOException { |
349 | 72 /* |
73 * SessionManagerから来たコマンドは、Editor関係のコマンドは、 | |
74 * sessionとeidを判定して、そのeditorにforwardしてやれば良い。 | |
75 * 残りは、manager.manage() で処理する。 | |
76 */ | |
77 REPSocketChannel<REPCommand> channel = key.channel1(); | |
78 REPCommand command = channel.read(); | |
79 SessionManager.logger.writeLog("REPHandlerImpl.handle() : command = " + command); | |
353 | 80 if (manager.sessionManage(this, command)) return; |
361 | 81 |
349 | 82 Session s = manager.getSession(command.sid); |
363 | 83 if (s==null) throw new IOException(); |
372 | 84 Forwarder editor = s.getFirstForwarder(channel); |
363 | 85 if (editor==null) throw new IOException(); |
373 | 86 if (!editor.isDirect()) { |
87 editor.send(command); | |
88 return; | |
89 } | |
90 if (command.cmd==REP.SMCMD_QUIT_2) { | |
91 editor.setQuit2(command); | |
92 } else if (command.eid==editor.eid) { | |
93 ((Editor)editor).checkReturnedCommand(command); | |
94 } else { | |
95 editor.manage(command); | |
96 } | |
349 | 97 } |
98 | |
355 | 99 public void setMode(REP cmd) { |
100 mode = cmd; | |
101 } | |
102 | |
103 public boolean isEditor() { | |
104 return mode==REP.SMCMD_JOIN||mode==REP.SMCMD_PUT; | |
105 } | |
106 | |
107 public boolean isForwarder() { | |
108 return mode==REP.SMCMD_SM_JOIN||mode==REP.SMCMD_SM_JOIN_ACK; | |
109 } | |
110 | |
358 | 111 public boolean isDirect() { |
112 return isEditor(); | |
113 } | |
114 | |
355 | 115 |
316 | 116 } |