Mercurial > hg > RemoteEditor > REPSessionManager
comparison rep/SessionManager.java @ 313:0585fd2410b8 single-insert-command
Single Insert Command worked.
author | kono |
---|---|
date | Sun, 05 Oct 2008 22:36:24 +0900 |
parents | c5be84d53c7f |
children | 20fb70068089 |
comparison
equal
deleted
inserted
replaced
312:f39a8045175d | 313:0585fd2410b8 |
---|---|
87 editorList = new LinkedList<Editor>(); | 87 editorList = new LinkedList<Editor>(); |
88 waitingCommandInMerge = new LinkedList<PacketSet>(); | 88 waitingCommandInMerge = new LinkedList<PacketSet>(); |
89 | 89 |
90 | 90 |
91 } | 91 } |
92 | |
93 /* | |
94 * We wrote everything in one thread, but we can assign | |
95 * one thread for each communication channel and GUI event. | |
96 */ | |
92 | 97 |
93 public void mainLoop() throws IOException { | 98 public void mainLoop() throws IOException { |
94 while(true){ | 99 while(true){ |
95 SessionManagerEvent e; | 100 if (checkInputEvent() || |
96 while((e = waitingEventQueue.poll())!=null){ | 101 checkWaitingWrite() || |
97 e.exec(); | 102 checkWaitingCommandInMerge()) { |
98 } | 103 // try to do fair execution for waiting task |
99 for(Session s:sessionList) { | 104 if(selector.selectNow() > 0) select(); |
100 for(Editor editor: s.getEditorList()) | 105 continue; |
101 if (editor.doWaitingWrite()) break; | 106 } |
102 } | 107 // now we can wait for input packet or event |
103 // if there are waiting command during merge operation, do process it | |
104 if(checkWaitingCommandInMerge()){ | |
105 if(selector.selectNow() > 0){ | |
106 select(); | |
107 } | |
108 continue; | |
109 } | |
110 selector.select(); | 108 selector.select(); |
111 select(); | 109 select(); |
112 } | 110 } |
111 } | |
112 | |
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; | |
113 } | 128 } |
114 | 129 |
115 /** | 130 /** |
116 * Check waiting command in merge | 131 * Check waiting command in merge |
117 * @return true if there is a processed waiting command | 132 * @return true if there is a processed waiting command |
385 | 400 |
386 case REPCMD_DELETE: | 401 case REPCMD_DELETE: |
387 case REPCMD_INSERT: | 402 case REPCMD_INSERT: |
388 case REPCMD_NOP: | 403 case REPCMD_NOP: |
389 { | 404 { |
390 //sid から Session を取得 | 405 // sid から Session を取得 |
391 Session session = getSession(receivedCommand.sid); | 406 Session session = getSession(receivedCommand.sid); |
392 if (session==null) throw new IOException(); | 407 if (session==null) throw new IOException(); |
393 //マージの処理と次のエディタへコマンドを送信する処理 | 408 // 次のエディタへコマンドを送信する処理 |
394 Editor editor = session.getEditor(channel); | 409 Editor editor = session.getEditor(channel); |
395 boolean old = editor.isMerging(); | 410 boolean old = editor.isMerging(); |
396 session.translate(channel, receivedCommand); | 411 session.translate(channel, receivedCommand); |
397 boolean newState = editor.isMerging(); | 412 if(editor.isMerging()!=old){ |
398 if (old!=newState) { | 413 assert(old==false); |
399 // prevEditor なのは変だと思うが... | 414 REPCommand mergeEnd = new REPCommand(REP.SMCMD_END_MERGE,receivedCommand.sid,editor.getEID(),editor.seq(),0,""); |
415 editor.send(mergeEnd); | |
400 Editor prevEditor = session.getPrevEditor(editor); | 416 Editor prevEditor = session.getPrevEditor(editor); |
417 setNormalState(prevEditor.getChannel(), session.getSID()); | |
418 } | |
419 break; | |
420 } | |
421 case SMCMD_START_MERGE_ACK: | |
422 { | |
423 // sid から Session を取得 | |
424 Session session = getSession(receivedCommand.sid); | |
425 if (session==null) throw new IOException(); | |
426 // マージの処理と次のエディタへコマンドを送信する処理 | |
427 Editor editor = session.getEditor(channel); | |
428 if (editor.merge(receivedCommand)) { | |
401 //マージ中のエディタはコマンドを受け取らない | 429 //マージ中のエディタはコマンドを受け取らない |
402 // この代入は状態が変わったときだけ行えば良い。毎回、new するのは変 | 430 Editor prevEditor = session.getPrevEditor(editor); |
403 if(editor.isMerging()){ | 431 setMergeState(prevEditor.getChannel(), session.getSID()); |
404 //Handlerを切り替える | 432 } |
405 setMergeState(prevEditor.getChannel(), session.getSID()); | 433 break; |
406 }else { | 434 } |
407 setNormalState(prevEditor.getChannel(), session.getSID()); | |
408 } | |
409 } | |
410 } | |
411 break; | |
412 case SMCMD_QUIT: | 435 case SMCMD_QUIT: |
413 { | 436 { |
414 Session session = getSession(receivedCommand.sid); | 437 Session session = getSession(receivedCommand.sid); |
415 if (session==null) throw new IOException(); | 438 if (session==null) throw new IOException(); |
416 session.sendToNextEditor(channel,receivedCommand); | 439 session.sendToNextEditor(channel,receivedCommand); |
457 } | 480 } |
458 } | 481 } |
459 return null; | 482 return null; |
460 } | 483 } |
461 | 484 |
462 public Editor getEditor(REPSocketChannel<REPCommand> channel){ | 485 public Session getSession(int sid) { |
463 for(Editor editor : editorList){ | |
464 if(editor.getChannel() == channel){ | |
465 return editor; | |
466 } | |
467 } | |
468 return null; | |
469 } | |
470 | |
471 private Session getSession(int sid) { | |
472 for(Session session : sessionList){ | 486 for(Session session : sessionList){ |
473 if(session.getSID() == sid) return session; | 487 if(session.getSID() == sid) return session; |
474 } | 488 } |
475 return null; | 489 return null; |
476 } | 490 } |