changeset 125:34b15dfcb83e

UsersSimulator
author kent
date Tue, 25 Dec 2007 20:07:37 +0900
parents f18510fc40e2
children b56b5950cb18
files src/pathfinder/mergetest/EditorSimulator.java src/pathfinder/mergetest/EditorSimulatorAsync.java src/pathfinder/mergetest/NetworkSimulator.java src/pathfinder/mergetest/NetworkSimulatorwithSeMa.java src/pathfinder/mergetest/NetworkSimulatorwithoutSeMa.java src/pathfinder/mergetest/SeMaSimulator.java src/pathfinder/mergetest/TestMerger.java src/pathfinder/mergetest/UsersSimulator.java
diffstat 8 files changed, 149 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/pathfinder/mergetest/EditorSimulator.java	Tue Dec 25 16:21:30 2007 +0900
+++ b/src/pathfinder/mergetest/EditorSimulator.java	Tue Dec 25 20:07:37 2007 +0900
@@ -9,7 +9,7 @@
 public class EditorSimulator extends Thread{
 	private int eid;
 	private int seq;
-	private boolean isOwner;
+	//private boolean isOwner;
 	private NetworkSimulator<REPCommand> ns;
 	private ChannelSimulator<REPCommand> cs;
 	private Queue<REPCommand> CmdList;
@@ -28,7 +28,7 @@
 	}
 
 	public void setOwner(boolean f){
-		isOwner = f;
+		//isOwner = f;
 	}
 	synchronized public void finish(){
 		running = false;
@@ -38,32 +38,36 @@
 		System.out.println("Editor"+eid+" start.");
 
 		// Send All Command that is included CmdList.
-		sendAllCommand();
+		//sendAllCommand();
 
 		// MainLoop, 
 		while(running){
 			REPCommand cmd = cs.read();
 			REPCommand[] cmds;
 
-			System.out.println("\tEditor"+eid+" catch command from "+cmd.eid+" NO."+cmd.seq);
+			//System.out.println("\tEditor"+eid+" catch command from "+cmd.eid+" NO."+cmd.seq);
 
 			if (cmd.eid==eid){
 				cmds = translater.catchOwnCommand(cmd);
 				for (int i=0; i<cmds.length; i++){
 					REPCommand tmp = cmds[i];
-					System.out.println("\t\tEditor"+eid+" edit text. ");
+					//System.out.println("\t\tEditor"+eid+" edit text. ");
 					text.edit(tmp);
 				}
 				/* 終了条件  */
 				if (cmd.cmd==REP.SMCMD_QUIT){
-					System.out.println("\tEditor"+eid+" catch QUIT command emited by itself.");
+					//System.out.println("\tEditor"+eid+" catch QUIT command emited by itself.");
 					running=false; break;
 				}
+			} else if (cmd.eid==-1){
+				/* 制御プロセスからの指令  */
+				System.out.println("\tEditor"+eid+" send command.");
+				sendOneCommand(cmd);
 			} else {
 				cmds = translater.transReceiveCmd(cmd);
 				for (int i=0; i<cmds.length; i++){
 					cmd = cmds[i];
-					System.out.println("\t\tEditor"+eid+" edit text and pass Cmd. ");
+					//System.out.println("\t\tEditor"+eid+" edit text and pass Cmd. ");
 					text.edit(cmd);
 					cs.write(new REPCommand(cmd));
 				}
@@ -73,18 +77,21 @@
 		System.out.println("Editor"+eid+" finish.");
 	}
 
-	private void sendOneCommand() {
+	private void sendOneCommand(REPCommand cmd) {
 		REPCommand[] cmds;
-		REPCommand cmd = CmdList.poll();
+		if (cmd==null) cmd = CmdList.poll();
+		if (cmd==null) return;
+
 		cmd.eid = eid;
 		cmds = translater.transSendCmd(cmd);
-		cmd.setString("this is inserted or replaced by Editor"+cmd.eid+":"+cmd.seq);
+		cmd.setString("inserted by Editor"+cmd.eid+":"+cmd.seq);
 
-		if (isOwner) cmd.setThroughMaster(true);
+		//if (isOwner) cmd.setThroughMaster(true);
 		for (int i=0; i<cmds.length; i++){
 			text.edit(cmds[i]);
 			cs.write(new REPCommand(cmds[i]));
 		}
+		Thread.yield();
 	}
 	private void sendAllCommand() {
 		REPCommand[] cmds;
--- a/src/pathfinder/mergetest/EditorSimulatorAsync.java	Tue Dec 25 16:21:30 2007 +0900
+++ b/src/pathfinder/mergetest/EditorSimulatorAsync.java	Tue Dec 25 20:07:37 2007 +0900
@@ -38,7 +38,12 @@
 			//終了条件
 			if (cmd.eid==eid && cmd.cmd==REP.SMCMD_QUIT){
 				System.out.println("\tEditor"+eid+" catch QUIT command emited by itself.");
-				translater.transReceiveCmd(cmd);
+				cmds = translater.catchOwnCommand(cmd);
+				for (int i=0; i<cmds.length; i++){
+					cmd = cmds[i];
+					System.out.println("\t\tEditor"+eid+" edit text. ");
+					text.edit(cmd);
+				}
 				running=false; break;
 			}
 			System.out.println("\tEditor"+eid+" catch command from "+cmd.eid+" NO."+cmd.seq);
--- a/src/pathfinder/mergetest/NetworkSimulator.java	Tue Dec 25 16:21:30 2007 +0900
+++ b/src/pathfinder/mergetest/NetworkSimulator.java	Tue Dec 25 20:07:37 2007 +0900
@@ -1,14 +1,36 @@
 package pathfinder.mergetest;
 
 
-public interface NetworkSimulator<P> {
+public abstract class NetworkSimulator<P> {
 
 	/**
 	 * Request to connect.
 	 * Client editor use this method to connect SeMa. 
 	 * @param cs
 	 */
-	public ChannelSimulator<P> connect();
-	public ChannelSimulator<P> accept();
-	public boolean checkAllCS();
+	public abstract ChannelSimulator<P> connect();
+	public abstract ChannelSimulator<P> accept();
+	public abstract boolean checkAllCS();
+	
+	public abstract ChannelSimulator<P> getAcceptedSession(int i);
+/*
+	synchronized public P read(ChannelSimulator<P> cs){
+		try {
+			return cs.getReadQ().take();
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+			return null;
+		}
+	}
+	synchronized public boolean write(ChannelSimulator<P> cs, P p){
+		try {
+			cs.getWriteQ().put(p);
+			notifyAll();
+			return true;
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+			return false;
+		}
+	}
+*/
 }
--- a/src/pathfinder/mergetest/NetworkSimulatorwithSeMa.java	Tue Dec 25 16:21:30 2007 +0900
+++ b/src/pathfinder/mergetest/NetworkSimulatorwithSeMa.java	Tue Dec 25 20:07:37 2007 +0900
@@ -1,23 +1,20 @@
 package pathfinder.mergetest;
 
 import java.util.LinkedList;
-import java.util.Queue;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
 
-import remoteeditor.command.REPCommand;
-
-public class NetworkSimulatorwithSeMa<P> implements NetworkSimulator<P> {
+public class NetworkSimulatorwithSeMa<P> extends NetworkSimulator<P> {
 
 	/** Waiting connectionRequest to be accepted by SessionManager. */
-	private Queue<ChannelSimulator<P>> acceptList;
+	private LinkedList<ChannelSimulator<P>> acceptedList;
 	/** Established connection */
-	private Queue<ChannelSimulator<P>> connectedList;
+	private LinkedList<ChannelSimulator<P>> connectedList;
 	
 	//private SeMaSimulator<REPCommand> sema;
 	
 	public NetworkSimulatorwithSeMa(){
-		acceptList = new LinkedList<ChannelSimulator<P>>();
+		acceptedList = new LinkedList<ChannelSimulator<P>>();
 		connectedList = new LinkedList<ChannelSimulator<P>>();
 	}
 
@@ -26,10 +23,10 @@
 	 * @return
 	 */
 	public ChannelSimulator<P> accept(){
-		ChannelSimulator<P> cs = acceptList.poll();
+		ChannelSimulator<P> cs = connectedList.poll();
 		if (cs==null) return null;
 
-		connectedList.offer(cs);
+		acceptedList.offer(cs);
 		return cs.getServerChannel();
 	}
 
@@ -42,7 +39,7 @@
 		BlockingQueue<P> rq = new LinkedBlockingQueue<P>();
 		BlockingQueue<P> wq = new LinkedBlockingQueue<P>();
 		ChannelSimulator<P> cs = new ChannelSimulator<P>(this, rq, wq);
-		acceptList.offer(cs);
+		connectedList.offer(cs);
 		return cs;
 	}
 		
@@ -52,4 +49,8 @@
 		}
 		return true;
 	}
+
+	public ChannelSimulator<P> getAcceptedSession(int i) {
+		return acceptedList.get(i).getServerChannel();
+	}
 }
--- a/src/pathfinder/mergetest/NetworkSimulatorwithoutSeMa.java	Tue Dec 25 16:21:30 2007 +0900
+++ b/src/pathfinder/mergetest/NetworkSimulatorwithoutSeMa.java	Tue Dec 25 20:07:37 2007 +0900
@@ -4,7 +4,7 @@
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
 
-public class NetworkSimulatorwithoutSeMa<P> implements NetworkSimulator<P> {
+public class NetworkSimulatorwithoutSeMa<P> extends NetworkSimulator<P> {
 	/** Established connection */
 	private LinkedList<ChannelSimulator<P>> connectedList;
 
@@ -51,5 +51,10 @@
 		}
 		return true;
 	}
+
+	public ChannelSimulator<P> getAcceptedSession(int i) {
+		// TODO Auto-generated method stub
+		return connectedList.get(i);
+	}
 	
 }
\ No newline at end of file
--- a/src/pathfinder/mergetest/SeMaSimulator.java	Tue Dec 25 16:21:30 2007 +0900
+++ b/src/pathfinder/mergetest/SeMaSimulator.java	Tue Dec 25 20:07:37 2007 +0900
@@ -39,13 +39,15 @@
 			csList.add(cs);
 		}
 	}
+	public void init(){
+		while(csList.size()<MAX_CLIENT){ checkAccept(); Thread.yield(); }
+	}
 
 	public void run(){
 		int i=0;
 		int count=0;
 		P packet;
 
-		while(csList.size()<MAX_CLIENT){ checkAccept(); Thread.yield(); }
 		System.out.println("SessionManager start.");
 
 		/* Main Loop */
@@ -62,7 +64,7 @@
 			}
 			if(!running) break;
 
-			System.out.println("SeMa pass packet to "+i+":>> "+packet.toString());
+			//System.out.println("SeMa pass packet to "+i+":>> "+packet.toString());
 			i = (i+1)%csList.size();   // i++
 			cs = csList.get(i);        // 次のChennelをゲット
 
--- a/src/pathfinder/mergetest/TestMerger.java	Tue Dec 25 16:21:30 2007 +0900
+++ b/src/pathfinder/mergetest/TestMerger.java	Tue Dec 25 20:07:37 2007 +0900
@@ -9,6 +9,7 @@
 	private NetworkSimulator<REPCommand> ns=null;
 	private LinkedList<EditorSimulator> editors;
 	private SeMaSimulator<REPCommand> sema;
+	private UsersSimulator users;
 
 	public TestMerger(){
 		editors = new LinkedList<EditorSimulator>();
@@ -25,10 +26,10 @@
 
 		/* create, initialize and start test.  */
 		tm = new TestMerger();
-		tm.init(false, i, j);
+		tm.init(true, i, j);
 		tm.startTest();
 
-		tm.printAllTexts();
+		//tm.printAllTexts();
 		//if (!tm.checkCS())
 		//	System.out.println("Error!! :some ChannelSimulator still have packet!");
 		if (!tm.checkEquality())
@@ -42,7 +43,10 @@
 			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){
@@ -66,12 +70,16 @@
 			sema = null;
 		}
 
+		/* 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,
@@ -80,6 +88,7 @@
 					str.length(), str);
 				cmds.add( cmd);
 			}
+			*/
 
 			/* create a Editor, and pass command list to it.  */
 			EditorSimulator ee = new EditorSimulator(i, ns, cmds, "Editor"+i);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/UsersSimulator.java	Tue Dec 25 20:07:37 2007 +0900
@@ -0,0 +1,67 @@
+package pathfinder.mergetest;
+
+import java.util.ArrayList;
+
+import remoteeditor.command.REPCommand;
+import remoteeditor.network.REP;
+
+public class UsersSimulator extends Thread {
+	private NetworkSimulator<REPCommand> ns;
+	private int N_client;
+	private int N_packet;
+	private ArrayList<ChannelSimulator<REPCommand>> channelList;
+	private int nextChannelIndex;
+
+	public UsersSimulator(NetworkSimulator<REPCommand> _ns, int max_client, int max_packet){
+		ns = _ns;
+		N_client = max_client;
+		N_packet = max_packet;
+		channelList = new ArrayList<ChannelSimulator<REPCommand>>();
+	}
+
+	public void run(){
+		int i=0;
+
+		System.out.println("UsersSimulator start.");
+		while( i++<N_packet ){
+			REPCommand cmd0 = createCmd();
+			ChannelSimulator<REPCommand> cs = channelList.get(nextChannelIndex);
+			cs.write(cmd0);
+	
+			nextChannelIndex++;
+			if (nextChannelIndex>=channelList.size()) nextChannelIndex=0;
+		}
+
+		/* send quitPacket to all editors.  */
+		for ( ChannelSimulator<REPCommand> cs : channelList){
+			REPCommand cmd0 = createQuitCmd();
+			cs.write(cmd0);
+		}
+		
+		System.out.println("UsersSimulator finish.");
+	}
+
+	private REPCommand createCmd(){
+		String str = new String("this text was created by Control thread.");
+		REPCommand cmd = new REPCommand(REP.REPCMD_INSERT, 0, -1, 0, 10, str.length(), str);
+		return cmd;
+	}
+	private REPCommand createQuitCmd(){
+		String str = new String("this text was created by Control thread.");
+		REPCommand cmd = new REPCommand(REP.SMCMD_QUIT, 0, -1, 0, 10, str.length(), str);
+		return cmd;
+	}
+	
+	public void init(){
+		getAllChannelSimulators();
+	}
+
+	private void getAllChannelSimulators(){
+		while (channelList.size()<N_client){
+			ChannelSimulator<REPCommand> cs;
+			cs = ns.getAcceptedSession( channelList.size() );
+			if (cs==null) continue;
+			channelList.add( cs );
+		}
+	}
+}