changeset 57:f055e65c7e3c

*** empty log message ***
author pin
date Tue, 24 Jul 2007 17:40:07 +0900
parents 679491395f8f
children 0eaf3f3ecadb
files src/sample/merge/PermEnum.java src/sample/merge/TestEditor.java src/sample/merge/TestLoop.java src/sample/merge/TestSessionManager.java src/sample/merge/TestTranslate2.java src/sample/merge/Translate.java
diffstat 6 files changed, 345 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/sample/merge/PermEnum.java	Tue Jul 24 17:40:07 2007 +0900
@@ -0,0 +1,52 @@
+package sample.merge;
+
+import java.util.Enumeration;
+
+public class PermEnum implements Enumeration {
+	private int N;
+	private int c[], k;
+	private Object[] objs;
+	
+	public PermEnum(Object[] items){
+		N = items.length;
+		c = new int [N+1];
+		for(int i = 0; i<=N; i++) c[i] = i;
+		objs = items;
+		k = 1;
+	}
+	
+	public boolean hasMoreElements(){
+		return (k < N);
+	}
+	
+	public Object nextElement(){
+		int i = 0;
+		if((k & 1) != 0) i = c[k];
+		
+		Object tmp = objs[k];
+		objs[k] = objs[i];
+		objs[i] = tmp;
+		
+		k = 1;
+		while(c[k] == 0) c[k] = k++;
+		c[k]--;
+		return objs;
+	}
+	
+	public static void main(String[] args){
+		String[] strs = {"1", "2", "3", "4"};
+		//String[] strs = {"a"};
+		System.out.println("N="+strs.length);
+		Enumeration e = new PermEnum(strs);
+		int count = 0;
+		while(e.hasMoreElements()) {
+			String[] a = (String[])e.nextElement();
+			System.out.print("{" + a[0]);
+			for(int i = 1; i<a.length; i++) System.out.print(", "+a[i]);
+			System.out.println("}");
+			count++;
+		}
+	System.out.
+	println("count="+count);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/sample/merge/TestEditor.java	Tue Jul 24 17:40:07 2007 +0900
@@ -0,0 +1,163 @@
+package sample.merge;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.channels.SocketChannel;
+import java.util.Enumeration;
+import java.util.LinkedList;
+import java.util.List;
+
+import remoteeditor.command.REPCommand;
+import remoteeditor.command.REPCommandEvent;
+import remoteeditor.command.REPCommandListener;
+import remoteeditor.network.REP;
+import remoteeditor.network.REPPacketReceive;
+import remoteeditor.network.REPPacketSend;
+
+public class TestEditor implements REPCommandListener, Runnable{
+	private SocketChannel sc;
+	private REPPacketReceive repreceive;
+	private REPPacketSend repsend;
+	private String myEditorName;
+	private int myseq;
+	private int mysid;
+	private int myeid;
+	List <REPCommand> userCmdList = new LinkedList<REPCommand>();
+	List <REPCommand> tokenCmdList = new LinkedList<REPCommand>();
+	Translate trans = new Translate(userCmdList, tokenCmdList);
+	private String myString = " ";
+	
+	List <String> myText = new LinkedList<String>();
+	private TestSessionManager sessionManager;
+	
+	public static void main(String[] args){
+		TestEditor editorA = new TestEditor("EditorA");
+		editorA.init();
+		editorA.join();
+		editorA.put();
+		editorA.select();
+		
+		TestEditor editorB = new TestEditor("EditorB");
+		editorB.init();
+		editorB.join();
+		editorB.select();
+		
+		//editorA.send(REP.REP_INSERT_CMD);
+		//editorB.send(REP.REP_INSERT_CMD);
+		//editorA.send(REP.REP_INSERT_CMD);
+		//editorB.send(REP.REP_INSERT_CMD);
+		//editorA.send(REP.REP_INSERT_CMD);
+		//editorB.send(REP.REP_INSERT_CMD);
+		
+		Integer[] cmdkind = {REP.REP_INSERT_CMD, REP.REP_DELETE_CMD, REP.REP_REPLACE_CMD};
+		Enumeration e = new PermEnum(cmdkind);
+		while(e.hasMoreElements()){
+			Integer[] a = (Integer[])e.nextElement();
+//			System.out.print("{" + a[0].intValue());
+//			for(int i=1; i<a.length; i++) System.out.print(", "+a[i]);
+//			System.out.println("}");
+			for(int i = 0; i<a.length; i++){
+				editorA.send(a[i].intValue());
+			}
+			System.out.println("");
+		}
+	}
+	
+	public TestEditor(String str){
+		myEditorName = str;
+	}
+
+	public void init(){
+		int port = 8765;
+		String host = "localhost";
+		InetSocketAddress addr = new InetSocketAddress(host, port);
+		try {
+			sc = SocketChannel.open();
+			sc.configureBlocking(true);
+			sc.connect(addr);
+			while(!sc.finishConnect()){
+				System.out.println("afro");
+			}
+		}catch (IOException e) {
+			e.printStackTrace();
+		}
+		repreceive = new REPPacketReceive(sc);
+		repsend = new REPPacketSend(sc);
+		
+	}
+	
+	public void join(){
+		repsend.send(new REPCommand(REP.REP_JOIN_CMD, 0, 0, myseq, 0, 0, "")); myseq++;
+		REPCommand command = repreceive.unpack();
+		myeid = command.eid; 
+		trans.setMyEID(myeid);
+	}
+	
+	public void put(){
+		repsend.send(new REPCommand(REP.REP_PUT_CMD, 0, myeid, myseq, 0, 4, "afro")); myseq++;
+		REPCommand command = repreceive.unpack();
+		mysid = command.sid;
+	}
+	
+	public void select(){
+		mysid = 1;
+		repsend.send(new REPCommand(REP.REP_SELECT_CMD, mysid, myeid, myseq, 0, 0, "")); myseq++;
+		REPCommand command = repreceive.unpack();
+		repreceive.addCommandListener(this);
+	}
+	
+	public void send(int cmdKind){
+		REPCommand sendCommand = new REPCommand(cmdKind, mysid, myeid, myseq, 10, myEditorName.length(), myEditorName);
+		myString = sendCommand.string + myString;
+		//System.out.println(myEditorName + " : " + myString);
+		trans.addUserList(sendCommand);
+		repsend.send(sendCommand);
+		myseq++;
+	}
+	
+	public void send(REPCommand command){
+		System.out.println(command.toString());
+	}
+
+	public void CommandReceived(REPCommandEvent event) {
+		trans.addTokenList(event.getCommand());
+		//System.out.println(myEditorName + " : userCmdList : " + userCmdList.toString());
+		//System.out.println(myEditorName + " : tokenCmdList : " + tokenCmdList.toString());
+		trans.merge();
+		REPCommand tokenCmd = null;
+		if (tokenCmdList.size() != 0) {
+			tokenCmd = tokenCmdList.get(0);
+			repsend.send(tokenCmd);
+			if(tokenCmd.lineno == 10){
+				myString = tokenCmd.string + myString;
+			}else if(tokenCmd.lineno == 11){
+				myString = myString + tokenCmd.string;
+			}
+			tokenCmdList.remove(0);
+		}
+		System.out.println(myEditorName + " : " + myString);
+	}
+
+	public void run() {
+		Integer[] cmdkind = {REP.REP_INSERT_CMD, REP.REP_DELETE_CMD, REP.REP_REPLACE_CMD};
+		//Integer[] cmdkind = {REP.REP_INSERT_CMD, REP.REP_DELETE_CMD};
+		Enumeration e = new PermEnum(cmdkind);
+		//TestEditor editorA = new TestEditor("EditorA");
+		while(e.hasMoreElements()){
+			Integer[] a = (Integer[])e.nextElement();
+			for(int i = 0; i<a.length; i++){
+				REPCommand command = new REPCommand(a[i].intValue(), 1, 1, myseq, 10, 1, myEditorName); myseq++;
+				send(command);
+			}
+		}
+	}
+
+	public int getEID() {
+		return myeid;		
+	}
+
+	public void setSessionManager(TestSessionManager sessionManager) {
+		// TODO Auto-generated method stub
+		this.sessionManager = sessionManager;
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/sample/merge/TestLoop.java	Tue Jul 24 17:40:07 2007 +0900
@@ -0,0 +1,9 @@
+package sample.merge;
+
+public class TestLoop {
+	public static void main(String[] args){
+		while(true){
+			System.out.println("test");
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/sample/merge/TestSessionManager.java	Tue Jul 24 17:40:07 2007 +0900
@@ -0,0 +1,63 @@
+package sample.merge;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import remoteeditor.command.REPCommand;
+import remoteeditor.command.REPCommandEvent;
+
+public class TestSessionManager implements Runnable{
+	List <REPCommandPlus> commandList = new LinkedList<REPCommandPlus>();
+	List <TestEditor> editorList = new LinkedList<TestEditor>();
+	
+	public synchronized void send(REPCommand command, int EID){
+		REPCommandPlus commandp = new REPCommandPlus(command, EID);
+		commandList.add(commandp);
+	}
+
+	public void run() {
+		
+		while(true){
+			if(commandList.size() > 0){
+				int eid = commandList.get(0).getEID();
+				for(int i = 0; i < editorList.size()-1; i++){
+					if(eid == editorList.get(i).getEID()){
+						System.out.println("test");
+						editorList.get(i+1).CommandReceived(new REPCommandEvent(commandList.get(0).getCommand()));
+					}
+				}
+				if(eid == editorList.get(editorList.size()-1).getEID()){
+					editorList.get(0).CommandReceived(new REPCommandEvent(commandList.get(0).getCommand()));
+				}
+			}
+			try {
+				Thread.sleep(100);
+			} catch (InterruptedException e) {
+				e.printStackTrace();
+			}
+		}
+	}
+
+	public void addEditor(TestEditor editor) {
+		editorList.add(editor);
+	}
+}
+
+class REPCommandPlus{
+	private REPCommand command;
+	private int EID;
+	
+	public REPCommandPlus(REPCommand command, int eid) {
+		this.command = command;
+		this.EID = eid;
+	}
+	
+	public int getEID(){
+		return EID;
+	}
+	
+	public REPCommand getCommand(){
+		return command;
+	}
+}
+
--- a/src/sample/merge/TestTranslate2.java	Tue Jul 17 15:26:37 2007 +0900
+++ b/src/sample/merge/TestTranslate2.java	Tue Jul 24 17:40:07 2007 +0900
@@ -1,5 +1,57 @@
 package sample.merge;
 
+import java.util.Enumeration;
+
+import remoteeditor.command.REPCommand;
+import remoteeditor.network.REP;
+
 public class TestTranslate2 {
-	
+	public static void main(String[] args){
+		TestEditor editorA = new TestEditor("EditorA");
+		TestEditor editorB = new TestEditor("EditorB");
+		TestSessionManager sessionManager = new TestSessionManager();
+		sessionManager.addEditor(editorA);
+		sessionManager.addEditor(editorB);
+		
+		Thread threadSM = new Thread(sessionManager);
+		//threadSM.start();
+		
+		Thread threadA = new Thread(editorA);
+		Thread threadB = new Thread(editorB);
+		
+		threadA.start();
+		threadB.start();
+		
+	}
+/*	public static void editorA(){
+		new Thread(new Runnable(){
+			public void run() {
+				Integer[] cmdkind = {REP.REP_INSERT_CMD, REP.REP_DELETE_CMD, REP.REP_REPLACE_CMD};
+				Enumeration e = new PermEnum(cmdkind);
+				TestEditor editorA = new TestEditor("EditorA");
+				while(e.hasMoreElements()){
+					Integer[] a = (Integer[])e.nextElement();
+					for(int i = 0; i<a.length; i++){
+						REPCommand command = new REPCommand(a[i].intValue(), 1, 1, 0, 10, 1, "A");
+						editorA.send(command);
+					}
+				}
+			}
+		}).start();
+	}
+	public static void editorB(){
+		new Thread(new Runnable(){
+			public void run() {
+				Integer[] cmdkind = {REP.REP_INSERT_CMD, REP.REP_DELETE_CMD, REP.REP_REPLACE_CMD};
+				Enumeration e = new PermEnum(cmdkind);
+				TestEditor editorB = new TestEditor("EditorB");
+				while(e.hasMoreElements()){
+					Integer[] a = (Integer[])e.nextElement();
+					for(int i = 0; i<a.length; i++){
+						editorB.send(new REPCommand(a[i].intValue(), 1, 1, 0, 10, 1, "B"));
+					}
+				}
+			}
+		}).start();
+	}*/
 }
--- a/src/sample/merge/Translate.java	Tue Jul 17 15:26:37 2007 +0900
+++ b/src/sample/merge/Translate.java	Tue Jul 24 17:40:07 2007 +0900
@@ -142,4 +142,9 @@
 //			}
 //		}
 	}
+
+	public void setMyEID(int myeid2) {
+		// TODO Auto-generated method stub
+		myeid = myeid2;
+	}
 }