changeset 164:3841cc75b808

*** empty log message ***
author pin
date Thu, 28 Aug 2008 17:58:42 +0900
parents fb516794ee28
children 9bb6bbb08ad7
files rep/Editor.java rep/SessionList.java rep/SessionManager.java rep/xml/SessionXMLEncoder.java
diffstat 4 files changed, 106 insertions(+), 118 deletions(-) [+]
line wrap: on
line diff
--- a/rep/Editor.java	Thu Aug 28 17:12:49 2008 +0900
+++ b/rep/Editor.java	Thu Aug 28 17:58:42 2008 +0900
@@ -43,6 +43,7 @@
 		if(command.eid == eid){
 			if(checkReturnedCommand(command)){
 				//エディタからのコマンドが元のエディタに戻ってきた
+				//マージして送信
 				REPCommand[] cmds = translater.catchOwnCommand(command);
 				sendMergedCommand(cmds);
 				return null;
@@ -65,7 +66,9 @@
 	private void sendMergedCommand(REPCommand[] cmds) {
 		for(int i = 0; i < cmds.length; i++){
 			try {
-				myChannel.write(pack(cmds[i]));
+				REPCommand mergeCommand = cmds[i];
+				mergeCommand.setEID(REP.MERGE_EID);
+				myChannel.write(pack(mergeCommand));
 			} catch (IOException e) {
 				e.printStackTrace();
 			}
@@ -148,4 +151,8 @@
 		myChannel = channel;
 	}
 
+	public boolean isMerging() {
+		return translater.isMerging();
+	}
+
 }
--- a/rep/SessionList.java	Thu Aug 28 17:12:49 2008 +0900
+++ b/rep/SessionList.java	Thu Aug 28 17:58:42 2008 +0900
@@ -118,40 +118,6 @@
 		return "{}";
 	}
 
-	public void sendToNextEditor(REPSocketChannel<REPCommand> channel, REPCommand repCmd) {
-		Editor editor2 = null;
-		Editor editor3;
-//		LinkedList <Editor> editorList = session3.get(repCmd.sid).getEditorList();
-//		this.getSession(sid);
-		LinkedList <Editor> editorList = sessionLinkedList.get(repCmd.sid-1).getEditorList();	//ここもforループで検索しないといけないよ。
-		for(Editor editor : editorList){
-			REPSocketChannel<REPCommand> channel2 = editor.getChannel();
-			if(channel.equals(channel2)){
-				editor2 = editor;
-			}
-		}
-		int size = editorList.size();
-		Editor lastEditor = (Editor) editorList.getLast();
-		if(size == 0){
-			editor3 = null;
-		}else if(editor2 == lastEditor){
-			editor3 = editorList.get(0);
-		}else{
-			editor3 = editorList.get(editorList.indexOf(editor2)+1);
-		}
-		if(editor3 != null){
-			//temp.add(repCmd.cmd);
-			REPCommand getUndoCommand = new REPCommand();
-			getUndoCommand.setCMD(REP.SMCMD_GET_UNDO);
-			getUndoCommand.lineno = repCmd.lineno;
-			editor3.send(getUndoCommand);
-			editor3.setKindOfUndoCmd(reverseCmd(repCmd.cmd));
-			editor3.send(repCmd);
-			//REPPacketSend send = new REPPacketSend(editor3.getChannel());
-			//send.send(repCmd);
-		}
-	}
-
 	private int reverseCmd(int cmd) {
 		// TODO Auto-generated method stub
 		int kindOfCmd = 0;
--- 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();
--- a/rep/xml/SessionXMLEncoder.java	Thu Aug 28 17:12:49 2008 +0900
+++ b/rep/xml/SessionXMLEncoder.java	Thu Aug 28 17:58:42 2008 +0900
@@ -26,8 +26,8 @@
 		this.list = sessionlist.getList();
 	}
 
-	public SessionXMLEncoder(LinkedList<Session> session) {
-		this.list = session;
+	public SessionXMLEncoder(LinkedList<Session> sessionList) {
+		this.list = sessionList;
 	}
 
 	public SessionXMLEncoder() {