Mercurial > hg > RemoteEditor > Eclipse
changeset 202:2e9aacccfbf4
remove pathfinders
author | one |
---|---|
date | Fri, 17 Dec 2010 17:23:19 +0900 |
parents | afa6f235d763 |
children | fce2776071d4 |
files | src/pathfinder/mergetest/DocumentSimulator.java src/pathfinder/mergetest/EditorObject.java src/pathfinder/mergetest/EditorSimulator.java src/pathfinder/mergetest/EditorSimulatorWithoutMerger.java src/pathfinder/mergetest/PacketSet.java src/pathfinder/mergetest/REPHandler.java src/pathfinder/mergetest/REPHandlerDoWaiting.java src/pathfinder/mergetest/REPHandlerImpl.java src/pathfinder/mergetest/REPHandlerInMerge.java src/pathfinder/mergetest/SessionManagerSimulatorWithMerger.java src/pathfinder/mergetest/TestMerger.java src/pathfinder/mergetest/TestMerger2.java src/pathfinder/mergetest/Text.java src/pathfinder/mergetest/UserSimulator.java src/pathfinder/mergetest/channels/ChannelSimulator.java src/pathfinder/mergetest/channels/NetworkSimulator.java src/pathfinder/mergetest/channels/SelectableChannelSimulator.java src/pathfinder/mergetest/channels/SelectionKeySimulator.java src/pathfinder/mergetest/channels/SelectorSimulator.java src/pathfinder/mergetest/channels/ServerChannelSimulator.java src/pathfinder/mergetest/channels/test/testEditor.java src/pathfinder/mergetest/channels/test/testNetworkSimulator.java src/pathfinder/mergetest/channels/test/testSeMa.java src/pathfinder/mergetest/channels/test/testSeMaSlave.java src/pathfinder/mergetest/channels2/NetworkSimulator.java src/pathfinder/mergetest/channels2/SelectableChannelSimulator.java src/pathfinder/mergetest/channels2/SelectionKeySimulator.java src/pathfinder/mergetest/channels2/SelectorSimulator.java src/pathfinder/mergetest/channels2/ServerChannelSimulator.java src/pathfinder/mergetest/channels2/SocketChannelSimulator.java src/pathfinder/mergetest/channels2/test/testEditor.java src/pathfinder/mergetest/channels2/test/testNetworkSimulator.java src/pathfinder/mergetest/channels2/test/testSeMa.java src/pathfinder/mergetest/channels2/test/testSeMaSlave.java src/pathfinder/mergetest/test/RepCommandOptimizeTest.java |
diffstat | 35 files changed, 0 insertions(+), 2765 deletions(-) [+] |
line wrap: on
line diff
--- a/src/pathfinder/mergetest/DocumentSimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -package pathfinder.mergetest; - -public class DocumentSimulator { - -}
--- a/src/pathfinder/mergetest/EditorObject.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,118 +0,0 @@ -package pathfinder.mergetest; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; - -import pathfinder.mergetest.channels.ChannelSimulator; - -import remoteeditor.command.REPCommand; -import sample.merge.TranslaterImp1; - -public class EditorObject { - - int eid; - private ChannelSimulator<REPCommand> channel; - private TranslaterImp1 translater; - private List<REPCommand> sentList; - private List<REPCommand> sendList; - - public EditorObject(int i, ChannelSimulator<REPCommand> cs) { - // TODO Auto-generated constructor stub - eid = i; - channel = cs; - } - - public EditorObject(int i, ChannelSimulator<REPCommand> cs, TranslaterImp1 imp1) { - // TODO Auto-generated constructor stub - eid = i; - channel = cs; - translater = imp1; - sentList = new ArrayList<REPCommand>(); - sendList = new LinkedList<REPCommand>(); - } - - public ChannelSimulator<REPCommand> getChannel() { - // TODO Auto-generated method stub - return channel; - } - - public int getEID() { - // TODO Auto-generated method stub - return eid; - } - - public REPCommand receive(REPCommand command) { - // TODO Auto-generated method stub - - if(command.eid == eid){ - if(checkReturnCommand(command)){ - REPCommand[] cmds = translater.catchOwnCommand(command); - sendMergedCommand(cmds); - return null; - }else{ - sentList.add(command); - } - } - - return command; - } - - private boolean checkReturnCommand(REPCommand command) { - // TODO Auto-generated method stub - - if(sentList.size() > 0){ - if(sentList.get(0).seq == command.seq){ - return true; - } - } - - return false; - } - - private void sendMergedCommand(REPCommand[] cmds) { - // TODO Auto-generated method stub - for(int i = 0; i < cmds.length; i++){ - channel.write(cmds[i]); - } - } - - public void send(REPCommand command) { - // TODO Auto-generated method stub - if(false) System.out.println("send to Editor : " + eid + " : " + command); - if(command !=null){ - //channel.write((P) command); - write(command); - } - } - - public void addWaitCommand(REPCommand command) { - // TODO Auto-generated method stub - sendList.add(command); - } - - public void sendWaitingCommands() { - // TODO Auto-generated method stub - for(REPCommand command : sendList){ - channel.write(pack(command)); - } - sendList.clear(); - } - - private REPCommand pack(REPCommand command) { - // TODO Auto-generated method stub - //P cmd = (P) new REPCommand(command); - return new REPCommand(command); - } - - public void write(REPCommand cmd) { - // TODO Auto-generated method stub - channel.write(pack(cmd)); - } - - public boolean isMerging() { - // TODO Auto-generated method stub - return translater.isMerging(); - } - -}
--- a/src/pathfinder/mergetest/EditorSimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,134 +0,0 @@ -package pathfinder.mergetest; - - - -import pathfinder.mergetest.channels.ChannelSimulator; -import pathfinder.mergetest.channels.NetworkSimulator; - -import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; -import sample.merge.TranslaterImp1; - -public class EditorSimulator extends Thread{ - protected int eid; - protected int seq; - //private boolean isOwner; - protected NetworkSimulator<REPCommand> ns; - protected ChannelSimulator<REPCommand> cs; - //protected Queue<REPCommand> CmdList; - private TranslaterImp1 translater; - protected Text text; - protected boolean running=true; - protected int semaIP; - - public EditorSimulator(int _eid, NetworkSimulator<REPCommand> _ns, String[] strings, String _name) { - super(_name); - eid = _eid; - ns = _ns; - - translater = new TranslaterImp1(_eid); - if(strings != null) text = new Text(strings); - cs = new ChannelSimulator<REPCommand>(ns); - cs.connect(semaIP); - } - - public void setOwner(boolean f){ - //isOwner = f; - } - synchronized public void finish(){ - running = false; - } - - public void run(){ - ns.writeLog("Editor"+eid+" start.", 1); - - // Send All Command that is included CmdList. - //sendAllCommand(); - - // MainLoop, - while(running){ - REPCommand cmd = cs.read(); - /* received Command */ - //*if(eid == 0)*/System.out.println("editor" + eid + ":" + cmd.string + ":"); - - REPCommand[] cmds; - - ns.writeLog("\tEditor"+eid+" catch command from "+cmd.eid+" NO."+cmd.seq, 3); - - if (cmd.eid==eid){ - cmds = translater.catchOwnCommand(cmd); - for (int i=0; i<cmds.length; i++){ - REPCommand tmp = cmds[i]; - ns.writeLog("\t\tEditor"+eid+" edit text. ", 4); - text.edit(tmp); - } - /* 終了条件 */ - if (cmd.cmd==REP.SMCMD_QUIT){ - ns.writeLog("\tEditor"+eid+" catch QUIT command emited by itself.", 2); - running=false; break; - } - } else if (cmd.eid==-1){ - /* 制御プロセスからの指令 */ - ns.writeLog("\tEditor"+eid+" send command.", 2); - if (cmd.cmd==REP.SMCMD_QUIT) - synchronized(ns){ ns.writeLog("send Quit cmd.", 1); } - sendOneCommand(cmd); - } else { - cmds = translater.transReceiveCmd(cmd); - for (int i=0; i<cmds.length; i++){ - cmd = cmds[i]; - ns.writeLog("\t\tEditor"+eid+" edit text and pass Cmd. ", 4); - text.edit(cmd); - cs.write(new REPCommand(cmd)); - } - } - } - - ns.writeLog("Editor"+eid+" finish.", 1); - } - - protected void sendOneCommand(REPCommand cmd) { - REPCommand cmds; - //if (cmd==null) cmd = CmdList.poll(); - if (cmd==null) return; - - //cmd.eid = eid; - cmds = translater.transSendCmd(cmd); - cmd.setString("inserted by Editor"+cmd.eid+":"+cmd.seq); - - //if (isOwner) cmd.setThroughMaster(true); -// for (int i=0; i<cmds.length; i++){ -// text.edit(cmds[i]); -// cs.write(new REPCommand(cmds[i])); -// } - text.edit(cmds); - cs.write(cmds); - Thread.yield(); - } -// private void sendAllCommand() { -// REPCommand[] cmds; -// -// // Send Quit Command -// cmds = translater.transSendCmd( new REPCommand(REP.SMCMD_QUIT, 0, eid, seq++, 0, 0, "QUIT by Editor"+eid)); -// for (int i=0; i<cmds.length; i++){ -// text.edit(cmds[i]); -// cs.write(new REPCommand(cmds[i])); -// } -// -// } -/* - private boolean checkQuit(REPCommand cmd) { - // 最初に全部のコマンドを送信するから、自分のQUITが来るのは最後 - return (cmd.eid==eid && cmd.cmd==REP.SMCMD_QUIT); - } -*/ - public Text getText(){ - return text; - } - - public ChannelSimulator<REPCommand> getChannelforUser(){ - return cs.createConjugatedChannel(); - } - - -} \ No newline at end of file
--- a/src/pathfinder/mergetest/EditorSimulatorWithoutMerger.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,136 +0,0 @@ -package pathfinder.mergetest; - -import pathfinder.mergetest.channels.NetworkSimulator; -import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; - -public class EditorSimulatorWithoutMerger extends EditorSimulator { - - public EditorSimulatorWithoutMerger(int _eid, NetworkSimulator<REPCommand> _ns, String[] strings, String _name) { - super(_eid, _ns, strings, _name); - } - - public void run(){ - ns.writeLog("Editor" + eid + " start.", 1); - - while(running){ - - // MainLoop - while(running){ - - REPCommand cmd = cs.read(); - /* received Command */ - - ns.writeLog("\tEditor"+eid+" catch command from "+cmd.eid+" NO."+cmd.seq, 3); - //if (eid == 0) ns.writeLog("\tEditor"+eid+" catch command from "+cmd.eid+" :"+cmd, 1); - - if(cmd.cmd == REP.SMCMD_QUIT_2){ - cs.write(new REPCommand(cmd)); - running = false; - break; - } - - if (cmd.eid==eid){ - //発行したコマンドが戻ってきた場合 - cs.write(new REPCommand(cmd)); - - } else if (cmd.eid==-1){ - /* 制御プロセスからの指令 */ - ns.writeLog("\tEditor"+eid+" send command.", 2); - if (cmd.cmd==REP.SMCMD_QUIT) { - //sendOneCommand(cmd); - cs.write(cmd); - synchronized(ns){ ns.writeLog("send Quit cmd.", 1); } - continue; - } - - //if(lock) continue; - String replacedText = text.edit(cmd); - sendOneCommand(cmd, replacedText); - - }else if(cmd.eid == -2){ - - /* 終了条件 */ - if (cmd.cmd==REP.SMCMD_QUIT){ - ns.writeLog("\tEditor"+eid+" catch QUIT command emited by itself.", 3); - running=false; break; - }else{ - // Merged Commands. - String replacedText = text.edit(cmd); - returnMergedCommand(cmd, replacedText); - } - } else { - - ns.writeLog("\t\tEditor"+eid+" edit text and pass Cmd. " + " : " + cmd, 3); - //if(eid == 2)ns.writeLog("\t\tEditor"+eid+" edit text and pass Cmd. " + " : " + cmd, 1); - text.edit(cmd); - cs.write(new REPCommand(cmd)); - } - } - - ns.writeLog("Editor"+eid+" finish.", 1); - } - } - - private void returnMergedCommand(REPCommand cmd, String replacedText) { - REPCommand command = new REPCommand(cmd); - if(command.cmd == REP.REPCMD_DELETE)command.setString(replacedText); - cs.write(command); - } - - private void manage(REPCommand cmd) { - // TODO Auto-generated method stub - switch(cmd.cmd){ - case REP.SMCMD_START_MERGE: - lockEdit(true); - break; - case REP.SMCMD_END_MERGE: - lockEdit(false); - break; - default: - break; - } - } - - private void lockEdit(boolean b) { - } - - protected void sendOneCommand(REPCommand cmd, String replacedText){ - - if (cmd==null) return; - - cmd.eid = eid; - cmd.seq = seq++; - - if(cmd.cmd == REP.REPCMD_INSERT){ - //cmd.setString("inserted by Editor"+cmd.eid+":"+cmd.seq); - cs.write(cmd); - - }else if(cmd.cmd == REP.REPCMD_DELETE){ - ///String line = text.get(cmd.lineno); - //cmd.setString(line); - cmd.setString(replacedText); - cs.write(cmd); - - }else if(cmd.cmd == REP.REPCMD_REPLACE){ - - REPCommand deleteCmd = new REPCommand(REP.REPCMD_DELETE, 0, eid, seq-1, cmd.lineno, 0, ""); - //undoCmd.setString(text.get(cmd.lineno)); - deleteCmd.setString(replacedText); - cs.write(deleteCmd); - - //cmd.setString("replaced by Editor"+cmd.eid+":"+cmd.seq); - cmd.setCMD(REP.REPCMD_INSERT); - cs.write(cmd); - - }else if(cmd.cmd == REP.SMCMD_QUIT){ - //cmd.setString("sent by Editor"+cmd.eid+":"+cmd.seq); - cs.write(cmd); - } - - //text.edit(cmd); - - //Thread.yield(); - } - -}
--- a/src/pathfinder/mergetest/PacketSet.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ -package pathfinder.mergetest; - -import pathfinder.mergetest.channels.ChannelSimulator; -import remoteeditor.command.REPCommand; - -public class PacketSet { - - private ChannelSimulator<REPCommand> channel; - private REPCommand packet; - - public PacketSet(ChannelSimulator<REPCommand> channel, REPCommand packet) { - // TODO Auto-generated constructor stub - this.channel = channel; - this.packet = packet; - } - -}
--- a/src/pathfinder/mergetest/REPHandler.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -package pathfinder.mergetest; - -import pathfinder.mergetest.channels.SelectionKeySimulator; -import remoteeditor.command.REPCommand; - -public interface REPHandler { - void handle(SelectionKeySimulator<REPCommand> key); - -}
--- a/src/pathfinder/mergetest/REPHandlerDoWaiting.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,18 +0,0 @@ -package pathfinder.mergetest; - -import pathfinder.mergetest.channels.SelectionKeySimulator; -import remoteeditor.command.REPCommand; - -public class REPHandlerDoWaiting implements REPHandler { - - private SessionManagerSimulatorWithMerger session; - - public REPHandlerDoWaiting(SessionManagerSimulatorWithMerger session) { - this.session = session; - } - - public void handle(SelectionKeySimulator<REPCommand> key) { - session.doWaiting(); - } - -}
--- a/src/pathfinder/mergetest/REPHandlerImpl.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -package pathfinder.mergetest; - -import pathfinder.mergetest.channels.ChannelSimulator; -import pathfinder.mergetest.channels.SelectionKeySimulator; -import remoteeditor.command.REPCommand; - -public class REPHandlerImpl implements REPHandler { - - private SessionManagerSimulatorWithMerger session; - - - public REPHandlerImpl(SessionManagerSimulatorWithMerger session) { - // TODO Auto-generated constructor stub - this.session = session; - } - - public void handle(SelectionKeySimulator<REPCommand> key) { - // TODO Auto-generated method stub - ChannelSimulator<REPCommand> channel = (ChannelSimulator<REPCommand>) key.channel(); - REPCommand packet = channel.read(); - REPCommand command = session.unpack(packet); - session.manage(channel, command); - } - -}
--- a/src/pathfinder/mergetest/REPHandlerInMerge.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -package pathfinder.mergetest; - -import java.util.LinkedList; -import java.util.List; - -import pathfinder.mergetest.channels.ChannelSimulator; -import pathfinder.mergetest.channels.SelectionKeySimulator; - -import remoteeditor.command.REPCommand; - -public class REPHandlerInMerge implements REPHandler { - - private SessionManagerSimulatorWithMerger session; - //List<PacketSet> packetList = new LinkedList<PacketSet>(); - - public REPHandlerInMerge(SessionManagerSimulatorWithMerger session) { - // TODO Auto-generated constructor stub - this.session = session; - } - - public void handle(SelectionKeySimulator<REPCommand> key) { - // TODO Auto-generated method stub - ChannelSimulator<REPCommand> channel = (ChannelSimulator<REPCommand>) key.channel(); - REPCommand packet = channel.read(); - session.addWaitingCommand(new PacketSet(channel, packet)); - REPCommand command = session.unpack(packet); - session.manage(channel, command); - } - -}
--- a/src/pathfinder/mergetest/SessionManagerSimulatorWithMerger.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,282 +0,0 @@ -package pathfinder.mergetest; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; - -import pathfinder.mergetest.channels.*; - -import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; -import sample.merge.TranslaterImp1; - -public class SessionManagerSimulatorWithMerger extends Thread{ - protected boolean running=true; - protected List<ChannelSimulator<REPCommand>> csList; - - private List<TranslaterImp1> transList; - private SelectorSimulator<REPCommand> selector; - private List<EditorObject> editorList; - private int startQuit2; - //private List<LinkedList<REPCommand>> sendList; - private List<PacketSet> waitingList; - protected NetworkSimulator<REPCommand> ns; - protected ServerChannelSimulator<REPCommand> scs; - protected int IP; - - - public SessionManagerSimulatorWithMerger(NetworkSimulator<REPCommand> _ns, int ip) { - IP = ip; - ns = _ns; - transList = new ArrayList<TranslaterImp1>(); - editorList = new ArrayList<EditorObject>(); - waitingList = new LinkedList<PacketSet>(); - selector = new SelectorSimulator<REPCommand>(); - scs = new ServerChannelSimulator<REPCommand>(ns, selector); - scs.bind(IP); - selector.register(scs, SelectionKeySimulator.OP_ACCEPT); - } - - /* - protected void checkAccept(){ - ChannelSimulator<REPCommand> cs; - while((cs=ns.accept())!=null){ - csList.add(cs); - transList.add(new TranslaterImp1(transList.size())); - editorList.add(new EditorObject<REPCommand>(editorList.size(), cs, new TranslaterImp1(editorList.size()))); - //sendList.add(new LinkedList<REPCommand>()); - registerChannel (selector, cs, SelectionKeySimulator.OP_READ); - } - }*/ - - private void registChannel(SelectorSimulator<REPCommand> selector2, ChannelSimulator<REPCommand> cs, int key) { - csList.add(cs); - transList.add(new TranslaterImp1(transList.size())); - editorList.add(new EditorObject(editorList.size(), cs, new TranslaterImp1(editorList.size()))); - REPHandler handler = new REPHandlerImpl(this); - selector.register(cs, SelectionKeySimulator.OP_READ, handler); - } - - public void init(){ - } - - public void run(){ - - ns.writeLog("SessionManager start.", 1); - - try { - mainLoop(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - ns.writeLog("SessionManager finish.", 1); - } - - private void mainLoop() throws IOException { - /* Main Loop */ - while(running){ - - selector.select(); - select(); - } - } - - private void select() { - for(SelectionKeySimulator<REPCommand> key : selector.selectedKeys()){ - - if(key.isAcceptable()){ - ChannelSimulator<REPCommand> channel = (ChannelSimulator<REPCommand>)key.channel(); - channel = channel.accept(); - //REPHandler handler = new REPHandlerImpl(this); - //selector.register(channel, SelectionKeySimulator.OP_READ, handler); - registChannel(selector, channel, SelectionKeySimulator.OP_READ); - - }else if(key.isReadable()){ - - REPHandler handler = (REPHandler)key.attachment(); - handler.handle(key); - - } - } - } - - void manage(ChannelSimulator<REPCommand> channel, REPCommand command) { - // コマンドの処理 - - int eid = getEID(channel); - int neid = getNextEID(eid); - - ns.writeLog("SessionManager received from " + ":" + eid + ": " + command, 3); - - EditorObject nextEditor = getEditor(neid); - //editor.receive(command); - - switch(command.cmd){ - case REP.SMCMD_QUIT_2: - quit2(nextEditor, command, 0); - break; - - case REP.SMCMD_QUIT: - quit(nextEditor, command); - break; - - default: - translate(eid, neid, command); - //REPCommand sendCommand = editor.receive(command); - //nextEditor.send(command); - } - - } - - private int getNextEID(int eid) { - return (eid+1)%editorList.size(); - } - - - private void quit(EditorObject nextEditor, REPCommand command) { - nextEditor.send(command); - if(command.eid == nextEditor.eid){ - //quitコマンドが一周してきた - if(transList.get(nextEditor.eid).isFinished()) { - command.setCMD(REP.SMCMD_QUIT_2); - }else{ - System.out.println("has Unmerged Commands."); - } - } - } - - private void quit2(EditorObject nextEditor, REPCommand command, int fromPort) { - nextEditor.send(command); - if(startQuit2 == -1) startQuit2 = fromPort; - else if(startQuit2 == nextEditor.eid) ;//finish(); - } - - private void translate(int eid, int neid, REPCommand command) { - - ChannelSimulator<REPCommand> channel = getChannel(eid); - - EditorObject editor = getEditor(eid); - EditorObject nextEditor = getEditor(neid); - - if(command.eid == eid){ - if(checkOwnCommand(command)){ - //エディタからの編集コマンドが戻ってきた場合、マージしてエディタへ反映 - //Hndlerを切り替える - setMergeState(channel, selector); - if(eid == 0)System.out.println("returned Editor Command : " + command); - REPCommand[] cmds = transList.get(eid).catchOwnCommand(command); - for(REPCommand cmd : cmds){ - REPCommand tmp2 = new REPCommand(cmd); - tmp2.eid = REP.MERGE_EID; - channel.write(pack(tmp2)); - } - - }else{ - //エディタからの新たな編集コマンド - if(eid == 0)System.out.println("new Editor Command : " + command); - transList.get(eid).transSendCmd(command); - nextEditor.send(command); - } - - }else if(command.eid == REP.MERGE_EID){ - //マージのときにエディタからの割り込みがないか確認 - if(eid == 0)System.out.println(" Merged Command : " + command); - if(transList.get(eid).checkMergeConflict(command)){ - LinkedList<REPCommand> againList = transList.get(eid).getMergeAgain(); - for(REPCommand againCommand : againList){ - channel.write(pack(againCommand)); - if(false)System.out.println(" reMerge : " + againCommand); - } - }else{ - //マージが終了 - //Handlerを元に戻す - if(!editor.isMerging()) setNormalState(channel, selector); - } - - }else{ - //他のエディタからのコマンドはマージャへ追加し次のエディタへ送信する - REPCommand[] cmds = transList.get(eid).transReceiveCmd(command); - for(REPCommand cmd : cmds){ - nextEditor.send(cmd); - } - } - } - - private void setMergeState(ChannelSimulator<REPCommand> channel, SelectorSimulator<REPCommand> selector2) { - SelectionKeySimulator<REPCommand> key = channel.keyFor(selector2); - key.attach(new REPHandlerInMerge(this)); - } - - private void setNormalState(ChannelSimulator<REPCommand> channel, SelectorSimulator<REPCommand> selector2) { - SelectionKeySimulator<REPCommand> key = channel.keyFor(selector2); - key.attach(new REPHandlerImpl(this)); - } - - private EditorObject getEditor(int eid) { - for(EditorObject editor : editorList){ - if(editor.getEID() == eid) { - return editor; - } - } - return null; - } - - private int getEID(ChannelSimulator<REPCommand> channel) { - int eid = 0; - for(EditorObject editor : editorList){ - if(editor.getChannel() == channel){ - eid = editor.getEID(); - } - } - return eid; - } - - private ChannelSimulator<REPCommand> getChannel(int eid) { - ChannelSimulator<REPCommand> channel = null; - for(EditorObject editor : editorList){ - if(editor.getEID() == eid){ - channel = editor.getChannel(); - } - } - return channel; - } - - private boolean checkOwnCommand(REPCommand command) { - boolean ownCommand = false; - LinkedList<REPCommand> sentCommands = transList.get(command.eid).getSentCmds(); - if(sentCommands.size() > 0){ - if(sentCommands.get(0).seq == command.seq){ - ownCommand = true; - } - } - return ownCommand; - } - - private REPCommand pack(REPCommand command) { - return new REPCommand(command); - } - - REPCommand unpack(REPCommand packet) { - return new REPCommand(packet); - } - - public void addWaitingCommand(PacketSet set) { - // TODO Auto-generated method stub - waitingList.add(set); - } - - public void doWaiting() { - - } - - synchronized public void finish(){ - synchronized(ns){ - running = false; - ns.notify(); - } - } - -}
--- a/src/pathfinder/mergetest/TestMerger.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,129 +0,0 @@ -package pathfinder.mergetest; - -import java.util.LinkedList; - -import pathfinder.mergetest.channels.NetworkSimulator; -import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; - -public class TestMerger { - static public int cmdNO[] = { REP.REPCMD_INSERT, REP.REPCMD_REPLACE, REP.REPCMD_DELETE }; - protected NetworkSimulator<REPCommand> ns=null; - protected LinkedList<EditorSimulator> editors; - protected SessionManagerSimulatorWithMerger sema; - protected UserSimulator users; - - public TestMerger(){ - editors = new LinkedList<EditorSimulator>(); - } - - public static void main(String[] args){ - TestMerger tm; - /* get the number of Editors. */ - int i = (args.length>0) ? Integer.parseInt(args[0]) : 3; - System.out.println("number of Editor = "+i); - /* get the number of Command. */ - int j = (args.length>1) ? Integer.parseInt(args[1]) : 3; - System.out.println("number of Packet = "+j); - - /* create, initialize and start test. */ - tm = new TestMerger(); - //tm.init(false, i, j, 1); // init( boolean SM?, int max_client, int max_packet, int loglevel); - tm.init(true, i, j, 1); - 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(); - } - - protected void startTest() { - /* start all Editors. */ - for (EditorSimulator ee: editors){ - ee.start(); - } - /* start SessionManager if it exist. */ - if (sema!=null) sema.init(); - if (sema!=null) sema.start(); - users.init(); - users.start(); - - /* wait Editors finish. */ - for (EditorSimulator ee: editors){ - try { - ee.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - /* inform SessionManager to finish. */ - if (sema!=null) sema.finish(); - } - - protected void init(boolean sm, int ne, int np, int ll){ - /* create NetworkSimulator, and SessionManager if it's required. */ - if (sm){ - ns = new NetworkSimulator<REPCommand>(); - sema = new SessionManagerSimulatorWithMerger(ns, ne); - } else { - ns = new NetworkSimulator<REPCommand>(); - sema = null; - } - ns.setLogLevel(ll); - - /* create UsersSimulator. */ - //users = new UsersSimulator(ns, ne, np*ne); - - /* create ne Editors and np commands. */ - for (int i=0; i<ne; i++){ - LinkedList<REPCommand> cmds = new LinkedList<REPCommand>(); - // 各エディタが送信するコマンド列を生成 - - /* create command list. */ - /* - 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); - } - */ - - /* create a Editor, and pass command list to it. */ - EditorSimulator ee = null; //new EditorSimulator(i, ns, cmds, "Editor"+i); - if(i==0) ee.setOwner(true); - editors.add(ee); - } - } - - protected void printAllTexts(){ - for(EditorSimulator ee: editors){ - System.out.println("--"+ee.getName()+"------------------------"); - ee.getText().printAllText(); - } - } -/* - private boolean checkCS(){ - return ns.checkAllCS(); - } -*/ - protected boolean checkEquality(){ - /* - Text ee0 = editors.remove().getText(); - return editors.remove().getText().equals(ee0); - */ - Text text0 = editors.element().getText(); - //System.out.println("------------------- 結果 --------------------"); - for(EditorSimulator ee: editors){ - //System.out.println(" "); - //ee.getText().printAllText(); - if (!text0.equals(ee.getText())) return false; - } - return true; - } -}
--- a/src/pathfinder/mergetest/TestMerger2.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,150 +0,0 @@ -package pathfinder.mergetest; - -import java.util.LinkedList; - -import pathfinder.mergetest.channels.NetworkSimulator; -import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; - -public class TestMerger2 { - static public int cmdNO[] = { REP.REPCMD_INSERT, REP.REPCMD_REPLACE, REP.REPCMD_DELETE }; - protected NetworkSimulator<REPCommand> ns=null; - protected LinkedList<EditorSimulatorWithoutMerger> editors; - protected SessionManagerSimulatorWithMerger sema; - protected LinkedList<UserSimulator> users = new LinkedList<UserSimulator>(); - - 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" - }; - public TestMerger2(){ - editors = new LinkedList<EditorSimulatorWithoutMerger>(); - } - - public static void main(String[] args){ - TestMerger2 tm; - /* get the number of Editors. */ - int i = (args.length>0) ? Integer.parseInt(args[0]) : 4; - System.out.println("number of Editor = "+i); - /* get the number of Command. */ - int j = (args.length>1) ? Integer.parseInt(args[1]) : 3; - System.out.println("number of Packet = "+j); - - /* create, initialize and start test. */ - tm = new TestMerger2(); - tm.init(true, i, j, 1); - 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(); - } - - protected void startTest() { - /* start all Editors. */ - for (EditorSimulatorWithoutMerger ee: editors){ - ee.start(); - } - /* start SessionManager if it exist. */ - //if (sema!=null) sema.init(); - if (sema!=null) sema.start(); - - for(UserSimulator u: users){ - u.start(); - } - for(UserSimulator u: users){ - try { - u.join(); - } catch (InterruptedException e) { } - } - - - /* wait Editors finish. */ - for (EditorSimulatorWithoutMerger ee: editors){ - try { - ee.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - /* inform SessionManager to finish. */ - if (sema!=null) { - sema.finish(); - try { - sema.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - - LinkedList<REPCommand> userCommand(int eid){ - LinkedList<REPCommand> cmds = new LinkedList<REPCommand>(); - int sid = 0; int eid2 = -1; int seq = 0; int line = 0; - cmds .add(new REPCommand(REP.REPCMD_INSERT, sid, eid2, seq++, line, 0, "inserted by editor:" + eid + ":0")); - cmds .add(new REPCommand(REP.REPCMD_INSERT, sid, eid2, seq++, line, 0, "inserted by editor:" + eid + ":1")); - cmds .add(new REPCommand(REP.REPCMD_INSERT, sid, eid2, seq++, line, 0, "inserted by editor:" + eid + ":2")); - if(eid == 0) cmds.add(new REPCommand(REP.SMCMD_QUIT, sid, eid, seq++, line, 0, "sent by editor:" + eid + ":3")); - return cmds; - } - - protected void init(boolean sm, int ne, int np, int ll){ - /* create NetworkSimulator, and SessionManager if it's required. */ - ns = new NetworkSimulator<REPCommand>(); - sema = new SessionManagerSimulatorWithMerger(ns, 1000); - ns.setLogLevel(ll); - - /* create UsersSimulator. */ - for(int i = 0; i < ne; i++){ - users.add(new UserSimulator(ns, i, userCommand(i))); - } - - /* create ne Editors and np commands. */ - for (int i=0; i<ne; i++){ - /* create a Editor, and pass command list to it. */ - EditorSimulatorWithoutMerger ee; - if(i == 0) { - ee = new EditorSimulatorWithoutMerger(i, ns, text0, "Editor:"+i); - }else { - ee = new EditorSimulatorWithoutMerger(i, ns, text0, "Editor:"+i); - } - - if(i==0) ee.setOwner(true); - editors.add(ee); - } - - - } - - protected void printAllTexts(){ - for(EditorSimulatorWithoutMerger ee: editors){ - System.out.println("--"+ee.getName()+"------------------------"); - ee.getText().printAllText(); - } - } -/* - private boolean checkCS(){ - return ns.checkAllCS(); - } -*/ - protected boolean checkEquality(){ - /* - Text ee0 = editors.remove().getText(); - return editors.remove().getText().equals(ee0); - */ - Text text0 = editors.element().getText(); - //System.out.println("------------------- 結果 --------------------"); - for(EditorSimulatorWithoutMerger ee: editors){ - //System.out.println(" "); - //ee.getText().printAllText(); - if (!text0.equals(ee.getText())) return false; - } - return true; - } -}
--- a/src/pathfinder/mergetest/Text.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -package pathfinder.mergetest; - -import java.util.Arrays; -import java.util.LinkedList; - -import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; - -public class Text { - - LinkedList<String> strList; - - public Text(String[] _strings){ - strList = new LinkedList<String>(Arrays.asList(_strings)); - } - - public String insert(int i, String str){ - assert 0<i && i<strList.size(); - strList.add(i, str); - return null; - } - public String delete(int i){ - assert 0<i && i<strList.size(); - return strList.remove(i); - } - public String replace(int i, String str){ - assert 0<i && i<strList.size(); - String replaced = strList.get(i); - strList.set(i, str); - return replaced; - } - public String get(int i){ - assert 0<i && i<strList.size(); - return strList.get(i); - } - public String edit(REPCommand cmd){ - if (cmd.cmd==REP.REPCMD_INSERT) return insert(cmd.lineno, cmd.string); - else if (cmd.cmd==REP.REPCMD_REPLACE) return replace(cmd.lineno, cmd.string); - else if (cmd.cmd==REP.REPCMD_DELETE) return delete(cmd.lineno); - //else assert false; - return null; - } - - 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/pathfinder/mergetest/UserSimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ -package pathfinder.mergetest; - -import java.util.LinkedList; - -import pathfinder.mergetest.channels.ChannelSimulator; -import pathfinder.mergetest.channels.NetworkSimulator; - -import remoteeditor.command.REPCommand; - -public class UserSimulator extends Thread { - private NetworkSimulator<REPCommand> ns; - private LinkedList<REPCommand> cmds; - //private int eid; - private EditorSimulator editor; - - public UserSimulator(NetworkSimulator<REPCommand> ns2, int _eid, LinkedList<REPCommand> _cmds) { - ns = ns2; - //eid = _eid; - cmds = _cmds; - } - - public void run(){ - ns.writeLog("UsersSimulator start.", 1); - ChannelSimulator<REPCommand> channel = editor.getChannelforUser(); - while(cmds.size()>0){ -// try { -// Thread.sleep(1000); -// } catch (InterruptedException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } - REPCommand cmd0 = createCmd(); - channel.write(cmd0); - } - - ns.writeLog("UsersSimulator finish.", 1); - } - - private REPCommand createCmd(){ - REPCommand cmd = cmds.remove(0); - return cmd; - } - - public void init() { - // TODO Auto-generated method stub - - } - -}
--- a/src/pathfinder/mergetest/channels/ChannelSimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,66 +0,0 @@ -package pathfinder.mergetest.channels; - - -public class ChannelSimulator<P> extends SelectableChannelSimulator<P>{ - //private BlockingQueue<P> qread; - //private BlockingQueue<P> qwrite; - //private SelectorSimulator<P> waitingSelector; - protected NetworkSimulator<P> ns; - - /** Constructors. */ - public ChannelSimulator(NetworkSimulator<P> _ns){ - this(_ns, null); - } - public ChannelSimulator(NetworkSimulator<P> _ns, SelectorSimulator<P> _selector){ - ns = _ns; - //ns = NetworkSimulator.singleton(); //どっちがいい? - } - public ChannelSimulator<P> createConjugatedChannel() { - ChannelSimulator<P> ret = new ChannelSimulator<P>(ns); - ret.qread=qwrite; - ret.qwrite=qread; - ret.readSelector=writeSelector; - ret.writeSelector=readSelector; - return ret; - } - - /** Connecting methods */ - // for clients. - public boolean connect(int ip){ - return ns.connect(ip, this); - } - - public ChannelSimulator<P> accept(){ - return null; - } - - /* return state of the Queue(debug) */ - /* - public boolean readQisEmpty() { - return qread.isEmpty(); - } - public boolean writeQisEmpty() { - return qwrite.isEmpty(); - } - */ - - @Override - public boolean isAcceptable() { - return false; - } - @Override - public boolean isReadable() { - synchronized (qread){ - return !qread.isEmpty(); - } - } - @Override - public boolean isWritable() { - return true; - } - - public SelectionKeySimulator<P> keyFor(SelectorSimulator<P> selector2) { - return selector2.getKey(this); - } - -}
--- a/src/pathfinder/mergetest/channels/NetworkSimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,126 +0,0 @@ -package pathfinder.mergetest.channels; - -import java.util.LinkedList; - - -public class NetworkSimulator<P> { - public static NetworkSimulator<?> ns; - synchronized public static <T> NetworkSimulator<T> singleton(){ - if (ns==null) - ns = new NetworkSimulator<T>(); - return (NetworkSimulator<T>) ns; - // NetworkSimulator<Obj> ns = NetworkSimulator.singleton(new NetworkSimulator<Obj>()); - } - - int logLevel=5; - /** Listening Servers. */ - private LinkedList<ServerData<P>> serverList; - - /** Constructor. */ - public NetworkSimulator(){ - serverList = new LinkedList<ServerData<P>>(); - writeLog("construct Networksimulator", 1); - printAllState(); - } - - - - /* */ - synchronized public void listen(int ip, SelectorSimulator<P> selector) { - serverList.add(new ServerData<P>(ip, selector)); - writeLog(Thread.currentThread(), "listen", 1); - printAllState(); - } - - synchronized public ChannelSimulator<P> accept(int ip) { - for (ServerData<P> sd: serverList){ - if (sd.virtualIP!=ip) continue; - writeLog(Thread.currentThread(), "accepting..", 1); - - ChannelSimulator<P> serverCH = sd.acceptWaitingList.remove(); - sd.establishedList.add(serverCH); - - writeLog(Thread.currentThread(), "accepted", 1); - printAllState(); - return serverCH; - } - return null; - } - synchronized public boolean canAccept(int ip){ - for (ServerData<P> sd: serverList){ - if (sd.virtualIP!=ip) continue; - return !sd.acceptWaitingList.isEmpty(); - } - return false; - } - - public boolean connect(int ip, ChannelSimulator<P> clientCH) { - ServerData<P> sd = null; - writeLog(Thread.currentThread(), "connecting..", 1); - synchronized (this){ - for (ServerData<P> sd0: serverList){ - if (sd0.virtualIP!=ip) continue; - - sd = sd0; - } - if (sd==null) return false; - - //ChannelSimulator<P> channel = new ChannelSimulator<P>(sd.selector); - clientCH.createReadQ(); - clientCH.createWriteQ(); - clientCH.setWriteSelector(sd.selector); - - ChannelSimulator<P> serverCH = clientCH.createConjugatedChannel(); - sd.acceptWaitingList.add(serverCH); - } - - synchronized (sd.selector) { - sd.selector.notifyAll(); - } - writeLog(Thread.currentThread(), "connected", 1); - printAllState(); - return true; - } - - /** for DEBUG methods. */ - synchronized void printAllState(){ - writeLog("NetworkSimulator State:"); - for (ServerData<P> sd: serverList){ - writeLog("\tSessionManager(ip="+sd.virtualIP+"): "); - writeLog("\tacceptWaitingList="+sd.acceptWaitingList.size()); - writeLog("\testablishedList="+sd.establishedList.size()); - } - } - - /** simulation log command */ - synchronized public void writeLog(String log, int level){ - if ( level<=logLevel ) - System.out.println(log); - System.out.flush(); - } - public void writeLog(String log){ - writeLog(log, 0); - } - public void writeLog(Thread thr, String log, int level){ - writeLog(thr.getName()+": "+log, level); - } - public void setLogLevel(int logLevel) { - this.logLevel = logLevel; - } - - -} - -class ServerData<P> { - int virtualIP; - SelectorSimulator<P> selector; - LinkedList<ChannelSimulator<P>> acceptWaitingList; - LinkedList<ChannelSimulator<P>> establishedList; - - ServerData(int ip, SelectorSimulator<P> _selector){ - virtualIP = ip; - selector = _selector; - acceptWaitingList = new LinkedList<ChannelSimulator<P>>(); - establishedList = new LinkedList<ChannelSimulator<P>>(); - } -} \ No newline at end of file
--- a/src/pathfinder/mergetest/channels/SelectableChannelSimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,71 +0,0 @@ -package pathfinder.mergetest.channels; - -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; - - - -public abstract class SelectableChannelSimulator<P> { - protected BlockingQueue<P> qread; - protected BlockingQueue<P> qwrite; - protected SelectorSimulator<P> writeSelector; - protected SelectorSimulator<P> readSelector; - - /* read from Queue. */ - public P read(){ - try { - if(readSelector!=null) - synchronized (readSelector){ - return qread.take(); - } - else{ - return qread.take(); - } - } catch (InterruptedException e) { - e.printStackTrace(); - return null; - } - } - /* write to Queue. */ - public boolean write(P p){ - try { - if (writeSelector!=null) - synchronized (writeSelector){ - qwrite.put(p); - writeSelector.notifyAll(); - } - else { - qwrite.put(p); - } - return true; - } catch (InterruptedException e) { - e.printStackTrace(); - return false; - } - } - public abstract ChannelSimulator<P> accept(); - - /* accessor methods. */ - public BlockingQueue<P> getReadQ(){ - return qread; - } - public BlockingQueue<P> getWriteQ(){ - return qwrite; - } - public void createReadQ(){ - qread = new LinkedBlockingQueue<P>(); - } - public void createWriteQ(){ - qwrite = new LinkedBlockingQueue<P>(); - } - public void setWriteSelector(SelectorSimulator<P> _selector){ - writeSelector = _selector; - } - - - /* return state of the Queue */ - abstract public boolean isReadable(); - abstract public boolean isWritable(); - abstract public boolean isAcceptable(); - -}
--- a/src/pathfinder/mergetest/channels/SelectionKeySimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -package pathfinder.mergetest.channels; - -public class SelectionKeySimulator<P> { - - public static final int OP_READ = 0x01; - public static final int OP_ACCEPT = 0x02; - public static final int OP_WRITE = 0x04; - - private int interestOpt; - private SelectableChannelSimulator<P> channel; - private Object attachment; - - public SelectionKeySimulator(SelectableChannelSimulator<P> cs, int opt) { - channel = cs; - interestOpt = opt; - } - - public boolean isAble() { - if ( (interestOpt&OP_READ)!=0 && isReadable() ) - return true; - else if( (interestOpt&OP_ACCEPT)!=0 && isAcceptable() ) - return true; - else if( (interestOpt&OP_WRITE)!=0 && isWritable() ) - return true; - else - return false; - } - - public boolean isAcceptable() { - return channel.isAcceptable(); - } - - public boolean isReadable() { - return channel.isReadable(); - } - public boolean isWritable() { - return channel.isWritable(); - } - - public SelectableChannelSimulator<P> channel() { - return channel; - } - - public Object attachment() { - return attachment; - } - - public void attach(Object handler) { - attachment = handler; - } - -}
--- a/src/pathfinder/mergetest/channels/SelectorSimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,67 +0,0 @@ -package pathfinder.mergetest.channels; - -import java.io.IOException; -import java.util.ArrayList; -//import java.util.Set; //書き直す? - - - -public class SelectorSimulator<P> { - - private ArrayList<SelectionKeySimulator<P>> keyList; - private ArrayList<SelectionKeySimulator<P>> selectedKeys; - - public SelectorSimulator() { - // TODO Auto-generated constructor stub - keyList = new ArrayList<SelectionKeySimulator<P>>(); - } - - public int select() throws IOException { - selectedKeys = new ArrayList<SelectionKeySimulator<P>>(); - - synchronized(this) { - - while(selectedKeys.isEmpty()){ - for(SelectionKeySimulator<P> key : keyList){ - if(key.isAble()) - selectedKeys.add(key); - } - - if(selectedKeys.isEmpty()) - try { - this.wait(); - } catch (InterruptedException e) { - throw new IOException("Error, Selector was interrupted!"); - } - } - } - return selectedKeys.size(); - } - - public SelectionKeySimulator<P> register(SelectableChannelSimulator<P> cs, int opt){ - SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs, opt); - keyList.add(key); - return key; - } - - public SelectionKeySimulator<P> register(ChannelSimulator<P> cs, int opt, Object handler){ - SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs, opt); - key.attach(handler); - keyList.add(key); - return key; - } - - public ArrayList<SelectionKeySimulator<P>> selectedKeys() { - - return selectedKeys; - } - - public SelectionKeySimulator<P> getKey(ChannelSimulator<P> channel){ - for(SelectionKeySimulator<P> key : keyList){ - if(key.channel() == channel) - return key; - } - return null; - } - -}
--- a/src/pathfinder/mergetest/channels/ServerChannelSimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,42 +0,0 @@ -package pathfinder.mergetest.channels; - - - -public class ServerChannelSimulator<P>extends SelectableChannelSimulator<P>{ - protected NetworkSimulator<P> ns; - private int virtualIP; - - /** Constructors. */ - public ServerChannelSimulator(NetworkSimulator<P> _ns, SelectorSimulator<P> rselector){ - ns = _ns; - readSelector = rselector; - writeSelector = null; - qread = null; - qwrite = null; - } - - /** Connecting methods */ - // for servers. - public void bind(int ip){ - virtualIP = ip; - ns.listen(ip, readSelector); - } - - public ChannelSimulator<P> accept(){ - ChannelSimulator<P> channel = ns.accept(virtualIP); - return channel; - } - - - /* state check methods for SelectionKeySimulator. */ - public boolean isReadable() { - return false; - } - public boolean isWritable() { - return false; - } - public boolean isAcceptable() { - return ns.canAccept(virtualIP); - } - -}
--- a/src/pathfinder/mergetest/channels/test/testEditor.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,34 +0,0 @@ -package pathfinder.mergetest.channels.test; - -import pathfinder.mergetest.channels.*; - -public class testEditor extends Thread{ - private NetworkSimulator<String> ns; - private int semaIP; - - public testEditor(NetworkSimulator<String> _ns, String name, int _ip){ - super(name); - ns = _ns; - semaIP = _ip; - } - - public void run(){ - ChannelSimulator<String> channel = new ChannelSimulator<String>(ns); - - ns.writeLog(this, "try to connect to SessionManager whose ip is "+semaIP, 1); - while (!channel.connect(semaIP)){ - ns.writeLog(this, "SeMa not listen to socket yet, wait", 1); - Thread.yield(); - } - ns.writeLog(this, "successes to connect", 1); - - channel.write("from "+getName()+": hello"); - ns.writeLog(this, "wrote packet", 1); - - String packet = channel.read(); - - ns.writeLog(this, "gets return string==> `"+packet+"\'", 1); - - ns.writeLog(this, "testEditor exits.", 1); - } -}
--- a/src/pathfinder/mergetest/channels/test/testNetworkSimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,69 +0,0 @@ -package pathfinder.mergetest.channels.test; - -import java.util.ArrayList; -import java.util.Random; -import pathfinder.mergetest.channels.NetworkSimulator; - -public class testNetworkSimulator { - private NetworkSimulator<String> ns; - private ArrayList<testSeMa> semaList; - private ArrayList<testSeMaSlave> semasList; - private ArrayList<testEditor> editorList; - private int NoSemaMaster; - private int NoSemaSlave; - private int NoEditor; - - public static void main(String[] args){ - testNetworkSimulator testns = new testNetworkSimulator(3, 10, 50); - - testns.startTest(); - } - - - /** Constructor. */ - public testNetworkSimulator(int sm, int ss,int e){ - //ns = new NetworkSimulator<String>(); - ns = NetworkSimulator.singleton(); - semaList = new ArrayList<testSeMa>(); - semasList = new ArrayList<testSeMaSlave>(); - editorList = new ArrayList<testEditor>(); - NoSemaMaster = sm; - NoSemaSlave = ss; - NoEditor = e; - } - - public void startTest(){ - Random rand = new Random(); - for (int i=0; i<NoSemaMaster; i++){ - testSeMa sm = new testSeMa(ns, "SeMa"+i, i); - semaList.add(sm); - sm.start(); - } - for (int i=0; i<NoSemaSlave; i++){ - testSeMaSlave sm = new testSeMaSlave(ns, "SeMaS"+i, i+NoSemaMaster, rand.nextInt(NoSemaMaster)); - semasList.add(sm); - sm.start(); - } - for (int i=0; i<NoEditor; i++){ - testEditor te = new testEditor(ns, "Editor"+i, rand.nextInt(NoSemaMaster+NoSemaSlave)); - editorList.add(te); - te.start(); - } - - Checker(); - - try { - for (testEditor te: editorList) - te.join(); - ns.writeLog("main: all clients exited.", 1); - - } catch (InterruptedException e) { - e.printStackTrace(); - } - System.exit(0); - } - - public void Checker(){ - - } -}
--- a/src/pathfinder/mergetest/channels/test/testSeMa.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,60 +0,0 @@ -package pathfinder.mergetest.channels.test; - -import java.io.IOException; -import java.util.LinkedList; - -import pathfinder.mergetest.channels.*; - - -public class testSeMa extends Thread{ - - int IP; - boolean running=true; - NetworkSimulator<String> ns; - LinkedList<ChannelSimulator<String>> channels; - - public testSeMa(NetworkSimulator<String> _ns, String name, int ip){ - super(name); - IP = ip; - ns = _ns; - channels = new LinkedList<ChannelSimulator<String>>(); - } - public void init(){ - - } - - public void run() { - SelectorSimulator<String> selector = new SelectorSimulator<String>(); - ServerChannelSimulator<String> scs = new ServerChannelSimulator<String>(ns, selector); - scs.bind(IP); - selector.register(scs, SelectionKeySimulator.OP_ACCEPT); - ns.writeLog("SessionManager starts mainroutin.", 1); - - /* Main Loop */ - while(running){ - - try { selector.select(); } - catch (IOException e) { e.printStackTrace();} - - for(SelectionKeySimulator<String> key : selector.selectedKeys()){ - - if(key.isAcceptable()){ - ns.writeLog(this, "gets acceptable channel", 1); - ServerChannelSimulator<String> sc = (ServerChannelSimulator<String>) key.channel(); - ChannelSimulator<String> channel = sc.accept(); - selector.register(channel, SelectionKeySimulator.OP_READ); - ns.writeLog(this, "accepts a client.", 1); - - }else if(key.isReadable()){ - ns.writeLog(this, "gets readable channel", 1); - //SelectableChannelSimulator<String> channel = key.channel(); - ChannelSimulator<String> channel = (ChannelSimulator<String>) key.channel(); - String packet = channel.read(); - ns.writeLog(this, "receives String==> `"+packet+"\'", 1); - channel.write("from SeMa"+this.getName()+": world"); - } - } - } - - } -}
--- a/src/pathfinder/mergetest/channels/test/testSeMaSlave.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,94 +0,0 @@ -package pathfinder.mergetest.channels.test; - -import java.io.IOException; -import java.util.LinkedList; - -import pathfinder.mergetest.channels.*; - - -public class testSeMaSlave extends Thread{ - - int ownIP; - int masterIP; - boolean running=true; - NetworkSimulator<String> ns; - LinkedList<ClientInfo> cis; - - public testSeMaSlave(NetworkSimulator<String> _ns, String name, int oIP, int mIP){ - super(name); - ownIP = oIP; - masterIP = mIP; - ns = _ns; - cis = new LinkedList<ClientInfo>(); - } - public void init(){ - - } - - public void run() { - SelectorSimulator<String> selector = new SelectorSimulator<String>(); - - ChannelSimulator<String> masterCH = connectToMaster(selector); - ServerChannelSimulator<String> scs = new ServerChannelSimulator<String>(ns, selector); - scs.bind(ownIP); - - selector.register(scs, SelectionKeySimulator.OP_ACCEPT); - selector.register(masterCH, SelectionKeySimulator.OP_READ); - ns.writeLog("SessionManager starts mainroutin.", 1); - - /* Main Loop */ - while(running){ - - try { selector.select(); } - catch (IOException e) { e.printStackTrace();} - - for(SelectionKeySimulator<String> key : selector.selectedKeys()){ - - if(key.isAcceptable()){ - ns.writeLog(this, "gets acceptable channel", 1); - ServerChannelSimulator<String> sc = (ServerChannelSimulator<String>) key.channel(); - ChannelSimulator<String> channel = sc.accept(); - selector.register(channel, SelectionKeySimulator.OP_READ); - ns.writeLog(this, "accepts a client.", 1); - - }else if(key.isReadable()){ - ns.writeLog(this, "gets readable channel", 1); - ChannelSimulator<String> channel = (ChannelSimulator<String>) key.channel(); - String packet = channel.read(); - if (channel==masterCH){ - ns.writeLog(this, "receives String from master ==> `"+packet+"\'", 1); - for (ClientInfo ci: cis){ - if (!packet.matches(ci.str)) continue; - ci.channel.write("from "+this.getName()+": loopback packet from master ==>`"+packet+"\'"); - } - }else{ - ns.writeLog(this, "receives String==> `"+packet+"\'", 1); - //channel.write("from "+this.getName()+": slave"); - masterCH.write("from "+this.getName()+": receive String==>`"+packet+"\' from editor"); - cis.add(new ClientInfo(packet, channel)); - } - } - } - } - - } - private ChannelSimulator<String> connectToMaster(SelectorSimulator<String> _selector) { - ChannelSimulator<String> channel = new ChannelSimulator<String>(ns, _selector); - ns.writeLog(this, "is connecting to masterSeMa whose ip is"+masterIP, 1); - while(!channel.connect(masterIP)){ - ns.writeLog(this, "SeMa not listen to socket yet, wait", 1); - Thread.yield(); - } - ns.writeLog(this, "connecting was successful.", 1); - - return channel; - } -} -class ClientInfo{ - String str; - ChannelSimulator<String> channel; - ClientInfo(String _str, ChannelSimulator<String> _channel){ - str = _str; - channel = _channel; - } -} \ No newline at end of file
--- a/src/pathfinder/mergetest/channels2/NetworkSimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,99 +0,0 @@ -package pathfinder.mergetest.channels2; - -import java.net.SocketAddress; -import java.util.LinkedList; - - -public class NetworkSimulator<P> { - public static NetworkSimulator<?> ns; - /** Listening Servers. */ - private LinkedList<ServerData<P>> serverList; - /** log Level */ - int logLevel=5; - - /** Constructor. */ - private NetworkSimulator(){ - serverList = new LinkedList<ServerData<P>>(); - writeLog("Networksimulator was constructed.", 1); - printAllState(); - } - - synchronized public static <T> NetworkSimulator<T> singleton(){ - if (ns==null) - ns = new NetworkSimulator<T>(); - return (NetworkSimulator<T>) ns; - } - - - /* */ - synchronized public void listen(Object ip, ServerChannelSimulator<P> _scs) { - serverList.add(new ServerData<P>(ip, _scs)); - writeLog("listen", 1); - printAllState(); - } - - synchronized public boolean connect(Object ip, SocketChannelSimulator<P> CHclient) { - for (ServerData<P> sd0: serverList){ - if (!sd0.virtualIP.equals(ip)) continue; - - SocketChannelSimulator<P> CHserver = SocketChannelSimulator.<P>open(); - CHserver.setOtherEnd(CHclient); - CHclient.setOtherEnd(CHserver); - - sd0.connectedListS.add(CHserver); - sd0.connectedListC.add(CHclient); - sd0.scs.enQ(CHserver); - - printAllState(); - return true; - } - return false; - } - - /** for DEBUG methods. */ - synchronized void printAllState(){ - writeLog("NetworkSimulator State:"); - for (ServerData<P> sd: serverList){ - writeLog("\tSessionManager(ip="+sd.virtualIP.toString()+"): "); - //writeLog("\tacceptWaitingList="+sd.acceptWaitingList.size()); - printChannelList(sd.connectedListC); - //writeLog("\testablishedList="+sd.establishedList.size()); - } - } - synchronized void printChannelList(LinkedList<SocketChannelSimulator<P>> list){ - String tmp = ""; - for (SocketChannelSimulator<P> ch: list){ - tmp += ch.toString()+" "; - } - writeLog("\t"+tmp); - } - - /** simulation log command */ - synchronized public void writeLog(String log, int level){ - if ( level<=logLevel ) - System.out.println(Thread.currentThread().getName()+": "+log); - System.out.flush(); - } - public void writeLog(String log){ - writeLog(log, 0); - } - public void setLogLevel(int logLevel) { - this.logLevel = logLevel; - } - - -} - -class ServerData<P> { - Object virtualIP; - ServerChannelSimulator<P> scs; - LinkedList<SocketChannelSimulator<P>> connectedListS; - LinkedList<SocketChannelSimulator<P>> connectedListC; - - ServerData(Object ip, ServerChannelSimulator<P> _scs){ - virtualIP = ip; - scs = _scs; - connectedListC = new LinkedList<SocketChannelSimulator<P>>(); - connectedListS = new LinkedList<SocketChannelSimulator<P>>(); - } -} \ No newline at end of file
--- a/src/pathfinder/mergetest/channels2/SelectableChannelSimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -package pathfinder.mergetest.channels2; - - - -public abstract class SelectableChannelSimulator { - protected Object lock; - - protected SelectableChannelSimulator(){ - lock = new Object(); - } - - public SelectionKeySimulator register(SelectorSimulator selector, int opt){ - Object tmp=lock; - synchronized (tmp){ - lock = selector; - } - return selector.register(this, opt); - } - - abstract boolean isBlocking(); - - abstract SelectionKeySimulator keyFor(SelectorSimulator sel); - abstract boolean isRegistered(); - abstract SelectionKeySimulator register(SelectorSimulator sel, int ops, Object att); - - /* return state of the Queue */ - abstract public boolean isReadable(); - abstract public boolean isWritable(); - abstract public boolean isAcceptable(); - -}
--- a/src/pathfinder/mergetest/channels2/SelectionKeySimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -package pathfinder.mergetest.channels2; - -public class SelectionKeySimulator { - - public static final int OP_READ = 0x01; - public static final int OP_ACCEPT = 0x02; - public static final int OP_WRITE = 0x04; - - private int interestOpt; - private SelectableChannelSimulator channel; - private Object attachment; - - public SelectionKeySimulator(SelectableChannelSimulator cs, int opt) { - channel = cs; - interestOpt = opt; - } - - public boolean isAble() { - if ( (interestOpt&OP_READ)!=0 && isReadable() ) - return true; - else if( (interestOpt&OP_ACCEPT)!=0 && isAcceptable() ) - return true; - else if( (interestOpt&OP_WRITE)!=0 && isWritable() ) - return true; - else - return false; - } - - public boolean isAcceptable() { - return channel.isAcceptable(); - } - - public boolean isReadable() { - return channel.isReadable(); - } - public boolean isWritable() { - return channel.isWritable(); - } - - public SelectableChannelSimulator channel() { - return channel; - } - - public Object attachment() { - return attachment; - } - - public void attach(Object handler) { - attachment = handler; - } - - -}
--- a/src/pathfinder/mergetest/channels2/SelectorSimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -package pathfinder.mergetest.channels2; - -import java.io.IOException; -import java.util.ArrayList; -//import java.util.Set; //書き直す? - - - -public class SelectorSimulator { - - private ArrayList<SelectionKeySimulator> keyList; - private ArrayList<SelectionKeySimulator> selectedKeys; - - public SelectorSimulator() { - // TODO Auto-generated constructor stub - keyList = new ArrayList<SelectionKeySimulator>(); - } - - public int select() throws IOException { - selectedKeys = new ArrayList<SelectionKeySimulator>(); - - synchronized(this) { - - while(selectedKeys.isEmpty()){ - for(SelectionKeySimulator key : keyList){ - if(key.isAble()) - selectedKeys.add(key); - } - - if(selectedKeys.isEmpty()) - try { - this.wait(); - } catch (InterruptedException e) { - throw new IOException("Error, Selector was interrupted!"); - } - } - } - return selectedKeys.size(); - } - - SelectionKeySimulator register(SelectableChannelSimulator cs, int opt){ - SelectionKeySimulator key = new SelectionKeySimulator(cs, opt); - keyList.add(key); - return key; - } - - SelectionKeySimulator register(SelectableChannelSimulator cs, int opt, Object handler){ - SelectionKeySimulator key = new SelectionKeySimulator(cs, opt); - key.attach(handler); - keyList.add(key); - return key; - } - - public ArrayList<SelectionKeySimulator> selectedKeys() { - - return selectedKeys; - } - -}
--- a/src/pathfinder/mergetest/channels2/ServerChannelSimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,92 +0,0 @@ -package pathfinder.mergetest.channels2; - -import java.io.IOException; -import java.util.LinkedList; -import java.util.Queue; - - - -public class ServerChannelSimulator<P> extends SelectableChannelSimulator{ - protected NetworkSimulator<P> ns; - protected Queue<SocketChannelSimulator<P>> acceptQ; - protected Object virtualIP; - protected boolean isBlocking=true; - protected SelectionKeySimulator key; - - /** Constructors. */ - private ServerChannelSimulator(){ - super(); - ns = NetworkSimulator.singleton(); - acceptQ = new LinkedList<SocketChannelSimulator<P>>(); - } - - public static <T> ServerChannelSimulator<T> open(){ - return new ServerChannelSimulator<T>(); - } - - /** Connecting methods */ - public void bind(Object ip){ - virtualIP = ip; - ns.listen(ip, this); - } - - public SocketChannelSimulator<P> accept() throws IOException{ // Blocking - SocketChannelSimulator<P> tmp; - synchronized (lock){ - while ( (tmp=acceptQ.poll())==null && isBlocking ) { - try { - lock.wait(); - } catch (InterruptedException e) { - throw new IOException(); - } - } - } - return tmp; - } - - void enQ(SocketChannelSimulator<P> scs){ // for NetworkSimulator - synchronized (lock){ - acceptQ.offer(scs); - lock.notifyAll(); - } - } - - /* state check methods for SelectionKeySimulator. */ - public boolean isReadable() { - return false; - } - public boolean isWritable() { - return false; - } - public boolean isAcceptable() { - synchronized (lock){ - return !acceptQ.isEmpty(); - } - } - - @Override - public SelectionKeySimulator keyFor(SelectorSimulator sel) { - // TODO Auto-generated method stub - return null; - } - - @Override - boolean isBlocking() { - return isBlocking; - } - - @Override - boolean isRegistered() { - return (lock instanceof SelectorSimulator); - } - - @Override - public SelectionKeySimulator register(SelectorSimulator sel, int opt, Object att) { - synchronized (lock){ - lock = sel; - key = sel.register(this, opt, att); - return key; - } - } - -}
--- a/src/pathfinder/mergetest/channels2/SocketChannelSimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,116 +0,0 @@ -package pathfinder.mergetest.channels2; - -import java.util.LinkedList; -import java.util.Queue; -import java.io.IOException; -import java.nio.channels.NotYetConnectedException; - -public class SocketChannelSimulator<P> extends SelectableChannelSimulator{ - protected NetworkSimulator<P> ns; - protected Queue<P> readQ; - protected SocketChannelSimulator<P> otherEnd; - boolean isBlocking=true; - protected SelectionKeySimulator key; - private String ownerName; - - /** Constructors. */ - private SocketChannelSimulator(){ - super(); - ns = NetworkSimulator.singleton(); - readQ = new LinkedList<P>(); - lock = new Object(); - otherEnd = null; - key = null; - ownerName = Thread.currentThread().getName(); - } - - public static <T> SocketChannelSimulator<T> open(){ - return new SocketChannelSimulator<T>(); - } - - /** read from own Queue. - * @throws IOException */ - public P read() throws IOException{ // Blocking - P tmp; - synchronized (lock){ - while ( (tmp=readQ.poll())==null && isBlocking ) { - try { - lock.wait(); - } catch (InterruptedException e) { - throw new IOException(); - } - } - } - return tmp; - } - - /** write to other end Queue. */ - public void write(P p){ - if (otherEnd==null) throw new NotYetConnectedException(); - otherEnd.enQ(p); - } - - /** other end Channel enqueue P to own queue using this method. */ - protected void enQ(P p){ - synchronized (lock){ - readQ.offer(p); - lock.notifyAll(); - } - } - - void setOtherEnd(SocketChannelSimulator<P> oe){ // for same package - otherEnd = oe; - } - - - /** Connecting methods */ - // for clients. - public boolean connect(Object ip){ - return ns.connect(ip, this); - } - - @Override - public boolean isAcceptable() { - return false; - } - @Override - public boolean isReadable() { - synchronized (lock){ - return !readQ.isEmpty(); - } - } - @Override - public boolean isWritable() { - return true; - } - - @Override - boolean isBlocking() { - return isBlocking; - } - - @Override - boolean isRegistered() { - return (lock instanceof SelectorSimulator); - } - - @Override - SelectionKeySimulator keyFor(SelectorSimulator sel) { - return key; - } - - @Override - public SelectionKeySimulator register(SelectorSimulator sel, int opt, Object att) { - synchronized (lock){ - lock = sel; - key = sel.register(this, opt, att); - return key; - } - } - - public String toString(){ - return "ChannelSimulator ("+ownerName+")"; - } - - -}
--- a/src/pathfinder/mergetest/channels2/test/testEditor.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,43 +0,0 @@ -package pathfinder.mergetest.channels2.test; - -import java.io.IOException; - -import pathfinder.mergetest.channels2.*; - -public class testEditor extends Thread{ - private NetworkSimulator<String> ns; - private int semaIP; - - public testEditor(NetworkSimulator<String> _ns, String name, int _ip){ - super(name); - ns = _ns; - semaIP = _ip; - } - - public void run(){ - SocketChannelSimulator<String> channel = SocketChannelSimulator.<String>open(); - - ns.writeLog("try to connect to SessionManager whose ip is "+semaIP, 1); - while (!channel.connect(semaIP)){ - ns.writeLog("SeMa not listen to socket yet, wait", 1); - Thread.yield(); - } - ns.writeLog("successes to connect", 1); - - channel.write("from "+getName()+": hello"); - ns.writeLog("wrote packet", 1); - - String packet=null; - try { - packet = channel.read(); - } catch (IOException e) { - e.printStackTrace(); - ns.writeLog("read Exception!", 1); - return; - } - - ns.writeLog("gets return string==> `"+packet+"\'", 1); - - ns.writeLog("testEditor exits.", 1); - } -}
--- a/src/pathfinder/mergetest/channels2/test/testNetworkSimulator.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,69 +0,0 @@ -package pathfinder.mergetest.channels2.test; - -import java.util.ArrayList; -import java.util.Random; -import pathfinder.mergetest.channels2.NetworkSimulator; - -public class testNetworkSimulator { - private NetworkSimulator<String> ns; - private ArrayList<testSeMa> semaList; - private ArrayList<testSeMaSlave> semasList; - private ArrayList<testEditor> editorList; - private int NoSemaMaster; - private int NoSemaSlave; - private int NoEditor; - - public static void main(String[] args){ - testNetworkSimulator testns = new testNetworkSimulator(1, 0, 2); - - testns.startTest(); - } - - - /** Constructor. */ - public testNetworkSimulator(int sm, int ss,int e){ - //ns = new NetworkSimulator<String>(); - ns = NetworkSimulator.singleton(); - semaList = new ArrayList<testSeMa>(); - semasList = new ArrayList<testSeMaSlave>(); - editorList = new ArrayList<testEditor>(); - NoSemaMaster = sm; - NoSemaSlave = ss; - NoEditor = e; - } - - public void startTest(){ - Random rand = new Random(); - for (int i=0; i<NoSemaMaster; i++){ - testSeMa sm = new testSeMa(ns, "SeMa"+i, i); - semaList.add(sm); - sm.start(); - } - for (int i=0; i<NoSemaSlave; i++){ - testSeMaSlave sm = new testSeMaSlave(ns, "SeMaS"+i, i+NoSemaMaster, rand.nextInt(NoSemaMaster)); - semasList.add(sm); - sm.start(); - } - for (int i=0; i<NoEditor; i++){ - testEditor te = new testEditor(ns, "Editor"+i, rand.nextInt(NoSemaMaster+NoSemaSlave)); - editorList.add(te); - te.start(); - } - - Checker(); - - try { - for (testEditor te: editorList) - te.join(); - ns.writeLog("main: all clients exited.", 1); - - } catch (InterruptedException e) { - e.printStackTrace(); - } - System.exit(0); - } - - public void Checker(){ - - } -}
--- a/src/pathfinder/mergetest/channels2/test/testSeMa.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,74 +0,0 @@ -package pathfinder.mergetest.channels2.test; - -import java.io.IOException; -import java.util.LinkedList; - -import pathfinder.mergetest.channels2.*; - - -public class testSeMa extends Thread{ - - int IP; - boolean running=true; - NetworkSimulator<String> ns; - LinkedList<SocketChannelSimulator<String>> channels; - - public testSeMa(NetworkSimulator<String> _ns, String name, int ip){ - super(name); - IP = ip; - ns = _ns; - channels = new LinkedList<SocketChannelSimulator<String>>(); - } - public void init(){ - - } - - public void run() { - SelectorSimulator selector = new SelectorSimulator(); - ServerChannelSimulator<String> scs = ServerChannelSimulator.<String>open(); - scs.bind(IP); - //selector.register(scs, SelectionKeySimulator.OP_ACCEPT); - scs.register(selector, SelectionKeySimulator.OP_ACCEPT, null); - ns.writeLog("SessionManager starts mainroutin.", 1); - - /* Main Loop */ - while(running){ - - try { selector.select(); } - catch (IOException e) { e.printStackTrace();} - - for(SelectionKeySimulator key : selector.selectedKeys()){ - - if(key.isAcceptable()){ - ns.writeLog("gets acceptable channel", 1); - ServerChannelSimulator<String> sc = (ServerChannelSimulator<String>) key.channel(); - SocketChannelSimulator<String> channel; - try { - channel = sc.accept(); - } catch (IOException e) { - e.printStackTrace(); - continue; - } - //selector.register(channel, SelectionKeySimulator.OP_READ); - channel.register(selector, SelectionKeySimulator.OP_READ); - ns.writeLog("accepts a client.", 1); - - }else if(key.isReadable()){ - ns.writeLog("gets readable channel", 1); - //SelectableChannelSimulator<String> channel = key.channel(); - SocketChannelSimulator<String> channel = (SocketChannelSimulator<String>) key.channel(); - String packet; - try { - packet = channel.read(); - } catch (IOException e) { - e.printStackTrace(); - continue; - } - ns.writeLog("receives String==> `"+packet+"\'", 1); - channel.write("from SeMa"+this.getName()+": world"); - } - } - } - - } -}
--- a/src/pathfinder/mergetest/channels2/test/testSeMaSlave.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,110 +0,0 @@ -package pathfinder.mergetest.channels2.test; - -import java.io.IOException; -import java.util.LinkedList; - -import pathfinder.mergetest.channels2.*; - - -public class testSeMaSlave extends Thread{ - - int ownIP; - int masterIP; - boolean running=true; - NetworkSimulator<String> ns; - LinkedList<ClientInfo> cis; - - public testSeMaSlave(NetworkSimulator<String> _ns, String name, int oIP, int mIP){ - super(name); - ownIP = oIP; - masterIP = mIP; - ns = _ns; - cis = new LinkedList<ClientInfo>(); - } - public void init(){ - - } - - @SuppressWarnings("unchecked") - public void run() { - SelectorSimulator selector = new SelectorSimulator(); - - SocketChannelSimulator<String> masterCH = connectToMaster(selector); - ServerChannelSimulator<String> scs = ServerChannelSimulator.<String>open(); - scs.bind(ownIP); - - //selector.register(scs, SelectionKeySimulator.OP_ACCEPT); - scs.register(selector, SelectionKeySimulator.OP_ACCEPT); - //selector.register(masterCH, SelectionKeySimulator.OP_READ); - masterCH.register(selector, SelectionKeySimulator.OP_READ); - ns.writeLog("SessionManager starts mainroutin.", 1); - - /* Main Loop */ - while(running){ - - try { selector.select(); } - catch (IOException e) { e.printStackTrace();} - - for(SelectionKeySimulator key : selector.selectedKeys()){ - - if(key.isAcceptable()){ - ns.writeLog("gets acceptable channel", 1); - ServerChannelSimulator<String> sc = (ServerChannelSimulator<String>) key.channel(); - SocketChannelSimulator<String> channel; - try { - channel = sc.accept(); - } catch (IOException e) { - e.printStackTrace(); - continue; - } - channel.register(selector, SelectionKeySimulator.OP_READ, null); - ns.writeLog("accepts a client.", 1); - - }else if(key.isReadable()){ - ns.writeLog("gets readable channel", 1); - SocketChannelSimulator<String> channel = (SocketChannelSimulator<String>) key.channel(); - String packet; - try { - packet = channel.read(); - } catch (IOException e) { - e.printStackTrace(); - continue; - } - if (channel==masterCH){ - ns.writeLog("receives String from master ==> `"+packet+"\'", 1); - for (ClientInfo ci: cis){ - if (!packet.matches(ci.str)) continue; - ci.channel.write("from "+this.getName()+": loopback packet from master ==>`"+packet+"\'"); - } - }else{ - ns.writeLog("receives String==> `"+packet+"\'", 1); - //channel.write("from "+this.getName()+": slave"); - masterCH.write("from "+this.getName()+": receive String==>`"+packet+"\' from editor"); - cis.add(new ClientInfo(packet, channel)); - } - } - } - } - - } - private SocketChannelSimulator<String> connectToMaster(SelectorSimulator _selector) { - SocketChannelSimulator<String> channel = SocketChannelSimulator.<String>open(); - channel.register(_selector, SelectionKeySimulator.OP_READ, null); - ns.writeLog("is connecting to masterSeMa whose ip is"+masterIP, 1); - while(!channel.connect(masterIP)){ - ns.writeLog("SeMa not listen to socket yet, wait", 1); - Thread.yield(); - } - ns.writeLog("connecting was successful.", 1); - - return channel; - } -} -class ClientInfo{ - String str; - SocketChannelSimulator<String> channel; - ClientInfo(String _str, SocketChannelSimulator<String> _channel){ - str = _str; - channel = _channel; - } -} \ No newline at end of file
--- a/src/pathfinder/mergetest/test/RepCommandOptimizeTest.java Fri Sep 03 09:43:01 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,181 +0,0 @@ -package pathfinder.mergetest.test; - -import java.util.LinkedList; -import pathfinder.mergetest.Text; - - -import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; - - - -public class RepCommandOptimizeTest { - //テストコマンド (command,string,lineno,eid) - static String[] test1 = { - Integer.toString(REP.REPCMD_DELETE),"d","1","1", - Integer.toString(REP.REPCMD_INSERT),"B","3","2", - Integer.toString(REP.REPCMD_INSERT),"B","1","3", - Integer.toString(REP.REPCMD_INSERT),"C","3","4", - Integer.toString(REP.REPCMD_DELETE),"d","13","5", - Integer.toString(REP.REPCMD_DELETE),"d","3","6", - Integer.toString(REP.REPCMD_DELETE),"d","1","7", - Integer.toString(REP.REPCMD_INSERT),"A","5","8", - Integer.toString(REP.REPCMD_DELETE),"d","1","9", - Integer.toString(REP.REPCMD_DELETE),"d","1","10", - Integer.toString(REP.REPCMD_INSERT),"B","10","11", - Integer.toString(REP.REPCMD_INSERT),"B","1","12", - Integer.toString(REP.REPCMD_INSERT),"C","3","13", - Integer.toString(REP.REPCMD_DELETE),"d","2","14", - Integer.toString(REP.REPCMD_DELETE),"d","3","15", - Integer.toString(REP.REPCMD_DELETE),"d","1","16", - Integer.toString(REP.REPCMD_INSERT),"A","3","17", - Integer.toString(REP.REPCMD_DELETE),"d","1","18" - }; - - 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" - }; - - static private String[] text2d = { - "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" - }; - - static LinkedList<REPCommand> cmdlist = new LinkedList<REPCommand>(); - - void makeCommand(String[] str){ - int seq = 0; - try{ - for( int i = 0;i < str.length; i+=4){ - int cmd = Integer.parseInt(str[i]); - int lineno = Integer.parseInt(str[i+2]); - - int sid = Integer.parseInt(str[i+3]); - int eid = sid; - cmdlist.add(new REPCommand(cmd, sid, eid, seq++, lineno, str[i+1].length(), str[i+1])); - } - }catch(Exception e){ - e.printStackTrace(); - } - } - - LinkedList<REPCommand> optimize(LinkedList<REPCommand> inp){ - LinkedList<REPCommand> output = new LinkedList<REPCommand>(); - output = reverse(inp); - - for(int i = 0; i < output.size(); i++){ - REPCommand r = output.get(i); - switch(r.cmd){ - case REP.REPCMD_INSERT: - break; - case REP.REPCMD_DELETE: - optimizedAdd(output,r,i); - break; - } - } - return reverse(output); - - } - private LinkedList<REPCommand> reverse(LinkedList<REPCommand> outp) { - LinkedList<REPCommand> reverse = new LinkedList<REPCommand>(); - for(REPCommand r : outp){ - reverse.addFirst(r); - } - return reverse; - } - private void optimizedAdd(LinkedList<REPCommand> output, REPCommand r, int ln) { - int lineno = r.lineno; - int minln = output.size(); - for(int i = ln; i < output.size(); i++){ - REPCommand s = output.get(i); - if(s.cmd==REP.REPCMD_INSERT) { - if(s.lineno < lineno){ - lineno --; - }else if(s.lineno == lineno){ - if(s.lineno < minln){ - minln = s.lineno; - } - lineNumberCorrection(output,minln,i,ln); - output.remove(r); - output.remove(s); - ln--; - break; - } - }else if(s.cmd==REP.REPCMD_DELETE){ - if(s.lineno < lineno){ - lineno ++; - } - }else{ - System.out.println("There are no such commands."); - } - } - } - - private void lineNumberCorrection(LinkedList<REPCommand> opt, int ln, int count, int r){ - for(int i = r; i < count; i++){ - REPCommand o = opt.get(i); - if(ln < o.lineno) o.lineno -= 1; - } - } - - void printCmdList(LinkedList<REPCommand> before){ - System.out.println("---------- CmdList ----------"); - for(REPCommand r: before){ - System.out.println(r.toString()); - } - System.out.println("Total = " + before.size()); - } - - static Text text1 = new Text(text1d); - static Text text2 = new Text(text2d); - - void edit(LinkedList<REPCommand> before, Text txt){ - for(REPCommand r : before){ - txt.edit(r); - } - } - - void printText(){ - System.out.println("------------ Text1 -----------"); - text1.printAllText(); - System.out.println("------------ Text2 -----------"); - text2.printAllText(); - } - - void check(LinkedList<REPCommand> before, LinkedList<REPCommand> after){ - int diff; - System.out.println("----------- Check -----------"); - if(!text1.equals(text2)){ - System.out.println("It isn't equal."); - }else{ - System.out.println("Equal."); - } - diff = before.size() - after.size(); - System.out.println("difference = " + diff); - - } - - public static void main(String[] s){ - - RepCommandOptimizeTest rco = new RepCommandOptimizeTest(); - LinkedList<REPCommand> result = new LinkedList<REPCommand>(); - rco.makeCommand(test1); - java.util.Collections.shuffle(cmdlist); - rco.printCmdList(cmdlist); - rco.edit(cmdlist,text1); - - result = rco.optimize(cmdlist); - rco.printCmdList(result); - rco.edit(result,text2); - - rco.printText(); - rco.check(cmdlist,result); - } -}