changeset 178:a097b1d619a1

*** empty log message ***
author pin
date Thu, 28 Aug 2008 22:04:48 +0900
parents 723187e39311
children 763aad2da6b3
files rep/ConnectionEvent.java rep/Editor.java rep/RPanel.java rep/Session.java rep/SessionManager.java rep/SessionManagerEvent.java rep/handler/PacketSet.java rep/handler/REPHandler.java rep/handler/REPHandlerImpl.java rep/handler/REPHandlerInMerge.java
diffstat 10 files changed, 107 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/rep/ConnectionEvent.java	Thu Aug 28 22:04:11 2008 +0900
+++ b/rep/ConnectionEvent.java	Thu Aug 28 22:04:48 2008 +0900
@@ -1,17 +1,21 @@
 package rep;
 
-public class ConnectionEvent {
+public class ConnectionEvent implements SessionManagerEvent{
 
 	private String host;
+	private SessionManager manager;
 
-	public ConnectionEvent(String host) {
-		// TODO Auto-generated constructor stub
+	public ConnectionEvent(SessionManager manager, String host) {
+		this.manager = manager;
 		this.host = host;
 	}
 
 	public String getHost() {
-		// TODO Auto-generated method stub
 		return host;
 	}
 
+	public void exec() {
+		manager.connectSession(host);
+	}
+
 }
--- a/rep/Editor.java	Thu Aug 28 22:04:11 2008 +0900
+++ b/rep/Editor.java	Thu Aug 28 22:04:48 2008 +0900
@@ -12,7 +12,7 @@
 	private int eid;
 	private REPSocketChannel<REPCommand> myChannel;
 	private String host;
-	private String port;
+	//private String port;
 	private String file;
 	private TranslaterImp1 translater;
 	private List<REPCommand> sentList;
@@ -88,13 +88,8 @@
 	}
 
 	private void setHostAndPort(REPSocketChannel<REPCommand> myChannel2) {
-		String socketString = myChannel2.socket().getRemoteSocketAddress().toString();
-		String[] split = socketString.split("/");
-		int length = split.length;
-		String hostAndPort = split[length-1];
-		split = hostAndPort.split(":");
-		host = split[0];
-		port = split[1];
+		host = myChannel2.socket().getRemoteSocketAddress().toString();
+		
 	}
 
 	public REPSocketChannel<REPCommand> getChannel() {
@@ -104,16 +99,12 @@
 	public void setHost(String host){
 		this.host = host;
 	}
-	public void setPort(String port){
-		this.port = port;
-	}
+
 	
 	public String getHost(){
 		return host;
 	}
-	public String getPort(){
-		return port;
-	}
+
 
 	public int getEID() {
 		return eid;
@@ -123,7 +114,7 @@
 		this.eid = eid;
 	}
 	public String toString(){
-		return (host + ":" + port + ":" + file);
+		return (host  + ":" + file);
 	}
 
 	public String getName() {
@@ -147,7 +138,7 @@
 		return null;
 	}
 
-	public void setChannel(REPSocketChannel channel) {
+	public void setChannel(REPSocketChannel<REPCommand> channel) {
 		myChannel = channel;
 	}
 
--- a/rep/RPanel.java	Thu Aug 28 22:04:11 2008 +0900
+++ b/rep/RPanel.java	Thu Aug 28 22:04:48 2008 +0900
@@ -50,7 +50,7 @@
 	private String s_eid;
 	private String e_eid;
 	private String e_socketchannel;
-	private ConnectionListener listener;
+	private SessionManager listener;
 	private JComboBox comboEditor;
 	private JComboBox comboSession;
 	private JButton buttonSelect;
@@ -126,7 +126,7 @@
 	public void actionPerformed(ActionEvent event) {
 		if (event.getSource() == button) {
 			host = textField.getText();
-			listener.connectionOccured(new ConnectionEvent(host));
+			listener.connectionOccured(new ConnectionEvent(listener, host));
 		}else if(event.getSource() == buttonSelect){
 			/*
 			actionListener.ActionOccured(new REPActionEvent((EditorPlus) comboEditor.getSelectedItem(),
@@ -142,7 +142,7 @@
 
 	public void addConnectionListener(ConnectionListener listener) {
 		System.out.println(listener.toString());
-		this.listener = listener;
+		this.listener = (SessionManager) listener;
 	}
 	
 	public void addUndoListener(REPActionListener<REPCommand> listener3){
--- a/rep/Session.java	Thu Aug 28 22:04:11 2008 +0900
+++ b/rep/Session.java	Thu Aug 28 22:04:48 2008 +0900
@@ -25,7 +25,7 @@
 	public Session(Editor editor) {
 		masterEditor = editor;
 		masterHost = editor.getHost();
-		masterPort = editor.getPort();
+		//masterPort = editor.getPort();
 		this.sessionID = 0;
 		this.sessionName = editor.getName();
 	}
--- a/rep/SessionManager.java	Thu Aug 28 22:04:11 2008 +0900
+++ b/rep/SessionManager.java	Thu Aug 28 22:04:48 2008 +0900
@@ -13,10 +13,12 @@
 import java.nio.charset.CharacterCodingException;
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetEncoder;
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
 import java.util.StringTokenizer;
+import java.util.concurrent.BlockingQueue;
 
 import rep.channel.REPServerSocketChannel;
 import rep.channel.REPSocketChannel;
@@ -58,6 +60,7 @@
 	private String maxHost;
 	private boolean isSimulation;
 	private List<PacketSet> packetSetList;
+	private BlockingQueue<SessionManagerEvent> waitingQueue;
 	//private List<SessionManagerNode> managerList;
 	private static int temp_port;
 	private static int send_port;
@@ -94,12 +97,35 @@
 
 	private void mainLoop() throws IOException {
 		while(true){
+			if(checkSend()){
+				if(selector.selectNow() > 0){
+					select();
+				}
+				continue;
+			}
 			selector.select();
 			select();
 		}
 	}
 
+	private boolean checkSend() {
+		for(Iterator<PacketSet> it = packetSetList.iterator(); it.hasNext();){
+			PacketSet p = it.next();
+			if(p.getEditor().isMerging()) {
+				continue;
+			}else{
+				manage(p.channel, p.command);
+				it.remove();
+			}
+		}
+		return false;
+	}
+
 	private void select() throws IOException {
+		SessionManagerEvent e = waitingQueue.poll();
+		if(e != null) {
+			e.exec();
+		}
 		for(SelectionKey key : selector.selectedKeys()){
 			if(key.isAcceptable()){
 				/*** serverChannelはenableになったSelectionKeyのchannel ***/
@@ -110,12 +136,6 @@
 
 			}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);
-				
 				REPHandler handler = (REPHandler)key.attachment();
 				handler.handle(key);
 				
@@ -125,12 +145,11 @@
 		}
 	}
 	
-	private synchronized void registerChannel(Selector selector, SelectableChannel channel, int ops) throws IOException {
+	private void registerChannel(Selector selector, SelectableChannel channel, int ops) throws IOException {
 		if(channel == null) {
 			return;
 		}
 		channel.configureBlocking(false);
-		selector.wakeup();
 		REPHandler handler = new REPHandlerImpl(-1, this);
 		channel.register(selector, ops, handler);
 	}
@@ -139,7 +158,7 @@
 		if(receivedCommand == null) return;
 		//Session session;
 		REPCommand sendCommand = new REPCommand(receivedCommand);
-		REPPacketSend send = new REPPacketSend(channel);
+		REPSocketChannel<REPCommand> send = channel;
 
 		switch(receivedCommand.cmd){
 
@@ -176,6 +195,7 @@
 			sessionList.add(new Session(sid, editor));
 
 			//GUIに反映
+			//gui.update();ぐらいで
 			gui.setComboSession(session.getSID(), session.getName());
 			gui.setComboEditor(editor.getEID(), editor.getChannel());
 
@@ -199,7 +219,7 @@
 
 		case REP.SMCMD_SELECT:
 		{
-			//エディタをSessionに追加
+			//他のSessionManagerをエディタとしてSessionに追加
 			Editor editor = new Editor(channel);
 			Session session = getSession(receivedCommand.sid);
 			session.addEditor(editor);
@@ -253,6 +273,7 @@
 			//sessionlist.update(channel, receivedSessionList);
 
 			//myHost を設定。
+			//立ち上げ時にやるとlocalhostしか取れない
 			if(myHost == null) setMyHostName(getLocalHostName(channel));
 
 			//maxHost を設定。
@@ -269,7 +290,7 @@
 			sendCommand = new REPCommand();
 			sendCommand.setCMD(REP.SMCMD_SM_JOIN_ACK);
 			sendCommand.setString(sessionlistEncoder.sessionListToXML());
-			send.send(sendCommand);
+			send.write(sendCommand);
 
 			//その他の SessionManager に対して SMCMD_UPDATEを 送信。
 			sendCommand = new REPCommand();
@@ -303,10 +324,11 @@
 
 			//SessionListへ追加し変換テーブルを生成する。
 			//sessionlist.update(channel, receivedSessionList3);
-
+			
 			smList.sendExcept(channel, receivedCommand);
 
 			for(Session session3 : receivedSessionList3.getList()){
+				//gui.update();
 				gui.setComboSession(session3.getSID(), session3.getName());
 			}
 
@@ -348,29 +370,22 @@
 			//マージの処理と次のエディタへコマンドを送信する処理
 			session.translate(channel, receivedCommand);
 			
-			//マージ中は前のエディタからのコマンドを受信しない
+			
 			Editor editor = session.getEditor(channel);
 			Editor prevEditor = session.getPrevEditor(editor);
 			
+//			マージ中は前のエディタからのコマンドを受信しない
 			if(editor.isMerging()){
 				//Handlerを切り替える
 				setMergeState(prevEditor.getChannel(), session.getSID());
 			}else {
-				setDoWaitingState(prevEditor.getChannel(), session.getSID());
+				setNormalState(prevEditor.getChannel(), session.getSID());
 			}
-			
-
 		}
 		break;
 		}
 	}
 
-	private void setDoWaitingState(REPSocketChannel<REPCommand> channel, int sid) {
-		if(packetSetList.size() < 0){
-			setNormalState(channel, sid);
-		}
-	}
-
 	private void setNormalState(REPSocketChannel<REPCommand> channel, int sid) {
 		SelectionKey key = channel.keyFor(selector);
 		key.attach(new REPHandlerImpl(sid, this));
@@ -382,6 +397,20 @@
 	}
 
 	private Editor getEditor(String hostport) {
+		for(Editor editor : editorList){
+			if(editor.getHost() == hostport){
+				return editor;
+			}
+		}
+		return null;
+	}
+	
+	public Editor getEditor(REPSocketChannel channel){
+		for(Editor editor : editorList){
+			if(editor.getChannel() == channel){
+				return editor;
+			}
+		}
 		return null;
 	}
 
@@ -436,7 +465,7 @@
 		gui.addREPActionListener(this);
 	}
 
-	private void connectSession(String host) {
+	public void connectSession(String host) {
 		int port = DEFAULT_PORT;
 		port = send_port;
 		InetSocketAddress addr = new InetSocketAddress(host, port);
@@ -486,7 +515,11 @@
 	}
 
 	public void connectionOccured(ConnectionEvent event) {
-		connectSession(event.getHost());
+		try {
+			waitingQueue.put(event);
+		} catch (InterruptedException e) {
+		}
+		selector.wakeup();
 	}
 
 	public void ActionOccured(REPActionEvent event) {
@@ -516,7 +549,7 @@
 			REPCommand command = new REPCommand();
 			command.setCMD(REP.SMCMD_SELECT);
 			command.setSID(sid);
-			command.setString(editor.getHost() + ":" + editor.getPort());
+			command.setString(editor.getHost());
 			owner.send(command);
 		}
 		
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/SessionManagerEvent.java	Thu Aug 28 22:04:48 2008 +0900
@@ -0,0 +1,7 @@
+package rep;
+
+public interface SessionManagerEvent {
+
+	void exec();
+
+}
--- a/rep/handler/PacketSet.java	Thu Aug 28 22:04:11 2008 +0900
+++ b/rep/handler/PacketSet.java	Thu Aug 28 22:04:48 2008 +0900
@@ -1,17 +1,25 @@
 package rep.handler;
 
+import rep.Editor;
 import rep.REPCommand;
 import rep.channel.ChannelSimulator;
 import rep.channel.REPSocketChannel;
 
 public class PacketSet {
 
-	private REPSocketChannel<REPCommand> channel;
-	private REPCommand packet;
+	public REPSocketChannel<REPCommand> channel;
+	public Editor editor;
+	public REPCommand command;
 
-	public PacketSet(REPSocketChannel<REPCommand> channel, REPCommand packet) {
+	public PacketSet(REPSocketChannel<REPCommand> channel, Editor editor, REPCommand command) {
 		this.channel = channel;
-		this.packet = packet;
+		this.editor = editor;
+		this.command = command;
 	}
 
+	public Editor getEditor() {
+		return editor;
+	}
+	
+
 }
--- a/rep/handler/REPHandler.java	Thu Aug 28 22:04:11 2008 +0900
+++ b/rep/handler/REPHandler.java	Thu Aug 28 22:04:48 2008 +0900
@@ -1,8 +1,9 @@
 package rep.handler;
 
+import java.io.IOException;
 import java.nio.channels.SelectionKey;
 
 public interface REPHandler {
-	void handle(SelectionKey key);
+	void handle(SelectionKey key)throws IOException;
 
 }
--- a/rep/handler/REPHandlerImpl.java	Thu Aug 28 22:04:11 2008 +0900
+++ b/rep/handler/REPHandlerImpl.java	Thu Aug 28 22:04:48 2008 +0900
@@ -5,9 +5,7 @@
 
 import rep.REPCommand;
 import rep.SessionManager;
-import rep.channel.ChannelSimulator;
 import rep.channel.REPSocketChannel;
-import rep.channel.SelectionKeySimulator;
 
 public class REPHandlerImpl implements REPHandler {
 
@@ -18,16 +16,12 @@
 		this.manager = manager;
 	}
 
-	public void handle(SelectionKeySimulator key) throws IOException {
+	@SuppressWarnings("unchecked")
+	public void handle(SelectionKey key) throws IOException {
 		REPSocketChannel<REPCommand> channel = (REPSocketChannel<REPCommand>) key.channel();
-		REPCommand packet = channel.read();
-		REPCommand command = packet;
+		REPCommand command = channel.read();
+
 		manager.manage(channel, command);
 	}
 
-	public void handle(SelectionKey key) {
-		// TODO Auto-generated method stub
-		
-	}
-
 }
--- a/rep/handler/REPHandlerInMerge.java	Thu Aug 28 22:04:11 2008 +0900
+++ b/rep/handler/REPHandlerInMerge.java	Thu Aug 28 22:04:48 2008 +0900
@@ -2,6 +2,8 @@
 
 import java.io.IOException;
 import java.nio.channels.SelectionKey;
+
+import rep.Editor;
 import rep.REPCommand;
 import rep.SessionManager;
 import rep.channel.ChannelSimulator;
@@ -23,6 +25,7 @@
 	}
 
 	public void handle(SelectionKey key) {
+		//マージ中のエディタの前のエディタのコマンドをWaitingListに追加する
 		REPSocketChannel<REPCommand> channel = (REPSocketChannel<REPCommand>) key.channel();
 		REPCommand command = null;
 		
@@ -33,7 +36,8 @@
 		}
 		
 		if(command.sid == sid){
-			manager.addWaitingCommand(new PacketSet(channel, command));
+			Editor editor = manager.getEditor(channel);
+			manager.addWaitingCommand(new PacketSet(channel, editor, command));
 		}else{
 			manager.manage(channel, command);
 		}