annotate rep/handler/Forwarder.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 1fca50ce3508
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
382
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
1 package rep.handler;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
2
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
3 import java.io.IOException;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
4
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
5 import rep.PacketSet;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
6 import rep.REP;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
7 import rep.REPCommand;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
8 import rep.SessionManager;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
9 import rep.channel.REPLogger;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
10 import rep.channel.REPSelectionKey;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
11 import rep.channel.REPSocketChannel;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
12
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
13 /**
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
14 * @author kono
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
15 * Forward Editor Command to the other Session Manager
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
16 * Basic send API is supported.
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
17 */
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
18 public class Forwarder extends REPNode {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
19 int seq = 0;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
20 // REPCommands we sent to the next editor
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
21 final int limit=100; // debugging purpose, assert check only
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
22 final REPLogger ns = REPLogger.singleton();
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
23 SessionManager manager;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
24 public REP mode = null;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
25
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
26 public Forwarder(SessionManager manager) {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
27 this.manager = manager;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
28 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
29
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
30 public int seq() {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
31 return seq++;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
32 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
33
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
34 public void send(REPCommand command) {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
35 assert(command!=null);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
36 assert(channel!=null);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
37 REPCommand c = new REPCommand(command);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
38 manager.addWriteQueue(new PacketSet(channel,null, c));
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
39 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
40
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
41 public void sendWithSeq(REPCommand command) {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
42 assert(command!=null);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
43 assert(channel!=null);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
44 REPCommand c = new REPCommand(command);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
45 c.setSEQID(seq());
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
46 manager.addWriteQueue(new PacketSet(channel,null, c));
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
47 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
48
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
49 public REPSocketChannel<REPCommand> getChannel() {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
50 return channel;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
51 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
52
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
53 public void setChannel(REPSocketChannel<REPCommand> channel) {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
54 this.channel = channel;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
55 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
56
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
57 public void setQuit2(REPCommand cmd) {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
58 send(cmd);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
59 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
60
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
61 public void setNext(Forwarder next) {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
62 this.next = next;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
63 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
64
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
65 public REPNode getNextForwarder() {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
66 return next;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
67 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
68
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
69 public boolean manage(REPCommand command) {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
70 next.send(command);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
71 return true;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
72 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
73
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
74 public String toString(){
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
75 return ("Forwarder:" + channel);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
76 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
77
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
78 public String getLocalHostName() {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
79 return channel.getLocalHostName();
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
80 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
81
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
82 public void cancel(REPSocketChannel<REPCommand> socketChannel) {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
83 manager.remove(socketChannel);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
84 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
85
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
86 public void handle(REPSelectionKey<REPCommand> key) throws IOException {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
87 assert false;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
88 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
89
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
90
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
91 public boolean isMerging() {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
92 return false;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
93 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
94
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
95
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
96 }