changeset 77:eb78312ea831

*** empty log message ***
author pin
date Tue, 11 Dec 2007 09:01:06 +0900
parents 97ca5f5447cd
children 8a4f92f31c58
files java.policy.applet rep/SessionList.java rep/SessionManager.java rep/xml/SessionXMLDecoder.java rep/xml/SessionXMLEncoder.java test/XMLTest.java
diffstat 6 files changed, 139 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java.policy.applet	Tue Dec 11 09:01:06 2007 +0900
@@ -0,0 +1,7 @@
+/* AUTOMATICALLY GENERATED ON Tue Apr 16 17:20:59 EDT 2002*/
+/* DO NOT EDIT */
+
+grant {
+  permission java.security.AllPermission;
+};
+
--- a/rep/SessionList.java	Tue Dec 04 10:33:06 2007 +0900
+++ b/rep/SessionList.java	Tue Dec 11 09:01:06 2007 +0900
@@ -41,6 +41,7 @@
 	private SocketChannel smchannel;
 	private boolean channelequals = false;
 	private LinkedList<Session> tmpSessionList = new LinkedList<Session>();
+	private String host;
 	
 	public SessionList(){
 		makeDefaultSession();
@@ -274,4 +275,20 @@
 		return sessionLinkedList.get(sid - 1);
 	}
 
+	public void setHost(String myHost) {
+		// TODO Auto-generated method stub
+		host = myHost;
+	}
+
+	public String getHost() {
+		// TODO Auto-generated method stub
+		return host;
+	}
+
+	public void setList(LinkedList<Session> list) {
+		// TODO Auto-generated method stub
+		sessionLinkedList = list;
+	}
+
+
 }
--- a/rep/SessionManager.java	Tue Dec 04 10:33:06 2007 +0900
+++ b/rep/SessionManager.java	Tue Dec 11 09:01:06 2007 +0900
@@ -36,7 +36,7 @@
 	private SessionManagerList smList;
 	private String myHost;
 	private boolean isMaster = true;
-	private EditorList  editorList;
+	private EditorList  allEditorList;
 	//private boolean addressIsGlobal;
 	//private SocketChannel sessionchannel;
 	//private boolean co;
@@ -58,7 +58,7 @@
 		
 		sessionlist = new SessionList();
 		smList = new SessionManagerList();
-		editorList = new EditorList();
+		allEditorList = new EditorList();
 		
 		while(true){
 			selector.select();
@@ -126,12 +126,12 @@
 		
 		case REP.SMCMD_JOIN:
 			if(isMaster){
-				int eid = editorList.addEditor(channel, receivedCommand);
+				int eid = allEditorList.addEditor(channel, receivedCommand);
 				receivedCommand.setEID(eid);
-				editorList.sendJoinAck(channel, receivedCommand);
+				allEditorList.sendJoinAck(channel, receivedCommand);
 				sessionmanagerGUI.setComboEditor(eid, channel);
 			}else{
-				editorList.addEditor(channel);
+				allEditorList.addEditor(channel);
 				smList.sendJoin(receivedCommand);
 				//sessionmanagerGUI.setComboEditor(repCmd.eid, channel);
 			}
@@ -204,11 +204,13 @@
 			break;
 			
 		case REP.SMCMD_SM_JOIN:
+			SessionXMLDecoder decoder = new SessionXMLDecoder();
+			SessionList sl = decoder.decode(receivedCommand.string);
 			if(myHost == null) setMyHostName(getLocalHostName(channel));
-			if(myHost.compareTo(receivedCommand.string) < 0){
-				setMaster(false);
+			if(myHost.compareTo(sl.getHost()) < 0){
+				setMaster(false, channel);
 			}
-
+			
 			if(isMaster){
 				smList.add(channel);
 				sendCommand.setCMD(REP.SMCMD_SM_JOIN_ACK);
@@ -228,7 +230,7 @@
 			break;
 			
 		case REP.SMCMD_UPDATE:
-			SessionXMLDecoder decoder = new SessionXMLDecoder(receivedCommand.string);
+			//SessionXMLDecoder decoder = new SessionXMLDecoder(receivedCommand.string);
 			
 			editor = new Editor(channel);
 			editor.setName(receivedCommand.string);
@@ -277,16 +279,18 @@
 	
 	private void setMyHostName(String localHostName) {
 		myHost = localHostName;
-		editorList.setHost(myHost);
+		sessionlist.setHost(myHost);
+		allEditorList.setHost(myHost);
 	}
 
 //	private void sendJoin(REPCommand repCmd) {
 //		smList.sendJoin(repCmd);
 //	}
 
-	private void setMaster(boolean b) {
+	private void setMaster(boolean b, SocketChannel channel) {
 		isMaster = b;
 		System.out.println("isMaster = " + b);
+		smList.setMaster(channel);
 	}
 
 	public static void main(String[] args) throws InterruptedException, IOException {
@@ -311,39 +315,37 @@
 
 	private void connectSession(String host) {
 		int port = 8766;
-		//SocketChannel sessionchannel;
-		//int port = Integer.parseInt(args[2]);
 		InetSocketAddress addr = new InetSocketAddress(host, port);
 		try {
 			SocketChannel sessionchannel = SocketChannel.open();
 			sessionchannel.configureBlocking(true);
 			sessionchannel.connect(addr);
-			REPPacketSend send = new REPPacketSend(sessionchannel);
 			while(!sessionchannel.finishConnect()){
-				System.out.print(".");
+				System.out.print("test afro");
 			}
 			System.out.println("");
 			registerChannel(selector, sessionchannel, SelectionKey.OP_READ);
-			//REPCommand sm_join_com = REPCommand.SMCMD_SESSION_JOIN;
-			//String socketString = getSocketString(sessionchannel);
-			//sm_join_com.setString(sm_join_com.string + ":" + socketString);
-//			SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist.getList());
 			
-			REPCommand command = new REPCommand();
-			command.setCMD(REP.SMCMD_SM_JOIN);
-			myHost = getLocalHostName(sessionchannel);
-			command.setString(myHost);
-			//command.setString(encoder.sessionListToXML());
-			send.send(command);
+			sm_join(sessionchannel);
 			
-			//send.send(REPCommand.SMCMD_SESSION_JOIN);
-			isMaster = false;
-			smList.add(sessionchannel);
-			smList.setMaster(sessionchannel);
 		}catch (IOException e) {
 			e.printStackTrace();
 		}
 	}
+	
+	private void sm_join(SocketChannel channel){
+		REPCommand command = new REPCommand();
+		command.setCMD(REP.SMCMD_SM_JOIN);
+		this.setMyHostName(getLocalHostName(channel));
+		SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist);
+		String string = encoder.sessionListToXML();
+		command.setString(string);
+		
+		REPPacketSend send = new REPPacketSend(channel);
+		send.send(command);
+		
+		smList.add(channel);
+	}
 
 	private String getLocalHostName(SocketChannel channel) {
 		String host = null;
@@ -351,17 +353,17 @@
 		return host;
 	}
 
-	private String getSocketString(SocketChannel sessionchannel) {
-		SocketAddress socket = sessionchannel.socket().getRemoteSocketAddress();
-		//String inetAddressString = sessionchannel.socket().getInetAddress().toString();
-		StringTokenizer stn = new StringTokenizer(socket.toString(), "/");
-		String socketString = null;
-		while(stn.hasMoreTokens()){
-			socketString = stn.nextToken();
-			//System.out.println(socketString);
-		}
-		return socketString;
-	}
+//	private String getSocketString(SocketChannel sessionchannel) {
+//		SocketAddress socket = sessionchannel.socket().getRemoteSocketAddress();
+//		//String inetAddressString = sessionchannel.socket().getInetAddress().toString();
+//		StringTokenizer stn = new StringTokenizer(socket.toString(), "/");
+//		String socketString = null;
+//		while(stn.hasMoreTokens()){
+//			socketString = stn.nextToken();
+//			//System.out.println(socketString);
+//		}
+//		return socketString;
+//	}
 
 	public void connectionOccured(ConnectionEvent event) {
 		connectSession(event.getHost());
--- a/rep/xml/SessionXMLDecoder.java	Tue Dec 04 10:33:06 2007 +0900
+++ b/rep/xml/SessionXMLDecoder.java	Tue Dec 11 09:01:06 2007 +0900
@@ -48,18 +48,73 @@
 			Document doc = builder.parse(source);
 			Element root = doc.getDocumentElement();
 			
-			//System.out.println(root.getTagName());
+			//sessionlist = createSessionList(root);
+			sessionlist = generateSessionList(root);
+			sessionlist.setHost(getSessionManagerHost(root));
 			
-			sessionlist = createSessionList(root);
-
-
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
 		return sessionlist;
 	}
 	
-	SessionList createSessionList(Element root){
+	private String getSessionManagerHost(Element root) {
+		NodeList nodelist = root.getChildNodes();
+		String host = null;
+		for(int i = 0; i < nodelist.getLength(); i++){
+			if(nodelist.item(i).getNodeName().equals("host")){
+				host = nodelist.item(i).getTextContent();
+			}
+		}
+		return host;
+	}
+	
+	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);
+			NodeList nodelistEditor = elementSession.getElementsByTagName("Editor");
+			
+			Session session = null;
+			for(int j = 0; j < nodelistEditor.getLength(); j++){
+				String eid = ((Element)nodelistEditor.item(j)).getAttribute("eid");
+				
+				Element elementEditor = (Element) nodelistEditor.item(j);
+				NodeList nodelistEditorHost = elementEditor.getElementsByTagName("host");
+				Element elementHost = (Element) nodelistEditorHost.item(0); 
+				String host = elementHost.getFirstChild().getNodeValue();
+				
+				NodeList nodelistEditorPort = elementEditor.getElementsByTagName("port");
+				Element elementPort = (Element) nodelistEditorPort.item(0);
+				String port = elementPort.getFirstChild().getNodeValue();
+				
+				if(elementEditor.getChildNodes().getLength() > 2){
+					NodeList nodelistEditorFile = elementEditor.getElementsByTagName("file");
+					Element elementFile = (Element) nodelistEditorFile.item(0); 
+					String file = elementFile.getFirstChild().getNodeValue();
+					
+					Editor editor = new Editor();
+					editor.setHost(host); editor.setPort(port); editor.setName(file); editor.setEID(Integer.parseInt(eid)); 
+					session = new Session(editor);
+					session.addEditor(editor);
+					sessionlist.addSession(session);
+					
+				}else {
+					Editor editor = new Editor();
+					editor.setHost(host); editor.setPort(port); editor.setName(null); editor.setEID(Integer.parseInt(eid));
+					if(session != null){
+						session.addEditor(editor);
+						//sessionlist.addSession(session);
+					}
+				}
+			}
+		}
+		return sessionlist;
+	}
+
+	private SessionList createSessionList(Element root){
 		NodeList nodelistSession = root.getChildNodes();
 		SessionList sessionlist = new SessionList();
 		
--- a/rep/xml/SessionXMLEncoder.java	Tue Dec 04 10:33:06 2007 +0900
+++ b/rep/xml/SessionXMLEncoder.java	Tue Dec 11 09:01:06 2007 +0900
@@ -19,8 +19,11 @@
 
 public class SessionXMLEncoder {
 	private LinkedList<Session> list = new LinkedList<Session>();
+	private String sessionmanagerHost;
 
-	public SessionXMLEncoder(SessionList list) {
+	public SessionXMLEncoder(SessionList sessionlist) {
+		sessionmanagerHost = sessionlist.getHost();
+		this.list = sessionlist.getList();
 	}
 
 	public SessionXMLEncoder(LinkedList<Session> session) {
@@ -64,6 +67,10 @@
 					Document doc = builder.newDocument();
 					Element root = doc.getDocumentElement();
 					root = doc.createElement("SessionList");
+					
+					Element elementSeMaHost = doc.createElement("host");
+					if(sessionmanagerHost != null) elementSeMaHost.setTextContent(sessionmanagerHost);
+					root.appendChild(elementSeMaHost);
 
 					for(Session session : list){
 						Element elementSession = doc.createElement("Session");
--- a/test/XMLTest.java	Tue Dec 04 10:33:06 2007 +0900
+++ b/test/XMLTest.java	Tue Dec 11 09:01:06 2007 +0900
@@ -26,10 +26,12 @@
 		session.addEditor(editor2);
 		
 		SessionList sessionlist = new SessionList();
+		sessionlist.setHost("naha.ie.u-ryukyu.ac.jp");
 		sessionlist.addSession(session);
 		
 		/*** SessionList から XML へ ***/
-		SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist.getList());
+		//SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist.getList());
+		SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist);
 		System.out.println(encoder.sessionListToXML());
 		
 		/*** XML から SessionList へ ***/
@@ -37,7 +39,8 @@
 		SessionList sessionlist2 = decoder.decode(encoder.sessionListToXML());
 		
 		/*** SessionList から XML へ ***/
-		SessionXMLEncoder encoder2 = new SessionXMLEncoder(sessionlist2.getList());
+		//SessionXMLEncoder encoder2 = new SessionXMLEncoder(sessionlist2.getList());
+		SessionXMLEncoder encoder2 = new SessionXMLEncoder(sessionlist2);
 		System.out.println(encoder2.sessionListToXML());
 		
 		/*** TestSAX ***/