changeset 144:0bf7f8d0f5f7

*** empty log message ***
author pin
date Wed, 27 Aug 2008 22:55:43 +0900
parents 785a3e8ea858
children ec625f8f8d7c
files rep/REPCommand.java rep/Session.java rep/SessionManager.java rep/handler/PacketSet.java rep/handler/REPHandler.java rep/handler/REPHandlerDoWaiting.java rep/handler/REPHandlerImpl.java rep/handler/REPHandlerInMerge.java
diffstat 8 files changed, 289 insertions(+), 141 deletions(-) [+]
line wrap: on
line diff
--- a/rep/REPCommand.java	Wed Aug 27 22:48:10 2008 +0900
+++ b/rep/REPCommand.java	Wed Aug 27 22:55:43 2008 +0900
@@ -1,6 +1,6 @@
 package rep;
 
-import remoteeditor.network.REP;
+import rep.REP;
 
 public class REPCommand {
 	public static REPCommand SMCMD_SESSION_JOIN = new REPCommand(REP.SMCMD_SM_JOIN, 0, 0, 0, 0, 0, "");
--- a/rep/Session.java	Wed Aug 27 22:48:10 2008 +0900
+++ b/rep/Session.java	Wed Aug 27 22:55:43 2008 +0900
@@ -30,6 +30,11 @@
 		this.sessionName = editor.getName();
 	}
 
+	public Session(int sid, Editor editor) {
+		sessionID = sid;
+		masterEditor = editor;
+		editorList.add(editor);
+	}
 	public void addEditor(int editorID, REPSocketChannel channel) {
 		editorList.add(new Editor(editorID, channel));
 	}
@@ -49,16 +54,10 @@
 	public String getName() {
 		return sessionName;
 	}
-	public int addEditor(Editor editor) {
-		incrementEID++;
-		for(Editor geteditor: editorList){
-			if(geteditor.getChannel().equals(editor.getChannel())){
-				//editorList.add(editor);
-				return incrementEID;
-			}
-		}
+	public void addEditor(Editor editor) {
+		int eid = editorList.size();
+		editor.setEID(eid);
 		editorList.add(editor);
-		return incrementEID;
 	}
 	public void setSID(int sessionID2) {
 		sessionID = sessionID2;
@@ -78,4 +77,26 @@
 			send.send(repCmd);
 		}
 	}
+	public Editor getEditor(REPSocketChannel<REPCommand> channel) {
+		// TODO Auto-generated method stub
+		for(Editor editor : editorList){
+			if(editor.getChannel() == channel) return editor;
+		}
+		return null;
+	}
+	public void translate(REPSocketChannel<REPCommand> channel, REPCommand command) {
+		Editor editor = getEditor(channel);
+		LinkedList<REPCommand> commandList = editor.translate(command);
+		Editor nextEditor = getNextEditor(editor);
+		
+		for(REPCommand cmd: commandList){
+			nextEditor.send(cmd);
+		}
+	}
+	private Editor getNextEditor(Editor editor) {
+		int eid = editor.getEID();
+		int neid = (eid+1)%editorList.size();
+		Editor nextEditor = editorList.get(neid);
+		return nextEditor;
+	}
 }
--- a/rep/SessionManager.java	Wed Aug 27 22:48:10 2008 +0900
+++ b/rep/SessionManager.java	Wed Aug 27 22:55:43 2008 +0900
@@ -14,10 +14,13 @@
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetEncoder;
 import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
 import java.util.StringTokenizer;
 
 import rep.channel.REPServerSocketChannel;
 import rep.channel.REPSocketChannel;
+import rep.handler.PacketSet;
 import rep.simulator.REPSelector;
 import rep.xml.SessionXMLDecoder;
 import rep.xml.SessionXMLEncoder;
@@ -39,13 +42,16 @@
 	
 	
 	private SessionList sessionlist;
+	private List<Session> sessionList;
 	private SessionManagerGUI gui;
 	private Selector selector;
 	private SessionManagerList smList;
 	private String myHost;
 	private boolean isMaster = true;
 	private EditorList  ownEditorList;
+	private List<Editor> editorList;
 	private String maxHost;
+	//private Set<SelectionKey> sessionList;
 	private static int temp_port;
 	private static int send_port;
 	
@@ -60,7 +66,7 @@
 		selector = REPSelector.open();
 	}
 	
-	public void sessionManagerNet(int port) throws InterruptedException, IOException {
+	public void mainLoop(int port) throws InterruptedException, IOException {
 	
 		REPServerSocketChannel<REPCommand> ssc = new REPServerSocketChannel<REPCommand>().create();
 		
@@ -73,32 +79,38 @@
 
 		
 		sessionlist = new SessionList();
+		sessionList = new LinkedList<Session>();
 		smList = new SessionManagerList();
 		ownEditorList = new EditorList();
+		editorList = new LinkedList<Editor>();
 		
 		while(true){
 			selector.select();
-			for(SelectionKey key : selector.selectedKeys()){
-				if(key.isAcceptable()){
-					/*** serverChannelはenableになったSelectionKeyのchannel ***/
-					ServerSocketChannel serverChannel = (ServerSocketChannel)key.channel();
-					SocketChannel channel = serverChannel.accept();	//keyからchannelを取って、accept 
-					registerChannel (selector, channel, SelectionKey.OP_READ);
-					channel = null;
+			select();
+		}
+	}
+
+	private void select() throws IOException {
+		for(SelectionKey key : selector.selectedKeys()){
+			if(key.isAcceptable()){
+				/*** serverChannelはenableになったSelectionKeyのchannel ***/
+				ServerSocketChannel serverChannel = (ServerSocketChannel)key.channel();
+				SocketChannel channel = serverChannel.accept();	//keyからchannelを取って、accept 
+				registerChannel (selector, channel, SelectionKey.OP_READ);
+				channel = null;
 
-					
-				}else if(key.isReadable()){
-					
-					REPSocketChannel<REPCommand> channel = (REPSocketChannel<REPCommand>)key.channel();
-					REPPacketReceive receive = new REPPacketReceive(channel);
-					receive.setkey(key);
-					REPCommand receivedCommand = receive.unpackUConv();
-					manager(channel, receivedCommand);
-					
-					
-				}else if(key.isConnectable()){
-					System.out.println("Connectable");
-				}
+				
+			}else if(key.isReadable()){
+				
+				REPSocketChannel<REPCommand> channel = (REPSocketChannel<REPCommand>)key.channel();
+				REPPacketReceive receive = new REPPacketReceive(channel);
+				receive.setkey(key);
+				REPCommand receivedCommand = receive.unpackUConv();
+				manage(channel, receivedCommand);
+				
+				
+			}else if(key.isConnectable()){
+				System.out.println("Connectable");
 			}
 		}
 	}
@@ -112,83 +124,89 @@
 		channel.register(selector, ops);
 	}
 
-	private void manager(REPSocketChannel channel, REPCommand receivedCommand) {
+	public void manage(REPSocketChannel<REPCommand> channel, REPCommand receivedCommand) {
 		if(receivedCommand == null) return;
-		Editor editor;
+		//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);
+//			editor = new Editor(channel);
+//			editor.setHost(myHost);
+//			int tempeid = ownEditorList.addEditor(editor);
+//			gui.setComboEditor(tempeid, channel);
 			
+			editorList.add(new Editor(editorList.size(), channel));
+
 			break;
-			
+
 		case REP.SMCMD_JOIN_ACK:
-//				editorList.setEID(repCmd);
-//				editorList.sendJoinAck(repCmd);
-//				sessionmanagerGUI.setComboEditor(repCmd.eid, channel);
+
 			break;
-			
+
 		case REP.SMCMD_PUT:
-			editor = new Editor(channel);
-			editor.setHost(myHost);
-			ownEditorList.addEditor(editor);
-			editor.setEID(1);
-			//String string2 = setUTF16(receivedCommand.string);
-			editor.setName(receivedCommand.string);
-			//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(isMaster){
-			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 = 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);
+
+//			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);
+
+			//エディタのリストに追加
+			editorList.add(new Editor(editorList.size(), channel));
+
+			//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);
+//			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);
+//			}
 			
-			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);
+
 			break;
-			
+
 		case REP.SMCMD_SELECT_ACK:
-			
+
 			String hostport = receivedCommand.string;
 			Editor editor2 = ownEditorList.getEditor(hostport);
 			if(editor2 != null) {
@@ -200,7 +218,7 @@
 			}else{
 				smList.sendExcept(channel, receivedCommand);
 			}
-			
+
 			//receivedCommand.setCMD(REP.SMCMD_JOIN_ACK);
 			//receivedCommand.setEID(receivedCommand.eid);
 			//session = sessionlist.getSession(receivedCommand.sid);
@@ -209,22 +227,22 @@
 			//REPPacketSend send = new REPPacketSend(editor3.getChannel());
 			//send.send(repCmd);
 			break;
-			
+
 		case REP.SMCMD_SM_JOIN:
-			
+
 			//SessionManagerのリストへ追加
 			smList.add(channel);
-			
+
 			//XMLからSessionListオブジェクトを生成する。
 			SessionXMLDecoder decoder = new SessionXMLDecoder();
 			SessionList receivedSessionList = decoder.decode(receivedCommand.string);
-			
+
 			//SessionListへ追加し変換テーブルを生成する。
 			sessionlist.update(channel, receivedSessionList);
-			
+
 			//myHost を設定。
 			if(myHost == null) setMyHostName(getLocalHostName(channel));
-			
+
 			//maxHost を設定。
 			if(setMaxHost(channel, receivedSessionList.getMaxHost())){
 				sendCommand = new REPCommand();
@@ -232,7 +250,7 @@
 				sendCommand.setString(maxHost);
 				smList.sendExcept(channel, sendCommand);
 			}
-			
+
 			//SessionListからXMLを生成。
 			//joinしてきたSessionManagerに対してACKを送信。
 			SessionXMLEncoder sessionlistEncoder = new SessionXMLEncoder(sessionlist);
@@ -240,31 +258,31 @@
 			sendCommand.setCMD(REP.SMCMD_SM_JOIN_ACK);
 			sendCommand.setString(sessionlistEncoder.sessionListToXML());
 			send.send(sendCommand);
-			
+
 			//その他の SessionManager に対して SMCMD_UPDATEを 送信。
 			sendCommand = new REPCommand();
 			sendCommand.setCMD(REP.SMCMD_UPDATE);
 			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 {
 			}
-			
+
 			break;
-			
+
 		case REP.SMCMD_SM_JOIN_ACK:
-			
+
 			//XMLからSessionListオブジェクトを生成。
 			SessionXMLDecoder decoder2 = new SessionXMLDecoder();
 			SessionList receivedSessionList2 = decoder2.decode(receivedCommand.string);
-			
+
 			//maxHostを決定。
 			if(setMaxHost(channel, receivedSessionList2.getMaxHost())){
 				sendCommand = new REPCommand();
@@ -272,40 +290,40 @@
 				sendCommand.setString(maxHost);
 				smList.sendExcept(channel, sendCommand);
 			}
-			
+
 			if(isMaster){
 			}else{
 			}
-			
+
 			break;
-			
+
 		case REP.SMCMD_UPDATE:
-			
+
 			SessionXMLDecoder decoder3 = new SessionXMLDecoder();
 			SessionList receivedSessionList3 = decoder3.decode(receivedCommand.string);
-			
+
 			//SessionListへ追加し変換テーブルを生成する。
 			sessionlist.update(channel, receivedSessionList3);
-			
+
 			smList.sendExcept(channel, receivedCommand);
-			
+
 			for(Session session3 : receivedSessionList3.getList()){
 				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);
@@ -314,26 +332,26 @@
 			//	smList.sendToMaster(receivedCommand);
 			//}
 			break;
-			
+
 		case REP.SMCMD_UPDATE_ACK:
 			if(receivedCommand.sid > sessionlist.getList().size()){
 				editor = new Editor(channel);
 				editor.setName(receivedCommand.string);
-				
+
 				session = new Session(editor);
 				session.addEditor(editor);
-				
+
 				sessionlist.addSession(session);
-				
+
 				gui.setComboSession(session.getSID(), session.getName());
 			}
 			smList.sendToSlave(receivedCommand);
 			break;
-			
-//		case REP.REPCMD_READ:
+
+//			case REP.REPCMD_READ:
 //			//sessionlist.sendCmd(channel, repCmd);
 //			break;
-			
+
 		case REP.SMCMD_CH_MASTER:
 			//maxHost を設定。
 			if(setMaxHost(channel, receivedCommand.string)){
@@ -343,26 +361,35 @@
 				smList.sendExcept(channel, sendCommand);
 			}
 			break;
-			
+
 		case REP.SMCMD_GET_UNDO_ACK:
 			editor = ownEditorList.getEditor(channel);
-			editor.addUndoCommand(receivedCommand);
 			break;
-			
+
 		default:
-			//sessionlist.sendCmd(channel, repCmd);
-			editor = ownEditorList.getEditor(channel);
-			if(receivedCommand.seq < 0){
-				//editor = ownEditorList.getEditor(channel);
-				if(editor != null) {
-					editor.addUndoCommand(receivedCommand);
-				}
-				break;
+			//sid から Session を取得
+			session = getSession(receivedCommand.sid);
+			//マージの処理と次のエディタへコマンドを送信する処理
+			session.translate(channel, receivedCommand);
+		break;
+		}
+	}
+
+	private Editor getEditor(REPSocketChannel<REPCommand> channel) {
+		// TODO Auto-generated method stub
+		for(Editor editor : editorList){
+			if(editor.getChannel() == channel){
+				return editor;
 			}
-			//editor.setKindOfUndoCmd(reverseCmd(receivedCommand.cmd));
-			sessionlist.sendToNextEditor(channel, receivedCommand);
-			break;
 		}
+		return null;
+	}
+
+	private Session getSession(int sid) {
+		for(Session session : sessionList){
+			if(session.getSID() == sid) return session;
+		}
+		return null;
 	}
 
 	private boolean setMaxHost(REPSocketChannel channel, String maxHost2) {
@@ -416,7 +443,7 @@
 		SessionManager sm = new SessionManager(port);
 		sm.openSelector();
 		sm.openWindow();
-		sm.sessionManagerNet(port);
+		sm.mainLoop(port);
 	}
 
 	private void openWindow() {
@@ -519,4 +546,9 @@
 		ownEditorList.undoAllEditors();
 		System.out.println("Undo!");
 	}
+
+	public void addWaitingCommand(PacketSet set) {
+		// TODO Auto-generated method stub
+		
+	}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/handler/PacketSet.java	Wed Aug 27 22:55:43 2008 +0900
@@ -0,0 +1,16 @@
+package rep.handler;
+
+import rep.REPCommand;
+import rep.channel.ChannelSimulator;
+
+public class PacketSet {
+
+	private ChannelSimulator<REPCommand> channel;
+	private REPCommand packet;
+
+	public PacketSet(ChannelSimulator<REPCommand> channel, REPCommand packet) {
+		this.channel = channel;
+		this.packet = packet;
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/handler/REPHandler.java	Wed Aug 27 22:55:43 2008 +0900
@@ -0,0 +1,8 @@
+package rep.handler;
+
+import rep.channel.SelectionKeySimulator;
+
+public interface REPHandler {
+	void handle(SelectionKeySimulator key);
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/handler/REPHandlerDoWaiting.java	Wed Aug 27 22:55:43 2008 +0900
@@ -0,0 +1,18 @@
+package rep.handler;
+
+import rep.SessionManager;
+import rep.channel.SelectionKeySimulator;
+
+public class REPHandlerDoWaiting implements REPHandler {
+	
+	private SessionManager manager;
+
+	public REPHandlerDoWaiting(SessionManager manager) {
+		this.manager = manager;
+	}
+
+	public void handle(SelectionKeySimulator key) {
+		//session.doWaiting();
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/handler/REPHandlerImpl.java	Wed Aug 27 22:55:43 2008 +0900
@@ -0,0 +1,25 @@
+package rep.handler;
+
+import rep.REPCommand;
+import rep.SessionManager;
+import rep.channel.ChannelSimulator;
+import rep.channel.REPSocketChannel;
+import rep.channel.SelectionKeySimulator;
+
+public class REPHandlerImpl implements REPHandler {
+
+	private SessionManager manager;
+
+
+	public REPHandlerImpl(SessionManager manager) {
+		this.manager = manager;
+	}
+
+	public void handle(SelectionKeySimulator key) {
+		REPSocketChannel<REPCommand> channel = (REPSocketChannel<REPCommand>) key.channel();
+		REPCommand packet = channel.read();
+		REPCommand command = packet;
+		manager.manage(channel, command);
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/handler/REPHandlerInMerge.java	Wed Aug 27 22:55:43 2008 +0900
@@ -0,0 +1,28 @@
+package rep.handler;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import rep.REPCommand;
+import rep.SessionManager;
+import rep.channel.ChannelSimulator;
+import rep.channel.SelectionKeySimulator;
+
+public class REPHandlerInMerge implements REPHandler {
+
+	private SessionManager manager;
+	//List<PacketSet> packetList = new LinkedList<PacketSet>();
+
+	public REPHandlerInMerge(SessionManager manager) {
+		this.manager = manager;
+	}
+
+	public void handle(SelectionKeySimulator key) {
+		ChannelSimulator<REPCommand> channel = (ChannelSimulator<REPCommand>) key.channel();
+		REPCommand packet = channel.read();
+		manager.addWaitingCommand(new PacketSet(channel, packet));
+		REPCommand command = packet;
+		manager.manage(channel, command);
+	}
+
+}