changeset 212:e8f716498caf

*** empty log message ***
author pin
date Sat, 30 Aug 2008 15:49:42 +0900
parents 44d502851c9e
children 4d9b32666ed2
files rep/Editor.java rep/REPActionEvent.java rep/RPanel.java rep/SessionManager.java
diffstat 4 files changed, 46 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/rep/Editor.java	Sat Aug 30 15:38:56 2008 +0900
+++ b/rep/Editor.java	Sat Aug 30 15:49:42 2008 +0900
@@ -10,7 +10,6 @@
 	private int eid;
 	private REPSocketChannel<REPCommand> myChannel;
 	private String host;
-	//private String port;
 	private String file;
 	private TranslaterImp1 translater;
 	private List<REPCommand> sentList;
--- a/rep/REPActionEvent.java	Sat Aug 30 15:38:56 2008 +0900
+++ b/rep/REPActionEvent.java	Sat Aug 30 15:49:42 2008 +0900
@@ -1,19 +1,19 @@
 package rep;
 
-import java.nio.channels.SocketChannel;
-
 import rep.channel.REPSocketChannel;
 
-public class REPActionEvent<P> {
+public class REPActionEvent<P> implements SessionManagerEvent{
 
 	private REPSocketChannel<P> editorChannel;
 	private int sid;
 	private int eid;
+	private SessionManager manager;
 
-	public REPActionEvent(EditorPlus<P> plus, SessionPlus<P> plus2) {
+	public REPActionEvent(EditorPlus<P> plus, SessionPlus plus2, SessionManager manager) {
 		this.editorChannel = plus.getChannel();
 		this.eid = plus.getEID();
 		this.sid = plus2.getSID();
+		this.manager = manager;
 	}
 
 	public REPSocketChannel<P> getEditorChannel() {
@@ -28,4 +28,9 @@
 		return eid;
 	}
 
+	public void exec() {
+		// TODO Auto-generated method stub
+		manager.selectSession(this);
+	}
+
 }
--- a/rep/RPanel.java	Sat Aug 30 15:38:56 2008 +0900
+++ b/rep/RPanel.java	Sat Aug 30 15:49:42 2008 +0900
@@ -109,7 +109,7 @@
 					(SessionPlus)comboSession.getSelectedItem()));
 			*/
 			actionListener.ActionOccured(new REPActionEvent<REPCommand>((EditorPlus<REPCommand>) e_list.get(editor_table.getSelectedRow()),
-					(SessionPlus)s_list.get(session_table.getSelectedRow())));
+					(SessionPlus)s_list.get(session_table.getSelectedRow()), listener));
 		}
 	}
 
--- a/rep/SessionManager.java	Sat Aug 30 15:38:56 2008 +0900
+++ b/rep/SessionManager.java	Sat Aug 30 15:49:42 2008 +0900
@@ -50,7 +50,7 @@
 	private List<Editor> editorList;
 	private String maxHost;
 	private boolean isSimulation;
-	private List<PacketSet> packetSetList;
+	private List<PacketSet> waitingCommandInMerge;
 	private BlockingQueue<SessionManagerEvent> waitingQueue;
 	private static int temp_port;
 	private static int send_port;
@@ -69,7 +69,8 @@
 		REPServerSocketChannel<REPCommand> ssc = REPServerSocketChannel.<REPCommand>open(new REPCommandPacker());
 		ssc.configureBlocking(false);	//reuse address 必須
 		ssc.socket().setReuseAddress(true);
-		ssc.socket().bind(new InetSocketAddress("localhost", port));
+		//getAllByNameで取れた全てのアドレスに対してbindする
+		ssc.socket().bind(new InetSocketAddress(port));
 		ssc.register(selector, SelectionKey.OP_ACCEPT, new REPHandlerImpl(-1, this));
 		//registerChannel(selector, ssc, SelectionKey.OP_ACCEPT);
 
@@ -79,7 +80,7 @@
 		smList = new SessionManagerList();
 		//ownEditorList = new EditorList();
 		editorList = new LinkedList<Editor>();
-		packetSetList = new LinkedList<PacketSet>();
+		waitingCommandInMerge = new LinkedList<PacketSet>();
 		
 		waitingQueue = new LinkedBlockingQueue<SessionManagerEvent>();
 		// main loop
@@ -94,53 +95,46 @@
 				}
 				continue;
 			}
-			selector.select();
+			System.out.println("selected number : " + selector.select());
 			select();
 		}
 	}
 
 	private boolean checkSend() {
-		for(Iterator<PacketSet> it = packetSetList.iterator(); it.hasNext();){
+		for(Iterator<PacketSet> it = waitingCommandInMerge.iterator(); it.hasNext();){
 			PacketSet p = it.next();
 			if(p.getEditor().isMerging()) {
 				continue;
 			}else{
 				manage(p.channel, p.command);
 				it.remove();
+				return true;
 			}
 		}
 		return false;
 	}
 
 	private void select() throws IOException {
-		SessionManagerEvent e = waitingQueue.poll();
-		if(e != null) {
+		SessionManagerEvent e;
+		while((e = waitingQueue.poll())!=null){
 			e.exec();
 		}
 		for(REPSelectionKey<REPCommand> key : selector.selectedKeys1()){
 			if(key.isAcceptable()){
 				/*** serverChannelはenableになったSelectionKeyのchannel ***/
-//				REPServerSocketChannel serverChannel = (REPServerSocketChannel)key.channel();
 				REPSocketChannel<REPCommand> channel = key.accept(new REPCommandPacker());
-				//REPSocketChannel channel = serverChannel.accept1();	//keyからchannelを取って、accept 
 				registerChannel (selector, channel, SelectionKey.OP_READ);
 				channel = null;
 
 			}else if(key.isReadable()){
-				
-				//REPHandler handler = (REPHandler)key.attachment();
-				
-				//いんちき
-				REPHandler handler = new REPHandlerImpl(-1, this);
+				REPHandler handler = (REPHandler)(key.attachment());
 				handler.handle(key);
 				
-			}else if(key.isConnectable()){
-				System.out.println("Connectable");
 			}
 		}
 	}
 	
-	private void registerChannel(Selector selector, SelectableChannel channel, int ops) throws IOException {
+	private void registerChannel(REPSelector selector, SelectableChannel channel, int ops) throws IOException {
 		if(channel == null) {
 			return;
 		}
@@ -160,23 +154,20 @@
 		case REP.SMCMD_JOIN:
 		{
 			//どのSessionにも属さないエディタをリストに追加
+			//エディタとchannelは1対1
+			//エディタが新しくputする場合は新しくソケットを作る
 			Editor editor = new Editor(editorList.size(), channel);
 			editor.setHost(myHost);
 			editorList.add(editor);
 
-			//リストのコピーをGUIに渡す
-			LinkedList<Session> sList = new LinkedList<Session>(sessionList);
-			LinkedList<Editor> eList = new LinkedList<Editor>(editorList);
-			//GUIに反映
-			Runnable doRun = new DoGUIUpdate(sList, eList, gui);
-			SwingUtilities.invokeLater(doRun);
+			guiUpdate();
 
 		}
 
 		break;
 
 		case REP.SMCMD_JOIN_ACK:
-
+			assert (false);
 			break;
 
 		case REP.SMCMD_PUT:
@@ -193,12 +184,7 @@
 			session.hasOwner(true);
 			sessionList.add(new Session(sid, editor));
 
-			//リストのコピーをGUIに渡す
-			LinkedList<Session> sList = new LinkedList<Session>(sessionList);
-			LinkedList<Editor> eList = new LinkedList<Editor>(editorList);
-			//GUIに反映
-			Runnable doRun = new DoGUIUpdate(sList, eList, gui);
-			SwingUtilities.invokeLater(doRun);
+			guiUpdate();
 
 			//エディタにAckを送信
 			sendCommand.setCMD(REP.SMCMD_PUT_ACK);
@@ -207,6 +193,7 @@
 			editor.send(sendCommand);
 
 			//他のSessionManagerへSessionの追加を報告
+			//親に送って、親から子へ
 			SessionXMLEncoder sessionEncoder = new SessionXMLEncoder(session);
 			REPCommand command = new REPCommand();
 			command.setSID(session.getSID());
@@ -270,9 +257,6 @@
 			SessionXMLDecoder decoder = new SessionXMLDecoder();
 			SessionList receivedSessionList = decoder.decode(receivedCommand.string);
 
-			//SessionListへ追加し変換テーブルを生成する。
-			//sessionlist.update(channel, receivedSessionList);
-
 			//myHost を設定。
 			//立ち上げ時にやるとlocalhostしか取れない
 			if(myHost == null) setMyHostName(getLocalHostName(channel));
@@ -399,6 +383,15 @@
 		}
 	}
 
+	private void guiUpdate() {
+		//リストのコピーをGUIに渡す
+		LinkedList<Session> sList = new LinkedList<Session>(sessionList);
+		LinkedList<Editor> eList = new LinkedList<Editor>(editorList);
+		//GUIに反映
+		Runnable doRun = new DoGUIUpdate(sList, eList, gui);
+		SwingUtilities.invokeLater(doRun);
+	}
+
 	private void setNormalState(REPSocketChannel<REPCommand> channel, int sid) {
 		SelectionKey key = channel.keyFor(selector);
 		key.attach(new REPHandlerImpl(sid, this));
@@ -458,8 +451,6 @@
 	}
 
 	public static void main(String[] args) throws InterruptedException, IOException {
-
-		REPServerSocketChannel.isSimulation = true;
 		
 		int port = DEFAULT_PORT;
 		int port_s = DEFAULT_PORT;
@@ -540,7 +531,15 @@
 	}
 
 	public void ActionOccured(REPActionEvent event) {
-		
+		//selectSession(event);
+		try {
+			waitingQueue.put(event);
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		}
+	}
+
+	public void selectSession(REPActionEvent event) {
 		REPSocketChannel<REPCommand> channel = event.getEditorChannel();
 		int sid = event.getSID();
 		Session session = getSession(sid);
@@ -568,12 +567,10 @@
 			command.setString(editor.getHost());
 			owner.send(command);
 		}
-		
-		
 	}
 
 	public void addWaitingCommand(PacketSet set) {
-		packetSetList.add(set);
+		waitingCommandInMerge.add(set);
 	}
 
 	public List<Session> getSessionList() {