changeset 69:3c9b8371681d

TestNetwork for StringPacket
author kent
date Thu, 08 Nov 2007 14:19:47 +0900
parents 735e70091564
children 9c19d6d61a03
files src/pathfinder/NetworkSimulator.java src/pathfinder/SeMaEmulator.java src/pathfinder/TestNetwork.java
diffstat 3 files changed, 111 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/src/pathfinder/NetworkSimulator.java	Thu Nov 08 12:56:56 2007 +0900
+++ b/src/pathfinder/NetworkSimulator.java	Thu Nov 08 14:19:47 2007 +0900
@@ -1,24 +1,25 @@
 package pathfinder;
 
-import java.util.ArrayList;
 import java.util.LinkedList;
-import java.util.List;
 import java.util.Queue;
 
 public class NetworkSimulator<P> {
 	
+	/** Waiting connectionRequest to be accepted by SessionManager. */
 	public Queue<ChannelSimulator<P>> acceptList;
-	public Queue<ChannelSimulator<P>> connectList;
+	/** Established connection */
+	public Queue<ChannelSimulator<P>> connectedList;
 
 	public NetworkSimulator(){
 		acceptList = new LinkedList<ChannelSimulator<P>>();
-		connectList = new LinkedList<ChannelSimulator<P>>();
+		connectedList = new LinkedList<ChannelSimulator<P>>();
 	}
 
 	public ChannelSimulator<P> accept(){
 		ChannelSimulator<P> cs = acceptList.poll();
-		connectList.offer(cs);
-		
+		if (cs==null) return null;
+
+		connectedList.offer(cs);
 		return cs.getServerChannel();
 	}
 	public void connect(ChannelSimulator<P> cs){
--- a/src/pathfinder/SeMaEmulator.java	Thu Nov 08 12:56:56 2007 +0900
+++ b/src/pathfinder/SeMaEmulator.java	Thu Nov 08 14:19:47 2007 +0900
@@ -3,30 +3,60 @@
 import java.util.ArrayList;
 import java.util.List;
 
-public class SeMaEmulator<P> implements Runnable {
+public class SeMaEmulator<P> extends Thread {
+	private NetworkSimulator<P> ns;
 	private List<ChannelSimulator<P>> csList;
-	
-	public SeMaEmulator(){
+
+	public SeMaEmulator(NetworkSimulator<P> _ns){
+		ns = _ns;
 		csList = new ArrayList<ChannelSimulator<P>>();
 	}
-
+/*
 	public ChannelSimulator<P> addChannelSimulator(ChannelSimulator<P> cs){
 		csList.add(cs);
 		return cs;
 	}
+*/
+	/**
+	 * Check whether NetworkSimulator hold waiting connections.
+	 */
+	private boolean checkAccept(){
+		ChannelSimulator<P> cs;
+		cs = ns.accept();
+		if (cs==null) return false;
+		csList.add(cs);
+		return true;
+	}
+	public void init(){
+		while(csList.size()<2){
+			checkAccept();
+			/*
+			try {
+				Thread.sleep(10);
+			} catch (InterruptedException e) {
+				e.printStackTrace();
+				break;
+			}
+			*/
+			//Thread.yield();
+		}
+	}
 
 	public void run(){
 		int i=0;
 		P packet;
-		ChannelSimulator<P> cs = csList.get(1);
 
+		init();
+		System.out.println("SessionManager was initialized.");
+		
 		/* Main Loop */
+		ChannelSimulator<P> cs = csList.get(i);
 		while(true){
-			packet=cs.read();
-			i = (i+1)%csList.size();
-			cs = csList.get(i+1);
-			if (packet!=null) cs.write(packet);
+			packet=cs.read();                    // [i]からread
+			i = (i+1)%csList.size();             // i++
+			cs = csList.get(i);                  // 次のChennelをゲット
+			if (packet!=null) cs.write(packet);  // readできていたならそれを書き込む
+			if (i==0) checkAccept();             //全部回ったらaccept待ちをチェック
 		}
 	}
-	
 }
--- a/src/pathfinder/TestNetwork.java	Thu Nov 08 12:56:56 2007 +0900
+++ b/src/pathfinder/TestNetwork.java	Thu Nov 08 14:19:47 2007 +0900
@@ -1,21 +1,48 @@
 package pathfinder;
 
+import java.util.LinkedList;
+import java.util.List;
+
 public class TestNetwork {
 	private NetworkSimulator<String> ns;
 	private SeMaEmulator<String> sm;
-	
-	ChannelSimulator<String>  client;
-	ChannelSimulator<String>  server;
+
+	private List<EditorEmulator1> editors;
+	private int N_editor;
 	
 	public static void main(String [] arg)  {
-		TestNetwork test = new TestNetwork();
-		test.test();
+		TestNetwork test = new TestNetwork(2);
+		test.startTest();
+	}
+
+	public TestNetwork(int _s){
+		N_editor = _s;
+		ns = new NetworkSimulator<String>();
+		sm = new SeMaEmulator<String>(ns);
+		editors = new LinkedList<EditorEmulator1>();
 	}
 
-	public TestNetwork(){
-		ns = new NetworkSimulator<String>();
+	private void startTest() {
+		for (int i=0; i<N_editor; i++){
+			editors.add(new EditorEmulator1("editor"+i, ns));
+		}
+		
+		for (EditorEmulator1 ee: editors){
+			ee.init();
+			ee.start();
+		}
+		sm.start();
+		
+		for (EditorEmulator1 ee: editors){
+			try {
+				ee.join();
+			} catch (InterruptedException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+		}
 	}
-
+/*
 	void test() {
 		ns = new NetworkSimulator<String>();
 		client = new ChannelSimulator<String>(ns);
@@ -27,29 +54,50 @@
 		System.out.println(server.read());
 		
 	}
-	
-	void thread() {
-		
-	}
+*/
 }
 
-class EditorEmulator1 implements Runnable {
+class EditorEmulator1 extends Thread{
+	private String name;
 	private NetworkSimulator<String> ns;
 	private ChannelSimulator<String> cs;
+	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 EditorEmulator1(NetworkSimulator<String> _ns){
+		this("NoName", _ns);
+	}
+	public EditorEmulator1(String _name, NetworkSimulator<String> _ns) {
+		name = _name;
 		ns = _ns;
 	}
+
 	public void init(){
 		cs = new ChannelSimulator<String>(ns);
 		ns.connect(cs);
 	}
 	public void run(){
+		int count=0;
 		String str;
-		while(true){
+		sendPackets();
+		System.out.println("thread "+name+" start.");
+		while(count<10){
 			str = cs.read();
-			if (cs==null) continue;
-			System.out.println("Catch String: "+str);
+			if (str==null) continue;
+			System.out.println(name+": Catch String '"+str+"'"); count++;
+		}
+	}
+	
+	public void sendPackets(){
+		for(String str: EditorEmulator1.text0){
+			cs.write(str+" < from "+name);
+			System.out.println(name+": Send String '"+str+"'");
 		}
 	}
 }