Mercurial > hg > RemoteEditor > Eclipse
changeset 75:ea4a1eec3b36
It's NOT completed.
author | kent |
---|---|
date | Thu, 08 Nov 2007 19:55:21 +0900 |
parents | d44d734502da |
children | f07d649cae30 |
files | src/pathfinder/EditorEmulator.java src/sample/merge/Merger.java src/sample/merge/Translater.java |
diffstat | 3 files changed, 296 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pathfinder/EditorEmulator.java Thu Nov 08 19:55:21 2007 +0900 @@ -0,0 +1,66 @@ +package pathfinder; + +import java.util.Random; + +import remoteeditor.command.REPCommand; +import remoteeditor.network.REP; +import sample.merge.Translater; + +public class EditorEmulator { + static public int cmdNO[] = { REP.REPCMD_INSERT, REP.REPCMD_REPLACE, REP.REPCMD_DELETE }; + private int eid; + private int seq; + private Random rand; + private int MAX_CMD; + private int ODDS; + public NetworkSimulator<REPCommand> ns; + public ChannelSimulator<REPCommand> cs; + public Translater translater; + public Text text; + + public EditorEmulator(int id, NetworkSimulator<REPCommand> _ns, int max_cmd, int odds) { + eid = id; + ns = _ns; + MAX_CMD = max_cmd; + ODDS = odds; + rand = new Random(); + translater = new Translater(eid); + } + public EditorEmulator(int id, NetworkSimulator<REPCommand> _ns) { + this(id, _ns, 3, 10); + } + + public void init(){ + cs = ns.connect(); + } + + public void run(){ + REPCommand cmd; + System.out.println("Editor"+eid+" start."); + + while(true){ + cmd = cs.read(); + if (cmd!=null) cmd = translater.transReceiveCmd(cmd); + if (cmd!=null) edit(cmd); + } + + System.out.println("Editor"+eid+" finish."); + } + + private REPCommand genRandomCmd(){ + if (seq>MAC_CMD || rand.nextInt(100)>ODDS) return null; + + String str = new String("inserted or replaced by editor"+eid); + return new REPCommand(cmdNO[rand.nextInt(2)], + 0, eid, seq++, + rand.nextInt(text.size()), //size-1? + str.length(), str); + } + + private void edit(REPCommand cmd){ + if (cmd.cmd==REP.REPCMD_INSERT) text.insert(cmd.lineno, cmd.string); + else if (cmd.cmd==REP.REPCMD_REPLACE) text.replace(cmd.lineno, cmd.string); + else if (cmd.cmd==REP.REPCMD_DELETE) text.delete(cmd.lineno); + else assert false; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sample/merge/Merger.java Thu Nov 08 19:55:21 2007 +0900 @@ -0,0 +1,61 @@ +package sample.merge; + +import remoteeditor.command.REPCommand; +import remoteeditor.network.*; + +public class Merger { + private REPPacketReceive receiver; + private REPPacketSend sender; + + private RemoteEditor; + private Translater translater; + + public Merger(){ + translater = new Translater(); + } + + public void pushRemoteCmd(REPCommand cmd){ + cmd = translater.transReceiveCmd(cmd); + if (cmd==null) return; + + sender.send(cmd); + edit(cmd); + } + + public void pushEditorCmd(REPCommand cmd){ + cmd = translater.transSendCmd(cmd); + sender.send(cmd); + } + + private void edit(REPCommand cmd){ + if (cmd.cmd==REP.REPCMD_NOP) return; + else if (cmd.cmd==REP.REPCMD_INSERT) + ; + else if (cmd.cmd==REP.REPCMD_DELETE) + ; + else if (cmd.cmd==REP.REPCMD_REPLACE) + ; + + editor.changeText( , ); + } +} + +/* + + +-----------------+ + pushEditor------> | Merger | <------ pushRemoteCmd + | | +Editor.changeText<------ | +-------+| ------> REPPacketSend + +--+-----+Trans- ++ + ^ | later| + +---->| | + +-------+ + + ------------- + pushEditorCmd --> | | <-- pushRemoteCmd + | Merger | + editor. <-- | | --> REPPacketSend.send + ------------- + + +*/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sample/merge/Translater.java Thu Nov 08 19:55:21 2007 +0900 @@ -0,0 +1,169 @@ +package sample.merge; + +import java.util.LinkedList; + +import remoteeditor.command.REPCommand; +import remoteeditor.network.REP; + +public class Translater { + //List <REPCommand> userList; + //List <REPCommand> tokenList; + private LinkedList<REPCommand> cmdList; + public int eid; + + public Translater(int _eid){ + eid = _eid; + cmdList = new LinkedList<REPCommand>(); + } + + /** + * Translate cmd When the editor send REPCommand. + * but now, Only adding cmd to the queue is available. + * @param cmd + * @return translated command. + */ + public REPCommand transSendCmd(REPCommand cmd){ + cmdList.add(cmd); + return cmd; + } + /** + * Dequeue command cmd that was returned. + * @param cmd + */ + public void catchOwnCommand(REPCommand cmd){ + // ringである以上、戻ってきたコマンドは確実にキューの先頭にある事を期待している + REPCommand cmd0 = cmdList.poll(); + assert cmd.eid==eid; + assert cmd0.seq!=cmd.seq; + // とりあえず今のところは何もしない事になっている + } + /** + * Translate Command cmd that was received from SeMa. + * @param cmd the command to be translated. + * @return translated commannd. + */ + public REPCommand transReceiveCmd(REPCommand cmd){ + if (cmd.eid==eid){ + catchOwnCommand(cmd); + return null; + } + // 拡張for構文、順序は保証されるの? 保証されないとダメ。 + for(REPCommand cmd0: cmdList){ + if (cmd0.lineno>cmd.lineno) continue; + else if (cmd0.lineno==cmd.lineno){ + // 既に送ったCommandと行番号が重なる限り、繰り返し変換する + cmd = translate(cmd, cmd0); + } + // ずれたlinenoを合わせる + else if (cmd0.cmd==REP.REPCMD_INSERT) cmd.lineno++; + else if (cmd0.cmd==REP.REPCMD_DELETE) cmd.lineno--; + } + return cmd; + } + + /* + * + * 比較時に行番号が重なったときの処理は以下の表。 + * なるべくテキストが残るようにしている。 + * 0 -- なにもしない + * +1 -- 行番号を +1 + * INS -- コマンド id を 'REPCMD_INSERT' にする。 + * NOP -- コマンドをNOP(何もしない)にする。 + * ?* -- TOKENがMasterを通っていなければ + * REMOTE + * | i | r | d + * ---|------------------ + * U i | +1* | +1 | +1 + * S ---|------------------ + * E r | 0 | NOP*| NOP + * R ---|------------------ + * d | 0 | INS | NOP + * + * + */ + private REPCommand translate(REPCommand Rcmd, REPCommand Lcmd){ + assert Rcmd.lineno==Lcmd.lineno; + int Lird = Lcmd.cmd; + int Rird = Rcmd.cmd; + + if (Lird==REP.REPCMD_INSERT){ + if (Rird==REP.REPCMD_INSERT){ + if (!Rcmd.throughMaster) Rcmd.lineno++; + }else if (Rird==REP.REPCMD_REPLACE){ + Rcmd.lineno++; + }else if (Rird==REP.REPCMD_DELETE){ + Rcmd.lineno++; + } + }else if (Lird==REP.REPCMD_REPLACE){ + if (Rird==REP.REPCMD_INSERT){ + }else if (Rird==REP.REPCMD_REPLACE){ + if (!Rcmd.throughMaster) Rcmd.cmd=REP.REPCMD_NOP; + }else if (Rird==REP.REPCMD_DELETE){ + Rcmd.cmd = REP.REPCMD_NOP; + } + }else if (Lird==REP.REPCMD_DELETE){ + if (Rird==REP.REPCMD_INSERT){ + }else if (Rird==REP.REPCMD_REPLACE){ + Rcmd.cmd = REP.REPCMD_INSERT; + }else if (Rird==REP.REPCMD_DELETE){ + Rcmd.cmd = REP.REPCMD_NOP; + } + } + + return Rcmd; + +/* + // あとで整形しよう + if (Lcmd.cmd==REP.REPCMD_INSERT && Rcmd.cmd==REP.REPCMD_INSERT){ + if (!Rcmd.throughMaster) Rcmd.lineno++; + } + if (Lcmd.cmd==REP.REPCMD_DELETE && Rcmd.cmd==REP.REPCMD_DELETE){ + Rcmd.cmd = REP.REPCMD_NOP; + } + if (Lcmd.cmd==REP.REPCMD_REPLACE && Rcmd.cmd==REP.REPCMD_REPLACE){ + if (!Rcmd.throughMaster) Rcmd.cmd=REP.REPCMD_NOP; + } + + if (Lcmd.cmd==REP.REPCMD_INSERT && Rcmd.cmd==REP.REPCMD_DELETE){ + Rcmd.lineno++; + } + if (Lcmd.cmd==REP.REPCMD_DELETE && Rcmd.cmd==REP.REPCMD_INSERT){ + } + + if (Lcmd.cmd==REP.REPCMD_REPLACE && Rcmd.cmd==REP.REPCMD_DELETE){ + Rcmd.cmd = REP.REPCMD_NOP; + } + if (Lcmd.cmd==REP.REPCMD_DELETE && Rcmd.cmd==REP.REPCMD_REPLACE){ + Rcmd.cmd = REP.REPCMD_INSERT; + } + + if (Lcmd.cmd==REP.REPCMD_INSERT && Rcmd.cmd==REP.REPCMD_REPLACE){ + Rcmd.lineno++; + } + if (Lcmd.cmd==REP.REPCMD_REPLACE && Rcmd.cmd==REP.REPCMD_INSERT){ + } + return Rcmd; +*/ + } + + public void setEid(int _eid){ + eid = _eid; + } + + /* + old version ( not ring type) + 0 -- なにもしない + +1 -- 行番号を +1 + i -- コマンド id を 'i' にする。 + X -- コマンドを削除(無視に)する。 + + USER + | i | r | d + ---|-------------------- + T i | 0\+1 | 0\+1 | 0\+1 + O ---|-------------------- + K r | +1\0 | 0\X | i\X + E ---|-------------------- + N d | +1\0 | X\i | X\X + */ +}