Mercurial > hg > RemoteEditor > REPSessionManager
changeset 74:391d44c94799
*** empty log message ***
author | pin |
---|---|
date | Sun, 25 Nov 2007 02:26:23 +0900 |
parents | 4d887f38284b |
children | a636b651dbce |
files | rep/Editor.java rep/SessionManager.java rep/xml/SessionXMLDecoder.java test/XMLTest.java |
diffstat | 4 files changed, 79 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/rep/Editor.java Sat Nov 17 06:38:33 2007 +0900 +++ b/rep/Editor.java Sun Nov 25 02:26:23 2007 +0900 @@ -22,6 +22,9 @@ setHostAndPort(myChannel); } + public Editor() { + } + private void setHostAndPort(SocketChannel channel) { String socketString = channel.socket().getRemoteSocketAddress().toString(); String[] split = socketString.split("/"); @@ -58,7 +61,8 @@ this.eid = eid; } public String toString(){ - return ("eid:" + eid + ":" + myChannel.socket().getLocalSocketAddress().toString()); + return (host + ":" + port + ":" + file); + //return ("eid:" + eid + ":" + myChannel.socket().getLocalSocketAddress().toString()); } public String getName() {
--- a/rep/SessionManager.java Sat Nov 17 06:38:33 2007 +0900 +++ b/rep/SessionManager.java Sun Nov 25 02:26:23 2007 +0900 @@ -209,6 +209,12 @@ break; case REP.SMCMD_SM_JOIN: + myHost = getHostName(channel); + if(myHost.compareTo(repCmd.string) > 0){ + + }else if(myHost.compareTo(repCmd.string) < 0){ + isMaster = false; + } if(isMaster){ smList.add(channel); repCmd.setCMD(REP.SMCMD_SM_JOIN_ACK); @@ -349,12 +355,14 @@ //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()); +// SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist.getList()); - REPCommand comm = new REPCommand(); - comm.setCMD(REP.SMCMD_SM_JOIN); - comm.setString(encoder.sessionListToXML()); - send.send(comm); + REPCommand command = new REPCommand(); + command.setCMD(REP.SMCMD_SM_JOIN); + myHost = getHostName(sessionchannel); + command.setString(myHost); + //command.setString(encoder.sessionListToXML()); + send.send(command); //send.send(REPCommand.SMCMD_SESSION_JOIN); isMaster = false; @@ -365,6 +373,12 @@ } } + private String getHostName(SocketChannel channel) { + String host = null; + host = channel.socket().getLocalAddress().getHostName(); + return host; + } + private String getSocketString(SocketChannel sessionchannel) { SocketAddress socket = sessionchannel.socket().getRemoteSocketAddress(); //String inetAddressString = sessionchannel.socket().getInetAddress().toString();
--- a/rep/xml/SessionXMLDecoder.java Sat Nov 17 06:38:33 2007 +0900 +++ b/rep/xml/SessionXMLDecoder.java Sun Nov 25 02:26:23 2007 +0900 @@ -5,6 +5,8 @@ import java.io.OutputStream; import java.io.StringReader; import java.io.StringWriter; +import java.nio.channels.SocketChannel; +import java.util.LinkedList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -20,20 +22,19 @@ import org.w3c.dom.NodeList; import org.xml.sax.InputSource; +import rep.Editor; +import rep.Session; import rep.SessionList; public class SessionXMLDecoder { - private String sessionListSize; - private String[] sessionInfo; - + private SocketChannel channel; public SessionXMLDecoder(String string) { decode(string); } public SessionXMLDecoder() { - // TODO Auto-generated constructor stub } public SessionList decode(String string) { @@ -46,11 +47,10 @@ source.setEncoding("UTF-8"); Document doc = builder.parse(source); Element root = doc.getDocumentElement(); - System.out.println(root.getTagName()); - seachNode(root.getChildNodes()); + //System.out.println(root.getTagName()); - System.out.println(sessionListSize); + sessionlist = createSessionList(root); } catch (Exception e) { @@ -58,7 +58,42 @@ } return sessionlist; } + + SessionList createSessionList(Element root){ + NodeList nodelistSession = root.getChildNodes(); + SessionList sessionlist = new SessionList(); + + for(int i = 0; i < nodelistSession.getLength(); i++){ + Node nodeSession = nodelistSession.item(i); + NodeList nodelistEditor = nodeSession.getChildNodes(); + Session session = null; + + for(int j = 0; j < nodelistEditor.getLength(); j++){ + Node nodeEditor = nodelistEditor.item(j); + NodeList nodelistEditorInfo = nodeEditor.getChildNodes(); + + Editor editor = new Editor(); + String host = nodelistEditorInfo.item(0).getTextContent(); + editor.setHost(host); + String port = nodelistEditorInfo.item(1).getTextContent(); + editor.setPort(port); + if(nodelistEditorInfo.getLength() == 3){ + String name = nodelistEditorInfo.item(2).getTextContent(); + editor.setName(name); + session = new Session(editor); + } + //System.out.println(editor.toString()); + session.addEditor(editor); + + } + sessionlist.addSession(session); + } + + return sessionlist; + } + + /* private void seachNode(NodeList list) { String[] host_port_name = new String[3]; for(int i = 0; i < list.getLength(); i++){ @@ -75,5 +110,6 @@ if(element != null) seachNode(element.getChildNodes()); } } + */ }
--- a/test/XMLTest.java Sat Nov 17 06:38:33 2007 +0900 +++ b/test/XMLTest.java Sun Nov 25 02:26:23 2007 +0900 @@ -11,13 +11,13 @@ public static void main(String[] args){ Editor editor = new Editor(0, null); - editor.setHost("testhost"); - editor.setPort("testport"); - editor.setName("testfilename"); + editor.setHost("firefly.cr.ie.u-ryukyu.ac.jp"); + editor.setPort("56789"); + editor.setName("Test.java"); Editor editor2 = new Editor(1, null); - editor2.setHost("testhost2"); - editor2.setPort("testport2"); + editor2.setHost("teto.cr.ie.u-ryukyu.ac.jp"); + editor2.setPort("45678"); Session session = new Session(editor); session.addEditor(editor); @@ -26,11 +26,17 @@ SessionList sessionlist = new SessionList(); sessionlist.addSession(session); + /*** SessionList から XML へ ***/ SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist.getList()); System.out.println(encoder.sessionListToXML()); + /*** XML から SessionList へ ***/ SessionXMLDecoder decoder = new SessionXMLDecoder(); - decoder.decode(encoder.sessionListToXML()); + SessionList sessionlist2 = decoder.decode(encoder.sessionListToXML()); + + /*** SessionList から XML へ ***/ + SessionXMLEncoder encoder2 = new SessionXMLEncoder(sessionlist2.getList()); + System.out.println(encoder2.sessionListToXML()); } }