changeset 281:c3969dd625b2

GUIless test routine.
author kono
date Sat, 27 Sep 2008 22:55:13 +0900
parents a549bd4dadb8
children c410eda661e8
files rep/Editor.java rep/Session.java rep/SessionManager.java rep/channel/NetworkSimulator.java test/sematest/TestSessionManager.java test/sematest/Tester.java test/sematest/testGUI.java
diffstat 7 files changed, 48 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/rep/Editor.java	Sat Sep 27 15:59:11 2008 +0900
+++ b/rep/Editor.java	Sat Sep 27 22:55:13 2008 +0900
@@ -9,7 +9,8 @@
 import rep.translater.TranslaterImp1;
 
 public class Editor {
-	private int eid;
+	private int eid;            // unique id in a session
+	private int sid = -1 ;      // globally unique session id
 	private REPSocketChannel<REPCommand> myChannel;
 	private String host;
 	private String file;
@@ -160,5 +161,11 @@
 	public boolean isMerging() {
 		return translater.isMerging();
 	}
+	public boolean hasSession() {
+		return sid != -1;
+	}
+	public void setSID(int sessionID) {
+		sid   = sessionID;
+	}
 
 }
--- a/rep/Session.java	Sat Sep 27 15:59:11 2008 +0900
+++ b/rep/Session.java	Sat Sep 27 22:55:13 2008 +0900
@@ -49,6 +49,7 @@
 	public void addEditor(Editor editor) {
 		int eid = editorList.size();
 		editor.setEID(eid);
+		editor.setSID(sessionID);
 		editorList.add(editor);
 	}
 	
--- a/rep/SessionManager.java	Sat Sep 27 15:59:11 2008 +0900
+++ b/rep/SessionManager.java	Sat Sep 27 22:55:13 2008 +0900
@@ -50,14 +50,11 @@
 	private List<Editor> editorList;
 	private String maxHost;
 	private List<PacketSet> waitingCommandInMerge;
-	private BlockingQueue<SessionManagerEvent> waitingQueue;
+	private BlockingQueue<SessionManagerEvent> waitingQueue = new LinkedBlockingQueue<SessionManagerEvent>();;
 	private static int temp_port;
 	private static int send_port;
 	static final int DEFAULT_PORT = 8766;
 	
-	public SessionManager(int port) {
-		
-	}
 	
 	public void openSelector() throws IOException{
 		selector = REPSelector.<REPCommand>create();
@@ -84,7 +81,6 @@
 		smList = new SessionManagerList();
 		editorList = new LinkedList<Editor>();
 		waitingCommandInMerge = new LinkedList<PacketSet>();
-		waitingQueue = new LinkedBlockingQueue<SessionManagerEvent>();
 		
 		//デフォルトのSessionを作っておく(テスト用に?)
 		//if(sessionList.size() > 0) System.out.println("Error : SessionManager.init():");
@@ -95,6 +91,10 @@
 
 	public void mainLoop() throws IOException {
 		while(true){
+			SessionManagerEvent e;
+			while((e = waitingQueue.poll())!=null){
+				e.exec();
+			}
 			if(checkSend()){
 				if(selector.selectNow() > 0){
 					select();
@@ -122,10 +122,6 @@
 
 	@SuppressWarnings("unchecked")
 	private void select() throws IOException {
-		SessionManagerEvent e;
-		while((e = waitingQueue.poll())!=null){
-			e.exec();
-		}
 		
 		Set<REPSelectionKey<REPCommand>> keys = selector.selectedKeys1();
 		for(REPSelectionKey<REPCommand> key : keys){
@@ -406,27 +402,16 @@
 	}
 
 	private void updateGUI() {
-		if(gui == null){
-			//System.out.println("SessionManager.guiUpdate() : gui = " + gui);
-			return;
-		}
 		//リストのコピーをGUIに渡す
 		LinkedList<Session> sList = new LinkedList<Session>(sessionList);
 		LinkedList<Editor> eList = new LinkedList<Editor>(editorList);
-//		for(Editor editor : eList){
-//			System.out.println("SessionManager.guiUpdate() : channel = " + editor.getChannel());
-//		}
-//		
 		//GUIに反映
 		Runnable doRun = new DoGUIUpdate(sList, eList, gui);
 		gui.invokeLater(doRun);
 	}
 
 	private void setNormalState(REPSocketChannel<REPCommand> channel, int sid) {
-		//System.out.println("SessionManager.setNormalState() : channel = " + channel);
-		//System.out.println("SessionManager.setNormalState() : selector = " + selector);
 		SelectionKey key = channel.keyFor(selector);
-		// System.out.println("SessionManager.setNormalState() : key = " + key);
 		key.attach(new REPHandlerImpl(sid, this));
 	}
 
@@ -494,7 +479,7 @@
 		}
 		temp_port = port;
 		send_port = port_s;
-		SessionManager sm = new SessionManager(port);
+		SessionManager sm = new SessionManager();
 		sm.init(port,new SessionManagerGUIimpl(sm));
 		
 
@@ -595,6 +580,13 @@
 		} catch (InterruptedException e) {}
 		selector.wakeup();
 	}
+	
+	public void syncExec(SessionManagerEvent event) {
+		try {
+			waitingQueue.put(event);
+		} catch (InterruptedException e) {
+		}
+	}
 
 	public void closeSession(SessionManagerEvent event) {
 		Session session = ((CloseButtonEvent) event).getSession();
--- a/rep/channel/NetworkSimulator.java	Sat Sep 27 15:59:11 2008 +0900
+++ b/rep/channel/NetworkSimulator.java	Sat Sep 27 22:55:13 2008 +0900
@@ -45,6 +45,7 @@
 	synchronized public boolean connect(SocketAddress ip, ChannelSimulator<P> CHclient) {
 		logger.writeLog("connecting..", 1);
 		for (ServerData<P> sd0: serverList){
+			// ANY address (0.0.0.0/0.0.0.0) should be considered.
 			if (!sd0.IP.equals(ip)) continue;
 
 			ChannelSimulator<P> CHserver = new ChannelSimulator<P>();
--- a/test/sematest/TestSessionManager.java	Sat Sep 27 15:59:11 2008 +0900
+++ b/test/sematest/TestSessionManager.java	Sat Sep 27 22:55:13 2008 +0900
@@ -6,6 +6,8 @@
 import rep.REP;
 import rep.REPCommand;
 import rep.SessionManager;
+import rep.SessionManagerEvent;
+import rep.SessionManagerGUI;
 import rep.channel.REPLogger;
 import rep.channel.REPServerSocketChannel;
 
@@ -25,7 +27,7 @@
 		
 		while(!isStart){
 			try {
-				Thread.sleep(50);
+				Thread.sleep(5);
 			} catch (InterruptedException e) {
 				e.printStackTrace();
 			}
@@ -74,13 +76,14 @@
 						port = Integer.parseInt(strs[0]);
 					}
 
-					SessionManager sm = new SessionManager(port);
-					sm.openSelector();
-					sm.init(port,new testGUI(sm));
+					SessionManager sm = new SessionManager();
+					SessionManagerGUI gui = new testGUI(sm);
 					logger.writeLog("TestSessionManager.startSessionManager() : start SessionManager");
-					isStart = true;
-					sm.mainLoop();
-					
+					sm.syncExec(new SessionManagerEvent() {
+						// executed before first select();
+						public void exec() {	isStart = true;	}
+					});
+					sm.init(port,gui);
 				} catch (InterruptedException e) {
 					e.printStackTrace();
 				} catch (IOException e) {
--- a/test/sematest/Tester.java	Sat Sep 27 15:59:11 2008 +0900
+++ b/test/sematest/Tester.java	Sat Sep 27 22:55:13 2008 +0900
@@ -22,7 +22,7 @@
 	public Tester(String name, String _host,int _port){
 		super(name);
 
-		REPServerSocketChannel.isSimulation = false;
+		// REPServerSocketChannel.isSimulation = false;
 		semaIP = new InetSocketAddress(_host, _port);
 		ns = REPLogger.singleton();
 		ns.setLogLevel(5);
--- a/test/sematest/testGUI.java	Sat Sep 27 15:59:11 2008 +0900
+++ b/test/sematest/testGUI.java	Sat Sep 27 22:55:13 2008 +0900
@@ -3,7 +3,9 @@
 import java.util.LinkedList;
 
 import rep.Editor;
+import rep.SelectButtonEvent;
 import rep.Session;
+import rep.SessionManagerEvent;
 import rep.SessionManagerEventListener;
 import rep.SessionManagerGUI;
 
@@ -18,6 +20,7 @@
 	public LinkedList<Session> slist;
 	public LinkedList<Editor> elist;
 	SessionManagerEventListener manager;
+	int count = 0;
 
 	public testGUI(SessionManagerEventListener manager) {
 		this.manager = manager;
@@ -30,6 +33,17 @@
 	public void update(LinkedList<Session> slist, LinkedList<Editor> elist) {
 		this.slist = slist;
 		this.elist = elist;
+		// fair and determistic select session for an empty editor
+		if (slist.size()==0) return;
+		Session s = slist.get(count++ % slist.size());
+		for(Editor e :elist) {
+			if (!e.hasSession()) {
+				SessionManagerEvent event = new SelectButtonEvent(e, s, manager);
+				System.out.println("Select session "+s.getSID()+" and editor "+e.getEID());
+				manager.buttonPressed(event);
+				s = slist.get(count++ % slist.size());
+			}
+		}
 	}
 
 }