changeset 148:6a5fe529b192

*** empty log message ***
author pin
date Thu, 28 Aug 2008 00:11:30 +0900
parents 4ff68518e9ca
children b3fa37915310
files rep/EditorList.java rep/SessionManager.java rep/handler/REPHandler.java rep/handler/REPHandlerImpl.java rep/handler/REPHandlerInMerge.java
diffstat 5 files changed, 92 insertions(+), 116 deletions(-) [+]
line wrap: on
line diff
--- a/rep/EditorList.java	Wed Aug 27 23:38:21 2008 +0900
+++ b/rep/EditorList.java	Thu Aug 28 00:11:30 2008 +0900
@@ -121,12 +121,4 @@
 		return editor1;
 	}
 
-
-	public void undoAllEditors() {
-		// TODO Auto-generated method stub
-		for(Editor editor : editorList){
-			editor.undo();
-		}
-	}
-
 }
--- a/rep/SessionManager.java	Wed Aug 27 23:38:21 2008 +0900
+++ b/rep/SessionManager.java	Thu Aug 28 00:11:30 2008 +0900
@@ -24,6 +24,7 @@
 import rep.channel.SelectorSimulator;
 import rep.handler.PacketSet;
 import rep.handler.REPHandler;
+import rep.handler.REPHandlerImpl;
 import rep.simulator.REPSelector;
 import rep.xml.SessionXMLDecoder;
 import rep.xml.SessionXMLEncoder;
@@ -54,6 +55,7 @@
 	private EditorList  ownEditorList;
 	private List<Editor> editorList;
 	private String maxHost;
+	private boolean isSimulation;
 	//private Set<SelectionKey> sessionList;
 	private static int temp_port;
 	private static int send_port;
@@ -70,7 +72,7 @@
 	
 	public void mainLoop(int port) throws InterruptedException, IOException {
 	
-		REPServerSocketChannel<REPCommand> ssc = new REPServerSocketChannel<REPCommand>().create();
+		REPServerSocketChannel<REPCommand> ssc = REPServerSocketChannel.<REPCommand>open();
 		
 		ssc.configureBlocking(false);	//reuse address 必須
 		
@@ -93,7 +95,7 @@
 	}
 
 	private void select() throws IOException {
-		for(SelectionKeySimulator key : selector.selectedKeys()){
+		for(SelectionKey key : selector.selectedKeys()){
 			if(key.isAcceptable()){
 				/*** serverChannelはenableになったSelectionKeyのchannel ***/
 				REPServerSocketChannel serverChannel = (REPServerSocketChannel)key.channel();
@@ -101,7 +103,6 @@
 				registerChannel (selector, channel, SelectionKey.OP_READ);
 				channel = null;
 
-				
 			}else if(key.isReadable()){
 				
 //				REPSocketChannel<REPCommand> channel = (REPSocketChannel<REPCommand>)key.channel();
@@ -125,26 +126,31 @@
 		}
 		channel.configureBlocking(false);
 		selector.wakeup();
-		channel.register(selector, ops);
+		REPHandler handler = new REPHandlerImpl(this);
+		//channel.register(selector, ops);
+		channel.register(selector, ops, handler);
 	}
 
 	public void manage(REPSocketChannel<REPCommand> channel, REPCommand receivedCommand) {
 		if(receivedCommand == null) return;
-		//Editor editor;
 		Session session;
 		REPCommand sendCommand = new REPCommand(receivedCommand);
 		REPPacketSend send = new REPPacketSend(channel);
-		//SessionXMLEncoder encoder = new SessionXMLEncoder();
 
 		switch(receivedCommand.cmd){
 
 		case REP.SMCMD_JOIN:
-//			editor = new Editor(channel);
-//			editor.setHost(myHost);
-//			int tempeid = ownEditorList.addEditor(editor);
-//			gui.setComboEditor(tempeid, channel);
 			
-			editorList.add(new Editor(editorList.size(), channel));
+			if(isSimulation){
+				editorList.add(new Editor(editorList.size(), channel));
+				
+			}else{
+				Editor editor = new Editor(channel);
+				editor.setHost(myHost);
+				int tempeid = ownEditorList.addEditor(editor);
+				gui.setComboEditor(tempeid, channel);
+			}
+			
 
 			break;
 
@@ -153,59 +159,66 @@
 			break;
 
 		case REP.SMCMD_PUT:
-//			editor = new Editor(channel);
-//			editor.setHost(myHost);
-//			ownEditorList.addEditor(editor);
-//			editor.setEID(1);
-//			editor.setName(receivedCommand.string);
-//			session = new Session(editor);
-//			session.setOwner(true);
-//			session.addEditor(editor);
-//			sessionlist.addSession(session);
-//			gui.setComboSession(session.getSID(), session.getName());
-//			gui.setComboEditor(editor.getEID(), editor.getChannel());
-//			session.addToRoutingTable(editor);
-//			sendCommand.setCMD(REP.SMCMD_PUT_ACK);
-//			sendCommand.setEID(1);
-//			sendCommand.setSID(session.getSID());
-//			editor.send(sendCommand);
+
+			if(isSimulation){
+				//エディタのリストに追加
+				editorList.add(new Editor(editorList.size(), channel));
+				//Sessionを生成
+				int sid = sessionList.size();
+				sessionList.add(new Session(sid, new Editor(0, channel)));
 
-//			SessionXMLEncoder sessionEncoder = new SessionXMLEncoder(session);
-//			REPCommand command = new REPCommand();
-//			command.setSID(session.getSID());
-//			command.setString(sessionEncoder.sessionListToXML());
+			}else{
+				Editor editor = new Editor(channel);
+				editor.setHost(myHost);
+				ownEditorList.addEditor(editor);
+				editor.setEID(1);
+				editor.setName(receivedCommand.string);
+				session = new Session(editor);
+				session.setOwner(true);
+				session.addEditor(editor);
+				sessionlist.addSession(session);
+				gui.setComboSession(session.getSID(), session.getName());
+				gui.setComboEditor(editor.getEID(), editor.getChannel());
+				session.addToRoutingTable(editor);
+				sendCommand.setCMD(REP.SMCMD_PUT_ACK);
+				sendCommand.setEID(1);
+				sendCommand.setSID(session.getSID());
+				editor.send(sendCommand);
 
-//			command.setCMD(REP.SMCMD_UPDATE);
-//			smList.sendExcept(channel, command);
+				SessionXMLEncoder sessionEncoder = new SessionXMLEncoder(session);
+				REPCommand command = new REPCommand();
+				command.setSID(session.getSID());
+				command.setString(sessionEncoder.sessionListToXML());
 
-			//エディタのリストに追加
-			editorList.add(new Editor(editorList.size(), channel));
+				command.setCMD(REP.SMCMD_UPDATE);
+				smList.sendExcept(channel, command);
+			}
 
-			//Sessionを生成
-			int sid = sessionList.size();
-			sessionList.add(new Session(sid, new Editor(0, channel)));
+
 
 			break;
 
 		case REP.SMCMD_SELECT:
-//			editor = new Editor(channel);
-//
-//			session = sessionlist.getSession(receivedCommand.sid);
-//
-//			if(session.isOwner()){
-//				int eid2 = session.addEditor(editor);
-//				editor.setEID(eid2);
-//				sendCommand.setCMD(REP.SMCMD_SELECT_ACK);
-//				sendCommand.setEID(eid2);
-//				send.send(sendCommand);
-//			}else {
-//				Editor master = session.getMaster();
-//				master.send(receivedCommand);
-//				session.addEditor(editor);
-//			}
 			
-			Editor editor = getEditor(channel);
-			sessionList.get(receivedCommand.sid).addEditor(editor);
+			if(isSimulation){
+				Editor editor = getEditor(channel);
+				sessionList.get(receivedCommand.sid).addEditor(editor);
+				
+			}else{
+				Editor editor = new Editor(channel);
+				session = sessionlist.getSession(receivedCommand.sid);
+				if(session.isOwner()){
+					session.addEditor(editor);
+					sendCommand.setCMD(REP.SMCMD_SELECT_ACK);
+					sendCommand.setEID(editor.getEID());
+					send.send(sendCommand);
+				}else {
+					Editor master = session.getMaster();
+					master.send(receivedCommand);
+					session.addEditor(editor);
+				}
+
+			}
 
 			break;
 
@@ -223,13 +236,6 @@
 				smList.sendExcept(channel, receivedCommand);
 			}
 
-			//receivedCommand.setCMD(REP.SMCMD_JOIN_ACK);
-			//receivedCommand.setEID(receivedCommand.eid);
-			//session = sessionlist.getSession(receivedCommand.sid);
-			//session.sendToEditor(receivedCommand);
-			//Editor editor3 = session3.getEditorList().get(0);
-			//REPPacketSend send = new REPPacketSend(editor3.getChannel());
-			//send.send(repCmd);
 			break;
 
 		case REP.SMCMD_SM_JOIN:
@@ -269,12 +275,6 @@
 			sendCommand.setString(receivedCommand.string);
 			smList.sendExcept(channel, sendCommand);
 
-			//その他のSessionManagerに対してSMCMD_SM_JOINを送信。
-			//sendCommand = new REPCommand();
-			//sendCommand.setCMD(REP.SMCMD_SM_JOIN);
-			//sendCommand.setString(receivedCommand.string);
-			//smList.sendExcept(channel, sendCommand);
-
 			if(isMaster){
 			}else {
 			}
@@ -315,31 +315,11 @@
 				gui.setComboSession(session3.getSID(), session3.getName());
 			}
 
-			//SessionのownerのEditor
-			//editor = new Editor(channel);
-			//editor.setName(receivedCommand.string);
-
-
-
-			//session = new Session(editor);
-			//session.addEditor(editor);
-
-			//sessionlist.addSession(session);
-
-			//gui.setComboSession(session.getSID(), session.getName());
-
-			//if(isMaster){
-			//	receivedCommand.setCMD(REP.SMCMD_UPDATE_ACK);
-			//	smList.sendToSlave(receivedCommand);
-			//}else{
-			//	receivedCommand.setCMD(REP.SMCMD_UPDATE);
-			//	smList.sendToMaster(receivedCommand);
-			//}
 			break;
 
 		case REP.SMCMD_UPDATE_ACK:
 			if(receivedCommand.sid > sessionlist.getList().size()){
-				editor = new Editor(channel);
+				Editor editor = new Editor(channel);
 				editor.setName(receivedCommand.string);
 
 				session = new Session(editor);
@@ -366,10 +346,6 @@
 			}
 			break;
 
-		case REP.SMCMD_GET_UNDO_ACK:
-			editor = ownEditorList.getEditor(channel);
-			break;
-
 		default:
 			//sid から Session を取得
 			session = getSession(receivedCommand.sid);
@@ -463,7 +439,7 @@
 		port = send_port;
 		InetSocketAddress addr = new InetSocketAddress(host, port);
 		try {
-			REPSocketChannel sessionchannel = new REPSocketChannel<REPCommand>().create();
+			REPSocketChannel sessionchannel = REPSocketChannel.<REPCommand>create();
 			sessionchannel.configureBlocking(true);
 			sessionchannel.connect(addr);
 			while(!sessionchannel.finishConnect()){
@@ -513,16 +489,15 @@
 
 	public void ActionOccured(REPActionEvent event) {
 		
-		
-		/*** 書き直し ***/
 		REPSocketChannel channel = event.getEditorChannel();
 		int sid = event.getSID();
 		Session session = sessionlist.getSession(sid);
 		if(session.isOwner()){
-			int eid = session.addEditor(new Editor(channel));
+			Editor editor = new Editor(channel);
+			session.addEditor(new Editor(channel));
 			REPCommand sendCommand = new REPCommand();
 			sendCommand.setCMD(REP.SMCMD_JOIN_ACK);
-			sendCommand.setEID(eid);
+			sendCommand.setEID(editor.getEID());
 			sendCommand.setSID(sid);
 			REPPacketSend sender = new REPPacketSend(channel);
 			sender.send(sendCommand);
@@ -546,13 +521,13 @@
 		
 	}
 
-	public void undo() {
-		ownEditorList.undoAllEditors();
-		System.out.println("Undo!");
-	}
-
 	public void addWaitingCommand(PacketSet set) {
 		// TODO Auto-generated method stub
 		
 	}
+
+	public void undo() {
+		// TODO Auto-generated method stub
+		
+	}
 }
--- a/rep/handler/REPHandler.java	Wed Aug 27 23:38:21 2008 +0900
+++ b/rep/handler/REPHandler.java	Thu Aug 28 00:11:30 2008 +0900
@@ -1,8 +1,8 @@
 package rep.handler;
 
-import rep.channel.SelectionKeySimulator;
+import java.nio.channels.SelectionKey;
 
 public interface REPHandler {
-	void handle(SelectionKeySimulator key);
+	void handle(SelectionKey key);
 
 }
--- a/rep/handler/REPHandlerImpl.java	Wed Aug 27 23:38:21 2008 +0900
+++ b/rep/handler/REPHandlerImpl.java	Thu Aug 28 00:11:30 2008 +0900
@@ -1,5 +1,7 @@
 package rep.handler;
 
+import java.nio.channels.SelectionKey;
+
 import rep.REPCommand;
 import rep.SessionManager;
 import rep.channel.ChannelSimulator;
@@ -22,4 +24,9 @@
 		manager.manage(channel, command);
 	}
 
+	public void handle(SelectionKey key) {
+		// TODO Auto-generated method stub
+		
+	}
+
 }
--- a/rep/handler/REPHandlerInMerge.java	Wed Aug 27 23:38:21 2008 +0900
+++ b/rep/handler/REPHandlerInMerge.java	Thu Aug 28 00:11:30 2008 +0900
@@ -1,8 +1,6 @@
 package rep.handler;
 
-import java.util.LinkedList;
-import java.util.List;
-
+import java.nio.channels.SelectionKey;
 import rep.REPCommand;
 import rep.SessionManager;
 import rep.channel.ChannelSimulator;
@@ -25,4 +23,8 @@
 		manager.manage(channel, command);
 	}
 
+	public void handle(SelectionKey key) {
+		
+	}
+
 }