Mercurial > hg > RemoteEditor > REPSessionManager
changeset 419:7ff127c8ad64
(no commit message)
author | one |
---|---|
date | Tue, 20 Jan 2009 18:39:02 +0900 |
parents | d1bfcff0cdd2 |
children | 5c95a9020e31 |
files | test/editortest/LogTarget.java test/editortest/Main.java test/editortest/REPCommandReceiver.java test/editortest/REPEditor.java test/editortest/REPSimpleEditor.java test/editortest/REPTextWithJTextArea.java test/editortest/SimpleEditorForREPEditor.java test/editortest/TestEditor2.java test/editortest/TestInterManagerSession2.java test/editortest/TestMerger.java test/editortest/TestSessionManager2.java test/editortest/TestSimpleEditor.java test/editortest/UserSimulator.java |
diffstat | 13 files changed, 262 insertions(+), 975 deletions(-) [+] |
line wrap: on
line diff
--- a/test/editortest/LogTarget.java Wed Dec 31 18:29:35 2008 +0900 +++ b/test/editortest/LogTarget.java Tue Jan 20 18:39:02 2009 +0900 @@ -2,6 +2,6 @@ public interface LogTarget { - void printLog(String msg); + public void printLog(String msg); }
--- a/test/editortest/Main.java Wed Dec 31 18:29:35 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -package test.editortest; - -public class Main { - - public static void main(String[] args){ - REPSimpleEditor frame = new REPSimpleEditor(); - frame.setDefaultCloseOperation(REPSimpleEditor.EXIT_ON_CLOSE); - frame.setVisible(true); - } - -}
--- a/test/editortest/REPCommandReceiver.java Wed Dec 31 18:29:35 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,71 +0,0 @@ -package test.editortest; - -import java.io.IOException; -import java.nio.channels.SelectionKey; - -import rep.REPCommand; -import rep.channel.REPSelectionKey; -import rep.channel.REPSelector; -import rep.channel.REPSocketChannel; - -public class REPCommandReceiver extends Thread{ - - REPSocketChannel<REPCommand> channel; - boolean running = true; - private boolean inputLock=false; - long timeout = 1; - private int syncCounter=0; - - public REPCommandReceiver(REPSocketChannel<REPCommand> channel, REPSelector<REPCommand> selector){ - this.channel = channel; - } - - public void run(){ - try { - mainloop(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private void mainloop() throws IOException { - - channel.configureBlocking(false); - REPSelector<REPCommand> selector = REPSelector.create(); - channel.register(selector, SelectionKey.OP_READ); - while(running) { - if (inputLock) { - // No user input during merge mode (optional) - if (selector.select(0)>0) { - handle(channel.read()); - } - continue; - } else if (selector.select(timeout)<=0) { - if (syncCounter>0) { - syncText(); // send the master editor buffer to clients. - } - userInput(); - } - // selector(timeout) returns 0, but it may contain readable channel.. - for(REPSelectionKey<REPCommand> key : selector.selectedKeys1()) { - REPSocketChannel<REPCommand> ch = key.channel1(); - handle(ch.read()); - } - } - } - - private void userInput() { - // TODO Auto-generated method stub - - } - - private void syncText() { - // TODO Auto-generated method stub - - } - - private void handle(REPCommand read) { - // TODO Auto-generated method stub - - } -}
--- a/test/editortest/REPEditor.java Wed Dec 31 18:29:35 2008 +0900 +++ b/test/editortest/REPEditor.java Tue Jan 20 18:39:02 2009 +0900 @@ -27,7 +27,7 @@ private int eid; private int sid; private REPText repText; - private boolean hasInputLock; + private boolean hasInputLock = false; private boolean master; private boolean syncEnable = true; private LogTarget logTarget; @@ -53,7 +53,7 @@ addUserInput(new REPCommand(REP.REPCMD_INSERT_USER, 0, 0, 0, event.getLineno(), event.getText())); } - private void addUserInput(final REPCommand command) { + public void addUserInput(final REPCommand command) { Runnable runner = new Runnable(){ public void run(){ userCommand.add(command); @@ -136,6 +136,7 @@ private void handle(REPCommand command) { Logger.print(logTarget, command); +// if(inputLock) Logger.print(logTarget, command); if(command == null) return; switch(command.cmd){ case REPCMD_DELETE: @@ -244,4 +245,8 @@ logTarget = target; } + public REPText getREPText() { + return repText; + } + }
--- a/test/editortest/REPSimpleEditor.java Wed Dec 31 18:29:35 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,227 +0,0 @@ -package test.editortest; - -import java.awt.BorderLayout; -import java.awt.Dimension; -import java.awt.Font; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.io.IOException; -import java.net.InetSocketAddress; - -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JScrollPane; -import javax.swing.JSplitPane; -import javax.swing.JTextArea; -import javax.swing.JTextField; -import javax.swing.JToolBar; -import javax.swing.event.DocumentEvent; -import javax.swing.event.DocumentListener; -import javax.swing.text.BadLocationException; -import javax.swing.text.Document; - -import rep.REP; -import rep.REPCommand; -import rep.REPCommandPacker; -import rep.channel.REPSocketChannel; -import test.Text; - -public class REPSimpleEditor extends JFrame implements DocumentListener, ActionListener, LogTarget{ - - /** - * - */ - private static final long serialVersionUID = 1L; - private String BR = System.getProperty("line.separator"); - private Document document; - private JTextArea textArea; - private int seq; - private int eid; - private int sid; - private JButton putButton; - private JButton joinButton; - private REPText repText; - private TestEditor2 testEditor; - private JSplitPane splitPane; - private JScrollPane scrollPane1; - private JTextArea console; - private JScrollPane scrollPane2; - private JTextField lineField; - private JTextField textField; - private REPSocketChannel<REPCommand> channel; - - public REPSimpleEditor(String title){ - super(title); - setSize(new Dimension(640, 480)); - setLayout(new BorderLayout()); - - setToolBar(); - setEditor(); - setConsole(); - setSplitPane(); - } - - public REPSimpleEditor(){ - this("Sample Editor"); - } - - private void setToolBar() { - JToolBar toolbar = new JToolBar(); - putButton = new JButton("put"); - joinButton = new JButton("join"); - - putButton.addActionListener(this); - joinButton.addActionListener(this); - - JLabel label1 = new JLabel("line"); - JLabel label2 = new JLabel("text"); - lineField = new JTextField(); - textField = new JTextField(); - - textField.addActionListener(this); - - toolbar.add(putButton); - toolbar.add(joinButton); - toolbar.addSeparator(); - toolbar.add(label1); - toolbar.add(lineField); - toolbar.add(label2); - toolbar.add(textField); - - add(toolbar, BorderLayout.NORTH); - } - - private void setEditor(){ - textArea = new JTextArea(); - textArea.setEditable(false); - textArea.setFont(new Font("Monaco", Font.PLAIN, textArea.getFont().getSize())); - document = textArea.getDocument(); - document.addDocumentListener(this); - - scrollPane1 = new JScrollPane(textArea); - - repText = new REPTextImpl(textArea); - } - - private void setConsole(){ - console = new JTextArea(); - console.setFont(new Font("Monaco", Font.PLAIN, console.getFont().getSize())); - console.setEditable(false); - scrollPane2 = new JScrollPane(console); - } - - private void setSplitPane(){ - splitPane = new JSplitPane(); - splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); - splitPane.add(scrollPane1, JSplitPane.TOP); - splitPane.add(scrollPane2, JSplitPane.BOTTOM); - splitPane.setDividerLocation(300); - add(splitPane, BorderLayout.CENTER); - } - - public void setButtonEnabled(boolean b) { - putButton.setEnabled(b); - joinButton.setEnabled(b); - } - - private REPCommand createREPCommand(int offset, int length) { - REPCommand command = null; - try { - int lineno = textArea.getLineOfOffset(offset); - int lineStart = textArea.getLineStartOffset(lineno); - int lineEnd = textArea.getLineEndOffset(lineno); - String text = textArea.getText(lineStart, lineEnd-lineStart); - command = new REPCommand(REP.REPCMD_INSERT_USER, sid, eid, seq++, lineno, text); - } catch (BadLocationException e1) { - e1.printStackTrace(); - } - return command; - } - - public void changedUpdate(DocumentEvent e) { - Logger.print(e); - } - - public void insertUpdate(DocumentEvent e) { - int offset = e.getOffset(); - int length = e.getLength(); - createREPCommand(offset, length); - } - - public void removeUpdate(DocumentEvent e) { - Logger.print(e); - } - - public void actionPerformed(ActionEvent e) { - if(e.getSource() == putButton){ - repPut(); - }else if(e.getSource() == joinButton){ - repJoin(); - }else if(e.getSource() == textField){ - testEditor.addUserInput(new REPCommand(REP.REPCMD_INSERT_USER, 0, 0, 0, 0, textField.getText())); - lineField.setText(""); - textField.setText(""); - } - } - - private void repJoin() { -// testEditor = new TestEditor2("TestEditor", "localhost", 8766, false); -// testEditor.setREPText(repText); -// testEditor.setLogTarget(this); -// testEditor.start(); - } - - private void repPut() { - setMasterText(); - connectToSessionManager(); -// testEditor = new TestEditor2("TestEditor", "localhost", 8766, true); -// testEditor.setText(repText.list()); -// testEditor.setREPText(repText); -// testEditor.setLogTarget(this); -// testEditor.start(); - } - - private void setMasterText() { - textArea.append("AAAAA" + BR); - textArea.append("BBBBB" + BR); - textArea.append("CCCCC" + BR); - textArea.append("DDDDD" + BR); - } - - private void connectToSessionManager() { - InetSocketAddress semaIP = new InetSocketAddress("localhost", 8766); - try { - channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker()); - } catch (IOException e) { - e.printStackTrace(); return; - } - try { - while (!channel.connect(semaIP)){ - Logger.print("SeMa not listen to socket yet, wait"); - } - } catch (IOException e) { - e.printStackTrace(); return; - } - - } - - public void printLog(String msg) { - console.append(msg + BR); - } - - public REPText getREPText() { - return repText; - } - - public void setText(Text text) { - for(String str : text){ - textArea.append(str + BR); - } - } - - public void setTestEditor(TestEditor2 testEditor2) { - testEditor = testEditor2; - } - -}
--- a/test/editortest/REPTextWithJTextArea.java Wed Dec 31 18:29:35 2008 +0900 +++ b/test/editortest/REPTextWithJTextArea.java Tue Jan 20 18:39:02 2009 +0900 @@ -12,6 +12,8 @@ import javax.swing.event.DocumentListener; import javax.swing.text.BadLocationException; +import rep.REPCommand; + public class REPTextWithJTextArea implements REPText, DocumentListener, ActionListener { private JTextArea textArea; @@ -152,5 +154,14 @@ listener.textInserted(new REPTextEvent(lineno, text)); } } + + public void userInsert(REPCommand command) { + int lineno = command.lineno; + String text = command.string; + insert(lineno, text); + for(REPTextListener listener : textListenerList){ + listener.textInserted(new REPTextEvent(lineno, text)); + } + } }
--- a/test/editortest/SimpleEditorForREPEditor.java Wed Dec 31 18:29:35 2008 +0900 +++ b/test/editortest/SimpleEditorForREPEditor.java Tue Jan 20 18:39:02 2009 +0900 @@ -33,6 +33,7 @@ private String BR = System.getProperty("line.separator"); private JButton deleteButton; private JButton insertButton; + private REPEditor repEditor; public SimpleEditorForREPEditor(String title){ super(title); @@ -106,16 +107,16 @@ } } - private void repPut() { - REPEditor repEditor = new REPEditor(new REPTextWithJTextArea(textArea, lineField, textField, deleteButton, insertButton), true); + public void repPut() { + repEditor = new REPEditor(new REPTextWithJTextArea(textArea, lineField, textField, deleteButton, insertButton), true); repEditor.start(); repEditor.setLogTarget(this); putButton.setEnabled(false); joinButton.setEnabled(false); } - private void repJoin() { - REPEditor repEditor = new REPEditor(new REPTextWithJTextArea(textArea, lineField, textField, deleteButton, insertButton), false); + public void repJoin() { + repEditor = new REPEditor(new REPTextWithJTextArea(textArea, lineField, textField, deleteButton, insertButton), false); repEditor.start(); repEditor.setLogTarget(this); putButton.setEnabled(false); @@ -131,4 +132,8 @@ editor.setVisible(true); } + public REPEditor getREPEditor() { + return repEditor; + } + }
--- a/test/editortest/TestEditor2.java Wed Dec 31 18:29:35 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,366 +0,0 @@ -package test.editortest; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.nio.channels.SelectionKey; -import java.util.LinkedList; -import java.util.List; - -import rep.REP; -import rep.REPCommand; -import rep.REPCommandPacker; -import rep.SessionManager; -import rep.channel.REPLogger; -import rep.channel.REPSelectionKey; -import rep.channel.REPSelector; -import rep.channel.REPSocketChannel; -import test.Text; - - -/** - * @author kono - * Basic Remote Editor client implementation - * should support multi-session - * currently multi-session requires new channel, that is - * only one session for this editor. - */ -public class TestEditor2 extends Thread{ - private InetSocketAddress semaIP; - private REPLogger ns; - private int seq = 0; - public Text text; - public LinkedList<REPCommand> cmds; - private int eid = 0; - private int sid = 0; - REPSocketChannel<REPCommand> channel; - REPCommand nop = new REPCommand(REP.REPCMD_NOP, 0, 0, 0, 0, ""); - boolean running = true; - long timeout = 1; - private String name; - private boolean inputLock=false; - public boolean detached=false; - public boolean master=false; - REPCommand quit=null; - private int syncCounter=0; - private boolean hasInputLock=false; - private int port; - private REPSelector<REPCommand> selector; - private boolean syncEnable=true; - private LogTarget logTarget; - private REPText repText; - private REPSimpleEditor simpleEditor; - - - public TestEditor2(String name, String _host,int _port, boolean master){ - super(name); - LinkedList<REPCommand>cmdList = new LinkedList<REPCommand>(); - String[] txts = { - "aaa", "bbb", // "ccc", "ddd", "eee", - }; - port = _port; - semaIP = new InetSocketAddress(_host, _port); - ns = REPLogger.singleton(); - this.name = name; - cmds = cmdList; - if (master) { - this.master=true; - text = new Text(txts); - cmds.add(new REPCommand(REP.SMCMD_PUT,0,0,0,0,name+"-file")); - cmds.add(new REPCommand(REP.REPCMD_INSERT_USER,0,0,0,0,"m0")); - cmds.add(new REPCommand(REP.REPCMD_DELETE_USER,0,0,0,0,"m0")); - cmds.add(new REPCommand(REP.SMCMD_QUIT,0,0,0,0,"")); - } else { - text = new Text(new String[0]); - cmds.add(new REPCommand(REP.SMCMD_JOIN,0,0,0,0,name)); - cmds.add(new REPCommand(REP.REPCMD_INSERT_USER,0,0,0,0,"c0")); - cmds.add(new REPCommand(REP.REPCMD_DELETE_USER,0,0,0,0,"c0")); - } - - simpleEditor = new REPSimpleEditor(); - simpleEditor.setVisible(true); - simpleEditor.setButtonEnabled(false); - simpleEditor.setTestEditor(this); - simpleEditor.setText(text); - repText = simpleEditor.getREPText(); - logTarget = simpleEditor; - } - - public TestEditor2(String name, String _host,int _port, boolean master, - String[] txts,LinkedList<REPCommand> cmdList){ - super(name); - port = _port; - semaIP = new InetSocketAddress(_host, _port); - ns = REPLogger.singleton(); - this.name = name; - cmds = cmdList; - if (master) { - this.master=true; - text = new Text(txts); - } else { - text = new Text(new String[0]); - } - } - - public void run(){ - /* - * Create Socket and connect to the session manager - */ - try { - channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker()); - } catch (IOException e) { return; } - - ns.writeLog("try to connect to SessionManager whose ip is "+semaIP+" "+name, 1); - try { - while (!channel.connect(semaIP)){ - ns.writeLog("SeMa not listen to socket yet, wait", 1); - } - } catch (IOException e) { return; } - ns.writeLog("successes to connect "+name); - /* - * Start editor main loop - * public REPCommand(REP cmd,int sid,int eid, int seq, int lineno, String string) - */ - try { - mainloop(); - } catch (IOException e) { - } - } - - /* - * Editor main loop with input lock - */ - private void mainloop() throws IOException { - - channel.configureBlocking(false); - REPSelector<REPCommand> selector = REPSelector.create(); - channel.register(selector, SelectionKey.OP_READ); - this.selector = selector; - while(running) { - if (inputLock) { - // No user input during merge mode (optional) - if (selector.select(0)>0) { - handle(channel.read()); - } - continue; - } else if (selector.select(timeout)<=0) { - if (syncCounter>0) { - syncText(); // send the master editor buffer to clients. - } - userInput(); - } - // selector(timeout) returns 0, but it may contain readable channel.. - for(REPSelectionKey<REPCommand> key : selector.selectedKeys1()) { - REPSocketChannel<REPCommand> ch = key.channel1(); - handle(ch.read()); - } - } - } - - private void syncText() { - /* - * Send delete/insert one at a time to synchronize - * all clients. SYNC is requested by the session manager. - */ - if (syncCounter>text.size()) { - SessionManager.logger.writeLog("Sync Completed."); - syncCounter=0; - } else { - if (inputLock) return; - int i=syncCounter-1; - REPCommand del = new REPCommand(REP.REPCMD_DELETE_USER,sid,eid,0,i, text.get(i)); - REPCommand ins = new REPCommand(REP.REPCMD_INSERT_USER,sid,eid,0,i, text.get(i)); - sendCommand(del); - sendCommand(ins); - syncCounter++; - } - } - - /* - * Simulate User Input - */ - private void userInput() { - REPCommand cmd = cmds.poll(); - if (cmd!=null) { - Logger.print(logTarget, "User Input : " + cmd); - switch(cmd.cmd) { - case REPCMD_INSERT_USER: - text.insert(cmd.lineno, cmd.string); - repText.insert(cmd.lineno, cmd.string); - sendCommand(cmd); - break; - case REPCMD_DELETE_USER: - String del = text.delete(cmd.lineno); - del = repText.delete(cmd.lineno); - cmd.setString(del); - sendCommand(cmd); - break; - case SMCMD_QUIT: - /* - * start termination phase 1 by the master editor. - * after this command do not send any user input. - * clients simply disconnect from the session manager. - */ - cmds.clear(); - cmd.eid = -1; - quit = cmd; - break; - case SMCMD_JOIN: - case SMCMD_PUT: - sendCommand(cmd); - /* - * To prevent confusion, stop user input until the ack - */ - inputLock = true; // wait until ACK - break; - default: - assert(false); - } - } else { - if(syncCounter==0) { - // no more command to send, and we don't have syncCounter - timeout = 0; - if (quit!=null) { - if (quit.eid==-1) - sendCommand(quit); - else - forwardCommand(quit); - quit=null; - } - } - } - } - - - private void sendCommand(REPCommand cmd1) { - REPCommand cmd = new REPCommand(cmd1); - cmd.setSEQID(seq++); - cmd.setEID(eid); - cmd.setSID(sid); - ns.writeLog(name +" send "+cmd); - channel.write(cmd); - } - - private void forwardCommand(REPCommand cmd1) { - REPCommand cmd = new REPCommand(cmd1); - ns.writeLog(name +" forward "+cmd); - //Logger.print(logTarget, "write : " + cmd); - channel.write(cmd); - } - - private void handle(REPCommand cmd) { - if (cmd==null) return; - ns.writeLog(name +": read "+cmd + " textsize="+text.size()); - Logger.print(logTarget, "read : " + cmd); - switch(cmd.cmd) { - case REPCMD_INSERT : - if (cmd.eid!=eid) { - text.insert(cmd.lineno, cmd.string); - repText.insert(cmd.lineno, cmd.string); - } - forwardCommand(cmd); - break; - case REPCMD_DELETE : - if (cmd.eid!=eid) { - String del=""; - if(cmd.lineno<text.size()) { - del = text.delete(cmd.lineno); - del = repText.delete(cmd.lineno); - } - cmd.setString(del); - } - forwardCommand(cmd); - break; - case REPCMD_NOP : - case REPCMD_INSERT_ACK : - case REPCMD_DELETE_ACK : - forwardCommand(cmd); - break; - case REPCMD_CLOSE : - case REPCMD_CLOSE_2 : - assert(false); - break; - - case SMCMD_JOIN_ACK : - sid = cmd.sid; - eid = cmd.eid; - name += "(eid="+eid+",sid="+sid+")"; - inputLock = false; - break; - case SMCMD_PUT_ACK : - sid = cmd.sid; - eid = cmd.eid; - name += "(eid="+eid+",sid="+sid+")"; - inputLock = false; - break; - case SMCMD_QUIT : - if (cmd.eid!=eid) - quit = cmd; - else // eid=-1 means do not forward but send it. - quit = new REPCommand(REP.SMCMD_QUIT_2, - sid, -1, seq, 0, ""); - timeout=1; - // stop input processing after this command - cmds.clear(); - break; - case SMCMD_START_MERGE : - // lock user input during merge (optional) - inputLock = hasInputLock; - cmd.cmd = REP.SMCMD_START_MERGE_ACK; - sendCommand(cmd); - break; - case SMCMD_END_MERGE : - inputLock = false; - break; - case SMCMD_QUIT_2 : - if (cmd.eid!=eid) { - forwardCommand(cmd); - } else { - cmd.cmd = REP.SMCMD_QUIT_2_ACK; - sendCommand(cmd); - } - running = false; - break; - case SMCMD_SYNC: - // start contents sync with newly joined editor - cmd.cmd = REP.SMCMD_SYNC_ACK; - forwardCommand(cmd); - //if (cmd.eid==eid) { - if (master && syncEnable ) { - syncCounter = 1; - timeout = 1; - } - break; - default: - assert(false); - break; - } - } - - - public int getPort() { - return port; - } - - public synchronized void setCommand(LinkedList<REPCommand> cmds) { - this.cmds = cmds; - timeout=1; - if(selector!=null) selector.wakeup(); - } - - public void setText(List<String> list) { - text = new Text(list); - } - - public void setLogTarget(LogTarget target) { - logTarget = target; - } - - public void setREPText(REPText repText) { - this.repText = repText; - } - - public void addUserInput(REPCommand command) { - cmds.add(command); - if(selector!=null) selector.wakeup(); - } -}
--- a/test/editortest/TestInterManagerSession2.java Wed Dec 31 18:29:35 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,162 +0,0 @@ -package test.editortest; - - -import java.util.LinkedList; - -import rep.REP; -import rep.REPCommand; -import rep.SessionManager; -import rep.channel.REPServerSocketChannel; -import rep.gui.SessionManagerEvent; - -public class TestInterManagerSession2 extends TestSessionManager2 { - - /* - * All test is performed in localhost, so all session manager - * should have differenct port number each other. - * Test Pattern List - * Connect port for each editor - * Master/client flag for each editor - * Editor or slave session manager must be started by - * master session managers using syncExec. - */ - - public int slavePort[] = {masterPort,masterPort,masterPort}; - public int editorPort[] = {masterPort,masterPort+1,masterPort+2}; - public boolean editorMaster[] = {true,false,false,false}; - private LinkedList<REPCommand> editorStartCmds; - - private SessionManagerEvent ev2[] = { - new SessionManagerEvent() { - public void exec(SessionManager manager) { - int i = sessionManagers.length; - for(SessionManager slave:slaveSessionManagers) { - if (slave.getParentPort()==masterPort) { - logger.writeLog("Start slave "+slave); - i = startSessionManager(slave,i,masterPort + i); - } - } - } - }, - new SessionManagerEvent() { - public void exec(SessionManager manager) { - manager.connectSessionManager(host); - } - }, - new SessionManagerEvent() { - public void exec(SessionManager manager) { - // try to make a loop - if (false) { - sessionManagers[0].connectSessionManager(host, - manager.getPort()); - } - manager.connectSessionManager(host); - manager.execAfterConnect( - new SessionManagerEvent() { - public void exec(SessionManager manager) { - for(SessionManager m:sessionManagers) { - startEditor(m); - } - for(SessionManager m:slaveSessionManagers) { - startEditor(m); - } - editors[0].setCommand(editorStartCmds); - } - } - ); - } - - } - }; - //private int inscnt=2; - - private void startEditor(SessionManager m) { - for(TestEditor2 editor:editors) { - if(editor.getPort()==m.getPort()) { - logger.writeLog("Start client "+editor); - editor.start(); - } - } - } - - /* - * Create all editors, master session managers and slave session - * managers with specified port. All instances are not started yet. - */ - - public TestInterManagerSession2(int sm, int ss, int e) { - super(sm,ss,e); - - sessionManagers = new SessionManager[sm]; - slaveSessionManagers = new SessionManager[ss]; - editors = new TestEditor2[e]; - for(int i=0;i<sm;i++) { - sessionManagers[i] = new SessionManager(); - } - for(int i=0;i<ss;i++) { - int port = slavePort[i%slavePort.length]; - slaveSessionManagers[i] = new SessionManager(); - slaveSessionManagers[i].setParentPort(port); - } - for(int i=0;i<e;i++) { - int port = editorPort[i%editorPort.length]; - boolean master = editorMaster[i%editorMaster.length]; - // TestEditor extends Thread - LinkedList<REPCommand>cmds = new LinkedList<REPCommand>(); - cmds.add(new REPCommand(REP.SMCMD_JOIN,0,0,0,0,"")); - //if (inscnt-->0) cmds.add(new REPCommand(REP.REPCMD_INSERT,0,0,0,0,"m0")); - editors[i] = new TestEditor2("Editor"+i,host,port,master); - //editors[i].setCommand(cmds); - } - setupEditor0(); - } - - private void setupEditor0() { - /* - * do not startup Editor0 until SessionManagers are ready. - * Define pending command and set null command for now. - */ - LinkedList<REPCommand>cmds = new LinkedList<REPCommand>(); - //cmds.add(new REPCommand(REP.SMCMD_JOIN,0,0,0,0,"Editor0-file")); - cmds.add(new REPCommand(REP.SMCMD_PUT,0,0,0,0,"Editor0-file")); - cmds.add(new REPCommand(REP.REPCMD_INSERT_USER,0,0,0,0,"m0")); - cmds.add(new REPCommand(REP.REPCMD_DELETE_USER,0,0,0,0,"m0")); - cmds.add(new REPCommand(REP.SMCMD_QUIT,0,0,0,0,"")); - editorStartCmds = cmds; - LinkedList<REPCommand>nullcmds = new LinkedList<REPCommand>(); - editors[0].setCommand(nullcmds); - } - - @Override - public void setSMEvent(SessionManager s,int i) { - if (i<ev2.length) { - s.syncExec(ev2[i]); - } - return ; - } - - @Override - protected void startTest() { - int i = 0; - for(SessionManager master:sessionManagers) { - logger.writeLog("Start master "+i+" "+master); - i = startSessionManager(master,i, masterPort + i); - } - } - - public static void main(String[] args){ - /* - * set simulation mode - * isSimulation=true thread base simulation for PathFinder - * isSimulation=false socket based communication mode - */ - REPServerSocketChannel.isSimulation = false; - //Editor.noMergeMode = true; - // At least 3 TestEditors are required. - TestInterManagerSession2 test = new TestInterManagerSession2(1, 2, 3); - logger.setLogLevel(5); - test.startTest(); - } - - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/editortest/TestMerger.java Tue Jan 20 18:39:02 2009 +0900 @@ -0,0 +1,132 @@ +package test.editortest; + +import java.io.IOException; +import java.util.LinkedList; + +import rep.REP; +import rep.REPCommand; +import rep.Session; +import rep.channel.REPSelectionKey; +import rep.channel.REPSocketChannel; +import rep.handler.REPNode; +import rep.handler.Translator; +import rep.optimizers.NullOptimizer; + +public class TestMerger extends REPNode{ + Translator trans; + private int seq; + private LinkedList<REPCommand> commandList; + private LinkedList<REPCommand> othersCommandList; + + public static void main(String[] args){ + TestMerger test = new TestMerger(); + test.setCommands(); + test.start(); + } + + public TestMerger(){ + eid = 1; + sid = 1; + trans = new Translator(eid, new NullOptimizer()); + } + + private void setCommands() { + commandList = new LinkedList<REPCommand>(); + commandList.add(new REPCommand(REP.REPCMD_INSERT, sid, eid, seq++, 10, "AAAAA")); + commandList.add(new REPCommand(REP.REPCMD_INSERT, sid, eid, seq++, 10, "BBBBB")); + othersCommandList = new LinkedList<REPCommand>(); + othersCommandList.add(new REPCommand(REP.REPCMD_INSERT, sid, eid+1, seq++, 10, "CCCCC")); + } + + private void start() { + for(REPCommand command : commandList){ + trans.transSendCmd(command); + } + for(REPCommand command : othersCommandList){ + trans.transSendCmd(command); + } + for(int i = 0; i < commandList.size(); i++){ + trans.catchOwnCommand(this); + System.out.println(); + } + } + + @Override + public void cancel(REPSocketChannel<REPCommand> channel1) { + // TODO Auto-generated method stub + + } + + @Override + public void checkWaitingCommandInMerge() { + // TODO Auto-generated method stub + + } + + @Override + public void forwardedCommandManage(REPCommand command) { + // TODO Auto-generated method stub + + } + + @Override + public String getLocalHostName() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void handle(REPCommand command, REPSelectionKey<REPCommand> key) + throws IOException { + // TODO Auto-generated method stub + + } + + @Override + public boolean isMerging() { + // TODO Auto-generated method stub + return false; + } + + @Override + public void joinAck(REPCommand sendCommand, int sid) { + // TODO Auto-generated method stub + + } + + @Override + public boolean manage(REPCommand command) { + // TODO Auto-generated method stub + return false; + } + + @Override + public void selectSession(REPCommand sendCommand, Session session) { + // TODO Auto-generated method stub + + } + + @Override + public void send(REPCommand command) { + // TODO Auto-generated method stub + Logger.print(command); + } + + @Override + public void sendWithSeq(REPCommand command) { + // TODO Auto-generated method stub + + } + + @Override + public int seq() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public void setQuit2(REPCommand receivedCommand) { + // TODO Auto-generated method stub + + } +}
--- a/test/editortest/TestSessionManager2.java Wed Dec 31 18:29:35 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,128 +0,0 @@ -package test.editortest; - -import java.io.IOException; - -import rep.SessionManager; -import rep.channel.REPLogger; -import rep.channel.REPServerSocketChannel; -import rep.gui.SessionManagerEvent; -import rep.gui.SessionManagerGUI; -import rep.handler.Editor; -import test.sematest.TestGUI; - - -public class TestSessionManager2 { - - static public REPLogger logger = REPLogger.singleton(); - public int masterPort = 8766; - public String host = "localhost"; - public SessionManager sessionManagers[]; - public SessionManager slaveSessionManagers[]; - public TestEditor2 editors[]; - - /* - * All test is performed in localhost, so all session manager - * should have differenct port number each other. - */ - - /* - * Test Pattern List - * Connect port for each editor - * Master/client flag for each editor - * Editor or slave session manager must be started by - * master session managers using syncExec. - */ - public int editorPort[] = {masterPort,masterPort,masterPort}; - public boolean editorMaster[] = {true,false,false,false}; - private SessionManagerEvent ev1[] = { - new SessionManagerEvent() { - // executed before first select(); - public void exec(SessionManager manager) { - for(TestEditor2 editor:editors) { - editor.start(); - } - int i = sessionManagers.length; - for(SessionManager slave:slaveSessionManagers) { - i = startSessionManager(slave,i,masterPort + i); - } - } - }}; - - /* - * Create all editors, master session managers and slave session - * managers with specified port. All instances are not started yet. - */ - - public TestSessionManager2(int sm, int ss, int e) { - sessionManagers = new SessionManager[sm]; - slaveSessionManagers = new SessionManager[ss]; - editors = new TestEditor2[e]; - for(int i=0;i<sm;i++) { - sessionManagers[i] = new SessionManager(); - } - for(int i=0;i<ss;i++) { - slaveSessionManagers[i] = new SessionManager(); - } - for(int i=0;i<e;i++) { - int port = editorPort[i%editorPort.length]; - boolean master = editorMaster[i%editorMaster.length]; - // TestEditor extends Thread - editors[i] = new TestEditor2("Editor"+i,host,port,master); - } - } - - /* - * start session manager. sm.init(port,guit) is a mainloop, so - * we need Thread here. - */ - public int startSessionManager(final SessionManager sm,int i,int port) { - final SessionManagerGUI gui = new TestGUI(sm); - final int port1 = port; - logger.writeLog("TestSessionManager.startSessionManager() : start SessionManager"); - // syncExec does not wake selector, do this before run(). - sm.setReceivePort(port1); - setSMEvent(sm,i); - Runnable start = new Runnable() { - public void run() { - try { - sm.init(port1,gui); - } catch (IOException e) { - } catch (InterruptedException e) { - } - } - }; - new Thread(start).start(); - return i+1; - } - - - public void setSMEvent(SessionManager s,int i) { - if (i<ev1.length) { - s.syncExec(ev1[i]); - } - return ; - } - - protected void startTest() { - int i = 0; - for(SessionManager master:sessionManagers) { - i = startSessionManager(master,i, masterPort + i); - } - } - - public static void main(String[] args){ - /* - * set simulation mode - * isSimulation=true thread base simulation for PathFinder - * isSimulation=false socket based communication mode - */ - REPServerSocketChannel.isSimulation = false; - Editor.noMergeMode = false; - // At least 3 TestEditors are required. - TestSessionManager2 test = new TestSessionManager2(1, 0, 3); - logger.setLogLevel(5); - test.startTest(); - } - - -}
--- a/test/editortest/TestSimpleEditor.java Wed Dec 31 18:29:35 2008 +0900 +++ b/test/editortest/TestSimpleEditor.java Tue Jan 20 18:39:02 2009 +0900 @@ -2,6 +2,7 @@ import java.io.IOException; +import rep.handler.Editor; import test.AutoSelectManager; public class TestSimpleEditor { @@ -28,14 +29,21 @@ Thread thread = new Thread(runnable); thread.start(); - SimpleEditorForREPEditor editor1 = new SimpleEditorForREPEditor("test1"); + SimpleEditorForREPEditor editor1 = new SimpleEditorForREPEditor("editor1"); editor1.setVisible(true); - SimpleEditorForREPEditor editor2 = new SimpleEditorForREPEditor("test2"); + SimpleEditorForREPEditor editor2 = new SimpleEditorForREPEditor("editor2"); editor2.setVisible(true); - SimpleEditorForREPEditor editor3 = new SimpleEditorForREPEditor("test3"); + SimpleEditorForREPEditor editor3 = new SimpleEditorForREPEditor("editor3"); editor3.setVisible(true); + + UserSimulator user = new UserSimulator("Starter"); + user.add(editor1); user.add(editor2); user.add(editor3); + user.setVisible(true); + + Editor.noMergeMode = false; + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/editortest/UserSimulator.java Tue Jan 20 18:39:02 2009 +0900 @@ -0,0 +1,91 @@ +package test.editortest; + +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.LinkedList; + +import javax.swing.JButton; +import javax.swing.JFrame; + +import rep.REP; +import rep.REPCommand; + +public class UserSimulator extends JFrame implements ActionListener{ + + /** + * + */ + private static final long serialVersionUID = 1L; + private JButton startButton; + private LinkedList<SimpleEditorForREPEditor> editorList; + private LinkedList<REPCommand> userInputList; + private JButton initButton; + + public UserSimulator(String title){ + super(title); + + editorList = new LinkedList<SimpleEditorForREPEditor>(); + + setButton(); + pack(); + } + + private void setUserInput() { + userInputList = new LinkedList<REPCommand>(); + userInputList.add(new REPCommand(REP.REPCMD_INSERT_USER, 0, 0, 0, 0, "AAAAA")); + userInputList.add(new REPCommand(REP.REPCMD_INSERT_USER, 0, 0, 0, 1, "BBBBB")); + userInputList.add(new REPCommand(REP.REPCMD_INSERT_USER, 0, 0, 0, 2, "CCCCC")); + } + + private void init(){ + setUserInput(); + editorList.get(0).repPut(); + for(int i = 1; i < editorList.size(); i++){ + editorList.get(i).repJoin(); + } + initButton.setEnabled(false); + } + + private void setButton() { + setLayout(new FlowLayout()); + + initButton = new JButton("init"); + startButton = new JButton("start"); + add(initButton); + add(startButton); + + initButton.addActionListener(this); + startButton.addActionListener(this); + } + + public void add(SimpleEditorForREPEditor editor) { + editorList.add(editor); + } + + public void actionPerformed(ActionEvent e) { + if(e.getSource() == startButton){ + userStart(); + }else if(e.getSource() == initButton){ + init(); + } + } + + private void userStart() { + UserThread user = new UserThread(); + user.start(); + } + + class UserThread extends Thread{ + public void run(){ +// for(REPCommand command : userInputList){ + for(int i = 0; i < 5; i++){ + for(SimpleEditorForREPEditor editor : editorList){ + REPCommand command = new REPCommand(REP.REPCMD_INSERT_USER, 0, 0, 0, i, editor.getTitle() + ":" + i); + ((REPTextWithJTextArea) editor.getREPEditor().getREPText()).userInsert(command); + } + } + } + } + +}