annotate rep/SessionManager.java @ 105:9c97532b9851

*** empty log message ***
author pin
date Sat, 22 Dec 2007 21:22:36 +0900
parents 91f053bdfb18
children eded5012be89
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
1 package rep;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
2
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
3 import java.io.IOException;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
4 import java.net.InetSocketAddress;
11
167694db4796 *** empty log message ***
pin
parents: 10
diff changeset
5 import java.net.SocketAddress;
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
6 import java.nio.channels.SelectableChannel;
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
7 import java.nio.channels.SelectionKey;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
8 import java.nio.channels.Selector;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
9 import java.nio.channels.ServerSocketChannel;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
10 import java.nio.channels.SocketChannel;
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
11 import java.util.LinkedList;
15
78c211017d3d *** empty log message ***
pin
parents: 14
diff changeset
12 import java.util.StringTokenizer;
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
13
56
6ead43b2475e *** empty log message ***
pin
parents: 53
diff changeset
14 import rep.xml.SessionXMLDecoder;
45
10f1adf6d74d *** empty log message ***
pin
parents: 39
diff changeset
15 import rep.xml.SessionXMLEncoder;
10f1adf6d74d *** empty log message ***
pin
parents: 39
diff changeset
16
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
17 //+-------+--------+--------+-------+--------+---------+------+
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
18 //| cmd | session| editor | seqid | lineno | textsiz | text |
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
19 //| | id | id | | | | |
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
20 //+-------+--------+--------+-------+--------+---------+------+
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
21 //o-------header section (network order)-------------o
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
22 /*int cmd; // command
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
23 int sid; // session ID : uniqu to editing file
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
24 int eid; // editor ID : owner editor ID = 1。Session に対してユニーク
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
25 int seqno; // Sequence number : sequence number はエディタごとに管理
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
26 int lineno; // line number
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
27 int textsize; // textsize : bytesize
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
28 byte[] text;*/
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
29
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
30 public class SessionManager implements ConnectionListener, REPActionListener{
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
31
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
32
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
33 private SessionList sessionlist;
5
91a33a634fef *** empty log message ***
pin
parents: 3
diff changeset
34 //SocketChannel sessionchannel;
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
35 private SessionManagerGUI gui;
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
36 private Selector selector;
7
5067a6a93d01 *** empty log message ***
pin
parents: 6
diff changeset
37 private SessionManagerList smList;
17
806e34a6fecd *** empty log message ***
pin
parents: 16
diff changeset
38 private String myHost;
21
2d4bab638a71 *** empty log message ***
pin
parents: 19
diff changeset
39 private boolean isMaster = true;
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
40 private EditorList allEditorList;
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
41 private String maxHost;
76
97ca5f5447cd *** empty log message ***
pin
parents: 75
diff changeset
42 //private boolean addressIsGlobal;
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
43 //private SocketChannel sessionchannel;
7
5067a6a93d01 *** empty log message ***
pin
parents: 6
diff changeset
44 //private boolean co;
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
45 private static int temp_port;
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
46 private static int send_port;
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
47
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
48 static final int DEFAULT_PORT = 8766;
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
49
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
50 public SessionManager(int port) {
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
51 gui = new SessionManagerGUI();
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
52 }
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
53
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
54 public void openSelector() throws IOException{
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
55 selector = Selector.open();
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
56 }
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
57
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
58 public void sessionManagerNet(int port) throws InterruptedException, IOException {
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
59
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
60 ServerSocketChannel ssc = ServerSocketChannel.open();
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
61 ssc.configureBlocking(false); //reuse address 必須
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
62
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
63 ssc.socket().setReuseAddress(true);
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
64
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
65 ssc.socket().bind(new InetSocketAddress(port));
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
66 ssc.register(selector, SelectionKey.OP_ACCEPT);
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
67
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
68
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
69 sessionlist = new SessionList();
7
5067a6a93d01 *** empty log message ***
pin
parents: 6
diff changeset
70 smList = new SessionManagerList();
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
71 allEditorList = new EditorList();
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
72
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
73 while(true){
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
74 selector.select();
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
75 for(SelectionKey key : selector.selectedKeys()){
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
76 if(key.isAcceptable()){
28
1544f5c45dd8 edit disconnet key.cancel + accept etc
fuchita
parents: 26
diff changeset
77 /*** serverChannelはenableになったSelectionKeyのchannel ***/
1544f5c45dd8 edit disconnet key.cancel + accept etc
fuchita
parents: 26
diff changeset
78 ServerSocketChannel serverChannel = (ServerSocketChannel)key.channel();
9
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
79 /*** EditorChannel を用いない記述 ***/
28
1544f5c45dd8 edit disconnet key.cancel + accept etc
fuchita
parents: 26
diff changeset
80 SocketChannel channel = serverChannel.accept(); //keyからchannelを取って、accept
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
81 registerChannel (selector, channel, SelectionKey.OP_READ);
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
82 channel = null;
9
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
83
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
84 /*** EditorChannel を用いた記述 ****/
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
85 //EditorChannel echannel = (EditorChannel) ssc.accept();
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
86 //echannel.setIO();
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
87 //registerChannel(selector, echannel, SelectionKey.OP_READ);
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
88 //echannel = null;
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
89
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
90 /*** SelectableEditorChannel ***/
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
91 //SocketChannel channel = ssc.accept();
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
92 //SelectableEditorChannel echannel2 = new SelectableEditorChannel(channel);
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
93 //registerChannel(selector, echannel2, SelectionKey.OP_READ);
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
94 //channel = null;
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
95 //echannel2 = null;
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
96
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
97 }else if(key.isReadable()){
9
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
98
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
99 /*** EditorChannel を用いない記述 ***/
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
100 SocketChannel channel = (SocketChannel)key.channel();
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
101 REPPacketReceive receive = new REPPacketReceive(channel); //getPacket(), putPacket() にする。
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
102 receive.setkey(key);
53
b87c9b39739a UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 45
diff changeset
103 //REPCommand repCom = repRec.unpackUConv();
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
104 REPCommand receivedCommand = receive.unpack();
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
105 manager(channel, receivedCommand);
9
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
106
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
107 /*** EditorChannel を用いた記述 ****/
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
108 //EditorChannel echannel = (EditorChannel) key.channel();
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
109 //REPCommand command = echannel.getPacket();
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
110 //manager(echannel, command);
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
111
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
112 }else if(key.isConnectable()){
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
113 System.out.println("Connectable");
21
2d4bab638a71 *** empty log message ***
pin
parents: 19
diff changeset
114 }
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
115 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
116 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
117 }
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
118
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
119 private synchronized void registerChannel(Selector selector, SelectableChannel channel, int ops) throws IOException {
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
120 if(channel == null) {
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
121 return;
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
122 }
9
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
123 //System.out.println("registerChannel()");
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
124 channel.configureBlocking(false);
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
125 selector.wakeup();
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
126 channel.register(selector, ops);
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
127 }
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
128
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
129 private void manager(SocketChannel channel, REPCommand receivedCommand) {
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
130 if(receivedCommand == null) return;
69
9dd7471b771a *** empty log message ***
pin
parents: 68
diff changeset
131 Editor editor;
9dd7471b771a *** empty log message ***
pin
parents: 68
diff changeset
132 Session session;
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
133 REPCommand sendCommand = receivedCommand.clone();
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
134 REPPacketSend send = new REPPacketSend(channel);
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
135 //SessionXMLEncoder encoder = new SessionXMLEncoder();
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
136
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
137 switch(receivedCommand.cmd){
38
de8638eb0edd *** empty log message ***
pin
parents: 31
diff changeset
138
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
139 case REP.SMCMD_JOIN:
105
9c97532b9851 *** empty log message ***
pin
parents: 104
diff changeset
140 editor = new Editor(channel);
9c97532b9851 *** empty log message ***
pin
parents: 104
diff changeset
141 int tempeid = allEditorList.addEditor(editor);
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
142 gui.setComboEditor(tempeid, channel);
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
143
38
de8638eb0edd *** empty log message ***
pin
parents: 31
diff changeset
144 break;
31
593f915dd6ff for JOIN and PUT Testing
pin
parents: 30
diff changeset
145
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
146 case REP.SMCMD_JOIN_ACK:
31
593f915dd6ff for JOIN and PUT Testing
pin
parents: 30
diff changeset
147 // editorList.setEID(repCmd);
593f915dd6ff for JOIN and PUT Testing
pin
parents: 30
diff changeset
148 // editorList.sendJoinAck(repCmd);
593f915dd6ff for JOIN and PUT Testing
pin
parents: 30
diff changeset
149 // sessionmanagerGUI.setComboEditor(repCmd.eid, channel);
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
150 break;
31
593f915dd6ff for JOIN and PUT Testing
pin
parents: 30
diff changeset
151
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
152 case REP.SMCMD_PUT:
69
9dd7471b771a *** empty log message ***
pin
parents: 68
diff changeset
153 editor = new Editor(channel);
90
c5017ea49e5f *** empty log message ***
pin
parents: 89
diff changeset
154 editor.setHost(myHost);
92
ec0b37884758 *** empty log message ***
pin
parents: 90
diff changeset
155 allEditorList.addEditor(editor);
56
6ead43b2475e *** empty log message ***
pin
parents: 53
diff changeset
156 editor.setEID(1);
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
157 editor.setName(receivedCommand.string);
69
9dd7471b771a *** empty log message ***
pin
parents: 68
diff changeset
158 session = new Session(editor);
67
13e4c3f2c6f9 *** empty log message ***
pin
parents: 66
diff changeset
159 session.setOwner(true);
63
b30c1c73d7ef *** empty log message ***
pin
parents: 62
diff changeset
160 session.addEditor(editor);
56
6ead43b2475e *** empty log message ***
pin
parents: 53
diff changeset
161 sessionlist.addSession(session);
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
162 gui.setComboSession(session.getSID(), session.getName());
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
163 gui.setComboEditor(editor.getEID(), editor.getChannel());
56
6ead43b2475e *** empty log message ***
pin
parents: 53
diff changeset
164 session.addToRoutingTable(editor);
84
54334d83194a *** empty log message ***
pin
parents: 83
diff changeset
165 sendCommand.setCMD(REP.SMCMD_PUT_ACK);
54334d83194a *** empty log message ***
pin
parents: 83
diff changeset
166 sendCommand.setEID(1);
54334d83194a *** empty log message ***
pin
parents: 83
diff changeset
167 sendCommand.setSID(session.getSID());
54334d83194a *** empty log message ***
pin
parents: 83
diff changeset
168 editor.send(sendCommand);
56
6ead43b2475e *** empty log message ***
pin
parents: 53
diff changeset
169
6ead43b2475e *** empty log message ***
pin
parents: 53
diff changeset
170 //if(isMaster){
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
171 SessionXMLEncoder sessionEncoder = new SessionXMLEncoder(session);
56
6ead43b2475e *** empty log message ***
pin
parents: 53
diff changeset
172 REPCommand command = new REPCommand();
6ead43b2475e *** empty log message ***
pin
parents: 53
diff changeset
173 command.setSID(session.getSID());
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
174 command.setString(sessionEncoder.sessionListToXML());
98
cc67a5a81535 *** empty log message ***
pin
parents: 97
diff changeset
175
cc67a5a81535 *** empty log message ***
pin
parents: 97
diff changeset
176 command.setCMD(REP.SMCMD_UPDATE);
cc67a5a81535 *** empty log message ***
pin
parents: 97
diff changeset
177 smList.sendExcept(channel, command);
cc67a5a81535 *** empty log message ***
pin
parents: 97
diff changeset
178
cc67a5a81535 *** empty log message ***
pin
parents: 97
diff changeset
179
cc67a5a81535 *** empty log message ***
pin
parents: 97
diff changeset
180 // if(isMaster){
cc67a5a81535 *** empty log message ***
pin
parents: 97
diff changeset
181 // command.setCMD(REP.SMCMD_UPDATE_ACK);
cc67a5a81535 *** empty log message ***
pin
parents: 97
diff changeset
182 // smList.sendToSlave(command);
cc67a5a81535 *** empty log message ***
pin
parents: 97
diff changeset
183 // }else{
cc67a5a81535 *** empty log message ***
pin
parents: 97
diff changeset
184 // command.setCMD(REP.SMCMD_UPDATE);
cc67a5a81535 *** empty log message ***
pin
parents: 97
diff changeset
185 // smList.sendToMaster(command);
cc67a5a81535 *** empty log message ***
pin
parents: 97
diff changeset
186 // }
38
de8638eb0edd *** empty log message ***
pin
parents: 31
diff changeset
187 break;
31
593f915dd6ff for JOIN and PUT Testing
pin
parents: 30
diff changeset
188
9
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
189 // case REP.SMCMD_PUT_ACK:
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
190 // break;
31
593f915dd6ff for JOIN and PUT Testing
pin
parents: 30
diff changeset
191
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
192 case REP.SMCMD_SELECT:
66
99196780bbf5 *** empty log message ***
pin
parents: 65
diff changeset
193 // sessionlist.addEditor(channel, repCmd.sid, repCmd); //sessionlistへ追加
70
248d774be113 *** empty log message ***
pin
parents: 69
diff changeset
194 editor = new Editor(channel);
94
2830eb33d4d3 *** empty log message ***
pin
parents: 93
diff changeset
195
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
196 session = sessionlist.getSession(receivedCommand.sid);
94
2830eb33d4d3 *** empty log message ***
pin
parents: 93
diff changeset
197
70
248d774be113 *** empty log message ***
pin
parents: 69
diff changeset
198 if(session.isOwner()){
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
199 int eid2 = session.addEditor(editor);
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
200 editor.setEID(eid2);
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
201 //REPPacketSend send = new REPPacketSend(channel);
84
54334d83194a *** empty log message ***
pin
parents: 83
diff changeset
202 //receivedCommand.setCMD(REP.SMCMD_SELECT_ACK);
54334d83194a *** empty log message ***
pin
parents: 83
diff changeset
203 //receivedCommand.setEID(eid2);
54334d83194a *** empty log message ***
pin
parents: 83
diff changeset
204 sendCommand.setCMD(REP.SMCMD_SELECT_ACK);
54334d83194a *** empty log message ***
pin
parents: 83
diff changeset
205 sendCommand.setEID(eid2);
54334d83194a *** empty log message ***
pin
parents: 83
diff changeset
206 send.send(sendCommand);
66
99196780bbf5 *** empty log message ***
pin
parents: 65
diff changeset
207 }else {
85
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
208 Editor master = session.getMaster();
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
209 master.send(receivedCommand);
94
2830eb33d4d3 *** empty log message ***
pin
parents: 93
diff changeset
210 session.addEditor(editor);
66
99196780bbf5 *** empty log message ***
pin
parents: 65
diff changeset
211 }
99196780bbf5 *** empty log message ***
pin
parents: 65
diff changeset
212
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
213 break;
38
de8638eb0edd *** empty log message ***
pin
parents: 31
diff changeset
214
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
215 case REP.SMCMD_SELECT_ACK:
85
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
216
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
217 String hostport = receivedCommand.string;
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
218 Editor editor2 = allEditorList.getEditor(hostport);
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
219 if(editor2 != null) {
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
220 REPCommand command2 = new REPCommand();
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
221 command2.setCMD(REP.SMCMD_JOIN_ACK);
93
1a5b3c2b2268 *** empty log message ***
pin
parents: 92
diff changeset
222 command2.setSID(receivedCommand.sid);
85
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
223 command2.setEID(receivedCommand.eid);
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
224 editor2.send(command2);
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
225 }else{
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
226 smList.sendExcept(channel, receivedCommand);
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
227 }
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
228
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
229 //receivedCommand.setCMD(REP.SMCMD_JOIN_ACK);
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
230 //receivedCommand.setEID(receivedCommand.eid);
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
231 //session = sessionlist.getSession(receivedCommand.sid);
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
232 //session.sendToEditor(receivedCommand);
69
9dd7471b771a *** empty log message ***
pin
parents: 68
diff changeset
233 //Editor editor3 = session3.getEditorList().get(0);
9dd7471b771a *** empty log message ***
pin
parents: 68
diff changeset
234 //REPPacketSend send = new REPPacketSend(editor3.getChannel());
9dd7471b771a *** empty log message ***
pin
parents: 68
diff changeset
235 //send.send(repCmd);
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
236 break;
38
de8638eb0edd *** empty log message ***
pin
parents: 31
diff changeset
237
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
238 case REP.SMCMD_SM_JOIN:
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
239
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
240 //SessionManagerのリストへ追加
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
241 smList.add(channel);
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
242
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
243 //XMLからSessionListオブジェクトを生成する。
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
244 SessionXMLDecoder decoder = new SessionXMLDecoder();
79
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
245 SessionList receivedSessionList = decoder.decode(receivedCommand.string);
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
246
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
247 //SessionListへ追加し変換テーブルを生成する。
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
248 sessionlist.update(channel, receivedSessionList);
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
249
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
250 //myHost を設定。
76
97ca5f5447cd *** empty log message ***
pin
parents: 75
diff changeset
251 if(myHost == null) setMyHostName(getLocalHostName(channel));
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
252
79
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
253 //maxHost を設定。
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
254 if(setMaxHost(channel, receivedSessionList.getMaxHost())){
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
255 sendCommand = new REPCommand();
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
256 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
257 sendCommand.setString(maxHost);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
258 smList.sendExcept(channel, sendCommand);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
259 }
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
260
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
261 //SessionListからXMLを生成。
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
262 //joinしてきたSessionManagerに対してACKを送信。
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
263 SessionXMLEncoder sessionlistEncoder = new SessionXMLEncoder(sessionlist);
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
264 sendCommand = new REPCommand();
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
265 sendCommand.setCMD(REP.SMCMD_SM_JOIN_ACK);
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
266 sendCommand.setString(sessionlistEncoder.sessionListToXML());
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
267 send.send(sendCommand);
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
268
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
269 //その他の SessionManager に対して SMCMD_UPDATEを 送信。
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
270 sendCommand = new REPCommand();
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
271 sendCommand.setCMD(REP.SMCMD_UPDATE);
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
272 sendCommand.setString(receivedCommand.string);
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
273 smList.sendExcept(channel, sendCommand);
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
274
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
275 //その他のSessionManagerに対してSMCMD_SM_JOINを送信。
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
276 //sendCommand = new REPCommand();
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
277 //sendCommand.setCMD(REP.SMCMD_SM_JOIN);
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
278 //sendCommand.setString(receivedCommand.string);
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
279 //smList.sendExcept(channel, sendCommand);
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
280
31
593f915dd6ff for JOIN and PUT Testing
pin
parents: 30
diff changeset
281 if(isMaster){
45
10f1adf6d74d *** empty log message ***
pin
parents: 39
diff changeset
282 }else {
31
593f915dd6ff for JOIN and PUT Testing
pin
parents: 30
diff changeset
283 }
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
284
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
285 break;
38
de8638eb0edd *** empty log message ***
pin
parents: 31
diff changeset
286
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
287 case REP.SMCMD_SM_JOIN_ACK:
82
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
288
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
289 //XMLからSessionListオブジェクトを生成。
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
290 SessionXMLDecoder decoder2 = new SessionXMLDecoder();
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
291 SessionList receivedSessionList2 = decoder2.decode(receivedCommand.string);
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
292
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
293 //maxHostを決定。
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
294 if(setMaxHost(channel, receivedSessionList2.getMaxHost())){
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
295 sendCommand = new REPCommand();
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
296 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
297 sendCommand.setString(maxHost);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
298 smList.sendExcept(channel, sendCommand);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
299 }
82
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
300
38
de8638eb0edd *** empty log message ***
pin
parents: 31
diff changeset
301 if(isMaster){
39
c873ee0318f8 *** empty log message ***
pin
parents: 38
diff changeset
302 }else{
38
de8638eb0edd *** empty log message ***
pin
parents: 31
diff changeset
303 }
de8638eb0edd *** empty log message ***
pin
parents: 31
diff changeset
304
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
305 break;
38
de8638eb0edd *** empty log message ***
pin
parents: 31
diff changeset
306
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
307 case REP.SMCMD_UPDATE:
70
248d774be113 *** empty log message ***
pin
parents: 69
diff changeset
308
99
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
309 SessionXMLDecoder decoder3 = new SessionXMLDecoder();
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
310 SessionList receivedSessionList3 = decoder3.decode(receivedCommand.string);
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
311
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
312 //SessionListへ追加し変換テーブルを生成する。
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
313 sessionlist.update(channel, receivedSessionList3);
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
314
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
315 smList.sendExcept(channel, receivedCommand);
70
248d774be113 *** empty log message ***
pin
parents: 69
diff changeset
316
100
25bf89525e70 *** empty log message ***
pin
parents: 99
diff changeset
317 for(Session session3 : receivedSessionList3.getList()){
25bf89525e70 *** empty log message ***
pin
parents: 99
diff changeset
318 gui.setComboSession(session3.getSID(), session3.getName());
25bf89525e70 *** empty log message ***
pin
parents: 99
diff changeset
319 }
25bf89525e70 *** empty log message ***
pin
parents: 99
diff changeset
320
99
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
321 //SessionのownerのEditor
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
322 //editor = new Editor(channel);
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
323 //editor.setName(receivedCommand.string);
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
324
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
325
70
248d774be113 *** empty log message ***
pin
parents: 69
diff changeset
326
99
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
327 //session = new Session(editor);
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
328 //session.addEditor(editor);
70
248d774be113 *** empty log message ***
pin
parents: 69
diff changeset
329
99
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
330 //sessionlist.addSession(session);
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
331
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
332 //gui.setComboSession(session.getSID(), session.getName());
70
248d774be113 *** empty log message ***
pin
parents: 69
diff changeset
333
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
334 //if(isMaster){
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
335 // receivedCommand.setCMD(REP.SMCMD_UPDATE_ACK);
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
336 // smList.sendToSlave(receivedCommand);
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
337 //}else{
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
338 // receivedCommand.setCMD(REP.SMCMD_UPDATE);
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
339 // smList.sendToMaster(receivedCommand);
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
340 //}
9
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
341 break;
38
de8638eb0edd *** empty log message ***
pin
parents: 31
diff changeset
342
9
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
343 case REP.SMCMD_UPDATE_ACK:
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
344 if(receivedCommand.sid > sessionlist.getList().size()){
73
4d887f38284b *** empty log message ***
pin
parents: 72
diff changeset
345 editor = new Editor(channel);
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
346 editor.setName(receivedCommand.string);
73
4d887f38284b *** empty log message ***
pin
parents: 72
diff changeset
347
4d887f38284b *** empty log message ***
pin
parents: 72
diff changeset
348 session = new Session(editor);
4d887f38284b *** empty log message ***
pin
parents: 72
diff changeset
349 session.addEditor(editor);
4d887f38284b *** empty log message ***
pin
parents: 72
diff changeset
350
4d887f38284b *** empty log message ***
pin
parents: 72
diff changeset
351 sessionlist.addSession(session);
4d887f38284b *** empty log message ***
pin
parents: 72
diff changeset
352
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
353 gui.setComboSession(session.getSID(), session.getName());
73
4d887f38284b *** empty log message ***
pin
parents: 72
diff changeset
354 }
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
355 smList.sendToSlave(receivedCommand);
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
356 break;
38
de8638eb0edd *** empty log message ***
pin
parents: 31
diff changeset
357
3
f31fcac5a949 *** empty log message ***
pin
parents: 2
diff changeset
358 case REP.REPCMD_READ:
17
806e34a6fecd *** empty log message ***
pin
parents: 16
diff changeset
359 //sessionlist.sendCmd(channel, repCmd);
3
f31fcac5a949 *** empty log message ***
pin
parents: 2
diff changeset
360 break;
38
de8638eb0edd *** empty log message ***
pin
parents: 31
diff changeset
361
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
362 case REP.SMCMD_CH_MASTER:
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
363 //maxHost を設定。
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
364 if(setMaxHost(channel, receivedCommand.string)){
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
365 sendCommand = new REPCommand();
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
366 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
367 sendCommand.setString(maxHost);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
368 smList.sendExcept(channel, sendCommand);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
369 }
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
370 break;
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
371
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
372 default:
9
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
373 //sessionlist.sendCmd(channel, repCmd);
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
374 sessionlist.sendToNextEditor(channel, receivedCommand);
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
375 break;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
376 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
377 }
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
378
79
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
379 private boolean setMaxHost(SocketChannel channel, String host) {
81
13819571691d *** empty log message ***
pin
parents: 80
diff changeset
380 if(maxHost == null) {
13819571691d *** empty log message ***
pin
parents: 80
diff changeset
381 maxHost = myHost;
13819571691d *** empty log message ***
pin
parents: 80
diff changeset
382 sessionlist.setMaxHost(maxHost);
13819571691d *** empty log message ***
pin
parents: 80
diff changeset
383 }
82
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
384 if(host.compareTo(maxHost) > 0){
79
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
385 //host > MaxHost なら maxHost = host
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
386 //masterを設定する。
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
387 maxHost = host;
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
388 sessionlist.setMaxHost(maxHost);
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
389 setMaster(false, channel);
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
390 return true;
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
391 }else{
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
392 return false;
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
393 }
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
394 }
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
395
76
97ca5f5447cd *** empty log message ***
pin
parents: 75
diff changeset
396 private void setMyHostName(String localHostName) {
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
397 myHost = localHostName + temp_port;
81
13819571691d *** empty log message ***
pin
parents: 80
diff changeset
398 if(maxHost == null) {
13819571691d *** empty log message ***
pin
parents: 80
diff changeset
399 maxHost = myHost;
13819571691d *** empty log message ***
pin
parents: 80
diff changeset
400 sessionlist.setMaxHost(maxHost);
13819571691d *** empty log message ***
pin
parents: 80
diff changeset
401 }
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
402 allEditorList.setHost(myHost);
76
97ca5f5447cd *** empty log message ***
pin
parents: 75
diff changeset
403 }
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
404
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
405 private void setMaster(boolean b, SocketChannel channel) {
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
406 isMaster = b;
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
407 System.out.println("isMaster = " + b);
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
408 smList.setMaster(channel);
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
409 }
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
410
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
411 public static void main(String[] args) throws InterruptedException, IOException {
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
412 int port = DEFAULT_PORT;
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
413 int port_s = DEFAULT_PORT;
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
414
82
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
415 if(args.length > 0){
39
c873ee0318f8 *** empty log message ***
pin
parents: 38
diff changeset
416 port = Integer.parseInt(args[0]);
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
417 port_s = Integer.parseInt(args[1]);
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
418 }
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
419 temp_port = port;
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
420 send_port = port_s;
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
421 SessionManager sm = new SessionManager(port);
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
422 sm.openSelector();
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
423 sm.openWindow();
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
424 sm.sessionManagerNet(port);
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
425 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
426
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
427 private void openWindow() {
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
428 Thread th = new Thread( gui );
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
429 th.start();
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
430 //System.out.println(sessionmanagerGUI.toString());
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
431 gui.addConnectionListener(this);
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
432 gui.addREPActionListener(this);
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
433 }
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
434
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
435 private void connectSession(String host) {
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
436 int port = DEFAULT_PORT;
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
437 port = send_port;
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
438 InetSocketAddress addr = new InetSocketAddress(host, port);
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
439 try {
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
440 SocketChannel sessionchannel = SocketChannel.open();
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
441 sessionchannel.configureBlocking(true);
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
442 sessionchannel.connect(addr);
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
443 while(!sessionchannel.finishConnect()){
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
444 System.out.print("test afro");
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
445 }
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
446 System.out.println("");
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
447 registerChannel(selector, sessionchannel, SelectionKey.OP_READ);
45
10f1adf6d74d *** empty log message ***
pin
parents: 39
diff changeset
448
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
449 sm_join(sessionchannel);
45
10f1adf6d74d *** empty log message ***
pin
parents: 39
diff changeset
450
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
451 }catch (IOException e) {
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
452 e.printStackTrace();
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
453 }
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
454 }
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
455
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
456 private void sm_join(SocketChannel channel){
79
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
457
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
458 //SM_JOINコマンドを生成。
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
459 REPCommand command = new REPCommand();
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
460 command.setCMD(REP.SMCMD_SM_JOIN);
79
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
461
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
462 //hostnameをセット。
82
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
463 setMyHostName(getLocalHostName(channel));
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
464
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
465 //XMLを生成。送信コマンドにセット。
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
466 SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist);
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
467 String string = encoder.sessionListToXML();
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
468 command.setString(string);
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
469
79
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
470 //SM_JOINコマンドを送信。
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
471 REPPacketSend send = new REPPacketSend(channel);
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
472 send.send(command);
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
473
79
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
474 //SessionManagerのListに追加。
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
475 smList.add(channel);
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
476 }
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
477
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
478 private String getLocalHostName(SocketChannel channel) {
74
391d44c94799 *** empty log message ***
pin
parents: 73
diff changeset
479 String host = null;
391d44c94799 *** empty log message ***
pin
parents: 73
diff changeset
480 host = channel.socket().getLocalAddress().getHostName();
391d44c94799 *** empty log message ***
pin
parents: 73
diff changeset
481 return host;
391d44c94799 *** empty log message ***
pin
parents: 73
diff changeset
482 }
391d44c94799 *** empty log message ***
pin
parents: 73
diff changeset
483
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
484 // private String getSocketString(SocketChannel sessionchannel) {
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
485 // SocketAddress socket = sessionchannel.socket().getRemoteSocketAddress();
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
486 // //String inetAddressString = sessionchannel.socket().getInetAddress().toString();
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
487 // StringTokenizer stn = new StringTokenizer(socket.toString(), "/");
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
488 // String socketString = null;
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
489 // while(stn.hasMoreTokens()){
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
490 // socketString = stn.nextToken();
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
491 // //System.out.println(socketString);
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
492 // }
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
493 // return socketString;
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
494 // }
14
47633cfb92d3 *** empty log message ***
pin
parents: 13
diff changeset
495
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
496 public void connectionOccured(ConnectionEvent event) {
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
497 connectSession(event.getHost());
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
498 }
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
499
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
500 public void ActionOccured(REPActionEvent event) {
103
caea2e66deb8 *** empty log message ***
pin
parents: 101
diff changeset
501
104
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
502 SocketChannel editorChannel = event.getEditorChannel();
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
503 int sid = event.getSID();
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
504 Editor editor = new Editor(editorChannel);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
505 editor.setHost(this.myHost);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
506 Session session = sessionlist.getSession(sid);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
507 session.addEditor(editor);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
508
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
509 Editor owner = session.getMaster();
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
510
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
511 REPCommand command = new REPCommand();
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
512 command.setCMD(REP.SMCMD_SELECT);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
513 command.setSID(sid);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
514 command.setString(editor.getHost() + ":" + editor.getPort());
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
515 owner.send(command);
103
caea2e66deb8 *** empty log message ***
pin
parents: 101
diff changeset
516
caea2e66deb8 *** empty log message ***
pin
parents: 101
diff changeset
517 /*** 書き直し ***/
104
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
518 // SocketChannel channel = event.getEditorChannel();
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
519 // int sid = event.getSID();
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
520 // Session session = sessionlist.getSession(sid);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
521 // if(session.isOwner()){
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
522 // int eid = session.addEditor(new Editor(channel));
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
523 // REPCommand sendCommand = new REPCommand();
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
524 // sendCommand.setCMD(REP.SMCMD_JOIN_ACK);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
525 // sendCommand.setEID(eid);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
526 // sendCommand.setSID(sid);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
527 // REPPacketSend sender = new REPPacketSend(channel);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
528 // sender.send(sendCommand);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
529 // }else {
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
530 // SocketChannel editorChannel = event.getEditorChannel();
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
531 // sid = event.getSID();
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
532 // Editor editor = new Editor(editorChannel);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
533 // editor.setHost(myHost);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
534 // session = sessionlist.getSession(sid);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
535 // session.addEditor(editor);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
536 //
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
537 // Editor owner = session.getMaster();
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
538 //
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
539 // REPCommand command = new REPCommand();
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
540 // command.setCMD(REP.SMCMD_SELECT);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
541 // command.setSID(sid);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
542 // command.setString(editor.getHost() + ":" + editor.getPort());
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
543 // owner.send(command);
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
544 // }
72
3cdf2d6d99d9 *** empty log message ***
pin
parents: 71
diff changeset
545
3cdf2d6d99d9 *** empty log message ***
pin
parents: 71
diff changeset
546
84
54334d83194a *** empty log message ***
pin
parents: 83
diff changeset
547 //REPPacketSend send = new REPPacketSend(editorChannel);
54334d83194a *** empty log message ***
pin
parents: 83
diff changeset
548 //send.send(new REPCommand(REP.SMCMD_SELECT_ACK, sid, eid, 0,0,0,""));
72
3cdf2d6d99d9 *** empty log message ***
pin
parents: 71
diff changeset
549
3cdf2d6d99d9 *** empty log message ***
pin
parents: 71
diff changeset
550
3cdf2d6d99d9 *** empty log message ***
pin
parents: 71
diff changeset
551
71
f03307651df8 *** empty log message ***
pin
parents: 70
diff changeset
552 //sessionlist.sendSelect(sid);
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
553 }
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
554 }