changeset 173:b4f2c0aeb474

*** empty log message ***
author kent
date Sat, 30 Aug 2008 01:55:39 +0900
parents a913949a0dd9
children d234a9d2a172
files src/pathfinder/mergetest/channels2/test/testEditor.java src/pathfinder/mergetest/channels2/test/testNetworkSimulator.java src/pathfinder/mergetest/channels2/test/testSeMa.java src/pathfinder/mergetest/channels2/test/testSeMaSlave.java
diffstat 4 files changed, 296 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/channels2/test/testEditor.java	Sat Aug 30 01:55:39 2008 +0900
@@ -0,0 +1,43 @@
+package pathfinder.mergetest.channels2.test;
+
+import java.io.IOException;
+
+import pathfinder.mergetest.channels2.*;
+
+public class testEditor extends Thread{
+	private NetworkSimulator<String> ns;
+	private int semaIP;
+	
+	public testEditor(NetworkSimulator<String> _ns, String name, int _ip){
+		super(name);
+		ns = _ns;
+		semaIP = _ip;
+	}
+
+	public void run(){
+		SocketChannelSimulator<String> channel = SocketChannelSimulator.<String>open();
+
+		ns.writeLog("try to connect to SessionManager whose ip is "+semaIP, 1);
+		while (!channel.connect(semaIP)){
+			ns.writeLog("SeMa not listen to socket yet, wait", 1);
+			Thread.yield();
+		}
+		ns.writeLog("successes to connect", 1);
+
+		channel.write("from "+getName()+": hello");
+		ns.writeLog("wrote packet", 1);
+		
+		String packet=null;
+		try {
+			packet = channel.read();
+		} catch (IOException e) {
+			e.printStackTrace();
+			ns.writeLog("read Exception!", 1);
+			return;
+		}
+		
+		ns.writeLog("gets return string==> `"+packet+"\'", 1);
+		
+		ns.writeLog("testEditor exits.", 1);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/channels2/test/testNetworkSimulator.java	Sat Aug 30 01:55:39 2008 +0900
@@ -0,0 +1,69 @@
+package pathfinder.mergetest.channels2.test;
+
+import java.util.ArrayList;
+import java.util.Random;
+import pathfinder.mergetest.channels2.NetworkSimulator;
+
+public class testNetworkSimulator {
+	private NetworkSimulator<String> ns;
+	private ArrayList<testSeMa> semaList;
+	private ArrayList<testSeMaSlave> semasList;
+	private ArrayList<testEditor> editorList;
+	private int NoSemaMaster;
+	private int NoSemaSlave;
+	private int NoEditor;
+
+	public static void main(String[] args){
+		testNetworkSimulator testns = new testNetworkSimulator(1, 0, 2);
+		
+		testns.startTest();
+	}
+
+
+	/** Constructor. */
+	public testNetworkSimulator(int sm, int ss,int e){
+		//ns = new NetworkSimulator<String>();
+		ns = NetworkSimulator.singleton();
+		semaList = new ArrayList<testSeMa>();
+		semasList = new ArrayList<testSeMaSlave>();
+		editorList = new ArrayList<testEditor>();
+		NoSemaMaster = sm;
+		NoSemaSlave = ss;
+		NoEditor = e;
+	}
+
+	public void startTest(){
+		Random rand = new Random();
+		for (int i=0; i<NoSemaMaster; i++){
+			testSeMa sm = new testSeMa(ns, "SeMa"+i, i);
+			semaList.add(sm);
+			sm.start();
+		}
+		for (int i=0; i<NoSemaSlave; i++){
+			testSeMaSlave sm = new testSeMaSlave(ns, "SeMaS"+i, i+NoSemaMaster, rand.nextInt(NoSemaMaster));
+			semasList.add(sm);
+			sm.start();
+		}
+		for (int i=0; i<NoEditor; i++){
+			testEditor te = new testEditor(ns, "Editor"+i, rand.nextInt(NoSemaMaster+NoSemaSlave)); 
+			editorList.add(te);
+			te.start();
+		}
+		
+		Checker();
+		
+		try {
+			for (testEditor te: editorList)
+				te.join();
+			ns.writeLog("main: all clients exited.", 1);
+
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		}
+		System.exit(0);
+	}
+	
+	public void Checker(){
+		
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/channels2/test/testSeMa.java	Sat Aug 30 01:55:39 2008 +0900
@@ -0,0 +1,74 @@
+package pathfinder.mergetest.channels2.test;
+
+import java.io.IOException;
+import java.util.LinkedList;
+
+import pathfinder.mergetest.channels2.*;
+
+
+public class testSeMa extends Thread{
+
+	int IP;
+	boolean running=true;
+	NetworkSimulator<String> ns;
+	LinkedList<SocketChannelSimulator<String>> channels;
+	
+	public testSeMa(NetworkSimulator<String> _ns, String name, int ip){
+		super(name);
+		IP = ip;
+		ns = _ns;
+		channels = new LinkedList<SocketChannelSimulator<String>>();
+	}
+	public void init(){
+		
+	}
+
+	public void run() {
+		SelectorSimulator selector = new SelectorSimulator();
+		ServerChannelSimulator<String> scs = ServerChannelSimulator.<String>open();
+		scs.bind(IP);
+		//selector.register(scs, SelectionKeySimulator.OP_ACCEPT);
+		scs.register(selector, SelectionKeySimulator.OP_ACCEPT, null);
+		ns.writeLog("SessionManager starts mainroutin.", 1);
+
+		/* Main Loop */
+		while(running){
+
+			try { selector.select(); }
+			catch (IOException e) { e.printStackTrace();}
+
+			for(SelectionKeySimulator key : selector.selectedKeys()){
+
+				if(key.isAcceptable()){
+					ns.writeLog("gets acceptable channel", 1);
+					ServerChannelSimulator<String> sc = (ServerChannelSimulator<String>) key.channel();
+					SocketChannelSimulator<String> channel;
+					try {
+						channel = sc.accept();
+					} catch (IOException e) {
+						e.printStackTrace();
+						continue;
+					}
+					//selector.register(channel, SelectionKeySimulator.OP_READ);
+					channel.register(selector, SelectionKeySimulator.OP_READ);
+					ns.writeLog("accepts a client.", 1);
+					
+				}else if(key.isReadable()){
+					ns.writeLog("gets readable channel", 1);
+					//SelectableChannelSimulator<String> channel = key.channel();
+					SocketChannelSimulator<String> channel = (SocketChannelSimulator<String>) key.channel();
+					String packet;
+					try {
+						packet = channel.read();
+					} catch (IOException e) {
+						e.printStackTrace();
+						continue;
+					}
+					ns.writeLog("receives String==> `"+packet+"\'", 1);
+					channel.write("from SeMa"+this.getName()+": world");
+				}
+			}
+		}
+		
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/channels2/test/testSeMaSlave.java	Sat Aug 30 01:55:39 2008 +0900
@@ -0,0 +1,110 @@
+package pathfinder.mergetest.channels2.test;
+
+import java.io.IOException;
+import java.util.LinkedList;
+
+import pathfinder.mergetest.channels2.*;
+
+
+public class testSeMaSlave extends Thread{
+
+	int ownIP;
+	int masterIP;
+	boolean running=true;
+	NetworkSimulator<String> ns;
+	LinkedList<ClientInfo> cis;
+	
+	public testSeMaSlave(NetworkSimulator<String> _ns, String name, int oIP, int mIP){
+		super(name);
+		ownIP = oIP;
+		masterIP = mIP;
+		ns = _ns;
+		cis = new LinkedList<ClientInfo>();
+	}
+	public void init(){
+		
+	}
+
+	@SuppressWarnings("unchecked")
+	public void run() {
+		SelectorSimulator selector = new SelectorSimulator();
+
+		SocketChannelSimulator<String> masterCH = connectToMaster(selector);
+		ServerChannelSimulator<String> scs = ServerChannelSimulator.<String>open();
+		scs.bind(ownIP);
+		
+		//selector.register(scs, SelectionKeySimulator.OP_ACCEPT);
+		scs.register(selector, SelectionKeySimulator.OP_ACCEPT);
+		//selector.register(masterCH, SelectionKeySimulator.OP_READ);
+		masterCH.register(selector, SelectionKeySimulator.OP_READ);
+		ns.writeLog("SessionManager starts mainroutin.", 1);
+
+		/* Main Loop */
+		while(running){
+
+			try { selector.select(); }
+			catch (IOException e) { e.printStackTrace();}
+
+			for(SelectionKeySimulator key : selector.selectedKeys()){
+
+				if(key.isAcceptable()){
+					ns.writeLog("gets acceptable channel", 1);
+					ServerChannelSimulator<String> sc = (ServerChannelSimulator<String>) key.channel();
+					SocketChannelSimulator<String> channel;
+					try {
+						channel = sc.accept();
+					} catch (IOException e) {
+						e.printStackTrace();
+						continue;
+					}
+					channel.register(selector, SelectionKeySimulator.OP_READ, null);
+					ns.writeLog("accepts a client.", 1);
+					
+				}else if(key.isReadable()){
+					ns.writeLog("gets readable channel", 1);
+					SocketChannelSimulator<String> channel = (SocketChannelSimulator<String>) key.channel();
+					String packet;
+					try {
+						packet = channel.read();
+					} catch (IOException e) {
+						e.printStackTrace();
+						continue;
+					}
+					if (channel==masterCH){
+						ns.writeLog("receives String from master ==> `"+packet+"\'", 1);
+						for (ClientInfo ci: cis){
+							if (!packet.matches(ci.str)) continue;
+							ci.channel.write("from "+this.getName()+": loopback packet from master ==>`"+packet+"\'");
+						}
+					}else{
+						ns.writeLog("receives String==> `"+packet+"\'", 1);
+						//channel.write("from "+this.getName()+": slave");
+						masterCH.write("from "+this.getName()+": receive String==>`"+packet+"\' from editor");
+						cis.add(new ClientInfo(packet, channel));
+					}
+				}
+			}
+		}
+		
+	}
+	private SocketChannelSimulator<String> connectToMaster(SelectorSimulator _selector) {
+		SocketChannelSimulator<String> channel = SocketChannelSimulator.<String>open();
+		channel.register(_selector, SelectionKeySimulator.OP_READ, null);
+		ns.writeLog("is connecting to masterSeMa whose ip is"+masterIP, 1);
+		while(!channel.connect(masterIP)){
+			ns.writeLog("SeMa not listen to socket yet, wait", 1);
+			Thread.yield();
+		}
+		ns.writeLog("connecting was successful.", 1);
+
+		return channel;
+	}
+}
+class ClientInfo{
+	String str;
+	SocketChannelSimulator<String> channel;
+	ClientInfo(String _str, SocketChannelSimulator<String> _channel){
+		str = _str;
+		channel = _channel;
+	}
+}
\ No newline at end of file