changeset 160:e9047957acc2

merge with pathfinder.simulator.*
author kent
date Tue, 26 Aug 2008 19:46:03 +0900
parents a5c56bff6359
children 66e9cebce3fa
files src/pathfinder/mergetest/ChannelSimulator.java src/pathfinder/mergetest/EditorObject.java src/pathfinder/mergetest/EditorSimulator.java src/pathfinder/mergetest/EditorSimulatorWithoutMerger.java src/pathfinder/mergetest/NetworkSimulator.java src/pathfinder/mergetest/NetworkSimulatorwithSeMa.java src/pathfinder/mergetest/NetworkSimulatorwithoutSeMa.java src/pathfinder/mergetest/PacketSet.java src/pathfinder/mergetest/REPHandler.java src/pathfinder/mergetest/REPHandlerDoWaiting.java src/pathfinder/mergetest/REPHandlerImpl.java src/pathfinder/mergetest/REPHandlerInMerge.java src/pathfinder/mergetest/SeMaSimulator.java src/pathfinder/mergetest/SelectionKeySimulator.java src/pathfinder/mergetest/SelectorSimulator.java src/pathfinder/mergetest/SessionManagerSimulatorWithMerger.java src/pathfinder/mergetest/TestMerger.java src/pathfinder/mergetest/TestMerger2.java src/pathfinder/mergetest/UserSimulator.java
diffstat 19 files changed, 102 insertions(+), 517 deletions(-) [+]
line wrap: on
line diff
--- a/src/pathfinder/mergetest/ChannelSimulator.java	Tue Aug 26 19:45:26 2008 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,103 +0,0 @@
-package pathfinder.mergetest;
-
-import java.util.concurrent.BlockingQueue;
-import java.util.concurrent.LinkedBlockingQueue;
-
-import remoteeditor.command.REPCommand;
-
-public class ChannelSimulator<P> {
-	private BlockingQueue<P> qread;
-	private BlockingQueue<P> qwrite;
-	private NetworkSimulator<P> ns;
-	private BlockingQueue<ChannelSimulator<P>> connectQueue = 
-		new LinkedBlockingQueue<ChannelSimulator<P>>();
-
-	public ChannelSimulator(NetworkSimulator<P> _ns, BlockingQueue<P> _a, BlockingQueue<P> _b){
-		ns = _ns;
-		qread = _a;
-		qwrite = _b;
-	}
-
-	/* read from Queue.  */
-	public P read(){
-		try {
-			return qread.take();
-		} catch (InterruptedException e) {
-			e.printStackTrace();
-			return null;
-		}
-	}
-	/* write to Queue.  */
-	public boolean write(P p){
-		try {
-			synchronized (ns){
-				qwrite.put(p);
-				ns.notifyAll();
-			}
-			return true;
-		} catch (InterruptedException e) {
-			e.printStackTrace();
-			return false;
-		}
-	}
-	/* polling read queue. write queue can be written any time.  */
-	public P poll() {
-		return qread.poll();
-	}
-
-	
-	/* accessor methods.  */
-	public BlockingQueue<P> getReadQ(){
-		return qread;
-	}
-	public BlockingQueue<P> getWriteQ(){
-		return qwrite;
-	}
-	public void setReadQ(BlockingQueue<P> bq){
-		qread = bq;
-	}
-	public void setWriteQ(BlockingQueue<P> bq){
-		qwrite = bq;
-	}
-
-	/* return state of the Queue(debug)  */
-	public boolean readQisEmpty() {
-		return qread.isEmpty();
-	}
-	public boolean writeQisEmpty() {
-		return qwrite.isEmpty();
-	}
-	
-
-	/* for Session Manager.  */
-	public ChannelSimulator<P> getServerChannel() {
-		return new ChannelSimulator<P>(ns, qwrite, qread);
-	}
-	
-	public void connect(ChannelSimulator<P> c){
-		synchronized(ns){
-			connectQueue.add(c);
-			ns.notifyAll();
-		}
-	}
-
-	public ChannelSimulator<P> accept() {
-		return connectQueue.remove();
-	}
-
-	public boolean isAcceptable() {
-		return !connectQueue.isEmpty();
-	}
-
-	public boolean isReadable() {
-		return !qread.isEmpty();
-	}
-
-	public SelectionKeySimulator keyFor(SelectorSimulator<REPCommand> selector2) {
-		// TODO Auto-generated method stub
-		
-		return selector2.getKey(this);
-	}
-
-
-}
--- a/src/pathfinder/mergetest/EditorObject.java	Tue Aug 26 19:45:26 2008 +0900
+++ b/src/pathfinder/mergetest/EditorObject.java	Tue Aug 26 19:46:03 2008 +0900
@@ -4,24 +4,26 @@
 import java.util.LinkedList;
 import java.util.List;
 
+import pathfinder.mergetest.channels.ChannelSimulator;
+
 import remoteeditor.command.REPCommand;
 import sample.merge.TranslaterImp1;
 
-public class EditorObject<P> {
+public class EditorObject {
 	
 	int eid;
-	private ChannelSimulator<P> channel;
+	private ChannelSimulator<REPCommand> channel;
 	private TranslaterImp1 translater;
 	private List<REPCommand> sentList;
 	private List<REPCommand> sendList;
 
-	public EditorObject(int i, ChannelSimulator<P> cs) {
+	public EditorObject(int i, ChannelSimulator<REPCommand> cs) {
 		// TODO Auto-generated constructor stub
 		eid = i;
 		channel = cs;
 	}
 
-	public EditorObject(int i, ChannelSimulator<P> cs, TranslaterImp1 imp1) {
+	public EditorObject(int i, ChannelSimulator<REPCommand> cs, TranslaterImp1 imp1) {
 		// TODO Auto-generated constructor stub
 		eid = i;
 		channel = cs;
@@ -30,7 +32,7 @@
 		sendList = new LinkedList<REPCommand>();
 	}
 
-	public ChannelSimulator<P> getChannel() {
+	public ChannelSimulator<REPCommand> getChannel() {
 		// TODO Auto-generated method stub
 		return channel;
 	}
@@ -71,7 +73,7 @@
 	private void sendMergedCommand(REPCommand[] cmds) {
 		// TODO Auto-generated method stub
 		for(int i = 0; i < cmds.length; i++){
-			channel.write((P) cmds[i]);
+			channel.write(cmds[i]);
 		}
 	}
 
@@ -97,10 +99,10 @@
 		sendList.clear();
 	}
 
-	private P pack(REPCommand command) {
+	private REPCommand pack(REPCommand command) {
 		// TODO Auto-generated method stub
-		P cmd = (P) new REPCommand(command);
-		return cmd;
+		//P cmd = (P) new REPCommand(command);
+		return new REPCommand(command);
 	}
 
 	public void write(REPCommand cmd) {
--- a/src/pathfinder/mergetest/EditorSimulator.java	Tue Aug 26 19:45:26 2008 +0900
+++ b/src/pathfinder/mergetest/EditorSimulator.java	Tue Aug 26 19:46:03 2008 +0900
@@ -2,6 +2,9 @@
 
 import java.util.Queue;
 
+import pathfinder.mergetest.channels.ChannelSimulator;
+import pathfinder.mergetest.channels.NetworkSimulator;
+
 import remoteeditor.command.REPCommand;
 import remoteeditor.network.REP;
 import sample.merge.TranslaterImp1;
@@ -16,6 +19,7 @@
 	private TranslaterImp1 translater;
 	protected Text text;
 	protected boolean running=true;
+	protected int semaIP;
 
 	public EditorSimulator(int _eid, NetworkSimulator<REPCommand> _ns, String[] strings, String _name) {
 		super(_name);
@@ -24,7 +28,8 @@
 		
 		translater = new TranslaterImp1(_eid);
 		if(strings != null) text = new Text(strings);
-		cs = ns.connect();
+		cs = new ChannelSimulator<REPCommand>(ns);
+		cs.connect(semaIP);
 	}
 
 	public void setOwner(boolean f){
@@ -120,4 +125,10 @@
 	public Text getText(){
 		return text;
 	}
+
+	public ChannelSimulator<REPCommand> getChannelforUser(){
+		return cs.createConjugatedChannel();
+	}
+	
+	
 }
\ No newline at end of file
--- a/src/pathfinder/mergetest/EditorSimulatorWithoutMerger.java	Tue Aug 26 19:45:26 2008 +0900
+++ b/src/pathfinder/mergetest/EditorSimulatorWithoutMerger.java	Tue Aug 26 19:46:03 2008 +0900
@@ -1,5 +1,6 @@
 package pathfinder.mergetest;
 
+import pathfinder.mergetest.channels.NetworkSimulator;
 import remoteeditor.command.REPCommand;
 import remoteeditor.network.REP;
 
--- a/src/pathfinder/mergetest/NetworkSimulator.java	Tue Aug 26 19:45:26 2008 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,41 +0,0 @@
-package pathfinder.mergetest;
-
-
-public abstract class NetworkSimulator<P> {
-	int logLevel;
-	
-	public abstract ChannelSimulator<P> connect();
-	public abstract ChannelSimulator<P> accept();
-	public abstract boolean checkAllCS();
-	
-	public abstract ChannelSimulator<P> getAcceptedSession(int i);
-	
-	public void writeLog(String log, int level){
-		if ( level<=logLevel )
-			System.out.println(log);
-	}
-
-	public void setLogLevel(int logLevel) {
-		this.logLevel = logLevel;
-	}
-/*
-	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 Aug 26 19:45:26 2008 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,56 +0,0 @@
-package pathfinder.mergetest;
-
-import java.util.LinkedList;
-import java.util.concurrent.BlockingQueue;
-import java.util.concurrent.LinkedBlockingQueue;
-
-public class NetworkSimulatorwithSeMa<P> extends NetworkSimulator<P> {
-
-	/** Waiting connectionRequest to be accepted by SessionManager. */
-	private LinkedList<ChannelSimulator<P>> acceptedList;
-	/** Established connection */
-	private LinkedList<ChannelSimulator<P>> connectedList;
-	
-	//private SeMaSimulator<REPCommand> sema;
-	
-	public NetworkSimulatorwithSeMa(){
-		acceptedList = new LinkedList<ChannelSimulator<P>>();
-		connectedList = new LinkedList<ChannelSimulator<P>>();
-	}
-
-	/**
-	 * Establish connection. It's called by SeMa.
-	 * @return
-	 */
-	public ChannelSimulator<P> accept(){
-		ChannelSimulator<P> cs = connectedList.poll();
-		if (cs==null) return null;
-
-		acceptedList.offer(cs);
-		return cs.getServerChannel();
-	}
-
-	/**
-	 * Request to connect.
-	 * Client editor use this method to connect SeMa. 
-	 * @param cs
-	 */
-	public ChannelSimulator<P> connect(){
-		BlockingQueue<P> rq = new LinkedBlockingQueue<P>();
-		BlockingQueue<P> wq = new LinkedBlockingQueue<P>();
-		ChannelSimulator<P> cs = new ChannelSimulator<P>(this, rq, wq);
-		connectedList.offer(cs);
-		return cs;
-	}
-		
-	public boolean checkAllCS(){
-		for(ChannelSimulator<P> cs: connectedList){
-			if(!cs.readQisEmpty()) return false;
-		}
-		return true;
-	}
-
-	public ChannelSimulator<P> getAcceptedSession(int i) {
-		return acceptedList.get(i).getServerChannel();
-	}
-}
--- a/src/pathfinder/mergetest/NetworkSimulatorwithoutSeMa.java	Tue Aug 26 19:45:26 2008 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,60 +0,0 @@
-package pathfinder.mergetest;
-
-import java.util.LinkedList;
-import java.util.concurrent.BlockingQueue;
-import java.util.concurrent.LinkedBlockingQueue;
-
-public class NetworkSimulatorwithoutSeMa<P> extends NetworkSimulator<P> {
-	/** Established connection */
-	private LinkedList<ChannelSimulator<P>> connectedList;
-
-	public NetworkSimulatorwithoutSeMa(){
-		connectedList = new LinkedList<ChannelSimulator<P>>();
-	}
-
-	/**
-	 * Request to connect.
-	 * Client editor use this method to connect SeMa. 
-	 * @param cs
-	 */
-	public ChannelSimulator<P> connect(){
-		ChannelSimulator<P> cs;
-		if (connectedList.isEmpty()){
-			BlockingQueue<P> q = new LinkedBlockingQueue<P>();
-			cs = new ChannelSimulator<P>(this, q, q);
-		}else{
-			BlockingQueue<P> rq = connectedList.getLast().getWriteQ();
-			BlockingQueue<P> wq = new LinkedBlockingQueue<P>();
-			connectedList.getFirst().setReadQ(wq);
-			
-/*			ChannelSimulator<P> lastcs = connectedList.getLast();
-			BlockingQueue<P> rq = lastcs.getWriteQ();
-
-			BlockingQueue<P> wq = new LinkedBlockingQueue<P>();
-			ChannelSimulator<P> firstcs = connectedList.getFirst();
-			firstcs.setReadQ(wq);
-*/
-			cs = new ChannelSimulator<P>(this, rq, wq);
-		}
-
-		connectedList.addLast(cs);
-		return cs;
-	}
-	
-	public ChannelSimulator<P> accept(){
-		return null;
-	}
-
-	public boolean checkAllCS(){
-		for(ChannelSimulator<P> cs: connectedList){
-			if(!cs.readQisEmpty()) return false;
-		}
-		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/PacketSet.java	Tue Aug 26 19:45:26 2008 +0900
+++ b/src/pathfinder/mergetest/PacketSet.java	Tue Aug 26 19:46:03 2008 +0900
@@ -1,5 +1,6 @@
 package pathfinder.mergetest;
 
+import pathfinder.mergetest.channels.ChannelSimulator;
 import remoteeditor.command.REPCommand;
 
 public class PacketSet {
--- a/src/pathfinder/mergetest/REPHandler.java	Tue Aug 26 19:45:26 2008 +0900
+++ b/src/pathfinder/mergetest/REPHandler.java	Tue Aug 26 19:46:03 2008 +0900
@@ -1,5 +1,6 @@
 package pathfinder.mergetest;
 
+import pathfinder.mergetest.channels.SelectionKeySimulator;
 import remoteeditor.command.REPCommand;
 
 public interface REPHandler {
--- a/src/pathfinder/mergetest/REPHandlerDoWaiting.java	Tue Aug 26 19:45:26 2008 +0900
+++ b/src/pathfinder/mergetest/REPHandlerDoWaiting.java	Tue Aug 26 19:46:03 2008 +0900
@@ -1,5 +1,6 @@
 package pathfinder.mergetest;
 
+import pathfinder.mergetest.channels.SelectionKeySimulator;
 import remoteeditor.command.REPCommand;
 
 public class REPHandlerDoWaiting implements REPHandler {
--- a/src/pathfinder/mergetest/REPHandlerImpl.java	Tue Aug 26 19:45:26 2008 +0900
+++ b/src/pathfinder/mergetest/REPHandlerImpl.java	Tue Aug 26 19:46:03 2008 +0900
@@ -1,5 +1,7 @@
 package pathfinder.mergetest;
 
+import pathfinder.mergetest.channels.ChannelSimulator;
+import pathfinder.mergetest.channels.SelectionKeySimulator;
 import remoteeditor.command.REPCommand;
 
 public class REPHandlerImpl implements REPHandler {
--- a/src/pathfinder/mergetest/REPHandlerInMerge.java	Tue Aug 26 19:45:26 2008 +0900
+++ b/src/pathfinder/mergetest/REPHandlerInMerge.java	Tue Aug 26 19:46:03 2008 +0900
@@ -3,6 +3,9 @@
 import java.util.LinkedList;
 import java.util.List;
 
+import pathfinder.mergetest.channels.ChannelSimulator;
+import pathfinder.mergetest.channels.SelectionKeySimulator;
+
 import remoteeditor.command.REPCommand;
 
 public class REPHandlerInMerge implements REPHandler {
--- a/src/pathfinder/mergetest/SeMaSimulator.java	Tue Aug 26 19:45:26 2008 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,81 +0,0 @@
-package pathfinder.mergetest;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import remoteeditor.command.REPCommand;
-
-public class SeMaSimulator<P extends REPCommand> extends Thread {
-	protected int MAX_PACKET;
-	protected int MAX_CLIENT;
-	protected boolean running=true;
-	protected NetworkSimulator<P> ns;
-	protected List<ChannelSimulator<P>> csList;
-
-	public SeMaSimulator(NetworkSimulator<P> _ns, int max_client, int max_packet){
-		ns = _ns;
-		MAX_CLIENT = max_client;
-		MAX_PACKET = max_packet;
-		csList = new ArrayList<ChannelSimulator<P>>();
-	}
-	public SeMaSimulator(NetworkSimulator<P> _ns, int max_client){
-		this(_ns, max_client, 0);
-	}
-	public SeMaSimulator(NetworkSimulator<P> _ns){
-		this(_ns, 2);
-	}
-
-	synchronized public void finish(){
-		synchronized(ns){
-			running = false;
-			ns.notify();
-		}
-	}
-
-	/**
-	 * Check whether the NetworkSimulator hold waiting connections.
-	 */
-	protected void checkAccept(){
-		ChannelSimulator<P> cs;
-		while((cs=ns.accept())!=null){
-			csList.add(cs);
-		}
-	}
-	public void init(){
-		ns.writeLog("SessionManager.init", 1);
-		while(csList.size()<MAX_CLIENT){ checkAccept(); Thread.yield(); }
-	}
-
-	public void run(){
-		int i=0;
-		int count=0;
-		P packet;
-
-		ns.writeLog("SessionManager start.", 1);
-
-		/* Main Loop */
-		ChannelSimulator<P> cs = csList.get(i);
-		while(running
-				&& (MAX_PACKET==0 || count<MAX_PACKET)){
-			synchronized(ns){
-				int prev_i=i;
-				while((packet=cs.poll())==null && running){
-					i = (i+1)%csList.size();   // i++
-					cs = csList.get(i);        // 次のChennelをゲット
-					if(i==prev_i) try { ns.wait(); } catch (InterruptedException e) { e.printStackTrace(); }
-				}
-			}
-			if(!running) break;
-
-			ns.writeLog("SeMa pass packet to "+i+":>> "+packet.toString(), 3);
-			i = (i+1)%csList.size();   // i++
-			cs = csList.get(i);        // 次のChennelをゲット
-
-			if ( !cs.write(packet) ){
-				ns.writeLog("Session Manager failed to write.", 0);
-			}
-			count++;
-		}
-		ns.writeLog("SessionManager finish.", 1);
-	}
-}
--- a/src/pathfinder/mergetest/SelectionKeySimulator.java	Tue Aug 26 19:45:26 2008 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,38 +0,0 @@
-package pathfinder.mergetest;
-
-public class SelectionKeySimulator<P> {
-
-	public static final int OP_READ = 0;
-	private ChannelSimulator<P> channel;
-	private Object attachment;
-
-	public SelectionKeySimulator(ChannelSimulator<P> cs) {
-		// TODO Auto-generated constructor stub
-		channel = cs;
-	}
-
-	public boolean isAcceptable() {
-		
-		return channel.isAcceptable();
-	}
-
-	public boolean isReadable() {
-		return channel.isReadable();
-	}
-
-	public ChannelSimulator<P> channel() {
-		// TODO Auto-generated method stub
-		return channel;
-	}
-
-	public Object attachment() {
-		// TODO Auto-generated method stub
-		return attachment;
-	}
-
-	public void attach(Object handler) {
-		// TODO Auto-generated method stub
-		attachment = handler;
-	}
-
-}
--- a/src/pathfinder/mergetest/SelectorSimulator.java	Tue Aug 26 19:45:26 2008 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,75 +0,0 @@
-package pathfinder.mergetest;
-
-import java.util.ArrayList;
-import java.util.Set;
-
-public class SelectorSimulator<P> {
-	
-	private ArrayList<SelectionKeySimulator<P>> keyList;
-	private NetworkSimulator<P> ns;
-	private ArrayList<SelectionKeySimulator<P>> selectedKeys;
-	
-	public SelectorSimulator(NetworkSimulator<P> _ns) {
-		// TODO Auto-generated constructor stub
-		ns = _ns;
-		keyList = new ArrayList<SelectionKeySimulator<P>>();
-	}
-
-	public int select(){
-		selectedKeys = new ArrayList<SelectionKeySimulator<P>>();
-		
-		synchronized(ns){
-			boolean empty = true;
-			//while(empty){
-				for(SelectionKeySimulator<P> key : keyList){
-					ChannelSimulator<P> channel = key.channel();
-//					if(channel.readQisEmpty()){
-//						empty = true;
-//					}else{
-//						empty = false;
-//						selectedKeys.add(key);
-//						break;
-//					}
-					if(!channel.readQisEmpty()){
-						empty = false;
-						selectedKeys.add(key);
-					}
-				}
-
-				try {
-					if(empty) ns.wait();
-				} catch (InterruptedException e) {
-					//e.printStackTrace();
-				}
-			//}
-		}
-		return selectedKeys.size();
-	}
-	
-	public SelectionKeySimulator<P> register(ChannelSimulator<P> cs){
-		SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs);
-		keyList.add(new SelectionKeySimulator<P>(cs));
-		return key;
-	}
-	
-	public SelectionKeySimulator<P> register(ChannelSimulator<P> cs, int opt, Object handler){
-		SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs);
-		key.attach(handler);
-		keyList.add(key);
-		return key;
-	}
-
-	public ArrayList<SelectionKeySimulator<P>> selectedKeys() {
-		
-		return selectedKeys;
-	}
-	
-	public SelectionKeySimulator getKey(ChannelSimulator channel){
-		for(SelectionKeySimulator key : keyList){
-			if(key.channel() == channel)
-				return key;
-		}
-		return null;
-	}
-
-}
--- a/src/pathfinder/mergetest/SessionManagerSimulatorWithMerger.java	Tue Aug 26 19:45:26 2008 +0900
+++ b/src/pathfinder/mergetest/SessionManagerSimulatorWithMerger.java	Tue Aug 26 19:46:03 2008 +0900
@@ -1,66 +1,81 @@
 package pathfinder.mergetest;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
 
+import pathfinder.mergetest.channels.*;
+
 import remoteeditor.command.REPCommand;
 import remoteeditor.network.REP;
 import sample.merge.TranslaterImp1;
 
-public class SessionManagerSimulatorWithMerger extends SeMaSimulator<REPCommand> {
+public class SessionManagerSimulatorWithMerger extends Thread{
+	protected boolean running=true;
+	protected List<ChannelSimulator<REPCommand>> csList;
 
 	private List<TranslaterImp1> transList;
 	private SelectorSimulator<REPCommand> selector;
-	private List<EditorObject<REPCommand>> editorList2;
+	private List<EditorObject> editorList;
 	private int startQuit2;
 	//private List<LinkedList<REPCommand>> sendList;
 	private List<PacketSet> waitingList;
+	protected NetworkSimulator<REPCommand> ns;
+	protected ServerChannelSimulator<REPCommand> scs;
+	protected int IP;
 
-	public SessionManagerSimulatorWithMerger(NetworkSimulator<REPCommand> _ns) {
-		super(_ns);
+
+	public SessionManagerSimulatorWithMerger(NetworkSimulator<REPCommand> _ns, int ip) {
+		IP = ip;
+		ns = _ns;
+		transList = new ArrayList<TranslaterImp1>();
+		editorList = new ArrayList<EditorObject>();
+		waitingList = new LinkedList<PacketSet>();
+		selector = new SelectorSimulator<REPCommand>();
+		scs = new ServerChannelSimulator<REPCommand>(ns, selector);
+		scs.bind(IP);
+		selector.register(scs, SelectionKeySimulator.OP_ACCEPT);
 	}
 
-	public SessionManagerSimulatorWithMerger(NetworkSimulator<REPCommand> ns, int ne) {
-		super(ns, ne);
-	}
-	
-	public SessionManagerSimulatorWithMerger(NetworkSimulator<REPCommand> _ns, int max_client, int max_packet) {
-		super(_ns, max_client, max_packet);
-		transList = new ArrayList<TranslaterImp1>();
-		selector = new SelectorSimulator<REPCommand>(_ns);
-		editorList2 = new ArrayList<EditorObject<REPCommand>>();
-		waitingList = new LinkedList<PacketSet>();
-		//sendList = new ArrayList<LinkedList<REPCommand>>();
-	}
-	
+	/*
 	protected void checkAccept(){
 		ChannelSimulator<REPCommand> cs;
 		while((cs=ns.accept())!=null){
 			csList.add(cs);
 			transList.add(new TranslaterImp1(transList.size()));
-			editorList2.add(new EditorObject<REPCommand>(editorList2.size(), cs, new TranslaterImp1(editorList2.size())));
+			editorList.add(new EditorObject<REPCommand>(editorList.size(), cs, new TranslaterImp1(editorList.size())));
 			//sendList.add(new LinkedList<REPCommand>());
 			registerChannel (selector, cs, SelectionKeySimulator.OP_READ);
 		}
-	}
+	}*/
 	
-	private void registerChannel(SelectorSimulator selector2, ChannelSimulator<REPCommand> cs, int key) {
-		//selector.register(cs);
+	private void registChannel(SelectorSimulator<REPCommand> selector2, ChannelSimulator<REPCommand> cs, int key) {
+		csList.add(cs);
+		transList.add(new TranslaterImp1(transList.size()));
+		editorList.add(new EditorObject(editorList.size(), cs, new TranslaterImp1(editorList.size())));
 		REPHandler handler = new REPHandlerImpl(this);
 		selector.register(cs, SelectionKeySimulator.OP_READ, handler);
 	}
-
+	
+	public void init(){
+	}
+	
 	public void run(){
 
 		ns.writeLog("SessionManager start.", 1);
 
-		mainLoop();
+		try {
+			mainLoop();
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
 		
 		ns.writeLog("SessionManager finish.", 1);
 	}
 
-	private void mainLoop() {
+	private void mainLoop() throws IOException {
 		/* Main Loop */
 		while(running){
 
@@ -73,10 +88,11 @@
 		for(SelectionKeySimulator<REPCommand> key : selector.selectedKeys()){
 			
 			if(key.isAcceptable()){
-				ChannelSimulator<REPCommand> channel = key.channel();
+				ChannelSimulator<REPCommand> channel = (ChannelSimulator<REPCommand>)key.channel();
 				channel = channel.accept();
-				REPHandler handler = new REPHandlerImpl(this);
-				selector.register(channel, SelectionKeySimulator.OP_READ, handler);
+				//REPHandler handler = new REPHandlerImpl(this);
+				//selector.register(channel, SelectionKeySimulator.OP_READ, handler);
+				registChannel(selector, channel, SelectionKeySimulator.OP_READ);
 				
 			}else if(key.isReadable()){
 
@@ -95,7 +111,6 @@
 		
 		ns.writeLog("SessionManager received from " + ":" + eid + ": " + command, 3);
 		
-		EditorObject editor = getEditor(eid);
 		EditorObject nextEditor = getEditor(neid);
 		//editor.receive(command);
 		
@@ -117,7 +132,7 @@
 	}
 
 	private int getNextEID(int eid) {
-		return (eid+1)%editorList2.size();
+		return (eid+1)%editorList.size();
 	}
 
 
@@ -141,11 +156,7 @@
 
 	private void translate(int eid, int neid, REPCommand command) {
 		
-		int peid = (eid + editorList2.size() - 1)%editorList2.size();
-
 		ChannelSimulator<REPCommand> channel = getChannel(eid);
-		ChannelSimulator<REPCommand> nextChannel = getChannel(neid);
-		ChannelSimulator<REPCommand> prevChannel = getChannel(peid);
 
 		EditorObject editor = getEditor(eid);
 		EditorObject nextEditor = getEditor(neid);
@@ -195,19 +206,17 @@
 	}
 
 	private void setMergeState(ChannelSimulator<REPCommand> channel, SelectorSimulator<REPCommand> selector2) {
-		// TODO Auto-generated method stub
-		SelectionKeySimulator key = channel.keyFor(selector2);
+		SelectionKeySimulator<REPCommand> key = channel.keyFor(selector2);
 		key.attach(new REPHandlerInMerge(this));
 	}
 
 	private void setNormalState(ChannelSimulator<REPCommand> channel, SelectorSimulator<REPCommand> selector2) {
-		// TODO Auto-generated method stub
-		SelectionKeySimulator key = channel.keyFor(selector2);
+		SelectionKeySimulator<REPCommand> key = channel.keyFor(selector2);
 		key.attach(new REPHandlerImpl(this));
 	}
 
 	private EditorObject getEditor(int eid) {
-		for(EditorObject editor : editorList2){
+		for(EditorObject editor : editorList){
 			if(editor.getEID() == eid) {
 				return editor;
 			}
@@ -217,7 +226,7 @@
 	
 	private int getEID(ChannelSimulator<REPCommand> channel) {
 		int eid = 0;
-		for(EditorObject editor : editorList2){
+		for(EditorObject editor : editorList){
 			if(editor.getChannel() == channel){
 				eid = editor.getEID();
 			}
@@ -227,7 +236,7 @@
 
 	private ChannelSimulator<REPCommand> getChannel(int eid) {
 		ChannelSimulator<REPCommand> channel = null;
-		for(EditorObject<REPCommand> editor : editorList2){
+		for(EditorObject editor : editorList){
 			if(editor.getEID() == eid){
 				channel = editor.getChannel();
 			}
@@ -263,4 +272,11 @@
 		
 	}
 	
+	synchronized public void finish(){
+		synchronized(ns){
+			running = false;
+			ns.notify();
+		}
+	}
+	
 }
--- a/src/pathfinder/mergetest/TestMerger.java	Tue Aug 26 19:45:26 2008 +0900
+++ b/src/pathfinder/mergetest/TestMerger.java	Tue Aug 26 19:46:03 2008 +0900
@@ -1,6 +1,8 @@
 package pathfinder.mergetest;
 
 import java.util.LinkedList;
+
+import pathfinder.mergetest.channels.NetworkSimulator;
 import remoteeditor.command.REPCommand;
 import remoteeditor.network.REP;
 
--- a/src/pathfinder/mergetest/TestMerger2.java	Tue Aug 26 19:45:26 2008 +0900
+++ b/src/pathfinder/mergetest/TestMerger2.java	Tue Aug 26 19:46:03 2008 +0900
@@ -1,6 +1,8 @@
 package pathfinder.mergetest;
 
 import java.util.LinkedList;
+
+import pathfinder.mergetest.channels.NetworkSimulator;
 import remoteeditor.command.REPCommand;
 import remoteeditor.network.REP;
 
@@ -18,8 +20,6 @@
 		"ppp", "qqq", "rrr", "sss", "ttt",
 		"uuu", "vvv", "www", "xxx", "yyy", "zzz"
 	};	
-	static private String[] text1 = {""};
-
 	public TestMerger2(){
 		editors = new LinkedList<EditorSimulatorWithoutMerger>();
 	}
@@ -52,7 +52,7 @@
 			ee.start();
 		}
 		/* start SessionManager if it exist.  */
-		if (sema!=null) sema.init();
+		//if (sema!=null) sema.init();
 		if (sema!=null) sema.start();
 		
 		for(UserSimulator u: users){
@@ -96,13 +96,8 @@
 	
 	protected void init(boolean sm, int ne, int np, int ll){
 		/* create NetworkSimulator, and SessionManager if it's required.   */
-		if (sm){
-			ns = new NetworkSimulatorwithSeMa<REPCommand>();
-			sema = new SessionManagerSimulatorWithMerger(ns, ne, 0);
-		} else {
-			ns = new NetworkSimulatorwithoutSeMa<REPCommand>();
-			sema = null;
-		}
+		ns = new NetworkSimulator<REPCommand>();
+		sema = new SessionManagerSimulatorWithMerger(ns, 1000);
 		ns.setLogLevel(ll);
 		
 		/* create UsersSimulator.  */
--- a/src/pathfinder/mergetest/UserSimulator.java	Tue Aug 26 19:45:26 2008 +0900
+++ b/src/pathfinder/mergetest/UserSimulator.java	Tue Aug 26 19:46:03 2008 +0900
@@ -2,22 +2,26 @@
 
 import java.util.LinkedList;
 
+import pathfinder.mergetest.channels.ChannelSimulator;
+import pathfinder.mergetest.channels.NetworkSimulator;
+
 import remoteeditor.command.REPCommand;
 
 public class UserSimulator extends Thread {
 	private NetworkSimulator<REPCommand> ns;
 	private LinkedList<REPCommand> cmds;
-	private int eid;
+	//private int eid;
+	private EditorSimulator editor;
 
 	public UserSimulator(NetworkSimulator<REPCommand> ns2, int _eid, LinkedList<REPCommand> _cmds) {
 		ns = ns2;
-		eid = _eid;
+		//eid = _eid;
 		cmds = _cmds;
 	}
 
 	public void run(){
 		ns.writeLog("UsersSimulator start.", 1);
-		ChannelSimulator<REPCommand> channel = ns.getAcceptedSession(eid);
+		ChannelSimulator<REPCommand> channel = editor.getChannelforUser();
 		while(cmds.size()>0){
 //			try {
 //				Thread.sleep(1000);