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