changeset 23:01657c033761

*** empty log message ***
author pin
date Thu, 08 Nov 2007 17:19:45 +0900
parents 850a9cc4963a
children 7012a944e58f
files rep/Editor.java rep/EditorList.java rep/SessionManager.java
diffstat 3 files changed, 59 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/rep/Editor.java	Thu Nov 08 15:51:43 2007 +0900
+++ b/rep/Editor.java	Thu Nov 08 17:19:45 2007 +0900
@@ -3,14 +3,15 @@
 import java.nio.channels.SocketChannel;
 
 public class Editor {
-	private int editorNo;
+	private int eid;
 	private SocketChannel channel;
 	private SocketChannel nextChannel;
 	private String host;
 	private String port;
+	//public int getEID;
 
 	public Editor(int editorNo, SocketChannel channel){
-		this.editorNo = editorNo;
+		this.eid = editorNo;
 		this.channel = channel;
 	}
 
@@ -32,4 +33,12 @@
 		return port;
 	}
 
+	public int getEID() {
+		return eid;
+	}
+
+	public void setEID(int eid) {
+		this.eid = eid;
+	}
+
 }
--- a/rep/EditorList.java	Thu Nov 08 15:51:43 2007 +0900
+++ b/rep/EditorList.java	Thu Nov 08 17:19:45 2007 +0900
@@ -1,15 +1,50 @@
 package rep;
 
+import java.nio.channels.SocketChannel;
+import java.util.LinkedList;
+
 public class EditorList {
 
+	private int numberOfEditor;
+	private LinkedList<Editor> editorList = new LinkedList<Editor>();
+
 	public void sendJoinAck(REPCommand repCmd) {
-		// TODO Auto-generated method stub
-		
+		Editor editor = null;
+		for(Editor editor2 : editorList){
+			if(editor2.getEID() == repCmd.eid){
+				editor = editor2;
+			}
+		}
+		if(editor == null){System.out.println("error");}
+		REPPacketSend send = new REPPacketSend(editor.getChannel());
+		send.send(repCmd);
 	}
 
-	public void addEditor(REPCommand repCmd) {
-		// TODO Auto-generated method stub
+	public void sendJoinAck(SocketChannel channel, REPCommand repCmd) {
+		REPCommand command = repCmd;
+		command.setCMD(REP.SMCMD_JOIN_ACK);
 		
+		REPPacketSend send = new REPPacketSend(channel);
+		send.send(command);
+	}
+
+	public int addEditor(SocketChannel channel, REPCommand repCmd) {
+		numberOfEditor++;
+		editorList.add(new Editor(numberOfEditor, channel));
+		return numberOfEditor;
+	}
+
+	public void addEditor(SocketChannel channel) {
+		editorList.add(new Editor(0, channel));
+	}
+
+	public void setEID(REPCommand repCmd) {
+		for(Editor editor : editorList){
+			if(editor.getEID() == 0){
+				editor.setEID(repCmd.eid);
+				break;
+			}
+		}
 	}
 
 }
--- a/rep/SessionManager.java	Thu Nov 08 15:51:43 2007 +0900
+++ b/rep/SessionManager.java	Thu Nov 08 17:19:45 2007 +0900
@@ -33,7 +33,7 @@
 	private SessionManagerList smList;
 	private String myHost;
 	private boolean isMaster = true;
-	private EditorList editorList;
+	private EditorList  editorList;
 	//private SocketChannel sessionchannel;
 	//private boolean co;
 	public SessionManager(int port) {
@@ -62,6 +62,7 @@
 		
 		sessionlist = new SessionList();
 		smList = new SessionManagerList();
+		editorList = new EditorList();
 		
 		while(true){
 			selector.select();
@@ -120,14 +121,17 @@
 		if(repCmd == null) return;
 		switch(repCmd.cmd){
 		case REP.SMCMD_JOIN:
-			editorList.addEditor(repCmd);
 			if(isMaster){
-				sendJoinAck(channel, repCmd);
+				int eid = editorList.addEditor(channel, repCmd);
+				repCmd.setEID(eid);
+				editorList.sendJoinAck(channel, repCmd);
 			}else{
+				editorList.addEditor(channel);
 				smList.sendJoin(repCmd);
 			}
 			break;
 		case REP.SMCMD_JOIN_ACK:
+				editorList.setEID(repCmd);
 				editorList.sendJoinAck(repCmd);
 			break;
 		case REP.SMCMD_PUT:
@@ -199,7 +203,8 @@
 	}
 
 	private void sendJoinAck(SocketChannel channel, REPCommand repCmd) {
-		int eid = sessionlist.getNumberOfEditor();	//eidを取得
+		//int eid = sessionlist.getNumberOfEditor();	//eidを取得
+		int eid = editorList.addEditor(channel, repCmd);
 		sessionmanagerGUI.setComboEditor(eid, channel);		//ComboBoxにEditorを追加
 		repCmd.setEID(eid);							//eidを決定して、
 		repCmd.setCMD(REP.SMCMD_JOIN_ACK);