266
|
1
|
0
|
2 package rep;
|
|
3
|
|
4 import java.io.IOException;
|
|
5 import java.net.InetSocketAddress;
|
267
|
6 import java.nio.channels.ClosedChannelException;
|
0
|
7 import java.nio.channels.SelectionKey;
|
178
|
8 import java.util.Iterator;
|
83
|
9 import java.util.LinkedList;
|
144
|
10 import java.util.List;
|
231
|
11 import java.util.Set;
|
178
|
12 import java.util.concurrent.BlockingQueue;
|
192
|
13 import java.util.concurrent.LinkedBlockingQueue;
|
0
|
14
|
198
|
15
|
123
|
16 import rep.channel.REPServerSocketChannel;
|
133
|
17 import rep.channel.REPSocketChannel;
|
144
|
18 import rep.handler.PacketSet;
|
146
|
19 import rep.handler.REPHandler;
|
148
|
20 import rep.handler.REPHandlerImpl;
|
164
|
21 import rep.handler.REPHandlerInMerge;
|
158
|
22 import rep.channel.REPSelector;
|
56
|
23 import rep.xml.SessionXMLDecoder;
|
45
|
24 import rep.xml.SessionXMLEncoder;
|
198
|
25 import rep.channel.REPSelectionKey;
|
264
|
26
|
198
|
27 /*
|
264
|
28 +-------+--------+--------+-------+--------+---------+------+
|
|
29 | cmd | session| editor | seqid | lineno | textsiz | text |
|
|
30 | | id | id | | | | |
|
|
31 +-------+--------+--------+-------+--------+---------+------+
|
|
32 o---------- header section (network order) ----------o
|
|
33
|
|
34 int cmd; kind of command
|
|
35 int sid; session ID : uniqu to editing file
|
|
36 int eid; editor ID : owner editor ID = 1。Session に対して unique
|
308
|
37 -1 session manager command
|
|
38 -2 merge command
|
264
|
39 int seqno; Sequence number : sequence number はエディタごとに管理
|
|
40 int lineno; line number
|
|
41 int textsize; textsize : bytesize
|
|
42 byte[] text;
|
198
|
43 */
|
1
|
44
|
250
|
45 public class SessionManager implements SessionManagerEventListener{
|
0
|
46
|
164
|
47 private LinkedList<Session> sessionList;
|
280
|
48 private SessionManagerGUI gui;
|
198
|
49 private REPSelector<REPCommand> selector;
|
7
|
50 private SessionManagerList smList;
|
144
|
51 private List<Editor> editorList;
|
304
|
52 // editorList は、sessionList に入っているeditorとは別なeditorのlistらしい。
|
78
|
53 private String maxHost;
|
212
|
54 private List<PacketSet> waitingCommandInMerge;
|
308
|
55 REPHandler normalHandler = new REPHandlerImpl(this);
|
|
56 REPHandler handlerInMerge =new REPHandlerInMerge(this);
|
|
57 private BlockingQueue<SessionManagerEvent> waitingEventQueue = new LinkedBlockingQueue<SessionManagerEvent>();;
|
|
58 private String myHost;
|
|
59 private static int receive_port;
|
|
60 private static int parent_port;
|
101
|
61 static final int DEFAULT_PORT = 8766;
|
|
62
|
2
|
63
|
|
64 public void openSelector() throws IOException{
|
231
|
65 selector = REPSelector.<REPCommand>create();
|
2
|
66 }
|
280
|
67
|
|
68 public void init(int port, SessionManagerGUI gui) throws IOException, InterruptedException {
|
|
69 this.gui = gui;
|
|
70 openSelector();
|
|
71 init(port);
|
|
72 mainLoop();
|
|
73 }
|
|
74
|
|
75
|
|
76 private void init(int port) throws InterruptedException, IOException {
|
2
|
77
|
186
|
78 REPServerSocketChannel<REPCommand> ssc = REPServerSocketChannel.<REPCommand>open(new REPCommandPacker());
|
122
|
79 ssc.configureBlocking(false); //reuse address 必須
|
101
|
80 ssc.socket().setReuseAddress(true);
|
212
|
81 //getAllByNameで取れた全てのアドレスに対してbindする
|
|
82 ssc.socket().bind(new InetSocketAddress(port));
|
308
|
83 ssc.register(selector, SelectionKey.OP_ACCEPT, normalHandler);
|
6
|
84
|
144
|
85 sessionList = new LinkedList<Session>();
|
7
|
86 smList = new SessionManagerList();
|
144
|
87 editorList = new LinkedList<Editor>();
|
212
|
88 waitingCommandInMerge = new LinkedList<PacketSet>();
|
228
|
89
|
215
|
90
|
155
|
91 }
|
313
|
92
|
|
93 /*
|
|
94 * We wrote everything in one thread, but we can assign
|
|
95 * one thread for each communication channel and GUI event.
|
|
96 */
|
155
|
97
|
231
|
98 public void mainLoop() throws IOException {
|
0
|
99 while(true){
|
313
|
100 if (checkInputEvent() ||
|
|
101 checkWaitingWrite() ||
|
|
102 checkWaitingCommandInMerge()) {
|
|
103 // try to do fair execution for waiting task
|
|
104 if(selector.selectNow() > 0) select();
|
|
105 continue;
|
300
|
106 }
|
313
|
107 // now we can wait for input packet or event
|
233
|
108 selector.select();
|
144
|
109 select();
|
|
110 }
|
|
111 }
|
|
112
|
313
|
113 private boolean checkInputEvent() {
|
|
114 SessionManagerEvent e;
|
|
115 if((e = waitingEventQueue.poll())!=null){
|
|
116 e.exec();
|
|
117 return true;
|
|
118 }
|
|
119 return false;
|
|
120 }
|
|
121
|
|
122 private boolean checkWaitingWrite() throws IOException {
|
|
123 for(Session s:sessionList) {
|
|
124 for(Editor editor: s.getEditorList())
|
|
125 if (editor.doWaitingWrite()) return true;
|
|
126 }
|
|
127 return false;
|
|
128 }
|
|
129
|
308
|
130 /**
|
|
131 * Check waiting command in merge
|
|
132 * @return true if there is a processed waiting command
|
|
133 * @throws IOException
|
|
134 */
|
|
135 private boolean checkWaitingCommandInMerge() throws IOException {
|
212
|
136 for(Iterator<PacketSet> it = waitingCommandInMerge.iterator(); it.hasNext();){
|
178
|
137 PacketSet p = it.next();
|
308
|
138 if(p.getEditor().isMerging()) { // still merging do nothing
|
178
|
139 continue;
|
|
140 }else{
|
308
|
141 // process one command and return true
|
178
|
142 manage(p.channel, p.command);
|
|
143 it.remove();
|
212
|
144 return true;
|
178
|
145 }
|
|
146 }
|
|
147 return false;
|
|
148 }
|
|
149
|
144
|
150 private void select() throws IOException {
|
231
|
151
|
|
152 Set<REPSelectionKey<REPCommand>> keys = selector.selectedKeys1();
|
|
153 for(REPSelectionKey<REPCommand> key : keys){
|
144
|
154 if(key.isAcceptable()){
|
199
|
155 REPSocketChannel<REPCommand> channel = key.accept(new REPCommandPacker());
|
300
|
156 System.out.println("SessionManager.select() : key.isAcceptable : channel = " + channel);
|
229
|
157 registerChannel (channel, SelectionKey.OP_READ);
|
144
|
158 channel = null;
|
123
|
159
|
144
|
160 }else if(key.isReadable()){
|
212
|
161 REPHandler handler = (REPHandler)(key.attachment());
|
267
|
162 try {
|
|
163 handler.handle(key);
|
|
164 } catch (ClosedChannelException x) {
|
|
165 key.cancel();
|
308
|
166 handler.cancel(key.channel1());
|
267
|
167 } catch (IOException x) {
|
|
168 key.cancel();
|
308
|
169 handler.cancel( key.channel1());
|
267
|
170 }
|
0
|
171 }
|
|
172 }
|
|
173 }
|
1
|
174
|
229
|
175 private void registerChannel(REPSocketChannel<REPCommand> channel, int ops) throws IOException {
|
2
|
176 if(channel == null) {
|
|
177 return;
|
|
178 }
|
|
179 channel.configureBlocking(false);
|
308
|
180 REPHandler handler = normalHandler;
|
148
|
181 channel.register(selector, ops, handler);
|
2
|
182 }
|
|
183
|
287
|
184 public void manage(REPSocketChannel<REPCommand> channel, REPCommand receivedCommand) throws IOException {
|
75
|
185 if(receivedCommand == null) return;
|
158
|
186 //Session session;
|
141
|
187 REPCommand sendCommand = new REPCommand(receivedCommand);
|
178
|
188 REPSocketChannel<REPCommand> send = channel;
|
144
|
189
|
75
|
190 switch(receivedCommand.cmd){
|
144
|
191
|
271
|
192 case SMCMD_JOIN:
|
164
|
193 {
|
|
194 //どのSessionにも属さないエディタをリストに追加
|
212
|
195 //エディタとchannelは1対1
|
|
196 //エディタが新しくputする場合は新しくソケットを作る
|
164
|
197 Editor editor = new Editor(editorList.size(), channel);
|
|
198 editor.setHost(myHost);
|
|
199 editorList.add(editor);
|
144
|
200
|
259
|
201 updateGUI();
|
199
|
202
|
164
|
203 }
|
|
204
|
|
205 break;
|
144
|
206
|
271
|
207 case SMCMD_JOIN_ACK:
|
212
|
208 assert (false);
|
1
|
209 break;
|
144
|
210
|
271
|
211 case SMCMD_PUT:
|
164
|
212 {
|
|
213 //エディタのリストに追加
|
|
214 Editor editor = new Editor(editorList.size(), channel);
|
227
|
215 //editorList.add(editor);
|
164
|
216
|
|
217 //Sessionを生成
|
|
218 int sid = sessionList.size();
|
|
219 editor = new Editor(0, channel);
|
|
220 editor.setHost(myHost);
|
227
|
221 Session session = new Session(sid, receivedCommand.string, editor);
|
164
|
222 session.hasOwner(true);
|
227
|
223 sessionList.add(session);
|
164
|
224
|
259
|
225 updateGUI();
|
158
|
226
|
164
|
227 //エディタにAckを送信
|
|
228 sendCommand.setCMD(REP.SMCMD_PUT_ACK);
|
|
229 sendCommand.setEID(editor.getEID());
|
|
230 sendCommand.setSID(session.getSID());
|
|
231 editor.send(sendCommand);
|
144
|
232
|
164
|
233 //他のSessionManagerへSessionの追加を報告
|
212
|
234 //親に送って、親から子へ
|
164
|
235 SessionXMLEncoder sessionEncoder = new SessionXMLEncoder(session);
|
|
236 REPCommand command = new REPCommand();
|
|
237 command.setSID(session.getSID());
|
|
238 command.setString(sessionEncoder.sessionListToXML());
|
|
239 command.setCMD(REP.SMCMD_UPDATE);
|
|
240 smList.sendExcept(channel, command);
|
|
241
|
|
242 }
|
|
243
|
|
244 break;
|
133
|
245
|
271
|
246 case SMCMD_SELECT:
|
164
|
247 {
|
178
|
248 //他のSessionManagerをエディタとしてSessionに追加
|
164
|
249 Editor editor = new Editor(channel);
|
|
250 Session session = getSession(receivedCommand.sid);
|
|
251 session.addEditor(editor);
|
|
252
|
|
253 if(session.hasOwner()){
|
|
254 //このSessionManagerがオーナーを持っている場合、Sessionにエディタを追加し、エディタへAckを返す
|
|
255 sendCommand.setCMD(REP.SMCMD_SELECT_ACK);
|
|
256 sendCommand.setEID(editor.getEID());
|
|
257 editor.send(sendCommand);
|
|
258 }else{
|
|
259 //オーナーを持ってない場合は、オーナーを持っているSessionManagerへSELECTコマンドを中継する
|
|
260 Editor owner = session.getOwner();
|
|
261 owner.send(receivedCommand);
|
148
|
262 }
|
164
|
263 }
|
144
|
264
|
164
|
265 break;
|
144
|
266
|
271
|
267 case SMCMD_SELECT_ACK:
|
160
|
268 {
|
85
|
269 String hostport = receivedCommand.string;
|
160
|
270 Editor editor = getEditor(hostport);
|
164
|
271
|
160
|
272 if(editor != null) {
|
|
273 //host, port を見て、このコマンドが自分が送信したSelectコマンドのAckかどうかを判断する
|
|
274 REPCommand command = new REPCommand();
|
|
275 command.setCMD(REP.SMCMD_JOIN_ACK);
|
|
276 command.setSID(receivedCommand.sid);
|
|
277 command.setEID(receivedCommand.eid);
|
|
278 editor.send(command);
|
164
|
279
|
85
|
280 }else{
|
160
|
281 //自分が送信したコマンドでなければ、次のSessionManagerへ中継する
|
85
|
282 smList.sendExcept(channel, receivedCommand);
|
|
283 }
|
160
|
284 }
|
144
|
285
|
164
|
286 break;
|
144
|
287
|
271
|
288 case SMCMD_SM_JOIN:
|
164
|
289
|
160
|
290 {
|
122
|
291 //SessionManagerのリストへ追加
|
83
|
292 smList.add(channel);
|
144
|
293
|
122
|
294 //XMLからSessionListオブジェクトを生成する。
|
77
|
295 SessionXMLDecoder decoder = new SessionXMLDecoder();
|
79
|
296 SessionList receivedSessionList = decoder.decode(receivedCommand.string);
|
144
|
297
|
122
|
298 //myHost を設定。
|
178
|
299 //立ち上げ時にやるとlocalhostしか取れない
|
76
|
300 if(myHost == null) setMyHostName(getLocalHostName(channel));
|
144
|
301
|
122
|
302 //maxHost を設定。
|
95
|
303 if(setMaxHost(channel, receivedSessionList.getMaxHost())){
|
|
304 sendCommand = new REPCommand();
|
|
305 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
|
|
306 sendCommand.setString(maxHost);
|
|
307 smList.sendExcept(channel, sendCommand);
|
|
308 }
|
144
|
309
|
122
|
310 //SessionListからXMLを生成。
|
|
311 //joinしてきたSessionManagerに対してACKを送信。
|
164
|
312 SessionXMLEncoder sessionlistEncoder = new SessionXMLEncoder(sessionList);
|
78
|
313 sendCommand = new REPCommand();
|
|
314 sendCommand.setCMD(REP.SMCMD_SM_JOIN_ACK);
|
|
315 sendCommand.setString(sessionlistEncoder.sessionListToXML());
|
178
|
316 send.write(sendCommand);
|
144
|
317
|
122
|
318 //その他の SessionManager に対して SMCMD_UPDATEを 送信。
|
78
|
319 sendCommand = new REPCommand();
|
83
|
320 sendCommand.setCMD(REP.SMCMD_UPDATE);
|
78
|
321 sendCommand.setString(receivedCommand.string);
|
|
322 smList.sendExcept(channel, sendCommand);
|
144
|
323
|
160
|
324 }
|
164
|
325 break;
|
144
|
326
|
271
|
327 case SMCMD_SM_JOIN_ACK:
|
144
|
328
|
122
|
329 //XMLからSessionListオブジェクトを生成。
|
82
|
330 SessionXMLDecoder decoder2 = new SessionXMLDecoder();
|
|
331 SessionList receivedSessionList2 = decoder2.decode(receivedCommand.string);
|
144
|
332
|
122
|
333 //maxHostを決定。
|
95
|
334 if(setMaxHost(channel, receivedSessionList2.getMaxHost())){
|
|
335 sendCommand = new REPCommand();
|
|
336 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
|
|
337 sendCommand.setString(maxHost);
|
|
338 smList.sendExcept(channel, sendCommand);
|
|
339 }
|
144
|
340
|
6
|
341 break;
|
144
|
342
|
271
|
343 case SMCMD_UPDATE:
|
200
|
344 {
|
99
|
345 SessionXMLDecoder decoder3 = new SessionXMLDecoder();
|
|
346 SessionList receivedSessionList3 = decoder3.decode(receivedCommand.string);
|
144
|
347
|
200
|
348 //UPDATEコマンドにより送られてきたSessionの情報を追加する
|
|
349 LinkedList<Session> list = receivedSessionList3.getList();
|
|
350 for(Session session : list){
|
|
351 session.getEditorList().get(0).setChannel(channel);
|
|
352 sessionList.add(session);
|
|
353 }
|
|
354
|
|
355 //他のSessionManagerへ中継する
|
99
|
356 smList.sendExcept(channel, receivedCommand);
|
144
|
357
|
200
|
358 //リストのコピーをGUIに渡す
|
|
359 LinkedList<Session> sList = new LinkedList<Session>(sessionList);
|
|
360 LinkedList<Editor> eList = new LinkedList<Editor>(editorList);
|
|
361 //GUIに反映
|
|
362 Runnable doRun = new DoGUIUpdate(sList, eList, gui);
|
279
|
363 gui.invokeLater(doRun);
|
200
|
364 }
|
9
|
365 break;
|
144
|
366
|
271
|
367 case SMCMD_UPDATE_ACK:
|
200
|
368 {
|
164
|
369 if(receivedCommand.sid > sessionList.size()){
|
148
|
370 Editor editor = new Editor(channel);
|
75
|
371 editor.setName(receivedCommand.string);
|
144
|
372
|
158
|
373 Session session = new Session(editor);
|
73
|
374 session.addEditor(editor);
|
144
|
375
|
164
|
376 sessionList.add(session);
|
200
|
377
|
|
378 //リストのコピーをGUIに渡す
|
|
379 LinkedList<Session> sList = new LinkedList<Session>(sessionList);
|
|
380 LinkedList<Editor> eList = new LinkedList<Editor>(editorList);
|
|
381 //GUIに反映
|
|
382 Runnable doRun = new DoGUIUpdate(sList, eList, gui);
|
279
|
383 gui.invokeLater(doRun);
|
73
|
384 }
|
75
|
385 smList.sendToSlave(receivedCommand);
|
200
|
386 }
|
1
|
387 break;
|
144
|
388
|
271
|
389 case SMCMD_CH_MASTER:
|
200
|
390 {
|
122
|
391 //maxHost を設定。
|
95
|
392 if(setMaxHost(channel, receivedCommand.string)){
|
|
393 sendCommand = new REPCommand();
|
|
394 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
|
|
395 sendCommand.setString(maxHost);
|
|
396 smList.sendExcept(channel, sendCommand);
|
|
397 }
|
200
|
398 }
|
95
|
399 break;
|
144
|
400
|
271
|
401 case REPCMD_DELETE:
|
|
402 case REPCMD_INSERT:
|
300
|
403 case REPCMD_NOP:
|
164
|
404 {
|
313
|
405 // sid から Session を取得
|
158
|
406 Session session = getSession(receivedCommand.sid);
|
287
|
407 if (session==null) throw new IOException();
|
313
|
408 // 次のエディタへコマンドを送信する処理
|
304
|
409 Editor editor = session.getEditor(channel);
|
315
|
410 Editor.TranslatorResult r = editor.translate(session.getNextEditor(editor), receivedCommand);
|
|
411 if(r==Editor.TranslatorResult.MERGE_END) {
|
|
412 endMerge(receivedCommand, session, editor);
|
|
413 } else if (r==Editor.TranslatorResult.START_MERGE) {
|
|
414 // マージ中のエディタはコマンドを受け取らない
|
|
415 // ここで止めることによって、SMCMD_START_MERGE_ACK
|
|
416 // が来た時には、editor.writeQueue はemptyになる
|
304
|
417 Editor prevEditor = session.getPrevEditor(editor);
|
315
|
418 setMergeState(prevEditor.getChannel(), session.getSID());
|
313
|
419 }
|
|
420 break;
|
|
421 }
|
|
422 case SMCMD_START_MERGE_ACK:
|
|
423 {
|
|
424 // sid から Session を取得
|
|
425 Session session = getSession(receivedCommand.sid);
|
|
426 if (session==null) throw new IOException();
|
|
427 // マージの処理と次のエディタへコマンドを送信する処理
|
|
428 Editor editor = session.getEditor(channel);
|
315
|
429 if (!editor.merge(editor,receivedCommand)) {
|
|
430 // nothing to do, send END_MERGE
|
|
431 endMerge(receivedCommand, session, editor);
|
164
|
432 }
|
313
|
433 break;
|
164
|
434 }
|
300
|
435 case SMCMD_QUIT:
|
|
436 {
|
|
437 Session session = getSession(receivedCommand.sid);
|
|
438 if (session==null) throw new IOException();
|
|
439 session.sendToNextEditor(channel,receivedCommand);
|
|
440 break;
|
|
441 }
|
|
442 case SMCMD_QUIT_2:
|
|
443 {
|
|
444 Session session = getSession(receivedCommand.sid);
|
|
445 if (session==null) throw new IOException();
|
|
446 Editor editor = session.getEditor(channel);
|
|
447 editor.setQuit2(receivedCommand);
|
|
448 break;
|
|
449 }
|
213
|
450 default:
|
|
451 assert(false);
|
|
452 break;
|
|
453
|
144
|
454 }
|
|
455 }
|
|
456
|
315
|
457 private void endMerge(REPCommand receivedCommand, Session session,
|
|
458 Editor editor) {
|
|
459 REPCommand mergeEnd = new REPCommand(REP.SMCMD_END_MERGE,receivedCommand.sid,editor.getEID(),editor.seq(),0,"");
|
|
460 editor.send(mergeEnd);
|
|
461 Editor prevEditor = session.getPrevEditor(editor);
|
|
462 setNormalState(prevEditor.getChannel(), session.getSID());
|
|
463 }
|
|
464
|
259
|
465 private void updateGUI() {
|
212
|
466 //リストのコピーをGUIに渡す
|
|
467 LinkedList<Session> sList = new LinkedList<Session>(sessionList);
|
|
468 LinkedList<Editor> eList = new LinkedList<Editor>(editorList);
|
|
469 //GUIに反映
|
|
470 Runnable doRun = new DoGUIUpdate(sList, eList, gui);
|
279
|
471 gui.invokeLater(doRun);
|
212
|
472 }
|
|
473
|
169
|
474 private void setNormalState(REPSocketChannel<REPCommand> channel, int sid) {
|
|
475 SelectionKey key = channel.keyFor(selector);
|
308
|
476 key.attach(normalHandler);
|
169
|
477 }
|
|
478
|
167
|
479 private void setMergeState(REPSocketChannel<REPCommand> channel, int sid) {
|
|
480 SelectionKey key = channel.keyFor(selector);
|
308
|
481 key.attach(handlerInMerge);
|
164
|
482 }
|
|
483
|
160
|
484 private Editor getEditor(String hostport) {
|
178
|
485 for(Editor editor : editorList){
|
|
486 if(editor.getHost() == hostport){
|
|
487 return editor;
|
|
488 }
|
|
489 }
|
|
490 return null;
|
|
491 }
|
|
492
|
313
|
493 public Session getSession(int sid) {
|
144
|
494 for(Session session : sessionList){
|
|
495 if(session.getSID() == sid) return session;
|
|
496 }
|
|
497 return null;
|
0
|
498 }
|
83
|
499
|
224
|
500 private boolean setMaxHost(REPSocketChannel<REPCommand> channel, String maxHost2) {
|
179
|
501 if(maxHost.compareTo(maxHost2) > 0){
|
|
502 return false;
|
|
503 }else{
|
|
504 maxHost = maxHost2;
|
|
505 return true;
|
|
506 }
|
139
|
507 }
|
|
508
|
76
|
509 private void setMyHostName(String localHostName) {
|
308
|
510 myHost = localHostName + receive_port;
|
81
|
511 if(maxHost == null) {
|
|
512 maxHost = myHost;
|
|
513 }
|
164
|
514 setHostToEditor(myHost);
|
|
515 }
|
|
516
|
|
517 private void setHostToEditor(String myHost2) {
|
|
518 for(Editor editor : editorList){
|
|
519 editor.setHost(myHost2);
|
|
520 }
|
76
|
521 }
|
0
|
522
|
|
523 public static void main(String[] args) throws InterruptedException, IOException {
|
191
|
524
|
101
|
525 int port = DEFAULT_PORT;
|
|
526 int port_s = DEFAULT_PORT;
|
300
|
527 //System.setProperty("file.encoding", "UTF-8");
|
82
|
528 if(args.length > 0){
|
39
|
529 port = Integer.parseInt(args[0]);
|
95
|
530 port_s = Integer.parseInt(args[1]);
|
0
|
531 }
|
308
|
532 receive_port = port;
|
|
533 parent_port = port_s;
|
281
|
534 SessionManager sm = new SessionManager();
|
280
|
535 sm.init(port,new SessionManagerGUIimpl(sm));
|
|
536
|
191
|
537
|
0
|
538 }
|
|
539
|
178
|
540 public void connectSession(String host) {
|
101
|
541 int port = DEFAULT_PORT;
|
308
|
542 port = parent_port;
|
1
|
543 InetSocketAddress addr = new InetSocketAddress(host, port);
|
|
544 try {
|
186
|
545 REPSocketChannel<REPCommand> sessionchannel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
|
1
|
546 sessionchannel.configureBlocking(true);
|
|
547 sessionchannel.connect(addr);
|
6
|
548 while(!sessionchannel.finishConnect()){
|
77
|
549 System.out.print("test afro");
|
6
|
550 }
|
|
551 System.out.println("");
|
229
|
552 registerChannel(sessionchannel, SelectionKey.OP_READ);
|
45
|
553
|
77
|
554 sm_join(sessionchannel);
|
45
|
555
|
1
|
556 }catch (IOException e) {
|
|
557 e.printStackTrace();
|
|
558 }
|
|
559 }
|
77
|
560
|
164
|
561 private void sm_join(REPSocketChannel<REPCommand> channel){
|
79
|
562
|
122
|
563 //SM_JOINコマンドを生成。
|
77
|
564 REPCommand command = new REPCommand();
|
|
565 command.setCMD(REP.SMCMD_SM_JOIN);
|
79
|
566
|
122
|
567 //hostnameをセット。
|
82
|
568 setMyHostName(getLocalHostName(channel));
|
|
569
|
122
|
570 //XMLを生成。送信コマンドにセット。
|
164
|
571 SessionXMLEncoder encoder = new SessionXMLEncoder(sessionList);
|
77
|
572 String string = encoder.sessionListToXML();
|
|
573 command.setString(string);
|
|
574
|
122
|
575 //SM_JOINコマンドを送信。
|
186
|
576 channel.write(command);
|
122
|
577 //SessionManagerのListに追加。
|
77
|
578 smList.add(channel);
|
|
579 }
|
2
|
580
|
271
|
581 private String getLocalHostName(REPSocketChannel<?> channel) {
|
74
|
582 String host = null;
|
|
583 host = channel.socket().getLocalAddress().getHostName();
|
|
584 return host;
|
|
585 }
|
|
586
|
250
|
587 public void selectSession(SelectButtonEvent event) {
|
|
588 REPSocketChannel<REPCommand> channel = event.getEditorChannel();
|
|
589 int sid = event.getSID();
|
|
590 int eid = event.getEID();
|
164
|
591 Session session = getSession(sid);
|
227
|
592
|
250
|
593 Editor editor = editorList.get(eid);
|
227
|
594 if(editor == null){
|
|
595 System.out.println("SessionManager.selectSession():editor = " + editor);
|
|
596 return;
|
|
597 }
|
|
598
|
|
599 session.addEditor(editor);
|
|
600
|
304
|
601 System.out.println("SessionManager.session.hasOnwer="+session.hasOwner());
|
158
|
602 if(session.hasOwner()){
|
107
|
603 REPCommand sendCommand = new REPCommand();
|
|
604 sendCommand.setCMD(REP.SMCMD_JOIN_ACK);
|
148
|
605 sendCommand.setEID(editor.getEID());
|
107
|
606 sendCommand.setSID(sid);
|
286
|
607 sendCommand.string = "";
|
186
|
608 channel.write(sendCommand);
|
107
|
609 }else {
|
250
|
610 sid = event.getSID();
|
227
|
611 editor = new Editor(channel);
|
107
|
612 editor.setHost(myHost);
|
164
|
613 session = getSession(sid);
|
107
|
614 session.addEditor(editor);
|
|
615
|
158
|
616 Editor owner = session.getOwner();
|
107
|
617
|
|
618 REPCommand command = new REPCommand();
|
|
619 command.setCMD(REP.SMCMD_SELECT);
|
|
620 command.setSID(sid);
|
178
|
621 command.setString(editor.getHost());
|
107
|
622 owner.send(command);
|
|
623 }
|
8
|
624 }
|
122
|
625
|
144
|
626 public void addWaitingCommand(PacketSet set) {
|
212
|
627 waitingCommandInMerge.add(set);
|
144
|
628 }
|
148
|
629
|
222
|
630 public void buttonPressed(SessionManagerEvent event) {
|
|
631 try {
|
308
|
632 waitingEventQueue.put(event);
|
222
|
633 } catch (InterruptedException e) {}
|
|
634 selector.wakeup();
|
|
635 }
|
281
|
636
|
|
637 public void syncExec(SessionManagerEvent event) {
|
|
638 try {
|
308
|
639 waitingEventQueue.put(event);
|
281
|
640 } catch (InterruptedException e) {
|
|
641 }
|
|
642 }
|
222
|
643
|
259
|
644 public void closeSession(SessionManagerEvent event) {
|
|
645 Session session = ((CloseButtonEvent) event).getSession();
|
|
646 session.closeSession();
|
|
647 sessionList.remove(session);
|
|
648 updateGUI();
|
|
649 }
|
|
650
|
274
|
651 public void remove(REPSocketChannel<REPCommand> channel) {
|
|
652 for(Session s:sessionList) {
|
|
653 if (s.deleteEditor(channel)) {
|
|
654 return ;
|
|
655 }
|
|
656 }
|
|
657 assert(false);
|
|
658 // can be other session manager? what should I do?
|
|
659 }
|
|
660
|
0
|
661 }
|