diff rep/SessionManager.java @ 308:c5be84d53c7f channel-simulator-update **INVALID**

*** empty log message ***
author kono
date Sat, 04 Oct 2008 22:12:34 +0900
parents 75192c844a8d
children 0585fd2410b8
line wrap: on
line diff
--- a/rep/SessionManager.java	Sat Oct 04 22:12:16 2008 +0900
+++ b/rep/SessionManager.java	Sat Oct 04 22:12:34 2008 +0900
@@ -34,6 +34,8 @@
 	int cmd;		kind of command
 	int sid;		session ID : uniqu to editing file
 	int eid;		editor ID : owner editor ID = 1。Session に対して unique
+	                -1 session manager command
+	                -2 merge command
 	int seqno;		Sequence number : sequence number はエディタごとに管理
 	int lineno;		line number
 	int textsize;   textsize : bytesize
@@ -46,14 +48,16 @@
 	private SessionManagerGUI gui;
 	private REPSelector<REPCommand> selector;
 	private SessionManagerList smList;
-	private String myHost;
 	private List<Editor> editorList;
 	// editorList は、sessionList に入っているeditorとは別なeditorのlistらしい。
 	private String maxHost;
 	private List<PacketSet> waitingCommandInMerge;
-	private BlockingQueue<SessionManagerEvent> waitingQueue = new LinkedBlockingQueue<SessionManagerEvent>();;
-	private static int temp_port;
-	private static int send_port;
+	REPHandler normalHandler = new REPHandlerImpl(this);
+	REPHandler handlerInMerge =new REPHandlerInMerge(this); 
+	private BlockingQueue<SessionManagerEvent> waitingEventQueue = new LinkedBlockingQueue<SessionManagerEvent>();;
+	private String myHost;
+	private static int receive_port;
+	private static int parent_port;
 	static final int DEFAULT_PORT = 8766;
 	
 	
@@ -76,31 +80,28 @@
 		ssc.socket().setReuseAddress(true);
 		//getAllByNameで取れた全てのアドレスに対してbindする
 		ssc.socket().bind(new InetSocketAddress(port));
-		ssc.register(selector, SelectionKey.OP_ACCEPT, new REPHandlerImpl(-1, this));
+		ssc.register(selector, SelectionKey.OP_ACCEPT, normalHandler);
 
 		sessionList = new LinkedList<Session>();
 		smList = new SessionManagerList();
 		editorList = new LinkedList<Editor>();
 		waitingCommandInMerge = new LinkedList<PacketSet>();
 		
-		//デフォルトのSessionを作っておく(テスト用に?)
-		//if(sessionList.size() > 0) System.out.println("Error : SessionManager.init():");
-		//Session defaultSession = new Session(sessionList.size(), "DefaultSession.txt", new Editor(0,null));
-		//sessionList.add(defaultSession);
 
 	}
 
 	public void mainLoop() throws IOException {
 		while(true){
 			SessionManagerEvent e;
-			while((e = waitingQueue.poll())!=null){
+			while((e = waitingEventQueue.poll())!=null){
 				e.exec();
 			}
 			for(Session s:sessionList) {
 				for(Editor editor: s.getEditorList()) 
 					if (editor.doWaitingWrite()) break;
 			}
-			if(checkSend()){
+			// if there are waiting command during merge operation, do process it
+			if(checkWaitingCommandInMerge()){
 				if(selector.selectNow() > 0){
 					select();
 				}
@@ -111,12 +112,18 @@
 		}
 	}
 
-	private boolean checkSend() throws IOException {
+	/**
+	 * Check waiting command in merge
+	 * @return true if there is a processed waiting command
+	 * @throws IOException
+	 */
+	private boolean checkWaitingCommandInMerge() throws IOException {
 		for(Iterator<PacketSet> it = waitingCommandInMerge.iterator(); it.hasNext();){
 			PacketSet p = it.next();
-			if(p.getEditor().isMerging()) {
+			if(p.getEditor().isMerging()) { // still merging do nothing
 				continue;
 			}else{
+				// process one command and return true
 				manage(p.channel, p.command);
 				it.remove();
 				return true;
@@ -125,7 +132,6 @@
 		return false;
 	}
 
-	@SuppressWarnings("unchecked")
 	private void select() throws IOException {
 		
 		Set<REPSelectionKey<REPCommand>> keys = selector.selectedKeys1();
@@ -142,10 +148,10 @@
 					handler.handle(key);
 				} catch (ClosedChannelException x) {
 					key.cancel();
-					handler.cancel((REPSocketChannel<REPCommand>)key.channel());
+					handler.cancel(key.channel1());
 				} catch (IOException x) {
 					key.cancel();
-					handler.cancel((REPSocketChannel<REPCommand>)key.channel());
+					handler.cancel( key.channel1());
 				}
 			}
 		}
@@ -156,7 +162,7 @@
 			return;
 		}
 		channel.configureBlocking(false);
-		REPHandler handler = new REPHandlerImpl(-1, this);
+		REPHandler handler = normalHandler;
 		channel.register(selector, ops, handler);
 	}
 
@@ -390,6 +396,7 @@
 			session.translate(channel, receivedCommand);
 			boolean newState = editor.isMerging();
 			if (old!=newState) {
+				// prevEditor なのは変だと思うが...
 				Editor prevEditor = session.getPrevEditor(editor);
 				//マージ中のエディタはコマンドを受け取らない
 				// この代入は状態が変わったときだけ行えば良い。毎回、new するのは変
@@ -435,12 +442,12 @@
 
 	private void setNormalState(REPSocketChannel<REPCommand> channel, int sid) {
 		SelectionKey key = channel.keyFor(selector);
-		key.attach(new REPHandlerImpl(sid, this));
+		key.attach(normalHandler);
 	}
 
 	private void setMergeState(REPSocketChannel<REPCommand> channel, int sid) {
 		SelectionKey key = channel.keyFor(selector);
-		key.attach(new REPHandlerInMerge(sid, this));
+		key.attach(handlerInMerge);
 	}
 
 	private Editor getEditor(String hostport) {
@@ -478,7 +485,7 @@
 	}
 
 	private void setMyHostName(String localHostName) {
-		myHost = localHostName + temp_port;
+		myHost = localHostName + receive_port;
 		if(maxHost == null) {
 			maxHost = myHost;
 		}
@@ -500,8 +507,8 @@
 			port = Integer.parseInt(args[0]);
 			port_s = Integer.parseInt(args[1]);
 		}
-		temp_port = port;
-		send_port = port_s;
+		receive_port = port;
+		parent_port = port_s;
 		SessionManager sm = new SessionManager();
 		sm.init(port,new SessionManagerGUIimpl(sm));
 		
@@ -510,7 +517,7 @@
 
 	public void connectSession(String host) {
 		int port = DEFAULT_PORT;
-		port = send_port;
+		port = parent_port;
 		InetSocketAddress addr = new InetSocketAddress(host, port);
 		try {
 			REPSocketChannel<REPCommand> sessionchannel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
@@ -600,14 +607,14 @@
 
 	public void buttonPressed(SessionManagerEvent event) {
 		try {
-			waitingQueue.put(event);
+			waitingEventQueue.put(event);
 		} catch (InterruptedException e) {}
 		selector.wakeup();
 	}
 	
 	public void syncExec(SessionManagerEvent event) {
 		try {
-			waitingQueue.put(event);
+			waitingEventQueue.put(event);
 		} catch (InterruptedException e) {
 		}
 	}