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