355
|
1 package rep;
|
|
2
|
|
3 import java.io.IOException;
|
|
4
|
|
5 import rep.channel.REPSelectionKey;
|
|
6 import rep.channel.REPSocketChannel;
|
|
7
|
|
8 public class FirstConnector extends Forwarder {
|
|
9
|
|
10 public FirstConnector(SessionManager manager) {
|
|
11 super(manager);
|
|
12 }
|
|
13
|
|
14 public void cancel(REPSocketChannel<REPCommand> socketChannel) {
|
|
15 manager.remove(socketChannel);
|
|
16 }
|
|
17
|
|
18 public void handle(REPSelectionKey<REPCommand> key) throws Exception {
|
|
19 /*
|
|
20 * 接続要求は、EditorかSlave Editorで、
|
|
21 * join, put, sm_join
|
|
22 * が来る。それ以外はエラー。master もありか?
|
|
23 * sm_join_ack
|
|
24 */
|
|
25 Forwarder fw;
|
|
26 REPSocketChannel<REPCommand> channel = key.channel1();
|
|
27 REPCommand command = channel.read();
|
|
28 SessionManager.logger.writeLog("FirstConnector: command = " + command);
|
|
29 switch(command.cmd) {
|
|
30 case SMCMD_JOIN:
|
|
31 {
|
|
32 //どのSessionにも属さないエディタをリストに追加
|
|
33 //エディタとchannelは1対1 (ではない)
|
|
34 //エディタが新しくputする場合は新しくソケットを作る
|
|
35 // 1対1でない場合は、multiplexerを挿めば良い
|
|
36 // ここのeditorList はsessionのとは別物
|
|
37 Editor editor = new Editor(manager,-1,channel);
|
|
38 editor.setHost(manager.myHost);
|
|
39 manager.editorList.add(editor);
|
|
40 fw = editor;
|
|
41 break;
|
|
42 }
|
|
43 case SMCMD_PUT:
|
|
44 {
|
|
45 // put の場合でも、eid は、masterまで聞きにいく必要が
|
|
46 // ある。
|
|
47 Editor editor = new Editor(manager,0,channel);
|
|
48 editor.setHost(manager.myHost);
|
|
49 manager.editorList.add(editor);
|
|
50 fw = editor;
|
|
51 break;
|
|
52 }
|
|
53 case SMCMD_SM_JOIN:
|
|
54 {
|
|
55 fw = new Forwarder(manager);
|
|
56 manager.smList.addWaitingSessionManager(fw, command);
|
|
57 break;
|
|
58 }
|
|
59 case SMCMD_SM_JOIN_ACK:
|
|
60 manager.setSessionManagerID(command.sid);
|
|
61 fw = new Forwarder(manager);
|
|
62 break;
|
|
63 default: throw new IOException();
|
|
64 }
|
|
65 //myHost を設定。
|
|
66 //立ち上げ時にやるとlocalhostしか取れない
|
|
67 if(manager.myHost == null) manager.setMyHostName(getLocalHostName());
|
|
68
|
|
69 fw.setMode(command.cmd);
|
|
70 manager.registerChannel(channel, fw);
|
|
71 manager.sessionManage(fw, command);
|
|
72
|
|
73 }
|
|
74
|
|
75 }
|