Mercurial > hg > RemoteEditor > REPSessionManager
changeset 302:4ee012f19855
*** empty log message ***
author | kono |
---|---|
date | Wed, 01 Oct 2008 17:03:20 +0900 |
parents | d12aac5ab798 |
children | 41f05c8ff02b |
files | rep/Editor.java test/sematest/TestEditor.java |
diffstat | 2 files changed, 43 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/rep/Editor.java Wed Oct 01 16:51:01 2008 +0900 +++ b/rep/Editor.java Wed Oct 01 17:03:20 2008 +0900 @@ -105,7 +105,7 @@ return true; }else{ System.err.println("Editor.checkReturnedCommand() : command = " + command); - assert(false); + //assert(false); } } return false; @@ -125,6 +125,7 @@ return true; } else if (quit2!=null && sentList.size()==0) { myChannel.write(quit2); + quit2 = null; return true; } return false;
--- a/test/sematest/TestEditor.java Wed Oct 01 16:51:01 2008 +0900 +++ b/test/sematest/TestEditor.java Wed Oct 01 17:03:20 2008 +0900 @@ -67,6 +67,9 @@ } public void run(){ + /* + * Create Socket and connect to the session manager + */ try { channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker()); } catch (IOException e) { return; } @@ -79,6 +82,7 @@ } catch (IOException e) { return; } ns.writeLog("successes to connect "+name); /* + * Start editor main loop * public REPCommand(REP cmd,int sid,int eid, int seq, int lineno, String string) */ try { @@ -87,6 +91,9 @@ } } + /* + * Editor main loop with input lock + */ private void mainloop() throws IOException { channel.configureBlocking(false); @@ -101,7 +108,7 @@ continue; } else if (selector.select(timeout)<=0) { if (syncCounter>0) { - syncText(); + syncText(); // send the master editor buffer to clients. } userInput(); } else { @@ -111,38 +118,53 @@ } private void syncText() { + /* + * Send delete/insert one at a time to synchronize + * all clients. SYNC is requested by the session manager. + */ if (syncCounter>text.size()) { syncCounter=0; } else { int i=syncCounter-1; REPCommand del = new REPCommand(REP.REPCMD_DELETE,sid,eid,0,i, text.get(i)); REPCommand ins = new REPCommand(REP.REPCMD_INSERT,sid,eid,0,i, text.get(i)); - sendCommand(del); - sendCommand(ins); + sendCommand(del,seq++); + sendCommand(ins,seq++); syncCounter++; } } + /* + * Simulate User Input + */ private void userInput() { REPCommand cmd = cmds.poll(); if (cmd!=null) { switch(cmd.cmd) { case REPCMD_INSERT: text.insert(cmd.lineno, cmd.string); - sendCommand(cmd); + sendCommand(cmd,seq++); break; case REPCMD_DELETE: String del = text.delete(cmd.lineno); cmd.setString(del); - sendCommand(cmd); + sendCommand(cmd,seq++); break; case SMCMD_QUIT: + /* + * start termination phase 1 by the master editor. + * after this command do not send any user input. + * clients simply disconnect from the session manager. + */ cmds.clear(); - sendCommand(cmd); + sendCommand(cmd,cmd.seq++); break; case SMCMD_JOIN: case SMCMD_PUT: - sendCommand(cmd); + sendCommand(cmd,seq++); + /* + * To prevent confusion, stop user input until the ack + */ inputLock = true; // wait until ACK break; default: @@ -155,8 +177,8 @@ } - private void sendCommand(REPCommand cmd) { - cmd.setSEQID(seq++); + private void sendCommand(REPCommand cmd,int seq) { + cmd.setSEQID(seq); cmd.setEID(eid); cmd.setSID(sid); ns.writeLog(name +" send "+cmd); @@ -168,7 +190,7 @@ switch(cmd.cmd) { case REPCMD_INSERT : text.insert(cmd.lineno, cmd.string); - sendCommand(cmd); + sendCommand(cmd,cmd.seq); // sendCommand(nop); session manager do this for me break; case REPCMD_INSERT_ACK : @@ -177,7 +199,7 @@ case REPCMD_DELETE : String del = text.delete(cmd.lineno); cmd.setString(del); - sendCommand(cmd); + sendCommand(cmd,cmd.seq); // sendCommand(nop); session manager do this for me break; case REPCMD_DELETE_ACK : @@ -188,8 +210,8 @@ assert(false); break; case REPCMD_NOP : - sendCommand(cmd); - sendCommand(nop); + sendCommand(cmd,cmd.seq); + sendCommand(nop,seq++); break; case SMCMD_JOIN_ACK : sid = cmd.sid; @@ -202,7 +224,7 @@ inputLock = false; break; case SMCMD_QUIT : - sendCommand(cmd); + sendCommand(cmd,seq); cmds.clear(); break; case SMCMD_QUIT_ACK : @@ -212,7 +234,7 @@ // lock user input during merge (optional) inputLock = hasInputLock; cmd.cmd = REP.SMCMD_START_MERGE_ACK; - sendCommand(cmd); + sendCommand(cmd,seq++); break; case SMCMD_START_MERGE_ACK : assert(false); @@ -221,13 +243,14 @@ inputLock = false; break; case SMCMD_QUIT_2 : - sendCommand(cmd); + sendCommand(cmd,cmd.seq); running = false; break; case SMCMD_SYNC: // start contents sync with newly joined editor - cmd.cmd = REP.SMCMD_SYNC_ACK; sendCommand(cmd); - syncCounter = 1; + cmd.cmd = REP.SMCMD_SYNC_ACK; sendCommand(cmd,cmd.seq); + if (cmd.eid==eid) + syncCounter = 1; break; default: assert(false);