Mercurial > hg > RemoteEditor > Eclipse
changeset 128:75fc44fda583
*** empty log message ***
author | pin |
---|---|
date | Tue, 08 Jan 2008 14:39:08 +0900 |
parents | 6f174e2b7503 |
children | e4c21b87223a |
files | src/pathfinder/BlockingQnoSeMa/ChannelSimulator.java src/pathfinder/BlockingQnoSeMa/EditorSimulator.java src/pathfinder/BlockingQnoSeMa/NetworkSimulator.java src/pathfinder/BlockingQnoSeMa/NetworkSimulator_withoutSeMa.java src/pathfinder/BlockingQnoSeMa/TestMerger.java src/pathfinder/BlockingQnoSeMa/Text.java src/remoteeditor/ui/REPSelectWindow.java src/sample/merge/TestEditor.java |
diffstat | 8 files changed, 5 insertions(+), 415 deletions(-) [+] |
line wrap: on
line diff
--- a/src/pathfinder/BlockingQnoSeMa/ChannelSimulator.java Mon Jan 07 05:41:45 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -package pathfinder.BlockingQnoSeMa; - -import java.util.concurrent.BlockingQueue; - -public class ChannelSimulator<P> { - private BlockingQueue<P> qread; - private BlockingQueue<P> qwrite; - - public ChannelSimulator(BlockingQueue<P> _a, BlockingQueue<P> _b){ - qread = _a; - qwrite = _b; - } - - public P read(){ - try { - return qread.take(); - } catch (InterruptedException e) { - e.printStackTrace(); - return null; - } - } - - public boolean write(P p){ - try { - qwrite.put(p); - return true; - } catch (InterruptedException e) { - e.printStackTrace(); - return false; - } - } - - public BlockingQueue<P> getReadQ(){ - return qread; - } - public BlockingQueue<P> getWriteQ(){ - return qwrite; - } - public void setReadQ(BlockingQueue<P> bq){ - qread = bq; - } - public void setWriteQ(BlockingQueue<P> bq){ - qwrite = bq; - } - - public boolean readQisEmpty() { - return qread.isEmpty(); - } - public boolean writeQisEmpty() { - return qwrite.isEmpty(); - } -}
--- a/src/pathfinder/BlockingQnoSeMa/EditorSimulator.java Mon Jan 07 05:41:45 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,106 +0,0 @@ -package pathfinder.BlockingQnoSeMa; - -import java.util.Queue; - -import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; -import sample.merge.Translater; -import sample.merge.TranslaterImp1; - -public class EditorSimulator extends Thread{ - private int eid; - private int seq; - private boolean isOwner; - private NetworkSimulator<REPCommand> ns; - private ChannelSimulator<REPCommand> cs; - private Queue<REPCommand> CmdList; - private TranslaterImp1 translater; - private Text text; - private boolean running=true; - - public EditorSimulator(int _eid, NetworkSimulator<REPCommand> _ns, Queue<REPCommand> q, String _name) { - super(_name); - eid = _eid; - ns = _ns; - CmdList = q; - translater = new TranslaterImp1(_eid); - text = new Text(); - cs = ns.connect(); - } - - public void setOwner(boolean f){ - isOwner = f; - } - synchronized public void finish(){ - running = false; - } - - public void run(){ - System.out.println("Editor"+eid+" start."); - - // Send All Command that is included CmdList. - sendAllCommand(); - - // MainLoop, - while(running){ - REPCommand cmd = cs.read(); - REPCommand[] cmds; - - //終了条件 - if (cmd.eid==eid && cmd.cmd==REP.SMCMD_QUIT){ - System.out.println("\tEditor"+eid+" catch QUIT command emited by itself."); - translater.transReceiveCmd(cmd); - running=false; break; - } - System.out.println("\tEditor"+eid+" catch command:>> "+cmd.toString()); - cmds = translater.transReceiveCmd(cmd); - if (cmds==null) continue; - - for (int i=0; i<cmds.length; i++){ - cmd = cmds[i]; - if (isOwner) cmd.setThroughMaster(true); - text.edit(cmd); - cs.write(cmd); - } - } - - System.out.println("Editor"+eid+" finish."); - } - - private void sendOneCommand() { - REPCommand[] cmds; - REPCommand cmd = CmdList.poll(); - cmd.eid = eid; - cmds = translater.transSendCmd(cmd); - cmd.setString("this is inserted or replaced by Editor"+cmd.eid+":"+cmd.seq); - - if (isOwner) cmd.setThroughMaster(true); - text.edit(cmd); - cs.write(cmd); - } - private void sendAllCommand() { - REPCommand[] cmds; - for (REPCommand cmd: CmdList){ - cmd.seq = seq; - cmd.eid = eid; - cmds = translater.transSendCmd(cmd); - cmd.setString("this is inserted or replaced by Editor"+cmd.eid+":"+cmd.seq); - //if (isOwner) cmd.setThroughMaster(true); - for (int i=0; i<cmds.length; i++){ - text.edit(cmd); - cs.write(cmd); - } - } - // Send Quit Command - cs.write( translater.transSendCmd( new REPCommand(REP.SMCMD_QUIT, 0, eid, seq++, 0, 0, "QUIT by Editor"+eid))); - } -/* - private boolean checkQuit(REPCommand cmd) { - // 最初に全部のコマンドを送信するから、自分のQUITが来るのは最後 - return (cmd.eid==eid && cmd.cmd==REP.SMCMD_QUIT); - } -*/ - public Text getText(){ - return text; - } -} \ No newline at end of file
--- a/src/pathfinder/BlockingQnoSeMa/NetworkSimulator.java Mon Jan 07 05:41:45 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -package pathfinder.BlockingQnoSeMa; - -import java.util.LinkedList; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; - -public class NetworkSimulator<P> { - /** Established connection */ - private LinkedList<ChannelSimulator<P>> connectedList; - - public NetworkSimulator(){ - connectedList = new LinkedList<ChannelSimulator<P>>(); - } - - /** - * Request to connect. - * Client editor use this method to connect SeMa. - * @param cs - */ - public ChannelSimulator<P> connect(){ - ChannelSimulator<P> cs; - if (connectedList.isEmpty()){ - BlockingQueue<P> q = new LinkedBlockingQueue<P>(); - cs = new ChannelSimulator<P>(q, q); - }else{ - BlockingQueue<P> rq = connectedList.getLast().getWriteQ(); - BlockingQueue<P> wq = new LinkedBlockingQueue<P>(); - connectedList.getFirst().setReadQ(wq); - -/* ChannelSimulator<P> lastcs = connectedList.getLast(); - BlockingQueue<P> rq = lastcs.getWriteQ(); - - BlockingQueue<P> wq = new LinkedBlockingQueue<P>(); - ChannelSimulator<P> firstcs = connectedList.getFirst(); - firstcs.setReadQ(wq); -*/ - cs = new ChannelSimulator<P>(rq, wq); - } - - connectedList.addLast(cs); - return cs; - } - - public boolean checkAllCS(){ - for(ChannelSimulator<P> cs: connectedList){ - if(!cs.readQisEmpty()) return false; - } - return true; - } -}
--- a/src/pathfinder/BlockingQnoSeMa/NetworkSimulator_withoutSeMa.java Mon Jan 07 05:41:45 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -package pathfinder.BlockingQnoSeMa; - -import java.util.LinkedList; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; - -public class NetworkSimulator_withoutSeMa<P> { - /** Established connection */ - private LinkedList<ChannelSimulator<P>> connectedList; - - public NetworkSimulator_withoutSeMa(){ - connectedList = new LinkedList<ChannelSimulator<P>>(); - } - - /** - * Request to connect. - * Client editor use this method to connect SeMa. - * @param cs - */ - public ChannelSimulator<P> connect(){ - ChannelSimulator<P> cs; - if (connectedList.isEmpty()){ - BlockingQueue<P> q = new LinkedBlockingQueue<P>(); - cs = new ChannelSimulator<P>(q, q); - }else{ - BlockingQueue<P> rq = connectedList.getLast().getWriteQ(); - BlockingQueue<P> wq = new LinkedBlockingQueue<P>(); - connectedList.getFirst().setReadQ(wq); - -/* ChannelSimulator<P> lastcs = connectedList.getLast(); - BlockingQueue<P> rq = lastcs.getWriteQ(); - - BlockingQueue<P> wq = new LinkedBlockingQueue<P>(); - ChannelSimulator<P> firstcs = connectedList.getFirst(); - firstcs.setReadQ(wq); -*/ - cs = new ChannelSimulator<P>(rq, wq); - } - - connectedList.addLast(cs); - return cs; - } - - public boolean checkAllCS(){ - for(ChannelSimulator<P> cs: connectedList){ - if(!cs.readQisEmpty()) return false; - } - return true; - } -}
--- a/src/pathfinder/BlockingQnoSeMa/TestMerger.java Mon Jan 07 05:41:45 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,92 +0,0 @@ -package pathfinder.BlockingQnoSeMa; - -import java.util.LinkedList; -import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; - -public class TestMerger { - static public int cmdNO[] = { REP.REPCMD_INSERT, REP.REPCMD_REPLACE, REP.REPCMD_DELETE }; - private NetworkSimulator<REPCommand> ns; - private LinkedList<EditorSimulator> editors; - - public TestMerger(){ - ns = new NetworkSimulator<REPCommand>(); - editors = new LinkedList<EditorSimulator>(); - } - - public static void main(String[] args){ - TestMerger tm; - int i = (args.length>0) ? Integer.parseInt(args[0]) : 2; - System.out.println("number of Editor = "+i); - int j = (args.length>1) ? Integer.parseInt(args[1]) : 3; - System.out.println("number of Packet = "+i); - tm = new TestMerger(); - - tm.createEditors(i, j); - tm.startTest(); - - tm.printAllTexts(); - //if (!tm.checkCS()) - // System.out.println("Error!! :some ChannelSimulator still have packet!"); - if (!tm.checkEquality()) - System.out.println("Error!! :all Editor's text is NOT mutch!"); - assert tm.checkEquality(); - } - - private void startTest() { - for (EditorSimulator ee: editors){ - ee.start(); - } - - for (EditorSimulator ee: editors){ - try { - ee.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - - private void createEditors(int ne, int np){ - for (int i=0; i<ne; i++){ - LinkedList<REPCommand> cmds = new LinkedList<REPCommand>(); - // 各エディタが送信するコマンド列を生成 - - for (int j=0; j<np; j++){ - String str = "created by Editor"+i+":"+j; - REPCommand cmd = new REPCommand(REP.REPCMD_INSERT, - 0, i, j, - 10, //Verify.random(text.size()-1), //size-1? - str.length(), str); - cmds.add( cmd); - } - - EditorSimulator ee = new EditorSimulator(i, ns, cmds, "Editor"+i); - if(i==0) ee.setOwner(true); - editors.add(ee); - } - } - - private void printAllTexts(){ - for(EditorSimulator ee: editors){ - System.out.println("--"+ee.getName()+"------------------------"); - ee.getText().printAllText(); - } - } -/* - private boolean checkCS(){ - return ns.checkAllCS(); - } -*/ - private boolean checkEquality(){ - /* - Text ee0 = editors.remove().getText(); - return editors.remove().getText().equals(ee0); - */ - Text text0 = editors.element().getText(); - for(EditorSimulator ee: editors){ - if (!text0.equals(ee.getText())) return false; - } - return true; - } -}
--- a/src/pathfinder/BlockingQnoSeMa/Text.java Mon Jan 07 05:41:45 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,60 +0,0 @@ -package pathfinder.BlockingQnoSeMa; - -import java.util.Arrays; -import java.util.LinkedList; - -import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; - -public class Text { - static private String[] text0 = { - "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" - }; - LinkedList<String> strList; - - public Text(){ - this(Text.text0); - } - public Text(String[] _strings){ - strList = new LinkedList<String>(Arrays.asList(_strings)); - } - - synchronized public void insert(int i, String str){ - assert 0<i && i<strList.size(); - strList.add(i, str); - } - synchronized public void delete(int i){ - assert 0<i && i<strList.size(); - strList.remove(i); - } - synchronized public void replace(int i, String str){ - assert 0<i && i<strList.size(); - strList.set(i, str); - } - public String get(int i){ - assert 0<i && i<strList.size(); - return strList.get(i); - } - public void edit(REPCommand cmd){ - if (cmd.cmd==REP.REPCMD_INSERT) insert(cmd.lineno, cmd.string); - else if (cmd.cmd==REP.REPCMD_REPLACE) replace(cmd.lineno, cmd.string); - else if (cmd.cmd==REP.REPCMD_DELETE) delete(cmd.lineno); - //else assert false; - } - - public int size(){ - return strList.size(); - } - public void printAllText(){ - for( String str: strList){ - System.out.println(str); - } - } - public boolean equals(Text _target){ - return strList.equals(_target.strList); - } -}
--- a/src/remoteeditor/ui/REPSelectWindow.java Mon Jan 07 05:41:45 2008 +0900 +++ b/src/remoteeditor/ui/REPSelectWindow.java Tue Jan 08 14:39:08 2008 +0900 @@ -112,7 +112,7 @@ private void put() { repsend.send(new REPCommand(REP.SMCMD_PUT, mysid, myeid, myseq, 0, filename.length(), filename)); - command = repreceive.unpack(); + command = repreceive.unpackUConv(); mysid = command.sid; } @@ -132,7 +132,7 @@ } public void join(){ repsend.send(new REPCommand(REP.SMCMD_JOIN, 0, 0, 0, 0, 0, "")); - command = repreceive.unpack(); + command = repreceive.unpackUConv(); myeid = command.eid; //System.out.println(command.toString()); StringTokenizer tokenizer = new StringTokenizer(command.string, ",{} ");
--- a/src/sample/merge/TestEditor.java Mon Jan 07 05:41:45 2008 +0900 +++ b/src/sample/merge/TestEditor.java Tue Jan 08 14:39:08 2008 +0900 @@ -98,21 +98,21 @@ public void rep_join(){ repsend.send(new REPCommand(REP.SMCMD_JOIN, 0, 0, myseq, 0, 0, "")); myseq++; - REPCommand command = repreceive.unpack(); + REPCommand command = repreceive.unpackUConv(); myeid = command.eid; trans.setMyEID(myeid); } public void put(){ repsend.send(new REPCommand(REP.SMCMD_PUT, 0, myeid, myseq, 0, 4, "afro")); myseq++; - REPCommand command = repreceive.unpack(); + REPCommand command = repreceive.unpackUConv(); mysid = command.sid; } public void select(){ mysid = 1; repsend.send(new REPCommand(REP.SMCMD_SELECT, mysid, myeid, myseq, 0, 0, "")); myseq++; - REPCommand command = repreceive.unpack(); + REPCommand command = repreceive.unpackUConv(); repreceive.addCommandListener(this); }