changeset 154:fafbaaa0abd0

*** empty log message ***
author kono
date Thu, 28 Aug 2008 15:33:02 +0900
parents 4cfed12aa3aa
children bbd2801d8ce0
files test/channeltest/testEditor.java test/channeltest/testNetworkSimulator.java test/channeltest/testSeMa.java test/channeltest/testSeMaSlave.java
diffstat 4 files changed, 268 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/channeltest/testEditor.java	Thu Aug 28 15:33:02 2008 +0900
@@ -0,0 +1,44 @@
+package test.channeltest;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+
+import rep.channel.REPLogger;
+import rep.channel.REPSocketChannel;
+
+
+public class testEditor extends Thread{
+	private SocketAddress semaIP;
+	private REPLogger ns;
+	
+	public testEditor(String name, String _host,int _port){
+		super(name);
+		semaIP = new InetSocketAddress(_host, _port);
+		ns = testNetworkSimulator.ns;
+	}
+
+	public void run(){
+		try {
+			REPSocketChannel<String> channel;
+			channel = REPSocketChannel.<String>create();
+
+			ns.writeLog(this, "try to connect to SessionManager whose ip is "+semaIP, 1);
+			while (!channel.connect(semaIP)){
+				ns.writeLog(this, "SeMa not listen to socket yet, wait", 1);
+				Thread.yield();
+			}
+			ns.writeLog(this, "successes to connect", 1);
+
+			channel.write("from "+getName()+": hello");
+			ns.writeLog(this, "wrote packet", 1);
+
+			String packet = channel.read();
+
+			ns.writeLog(this, "gets return string==> `"+packet+"\'", 1);
+
+			ns.writeLog(this, "testEditor exits.", 1);
+		} catch (IOException e) {
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/channeltest/testNetworkSimulator.java	Thu Aug 28 15:33:02 2008 +0900
@@ -0,0 +1,70 @@
+package test.channeltest;
+
+import java.util.ArrayList;
+import java.util.Random;
+import pathfinder.mergetest.channels.NetworkSimulator;
+import rep.channel.REPLogger;
+
+public class testNetworkSimulator {
+	private ArrayList<testSeMa> semaList;
+	private ArrayList<testSeMaSlave> semasList;
+	private ArrayList<testEditor> editorList;
+	private int NoSemaMaster;
+	private int NoSemaSlave;
+	private int NoEditor;
+	static public REPLogger ns = new REPLogger();
+
+	public static void main(String[] args){
+		testNetworkSimulator testns = new testNetworkSimulator(3, 10, 50);
+		
+		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/test/channeltest/testSeMa.java	Thu Aug 28 15:33:02 2008 +0900
@@ -0,0 +1,60 @@
+package test.channeltest;
+
+import java.io.IOException;
+import java.util.LinkedList;
+
+import pathfinder.mergetest.channels.*;
+
+
+public class testSeMa extends Thread{
+
+	int IP;
+	boolean running=true;
+	NetworkSimulator<String> ns;
+	LinkedList<ChannelSimulator<String>> channels;
+	
+	public testSeMa(NetworkSimulator<String> _ns, String name, int ip){
+		super(name);
+		IP = ip;
+		ns = _ns;
+		channels = new LinkedList<ChannelSimulator<String>>();
+	}
+	public void init(){
+		
+	}
+
+	public void run() {
+		SelectorSimulator<String> selector = new SelectorSimulator<String>();
+		ServerChannelSimulator<String> scs = new ServerChannelSimulator<String>(ns, selector);
+		scs.bind(IP);
+		selector.register(scs, SelectionKeySimulator.OP_ACCEPT);
+		ns.writeLog("SessionManager starts mainroutin.", 1);
+
+		/* Main Loop */
+		while(running){
+
+			try { selector.select(); }
+			catch (IOException e) { e.printStackTrace();}
+
+			for(SelectionKeySimulator<String> key : selector.selectedKeys()){
+
+				if(key.isAcceptable()){
+					ns.writeLog(this, "gets acceptable channel", 1);
+					ServerChannelSimulator<String> sc = (ServerChannelSimulator<String>) key.channel();
+					ChannelSimulator<String> channel = sc.accept();
+					selector.register(channel, SelectionKeySimulator.OP_READ);
+					ns.writeLog(this, "accepts a client.", 1);
+					
+				}else if(key.isReadable()){
+					ns.writeLog(this, "gets readable channel", 1);
+					//SelectableChannelSimulator<String> channel = key.channel();
+					ChannelSimulator<String> channel = (ChannelSimulator<String>) key.channel();
+					String packet = channel.read();
+					ns.writeLog(this, "receives String==> `"+packet+"\'", 1);
+					channel.write("from SeMa"+this.getName()+": world");
+				}
+			}
+		}
+		
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/channeltest/testSeMaSlave.java	Thu Aug 28 15:33:02 2008 +0900
@@ -0,0 +1,94 @@
+package test.channeltest;
+
+import java.io.IOException;
+import java.util.LinkedList;
+
+import pathfinder.mergetest.channels.*;
+
+
+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(){
+		
+	}
+
+	public void run() {
+		SelectorSimulator<String> selector = new SelectorSimulator<String>();
+
+		ChannelSimulator<String> masterCH = connectToMaster(selector);
+		ServerChannelSimulator<String> scs = new ServerChannelSimulator<String>(ns, selector);
+		scs.bind(ownIP);
+		
+		selector.register(scs, SelectionKeySimulator.OP_ACCEPT);
+		selector.register(masterCH, SelectionKeySimulator.OP_READ);
+		ns.writeLog("SessionManager starts mainroutin.", 1);
+
+		/* Main Loop */
+		while(running){
+
+			try { selector.select(); }
+			catch (IOException e) { e.printStackTrace();}
+
+			for(SelectionKeySimulator<String> key : selector.selectedKeys()){
+
+				if(key.isAcceptable()){
+					ns.writeLog(this, "gets acceptable channel", 1);
+					ServerChannelSimulator<String> sc = (ServerChannelSimulator<String>) key.channel();
+					ChannelSimulator<String> channel = sc.accept();
+					selector.register(channel, SelectionKeySimulator.OP_READ);
+					ns.writeLog(this, "accepts a client.", 1);
+					
+				}else if(key.isReadable()){
+					ns.writeLog(this, "gets readable channel", 1);
+					ChannelSimulator<String> channel = (ChannelSimulator<String>) key.channel();
+					String packet = channel.read();
+					if (channel==masterCH){
+						ns.writeLog(this, "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(this, "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 ChannelSimulator<String> connectToMaster(SelectorSimulator<String> _selector) {
+		ChannelSimulator<String> channel = new ChannelSimulator<String>(ns, _selector);
+		ns.writeLog(this, "is connecting to masterSeMa whose ip is"+masterIP, 1);
+		while(!channel.connect(masterIP)){
+			ns.writeLog(this, "SeMa not listen to socket yet, wait", 1);
+			Thread.yield();
+		}
+		ns.writeLog(this, "connecting was successful.", 1);
+
+		return channel;
+	}
+}
+class ClientInfo{
+	String str;
+	ChannelSimulator<String> channel;
+	ClientInfo(String _str, ChannelSimulator<String> _channel){
+		str = _str;
+		channel = _channel;
+	}
+}
\ No newline at end of file