diff rep/SessionManager.java @ 164:3841cc75b808

*** empty log message ***
author pin
date Thu, 28 Aug 2008 17:58:42 +0900
parents fb516794ee28
children 3dc194f5e28f
line wrap: on
line diff
--- a/rep/SessionManager.java	Thu Aug 28 17:12:49 2008 +0900
+++ b/rep/SessionManager.java	Thu Aug 28 17:58:42 2008 +0900
@@ -25,6 +25,7 @@
 import rep.handler.PacketSet;
 import rep.handler.REPHandler;
 import rep.handler.REPHandlerImpl;
+import rep.handler.REPHandlerInMerge;
 import rep.channel.REPSelector;
 import rep.xml.SessionXMLDecoder;
 import rep.xml.SessionXMLEncoder;
@@ -46,7 +47,7 @@
 	
 	
 	//private SessionList sessionlist;
-	private List<Session> sessionList;
+	private LinkedList<Session> sessionList;
 	private SessionManagerGUI gui;
 	private Selector selector;
 	private SessionManagerList smList;
@@ -143,85 +144,85 @@
 		switch(receivedCommand.cmd){
 
 		case REP.SMCMD_JOIN:
-			{
-				//どのSessionにも属さないエディタをリストに追加
-				Editor editor = new Editor(editorList.size(), channel);
-				editor.setHost(myHost);
-				editorList.add(editor);
-				
-				//GUIに反映
-				gui.setComboEditor(editor.getEID(), channel);
-			}
-			
+		{
+			//どのSessionにも属さないエディタをリストに追加
+			Editor editor = new Editor(editorList.size(), channel);
+			editor.setHost(myHost);
+			editorList.add(editor);
 
-			break;
+			//GUIに反映
+			gui.setComboEditor(editor.getEID(), channel);
+		}
+
+
+		break;
 
 		case REP.SMCMD_JOIN_ACK:
 
 			break;
 
 		case REP.SMCMD_PUT:
-			{
-				//エディタのリストに追加
-				Editor editor = new Editor(editorList.size(), channel);
-				editorList.add(editor);
-				
-				//Sessionを生成
-				int sid = sessionList.size();
-				editor = new Editor(0, channel);
-				editor.setHost(myHost);
-				Session session = new Session(sid, editor);
-				session.hasOwner(true);
-				sessionList.add(new Session(sid, editor));
-				
-				//GUIに反映
-				gui.setComboSession(session.getSID(), session.getName());
-				gui.setComboEditor(editor.getEID(), editor.getChannel());
-				
-				//エディタにAckを送信
-				sendCommand.setCMD(REP.SMCMD_PUT_ACK);
-				sendCommand.setEID(editor.getEID());
-				sendCommand.setSID(session.getSID());
-				editor.send(sendCommand);
-				
-				//他のSessionManagerへSessionの追加を報告
-				SessionXMLEncoder sessionEncoder = new SessionXMLEncoder(session);
-				REPCommand command = new REPCommand();
-				command.setSID(session.getSID());
-				command.setString(sessionEncoder.sessionListToXML());
-				command.setCMD(REP.SMCMD_UPDATE);
-				smList.sendExcept(channel, command);
+		{
+			//エディタのリストに追加
+			Editor editor = new Editor(editorList.size(), channel);
+			editorList.add(editor);
+
+			//Sessionを生成
+			int sid = sessionList.size();
+			editor = new Editor(0, channel);
+			editor.setHost(myHost);
+			Session session = new Session(sid, editor);
+			session.hasOwner(true);
+			sessionList.add(new Session(sid, editor));
+
+			//GUIに反映
+			gui.setComboSession(session.getSID(), session.getName());
+			gui.setComboEditor(editor.getEID(), editor.getChannel());
 
-			}
+			//エディタにAckを送信
+			sendCommand.setCMD(REP.SMCMD_PUT_ACK);
+			sendCommand.setEID(editor.getEID());
+			sendCommand.setSID(session.getSID());
+			editor.send(sendCommand);
 
-			break;
+			//他のSessionManagerへSessionの追加を報告
+			SessionXMLEncoder sessionEncoder = new SessionXMLEncoder(session);
+			REPCommand command = new REPCommand();
+			command.setSID(session.getSID());
+			command.setString(sessionEncoder.sessionListToXML());
+			command.setCMD(REP.SMCMD_UPDATE);
+			smList.sendExcept(channel, command);
+
+		}
+
+		break;
 
 		case REP.SMCMD_SELECT:
-			{
-				//エディタをSessionに追加
-				Editor editor = new Editor(channel);
-				Session session = getSession(receivedCommand.sid);
-				session.addEditor(editor);
-				
-				if(session.hasOwner()){
-					//このSessionManagerがオーナーを持っている場合、Sessionにエディタを追加し、エディタへAckを返す
-					sendCommand.setCMD(REP.SMCMD_SELECT_ACK);
-					sendCommand.setEID(editor.getEID());
-					editor.send(sendCommand);
-				}else{
-					//オーナーを持ってない場合は、オーナーを持っているSessionManagerへSELECTコマンドを中継する
-					Editor owner = session.getOwner();
-					owner.send(receivedCommand);
-				}
+		{
+			//エディタをSessionに追加
+			Editor editor = new Editor(channel);
+			Session session = getSession(receivedCommand.sid);
+			session.addEditor(editor);
+
+			if(session.hasOwner()){
+				//このSessionManagerがオーナーを持っている場合、Sessionにエディタを追加し、エディタへAckを返す
+				sendCommand.setCMD(REP.SMCMD_SELECT_ACK);
+				sendCommand.setEID(editor.getEID());
+				editor.send(sendCommand);
+			}else{
+				//オーナーを持ってない場合は、オーナーを持っているSessionManagerへSELECTコマンドを中継する
+				Editor owner = session.getOwner();
+				owner.send(receivedCommand);
 			}
+		}
 
-			break;
+		break;
 
 		case REP.SMCMD_SELECT_ACK:
 		{
 			String hostport = receivedCommand.string;
 			Editor editor = getEditor(hostport);
-			
+
 			if(editor != null) {
 				//host, port を見て、このコマンドが自分が送信したSelectコマンドのAckかどうかを判断する
 				REPCommand command = new REPCommand();
@@ -229,17 +230,17 @@
 				command.setSID(receivedCommand.sid);
 				command.setEID(receivedCommand.eid);
 				editor.send(command);
-				
+
 			}else{
 				//自分が送信したコマンドでなければ、次のSessionManagerへ中継する
 				smList.sendExcept(channel, receivedCommand);
 			}
 		}
 
-			break;
+		break;
 
 		case REP.SMCMD_SM_JOIN:
-		
+
 		{
 			//SessionManagerのリストへ追加
 			smList.add(channel);
@@ -264,7 +265,7 @@
 
 			//SessionListからXMLを生成。
 			//joinしてきたSessionManagerに対してACKを送信。
-			SessionXMLEncoder sessionlistEncoder = new SessionXMLEncoder(sessionlist);
+			SessionXMLEncoder sessionlistEncoder = new SessionXMLEncoder(sessionList);
 			sendCommand = new REPCommand();
 			sendCommand.setCMD(REP.SMCMD_SM_JOIN_ACK);
 			sendCommand.setString(sessionlistEncoder.sessionListToXML());
@@ -277,7 +278,7 @@
 			smList.sendExcept(channel, sendCommand);
 
 		}
-			break;
+		break;
 
 		case REP.SMCMD_SM_JOIN_ACK:
 
@@ -293,10 +294,6 @@
 				smList.sendExcept(channel, sendCommand);
 			}
 
-			if(isMaster){
-			}else{
-			}
-
 			break;
 
 		case REP.SMCMD_UPDATE:
@@ -316,14 +313,14 @@
 			break;
 
 		case REP.SMCMD_UPDATE_ACK:
-			if(receivedCommand.sid > sessionlist.getList().size()){
+			if(receivedCommand.sid > sessionList.size()){
 				Editor editor = new Editor(channel);
 				editor.setName(receivedCommand.string);
 
 				Session session = new Session(editor);
 				session.addEditor(editor);
 
-				sessionlist.addSession(session);
+				sessionList.add(session);
 
 				gui.setComboSession(session.getSID(), session.getName());
 			}
@@ -345,14 +342,27 @@
 			break;
 
 		default:
+		{
 			//sid から Session を取得
 			Session session = getSession(receivedCommand.sid);
 			//マージの処理と次のエディタへコマンドを送信する処理
 			session.translate(channel, receivedCommand);
+			
+			Editor editor = getEditor(channel);
+			if(editor.isMerging()){
+				//Handlerを切り替える
+				setMergeState(channel, selector);
+			}
+		}
 		break;
 		}
 	}
 
+	private void setMergeState(REPSocketChannel<REPCommand> channel, Selector selector2) {
+		SelectionKey key = channel.keyFor(selector2);
+		key.attach(new REPHandlerInMerge(this));
+	}
+
 	private Editor getEditor(String hostport) {
 		return null;
 	}
@@ -383,9 +393,14 @@
 		myHost = localHostName + temp_port;
 		if(maxHost == null) {
 			maxHost = myHost;
-			sessionlist.setMaxHost(maxHost);
 		}
-		//ownEditorList.setHost(myHost);
+		setHostToEditor(myHost);
+	}
+
+	private void setHostToEditor(String myHost2) {
+		for(Editor editor : editorList){
+			editor.setHost(myHost2);
+		}
 	}
 
 	public static void main(String[] args) throws InterruptedException, IOException {
@@ -418,7 +433,7 @@
 		port = send_port;
 		InetSocketAddress addr = new InetSocketAddress(host, port);
 		try {
-			REPSocketChannel sessionchannel = REPSocketChannel.<REPCommand>create();
+			REPSocketChannel<REPCommand> sessionchannel = REPSocketChannel.<REPCommand>create();
 			sessionchannel.configureBlocking(true);
 			sessionchannel.connect(addr);
 			while(!sessionchannel.finishConnect()){
@@ -434,7 +449,7 @@
 		}
 	}
 	
-	private void sm_join(REPSocketChannel channel){
+	private void sm_join(REPSocketChannel<REPCommand> channel){
 		
 		//SM_JOINコマンドを生成。
 		REPCommand command = new REPCommand();
@@ -444,7 +459,7 @@
 		setMyHostName(getLocalHostName(channel));
 		
 		//XMLを生成。送信コマンドにセット。
-		SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist);
+		SessionXMLEncoder encoder = new SessionXMLEncoder(sessionList);
 		String string = encoder.sessionListToXML();
 		command.setString(string);
 		
@@ -470,7 +485,7 @@
 		
 		REPSocketChannel<REPCommand> channel = event.getEditorChannel();
 		int sid = event.getSID();
-		Session session = sessionlist.getSession(sid);
+		Session session = getSession(sid);
 		if(session.hasOwner()){
 			Editor editor = new Editor(channel);
 			session.addEditor(new Editor(channel));
@@ -481,11 +496,11 @@
 			REPPacketSend sender = new REPPacketSend(channel);
 			sender.send(sendCommand);
 		}else {
-			REPSocketChannel editorChannel = event.getEditorChannel();
+			REPSocketChannel<REPCommand> editorChannel = event.getEditorChannel();
 			sid = event.getSID();
 			Editor editor = new Editor(editorChannel);
 			editor.setHost(myHost);
-			session = sessionlist.getSession(sid);
+			session = getSession(sid);
 			session.addEditor(editor);
 			
 			Editor owner = session.getOwner();