annotate rep/handler/FirstConnector.java @ 393:18cacd0b3ccf

update tags
author one@firefly.cr.ie.u-ryukyu.ac.jp
date Mon, 10 Nov 2008 22:26:43 +0900
parents 6f356d160e58
children 21cb16b7f3df
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
385
1fca50ce3508 first-working-version
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 384
diff changeset
5 import rep.PacketSet;
1fca50ce3508 first-working-version
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 384
diff changeset
6 import rep.REP;
382
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
7 import rep.REPCommand;
384
bcdf5476b8e4 restructured-version
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 382
diff changeset
8 import rep.ServerMainLoop;
382
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
9 import rep.Session;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
10 import rep.SessionManager;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
11 import rep.channel.REPSelectionKey;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
12 import rep.channel.REPSocketChannel;
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 public class FirstConnector extends Forwarder {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
15
393
18cacd0b3ccf update tags
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 387
diff changeset
16 /**
18cacd0b3ccf update tags
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 387
diff changeset
17 * Handle first incoming packet, and determines master editor or
18cacd0b3ccf update tags
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 387
diff changeset
18 * client editor ( or possibly inter-connected session manager).
18cacd0b3ccf update tags
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 387
diff changeset
19 * We cannot send anything but sm_join().
18cacd0b3ccf update tags
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 387
diff changeset
20 *
18cacd0b3ccf update tags
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 387
diff changeset
21 * @param manager
18cacd0b3ccf update tags
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 387
diff changeset
22 * @param channel
18cacd0b3ccf update tags
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 387
diff changeset
23 */
387
6f356d160e58 IPv6 any address
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 385
diff changeset
24 public FirstConnector(SessionManager manager,
6f356d160e58 IPv6 any address
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 385
diff changeset
25 REPSocketChannel<REPCommand> channel) {
6f356d160e58 IPv6 any address
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 385
diff changeset
26 super(manager,channel);
382
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
27 }
387
6f356d160e58 IPv6 any address
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 385
diff changeset
28
382
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
29 public void cancel(REPSocketChannel<REPCommand> socketChannel) {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
30 manager.remove(socketChannel);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
31 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
32
387
6f356d160e58 IPv6 any address
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 385
diff changeset
33 @Override
6f356d160e58 IPv6 any address
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 385
diff changeset
34 public void handle(REPCommand command, REPSelectionKey<REPCommand> key) throws IOException {
382
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
35 /*
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
36 * 接続要求は、EditorかSlave Editorで、
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
37 *    join, put, sm_join
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
38 * が来る。それ以外はエラー。master もありか?
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
39 * sm_join_ack
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 REPNode fw;
384
bcdf5476b8e4 restructured-version
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 382
diff changeset
42 ServerMainLoop.logger.writeLog("FirstConnector: command = " + command);
382
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
43 switch(command.cmd) {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
44 case SMCMD_JOIN:
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
45 {
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
46 //どのSessionにも属さないエディタをリストに追加
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
47 //エディタとchannelは1対1 (ではないかも)
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
48 //エディタが新しくputする場合は新しくソケットを作る
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
49 // 1対1でない場合は、multiplexerを挿めば良い
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
50 REPNode editor = manager.newEditor(channel);
384
bcdf5476b8e4 restructured-version
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 382
diff changeset
51 editor.setHost(manager.myHost());
382
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
52 command.eid = editor.eid;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
53 command.sid = -1;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
54 editor.setSID(-1);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
55 fw = editor;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
56 break;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
57 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
58 case SMCMD_PUT:
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 // 新しいeditorとsessionをここで作る。eid,sidは、
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
61 // session manager IDが付いているので、global unique
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
62 REPNode editor = manager.newEditor(channel);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
63 Session session = manager.newSession(editor);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
64 session.setName(command.string);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
65 editor.setName(command.string);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
66 editor.setSID(session.getSID());
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
67 command.eid = editor.eid;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
68 command.sid = editor.sid;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
69 fw = editor;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
70 break;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
71 }
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
72 case SMCMD_SM_JOIN:
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
73 {
387
6f356d160e58 IPv6 any address
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 385
diff changeset
74 fw = new Dispatcher(manager,channel); // FirstConnector?
382
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
75 manager.addWaitingSessionManager(fw, command);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
76 break;
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 case SMCMD_SM_JOIN_ACK:
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
79 manager.setSessionManagerID(command.sid);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
80 manager.afterConnect();
387
6f356d160e58 IPv6 any address
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 385
diff changeset
81 fw = new Dispatcher(manager,channel);
382
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
82 manager.setParent(fw);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
83 break;
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
84 default: throw new IOException();
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 //myHost を設定。
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
87 //立ち上げ時にやるとlocalhostしか取れない
384
bcdf5476b8e4 restructured-version
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 382
diff changeset
88 if(manager.myHost() == null) manager.setMyHostName(getLocalHostName());
382
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
89 fw.setMode(command.cmd);
384
bcdf5476b8e4 restructured-version
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 382
diff changeset
90 fw.setHost(manager.myHost());
382
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
91 manager.registerChannel(channel, fw);
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
92 manager.sessionManage(fw, command);
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
385
1fca50ce3508 first-working-version
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 384
diff changeset
96 @Override
1fca50ce3508 first-working-version
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 384
diff changeset
97 public void send(REPCommand command) {
1fca50ce3508 first-working-version
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 384
diff changeset
98 assert(command!=null && command.cmd==REP.SMCMD_SM_JOIN);
1fca50ce3508 first-working-version
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 384
diff changeset
99 assert(channel!=null);
1fca50ce3508 first-working-version
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 384
diff changeset
100 REPCommand c = new REPCommand(command);
1fca50ce3508 first-working-version
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 384
diff changeset
101 manager.addWriteQueue(new PacketSet(channel,null, c));
1fca50ce3508 first-working-version
one@firefly.cr.ie.u-ryukyu.ac.jp
parents: 384
diff changeset
102 }
382
4b87f89b3afd REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
103 }