changeset 322:5893fd8c0f50

*** empty log message ***
author kono
date Fri, 10 Oct 2008 16:18:03 +0900
parents d46f1e255919
children 1e605880d49e
files Todo rep/Session.java rep/SessionManager.java rep/gui/RPanel.java rep/xml/SessionXMLDecoder.java rep/xml/SessionXMLEncoder.java test/XMLTest.java
diffstat 7 files changed, 51 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/Todo	Fri Oct 10 14:20:23 2008 +0900
+++ b/Todo	Fri Oct 10 16:18:03 2008 +0900
@@ -1,3 +1,6 @@
+Fri Oct 10 15:24:42 JST 2008
+host name とsidで unique  にしているらしい。なので、SessionにはsessionNameが必須。(kono)
+
 Mon Oct  6 16:39:57 JST 2008
 
 Todo: translator にある5つのqueueが、Editor にもある。merge のアルゴリズムの
--- a/rep/Session.java	Fri Oct 10 14:20:23 2008 +0900
+++ b/rep/Session.java	Fri Oct 10 16:18:03 2008 +0900
@@ -18,9 +18,6 @@
 		sessionName = name;
 	}
 	
-	public Session(Editor editor) {
-		this(0,editor.getName(),editor);
-	}
 
 	public Session(int sid, Forwarder editor) {
 		sessionID = sid;
@@ -71,7 +68,7 @@
 		while(flag) {
 			eid=eidSeed++;
 			for(EditorPlus e:editorList) {
-				if(!(flag = (eid!=e.eid))) break;
+				if((flag = (eid==e.eid))) break;
 			}
 		}
 		return eid;
--- a/rep/SessionManager.java	Fri Oct 10 14:20:23 2008 +0900
+++ b/rep/SessionManager.java	Fri Oct 10 16:18:03 2008 +0900
@@ -238,12 +238,14 @@
 		case SMCMD_PUT:
 		{
 			//Sessionを生成
-			// sessionIDってglobaly uniqueじゃないの?
+			// sessionIDってglobaly uniqueだから、本来は、
+			// 自分の親に作ってもらう必要がある。自分が親なら自分で作って良い。
+			
 			int sid = sessionList.size();
-			Editor editor2 = new Editor(this,0, channel);
-			editorList.add(editor2);
-			editor2.setHost(myHost);
-			Session session = new Session(sid, receivedCommand.string, editor2);
+			Editor editor = new Editor(this,0, channel);
+			editorList.add(editor);
+			editor.setHost(myHost);
+			Session session = new Session(sid, receivedCommand.string, editor);
 			session.hasOwner(true);
 			sessionList.add(session);
 	
@@ -252,9 +254,9 @@
 			//エディタにAckを送信
 			REPCommand sendCommand = new REPCommand(receivedCommand);
 			sendCommand.setCMD(REP.SMCMD_PUT_ACK);
-			sendCommand.setEID(editor2.getEID());
+			sendCommand.setEID(editor.getEID());
 			sendCommand.setSID(session.getSID());
-			editor2.send(sendCommand);
+			editor.send(sendCommand);
 	
 			//他のSessionManagerへSessionの追加を報告
 			//親に送って、親から子へ
@@ -395,9 +397,10 @@
 
 		case SMCMD_UPDATE_ACK:
 		{
-			assert false;
 			if(!hasSession(receivedCommand.sid)) {
 				// accept new Session
+				// ここで初めてsession id が決まる。
+				// このコマンドは、master session manager が出すはず
 				Forwarder sm = new Forwarder(this);
 				sm.setChannel(channel);
 				Session session = new Session(receivedCommand.sid,receivedCommand.string,null);
@@ -560,6 +563,7 @@
 		System.out.println("SessionManager.session.hasOnwer="+session.hasOwner());
 		if(session.hasOwner()){
 			editor.setEID(session.newEid());
+			editor.setSID(sid);
 			session.addForwarder(editor);
 			REPCommand sendCommand = new REPCommand();
 			sendCommand.setCMD(REP.SMCMD_JOIN_ACK);
@@ -568,9 +572,8 @@
 			sendCommand.string = "";
 			channel.write(sendCommand);
 		}else {
-			sid = event.getSID();
 			editor.setHost(myHost);
-			session = getSession(sid);
+			editor.setSID(sid);
 			session.addEditor(editor);
 			Forwarder owner = session.getOwner();
 			
--- a/rep/gui/RPanel.java	Fri Oct 10 14:20:23 2008 +0900
+++ b/rep/gui/RPanel.java	Fri Oct 10 16:18:03 2008 +0900
@@ -160,6 +160,10 @@
 	}
 
 	public void setTableEditor(LinkedList<Editor> list) {
+		/*
+		 * Editor.eid is unique in a session, we cannot use.
+		 * number it simply based on the order in the list 
+		 */
 		e_tableModel.setRowCount(0);
 		editorList = list;
 		int i=0;
--- a/rep/xml/SessionXMLDecoder.java	Fri Oct 10 14:20:23 2008 +0900
+++ b/rep/xml/SessionXMLDecoder.java	Fri Oct 10 16:18:03 2008 +0900
@@ -25,7 +25,7 @@
 
 	public SessionList decode(String string) {
 		SessionList sessionlist = null;
-		System.out.println("");
+		//System.out.println("");
 		try {
 			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 			DocumentBuilder builder = factory.newDocumentBuilder();
@@ -58,9 +58,9 @@
 	private SessionList generateSessionList(Element element){
 		SessionList sessionlist = new SessionList();
 		NodeList nodelistSession = element.getElementsByTagName("Session");
-		
 		for(int i = 0; i < nodelistSession.getLength(); i++){
 			Element elementSession = (Element) nodelistSession.item(i);
+			int sid = Integer.parseInt(elementSession.getAttribute("sid"));
 			NodeList nodelistEditor = elementSession.getElementsByTagName("Editor");
 			
 			Session session = null;
@@ -79,7 +79,7 @@
 					
 					Editor editor = new Editor(null, false, 0);
 					editor.setHost(host);/* editor.setPort(port)*/; editor.setName(file); editor.setEID(Integer.parseInt(eid)); 
-					session = new Session(editor);
+					session = new Session(sid, editor);
 					session.addEditor(editor);
 					sessionlist.addSession(session);
 					
--- a/rep/xml/SessionXMLEncoder.java	Fri Oct 10 14:20:23 2008 +0900
+++ b/rep/xml/SessionXMLEncoder.java	Fri Oct 10 16:18:03 2008 +0900
@@ -39,25 +39,25 @@
 
 	public String sessionListToXML() {
 		/* SessionListをXMLに書き出す。ときの形式
-		 * 	<Session>
-		 * 		<editor>
-		 * 			<sid> 1 </sid>
-		 * 			<host> firefly </host>
-		 * 			<port> 5555 </port>
-		 * 			<filename> text.java </filename>
-		 * 		</editor>
-		 * 		<editor>
-		 * 			<sid> 2 </sid>
-		 * 			<host> asbel </host>
-		 * 			<port> 6666 </port>
-		 * 		</editor>
-		 * 	</Session>
-		 * 	<SessionManager>
-		 * 		<host> karateka </host>
-		 * 		<port> 7777 </port>
-		 * 	</SessionManager>
-		 * <Session>
-		 * </Session>
+		 * <?xml version="1.0" encoding="UTF-8"?>
+		 * <SessionList><host>naha.ie.u-ryukyu.ac.jp</host>
+		 * <Session sid="1">
+		 *  <Editor eid="1">
+		 *   <host>firefly.cr.ie.u-ryukyu.ac.jp</host>
+		 *   <port/>
+		 *   <file>Test.java</file>
+		 *  </Editor>
+		 *  <Editor eid="1">
+		 *   <host>firefly.cr.ie.u-ryukyu.ac.jp</host>
+		 *   <port/>
+		 *   <file>Test.java</file>
+		 *   </Editor>
+		 *   <Editor eid="2">
+		 *    <host>teto.cr.ie.u-ryukyu.ac.jp</host>
+		 *    <port/>
+		 *    </Editor>
+		 *   </Session>
+		 * </SessionList>
 		 */
 				StringWriter str = null;
 				try {
@@ -75,6 +75,7 @@
 					for(Session session : list){
 						Element elementSession = doc.createElement("Session");
 						root.appendChild(elementSession);
+						elementSession.setAttribute("sid", Integer.toString(session.getSID()));
 
 						LinkedList <EditorPlus> editorlist = session.getEditorList();
 						for(EditorPlus editor : editorlist){
@@ -94,7 +95,7 @@
 								Element elementName = doc.createElement("file");
 								elementEditor.appendChild(elementName);
 								elementName.setTextContent(editor.getName());
-								System.out.println(editor.getName());
+								//System.out.println(editor.getName());
 							}
 						}
 					}
@@ -106,9 +107,9 @@
 					transformer.transform(new DOMSource(doc), result); 
 
 				}catch (Exception e){
-					e.printStackTrace();
+					
 				}
-				System.out.println(str.toString());
+				//System.out.println(str.toString());
 				return str.toString();
 			}
 }
--- a/test/XMLTest.java	Fri Oct 10 14:20:23 2008 +0900
+++ b/test/XMLTest.java	Fri Oct 10 16:18:03 2008 +0900
@@ -21,7 +21,7 @@
 		editor2.setHost("teto.cr.ie.u-ryukyu.ac.jp");
 		//editor2.setPort("45678");
 		
-		Session session = new Session(editor);
+		Session session = new Session(1,editor);
 		session.addEditor(editor);
 		session.addEditor(editor2);
 		
@@ -36,7 +36,9 @@
 		
 		/*** XML から SessionList へ ***/
 		SessionXMLDecoder decoder = new SessionXMLDecoder();
-		SessionList sessionlist2 = decoder.decode(encoder.sessionListToXML());
+		String t1 = encoder.sessionListToXML();
+		System.out.println(t1);
+		SessionList sessionlist2 = decoder.decode(t1);
 		
 		/*** SessionList から XML へ ***/
 		//SessionXMLEncoder encoder2 = new SessionXMLEncoder(sessionlist2.getList());