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