annotate rep/FirstConnector.java @ 356:b18c24dcc5d2

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