Mercurial > hg > RemoteEditor > REPSessionManager
diff test/sematest/TestEditor.java @ 285:b468f24c3e09
TestEditor
author | kono |
---|---|
date | Sun, 28 Sep 2008 14:50:46 +0900 |
parents | 90965a3bd4f3 |
children | 30c993e89286 |
line wrap: on
line diff
--- a/test/sematest/TestEditor.java Sun Sep 28 14:16:13 2008 +0900 +++ b/test/sematest/TestEditor.java Sun Sep 28 14:50:46 2008 +0900 @@ -2,11 +2,14 @@ import java.io.IOException; import java.net.InetSocketAddress; +import java.nio.channels.SelectionKey; +import java.util.LinkedList; import rep.REP; import rep.REPCommand; import rep.REPCommandPacker; import rep.channel.REPLogger; +import rep.channel.REPSelector; import rep.channel.REPSocketChannel; import test.Text; @@ -19,13 +22,15 @@ private int seq = 0; public static int editorNo = 0; public Text text; + public LinkedList<REPCommand> cmds; + private int eid; + private int sid; + REPSocketChannel<REPCommand> channel; + boolean running = true; + long timeout = 1; static private String[] text1d = { "aaa", "bbb", "ccc", "ddd", "eee", - "fff", "ggg", "hhh", "iii", "jjj", - "kkk", "lll", "mmm", "nnn", "ooo", - "ppp", "qqq", "rrr", "sss", "ttt", - "uuu", "vvv", "www", "xxx", "yyy", "zzz" }; public TestEditor(String name, String _host,int _port, boolean master){ @@ -34,40 +39,82 @@ ns = REPLogger.singleton(); this.master = master; myid = editorNo++; - if (master) + if (master) { text = new Text(text1d); - else + cmds = new LinkedList<REPCommand>(); + cmds.add(new REPCommand(REP.REPCMD_INSERT,0,0,0,0,"m0")); + cmds.add(new REPCommand(REP.REPCMD_DELETE,0,0,0,0,"m0")); + cmds.add(new REPCommand(REP.SMCMD_QUIT,0,0,0,0,"")); + } else { text = new Text(new String[0]); + cmds.add(new REPCommand(REP.REPCMD_INSERT,0,0,0,0,"c0")); + cmds.add(new REPCommand(REP.REPCMD_DELETE,0,0,0,0,"c0")); + cmds.add(new REPCommand(REP.SMCMD_QUIT,0,0,0,0,"")); + } } public void run(){ try { - REPSocketChannel<REPCommand> channel; channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker()); + } catch (IOException e) { return; } - ns.writeLog("try to connect to SessionManager whose ip is "+semaIP+" editor-"+myid, 1); + ns.writeLog("try to connect to SessionManager whose ip is "+semaIP+" editor-"+myid, 1); + try { while (!channel.connect(semaIP)){ ns.writeLog("SeMa not listen to socket yet, wait", 1); - Thread.yield(); } - ns.writeLog("successes to connect editor-"+myid, 1); - REPCommand command; - /* - * public REPCommand(REP cmd,int sid,int eid, int seq, int lineno, String string) - */ - if (master) - command = new REPCommand(REP.SMCMD_PUT,0,0,seq++,0,"master-file"); - else - command = new REPCommand(REP.SMCMD_JOIN,0,0,seq++,0,"editor-"+myid); - channel.write(command); + } catch (IOException e) { return; } + ns.writeLog("successes to connect editor-"+myid, 1); + REPCommand command; + /* + * public REPCommand(REP cmd,int sid,int eid, int seq, int lineno, String string) + */ + if (master) + command = new REPCommand(REP.SMCMD_PUT,0,0,seq++,0,"master-file"); + else + command = new REPCommand(REP.SMCMD_JOIN,0,0,seq++,0,"editor-"+myid); + channel.write(command); - mainloop(channel); + try { + mainloop(); } catch (IOException e) { } } - private void mainloop(REPSocketChannel<REPCommand> channel) { + private void mainloop() throws IOException { + + channel.configureBlocking(false); + REPSelector<REPCommand> selector = REPSelector.create(); + channel.register(selector, SelectionKey.OP_READ); + while(running) { + if (selector.select(timeout)<=0) { + REPCommand cmd = cmds.poll(); + if (cmd!=null) { + text.edit(cmd); + sendCommand(cmd); + } else { + // no more command to send + timeout = 0; + } + } else { + handle(channel.read()); + } + } + } + + private void sendCommand(REPCommand cmd) { + cmd.setSEQID(seq++); + cmd.setEID(eid); + cmd.setSID(sid); + ns.writeLog("editor-"+myid+" send "+cmd); + channel.write(cmd); + } + + private void handle(REPCommand cmd) { + ns.writeLog("editor-"+myid+" read "+cmd); + switch(cmd.cmd) { + } } }