changeset 59:aa47ea5bdac9

*** empty log message ***
author pin
date Tue, 11 Sep 2007 18:05:15 +0900
parents 0eaf3f3ecadb
children 27123649e11f
files src/sample/merge/SMRoutingTable.java src/sample/merge/TestEditor.java src/sample/merge/TestSessionManager.java src/sample/merge/TestTranslate2.java
diffstat 4 files changed, 137 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/sample/merge/SMRoutingTable.java	Tue Sep 11 18:05:15 2007 +0900
@@ -0,0 +1,34 @@
+package sample.merge;
+
+import java.util.LinkedList;
+import java.util.List;
+
+public class SMRoutingTable {
+
+	private List<TestEditor> editorList = new LinkedList<TestEditor>();
+
+	public TestEditor getNextEditor(int eid) {
+		return null ;
+	}
+
+	public void setNext(int eid) {
+		
+	}
+
+	public void addEditor(TestEditor editor) {
+		editorList.add(editor);
+	}
+
+	public TestEditor getNextEditor(TestEditor editor) {
+		int size = editorList.size();
+		TestEditor lastEditor = ((LinkedList<TestEditor>) editorList).getLast();
+		if(size == 0){
+			return null;
+		}else if(editor == lastEditor){
+			return editorList.get(0);
+		}else{
+			return editorList.get(editorList.indexOf(editor)+1);
+		}
+	}
+	
+}
--- a/src/sample/merge/TestEditor.java	Wed Jul 25 14:49:02 2007 +0900
+++ b/src/sample/merge/TestEditor.java	Tue Sep 11 18:05:15 2007 +0900
@@ -14,7 +14,7 @@
 import remoteeditor.network.REPPacketReceive;
 import remoteeditor.network.REPPacketSend;
 
-public class TestEditor implements REPCommandListener, Runnable{
+public class TestEditor extends Thread implements REPCommandListener, Runnable{
 	private SocketChannel sc;
 	private REPPacketReceive repreceive;
 	private REPPacketSend repsend;
@@ -24,6 +24,8 @@
 	private int myeid;
 	List <REPCommand> userCmdList = new LinkedList<REPCommand>();
 	List <REPCommand> tokenCmdList = new LinkedList<REPCommand>();
+	List <REPCommand> userQueue = new LinkedList<REPCommand>();
+	List <String> myTextList = new LinkedList<String>();
 	Translate trans = new Translate(userCmdList, tokenCmdList);
 	private String myString = " ";
 	
@@ -34,13 +36,13 @@
 	public static void main(String[] args){
 		TestEditor editorA = new TestEditor("EditorA");
 		editorA.init();
-		editorA.join();
+		editorA.rep_join();
 		editorA.put();
 		editorA.select();
 		
 		TestEditor editorB = new TestEditor("EditorB");
 		editorB.init();
-		editorB.join();
+		editorB.rep_join();
 		editorB.select();
 		
 		//editorA.send(REP.REP_INSERT_CMD);
@@ -68,6 +70,13 @@
 		myEditorName = str;
 	}
 
+	public TestEditor(int sid, int eid, String string) {
+		mysid = sid; myeid = eid; myEditorName = string;
+		for(int i = 0; i < 20; i++){
+			myTextList.add(" ");
+		}
+	}
+
 	public void init(){
 		int port = 8765;
 		String host = "localhost";
@@ -87,7 +96,7 @@
 		
 	}
 	
-	public void join(){
+	public void rep_join(){
 		repsend.send(new REPCommand(REP.REP_JOIN_CMD, 0, 0, myseq, 0, 0, "")); myseq++;
 		REPCommand command = repreceive.unpack();
 		myeid = command.eid; 
@@ -118,6 +127,8 @@
 	
 	public void send(REPCommand command){
 		System.out.println(command.toString());
+		//sessionManager.send(command, myeid);
+		sessionManager.send(command, this);
 	}
 
 	public void CommandReceived(REPCommandEvent event) {
@@ -138,7 +149,8 @@
 		}
 		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};
@@ -152,6 +164,23 @@
 			}
 		}
 	}
+*/
+	
+	public void run() {
+		while(true){
+			if(userQueue.size() > 0){
+				REPCommand command = userQueue.get(0);
+				myTextList.add(command.lineno, command.string);
+				if(command.string == "exit") {
+					System.out.println(myTextList.toString());
+					break;
+				}
+				send(command);
+				userCmdList.add(command);
+				userQueue.remove(0);
+			}
+		}
+	}
 
 	public int getEID() {
 		return myeid;		
@@ -166,12 +195,26 @@
 	}
 
 	public void setSessionManager(TestSessionManager sessionManager) {
-		// TODO Auto-generated method stub
 		this.sessionManager = sessionManager;
 	}
 
 	public String getEditorName() {
-		// TODO Auto-generated method stub
 		return myEditorName;
 	}
+
+	public synchronized void changeText(int kind, int lineno, String string) {
+		userQueue.add(new REPCommand(REP.REP_INSERT_CMD, mysid, myeid, getMyseq(), lineno, string.length(), string));
+	}
+
+	private int getMyseq() {
+		myseq++;
+		return myseq;
+	}
+
+	public void CommandReceived(REPCommand command) {
+		if(command.eid != myeid){
+			myTextList.add(command.string);
+		}
+	}
+
 }
--- a/src/sample/merge/TestSessionManager.java	Wed Jul 25 14:49:02 2007 +0900
+++ b/src/sample/merge/TestSessionManager.java	Tue Sep 11 18:05:15 2007 +0900
@@ -6,52 +6,55 @@
 import remoteeditor.command.REPCommand;
 import remoteeditor.command.REPCommandEvent;
 
-public class TestSessionManager implements Runnable{
+public class TestSessionManager extends Thread{
 	List <REPCommandPlus> commandList = new LinkedList<REPCommandPlus>();
 	List <TestEditor> editorList = new LinkedList<TestEditor>();
+	SMRoutingTable table = new SMRoutingTable();
+	private boolean go = true;
 	
-	public synchronized void send(REPCommand command, int EID){
-		REPCommandPlus commandp = new REPCommandPlus(command, EID);
+	public synchronized void send(REPCommand command, TestEditor editor){
+		REPCommandPlus commandp = new REPCommandPlus(command, editor);
 		commandList.add(commandp);
 	}
 
 	public void run() {
-		
-		while(true){
+		while(go){
 			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();
+				//int eid = commandList.get(0).getEID();
+				TestEditor editor = commandList.get(0).getEditor();
+				TestEditor nextEditor = table.getNextEditor(editor);
+				if (nextEditor != null) nextEditor.CommandReceived(commandList.get(0).getCommand());
+				commandList.remove(0);
 			}
 		}
 	}
 
 	public void addEditor(TestEditor editor) {
 		editorList.add(editor);
+		table.addEditor(editor);
+	}
+
+	public void setRun(boolean b) {
+		go = b;
 	}
 }
 
 class REPCommandPlus{
 	private REPCommand command;
 	private int EID;
+	private TestEditor editor;
 	
 	public REPCommandPlus(REPCommand command, int eid) {
 		this.command = command;
 		this.EID = eid;
 	}
 	
+	public REPCommandPlus(REPCommand command2, TestEditor editor) {
+		this.command = command2;
+		this.EID = editor.getEID();
+		this.editor = editor;
+	}
+
 	public int getEID(){
 		return EID;
 	}
@@ -59,5 +62,9 @@
 	public REPCommand getCommand(){
 		return command;
 	}
+	
+	public TestEditor getEditor(){
+		return editor;
+	}
 }
 
--- a/src/sample/merge/TestTranslate2.java	Wed Jul 25 14:49:02 2007 +0900
+++ b/src/sample/merge/TestTranslate2.java	Tue Sep 11 18:05:15 2007 +0900
@@ -7,70 +7,38 @@
 
 public class TestTranslate2 {
 	public static void main(String[] args){
-		TestEditor editorA = new TestEditor("EditorA");
-		TestEditor editorB = new TestEditor("EditorB");
+		TestEditor editorA = new TestEditor(1, 1, "EditorA");
+		TestEditor editorB = new TestEditor(1, 2, "EditorB");
 		TestSessionManager sessionManager = new TestSessionManager();
+		
 		sessionManager.addEditor(editorA);
 		sessionManager.addEditor(editorB);
-		
-		//TestEditor[] editors = {editorA, editorB};
+		sessionManager.start();
 		
-		//Thread threadSM = new Thread(sessionManager);
-		//threadSM.start();
-		
-		Thread threadA = new Thread(editorA);
-		Thread threadB = new Thread(editorB);
+		editorA.setSessionManager(sessionManager);
+		editorB.setSessionManager(sessionManager);
+
+		editorA.start();
+		editorB.start();
 		
-		/*
-		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();
-			for(int i =0; i<a.length; i++){
-				Enumeration e2 = new PermEnum(editors);
-				while(e2.hasMoreElements()){
-					TestEditor[] a2 = (TestEditor[])e2.nextElement();
-					for(int j = 0; j<a2.length; j++){
-						REPCommand command = new REPCommand(a[i].intValue(), 1, 1, 0, 10, 1, a2[j].getEditorName());
-						a2[j].send(command);
-					}
-				}
-			}
-		}*/
+		/* test */
+		int lineno = 10;
+		editorA.changeText(REP.REP_INSERT_CMD, lineno, "testA");
+		editorA.changeText(REP.REP_INSERT_CMD, lineno, "testA2");
+
+		editorB.changeText(REP.REP_INSERT_CMD, lineno, "testB");
+		editorB.changeText(REP.REP_INSERT_CMD, lineno, "testB2");
 		
-		threadA.start();
-		threadB.start();
+		editorA.changeText(REP.REP_INSERT_CMD, lineno, "exit");
+		editorB.changeText(REP.REP_INSERT_CMD, lineno, "exit");
+		
+		try {
+			editorA.join(); // Thread.join() このスレッドが終了するのを待機します。(REPのjoinとは別)
+			editorB.join(); // このスレッドが終了するのを待機します。
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		}
+		sessionManager.setRun(false);
 		
 	}
-/*	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();
-	}*/
 }