Mercurial > hg > RemoteEditor > REPSessionManager
changeset 39:c873ee0318f8
*** empty log message ***
author | pin |
---|---|
date | Sat, 10 Nov 2007 21:06:18 +0900 |
parents | de8638eb0edd |
children | 5c0cd6c01ba8 |
files | rep/Editor.java rep/Session.java rep/SessionList.java rep/SessionManager.java rep/SessionManagerList.java test/SessionXMLEncoder.java test/XMLTest.java |
diffstat | 7 files changed, 243 insertions(+), 71 deletions(-) [+] |
line wrap: on
line diff
--- a/rep/Editor.java Sat Nov 10 18:51:09 2007 +0900 +++ b/rep/Editor.java Sat Nov 10 21:06:18 2007 +0900 @@ -9,6 +9,7 @@ private String host; private String port; //public int getEID; + private String file; public Editor(int editorNo, SocketChannel channel){ this.eid = editorNo; @@ -45,7 +46,11 @@ } public String getName() { - return null; + return file; + } + + public void setName(String string) { + file = string; } }
--- a/rep/Session.java Sat Nov 10 18:51:09 2007 +0900 +++ b/rep/Session.java Sat Nov 10 21:06:18 2007 +0900 @@ -15,10 +15,17 @@ this.sessionID = sessionID; this.sessionName = string; } + public Session(Editor editor) { + masterEditor = editor; + this.sessionID = 0; + this.sessionName = editor.getName(); + } + public void addEditor(int editorID, SocketChannel channel) { editorList.add(new Editor(editorID, channel)); } - public LinkedList getEditorList() { + public LinkedList<Editor> getEditorList() { + if(editorList == null) System.out.println("null!"); return editorList; } public String toString(){ @@ -33,4 +40,7 @@ public String getName() { return sessionName; } + public void addEditor(Editor editor) { + editorList.add(editor); + } }
--- a/rep/SessionList.java Sat Nov 10 18:51:09 2007 +0900 +++ b/rep/SessionList.java Sat Nov 10 21:06:18 2007 +0900 @@ -28,6 +28,8 @@ import org.w3c.dom.NodeList; import org.xml.sax.SAXException; +import test.SessionXMLEncoder; + public class SessionList { //List<LinkedList<SocketChannel>> sessions = new LinkedList<LinkedList<SocketChannel>>(); @@ -52,7 +54,7 @@ public void makeDefaultSession(){ session3.put(0, new Session(0, null, null)); - session4.add(new Session(0, "defaul", smchannel)); + //session4.add(new Session(0, "defaul", smchannel)); } public int getEditorNumber() { @@ -186,70 +188,91 @@ tmpSessionList.add(new Session(0, string, channel)); } - public String getXML() { -/* SessionListをXMLに書き出す。ときの形式 - * <Session> - * <editor> - * <sid/> - * <host/> - * <port/> - * <filename/> - * </editor> - * <editor> - * <sid/> - * <host/> - * <port/> - * </editor> - * </Session> - * <SessionManager> - * <host/> - * <port/> - * </SessionManager> - * <Session> - * </Session> - */ - StringWriter str = null; - try { - str = new StringWriter(); - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = factory.newDocumentBuilder(); - Document doc = builder.newDocument(); - Element root = doc.getDocumentElement(); - root = doc.createElement("SessionList"); - for(Session session : session4){ - Element element = doc.createElement("Session"); - root.appendChild(element); - Element element2 = doc.createElement("editor"); - element.appendChild(element2); - Editor editor = session.getMaster(); - Element element3 = doc.createElement(editor.getHost()); - Element element4 = doc.createElement(editor.getPort()); - Element element5 = doc.createElement(editor.getName()); - element5 = doc.createElement(session.getName()); - element2.appendChild(element3); - element2.appendChild(element4); - element2.appendChild(element5); - - } - doc.appendChild(root); - - TransformerFactory tfactory = TransformerFactory.newInstance(); - Transformer transformer = tfactory.newTransformer(); - StreamResult result = new StreamResult(str); - //transformer.transform(new DOMSource(doc), new StreamResult(System.out)); - transformer.transform(new DOMSource(doc), result); - System.out.println(str); +// public String getXML() { +///* SessionListをXMLに書き出す。ときの形式 +// * <Session> +// * <editor> +// * <sid/> +// * <host/> +// * <port/> +// * <filename/> +// * </editor> +// * <editor> +// * <sid/> +// * <host/> +// * <port/> +// * </editor> +// * </Session> +// * <SessionManager> +// * <host/> +// * <port/> +// * </SessionManager> +// * <Session> +// * </Session> +// */ +// StringWriter str = null; +// try { +// str = new StringWriter(); +// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); +// DocumentBuilder builder = factory.newDocumentBuilder(); +// Document doc = builder.newDocument(); +// Element root = doc.getDocumentElement(); +// root = doc.createElement("SessionList"); +// for(Session session : session4){ +// Element element = doc.createElement("Session"); +// root.appendChild(element); +// Element element2 = doc.createElement("editor"); +// element.appendChild(element2); +// Editor editor = session.getMaster(); +// Element element3 = doc.createElement(editor.getHost()); +// Element element4 = doc.createElement(editor.getPort()); +// Element element5 = doc.createElement(editor.getName()); +// element5 = doc.createElement(session.getName()); +// element2.appendChild(element3); +// element2.appendChild(element4); +// element2.appendChild(element5); +// +// } +// doc.appendChild(root); // -// XMLEncoder e = new XMLEncoder(System.out); -// e.writeObject(session4); -// e.writeObject(stringlist); -// e.close(); - - //System.out.println(doc); - }catch (Exception e){ - e.printStackTrace(); - } - return str.toString(); +// TransformerFactory tfactory = TransformerFactory.newInstance(); +// Transformer transformer = tfactory.newTransformer(); +// StreamResult result = new StreamResult(str); +// //transformer.transform(new DOMSource(doc), new StreamResult(System.out)); +// transformer.transform(new DOMSource(doc), result); +// System.out.println(str); +//// +//// XMLEncoder e = new XMLEncoder(System.out); +//// e.writeObject(session4); +//// e.writeObject(stringlist); +//// e.close(); +// +// //System.out.println(doc); +// }catch (Exception e){ +// e.printStackTrace(); +// } +// return str.toString(); +// } + + public LinkedList<Session> getList() { + return session4; + } + + public String toXML() { + SessionXMLEncoder encoder = new SessionXMLEncoder(session4); + encoder.sessionListToXML(); + return null; + } + + public int addSession(Session session) { + sessionID++; + //sessions2.put(sessionID, new LinkedList<SocketChannel>()); + //sessions.add(new LinkedList<SocketChannel>()); + //return sessions2.size(); + session3.put(sessionID, session); + //addEditor(channel, sid, command); + session4.add(session); + return sessionID; } }
--- a/rep/SessionManager.java Sat Nov 10 18:51:09 2007 +0900 +++ b/rep/SessionManager.java Sat Nov 10 21:06:18 2007 +0900 @@ -178,6 +178,8 @@ case REP.SMCMD_SM_JOIN_ACK: if(isMaster){ smList.send(channel, repCmd); + }else{ + smList.send(channel, repCmd); } break; @@ -242,7 +244,7 @@ int port = 8766; if(args.length == 1){ - port = Integer.parseInt(args[1]); + port = Integer.parseInt(args[0]); } SessionManager sm = new SessionManager(port); sm.openSelector();
--- a/rep/SessionManagerList.java Sat Nov 10 18:51:09 2007 +0900 +++ b/rep/SessionManagerList.java Sat Nov 10 21:06:18 2007 +0900 @@ -48,7 +48,7 @@ } public void sendSessionList(SessionList sessionlist, REPCommand command) { - String xml = sessionlist.getXML(); + String xml = sessionlist.toXML(); command.setString(xml); for(SocketChannel channel : list){
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/SessionXMLEncoder.java Sat Nov 10 21:06:18 2007 +0900 @@ -0,0 +1,108 @@ +package test; + +import java.io.StringWriter; +import java.util.LinkedList; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import rep.Editor; +import rep.Session; +import rep.SessionList; + +public class SessionXMLEncoder { + private LinkedList<Session> list; + + public SessionXMLEncoder(SessionList list) { + } + + public SessionXMLEncoder(LinkedList<Session> session) { + this.list = session; + } + + public SessionXMLEncoder() { + // TODO Auto-generated constructor stub + } + + public String sessionListToXML() { + /* SessionListをXMLに書き出す。ときの形式 + * <Session> + * <editor> + * <sid/> + * <host/> + * <port/> + * <filename/> + * </editor> + * <editor> + * <sid/> + * <host/> + * <port/> + * </editor> + * </Session> + * <SessionManager> + * <host/> + * <port/> + * </SessionManager> + * <Session> + * </Session> + */ + StringWriter str = null; + try { + str = new StringWriter(); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.newDocument(); + Element root = doc.getDocumentElement(); + root = doc.createElement("SessionList"); + for(Session session : list){ + Element elementSession = doc.createElement("Session"); + root.appendChild(elementSession); +// Element elementeditor = doc.createElement("editor"); +// elementSession.appendChild(elementeditor); +// Editor editor = session.getMaster(); +// Element element3 = doc.createElement(editor.getHost()); +// Element element4 = doc.createElement(editor.getPort()); +// Element element5 = doc.createElement(editor.getName()); + //element5 = doc.createElement(session.getName()); +// elementeditor.appendChild(element3); +// elementeditor.appendChild(element4); +// elementeditor.appendChild(element5); + LinkedList <Editor> editorlist = session.getEditorList(); + for(Editor editor : editorlist){ + Element elementEditor = doc.createElement("Editor"); + Element elementHost = doc.createElement(editor.getHost()); + Element elementPort = doc.createElement(editor.getPort()); + Element elementName = null; + if(editor.getName() != null) { + elementName = doc.createElement(editor.getName()); + elementEditor.appendChild(elementName); + } + elementEditor.appendChild(elementHost); + elementEditor.appendChild(elementPort); +// elementEditor.appendChild(elementName); + elementSession.appendChild(elementEditor); + } + + } + doc.appendChild(root); + + TransformerFactory tfactory = TransformerFactory.newInstance(); + Transformer transformer = tfactory.newTransformer(); + StreamResult result = new StreamResult(str); + //transformer.transform(new DOMSource(doc), new StreamResult(System.out)); + transformer.transform(new DOMSource(doc), result); + System.out.println(str); + + }catch (Exception e){ + e.printStackTrace(); + } + return str.toString(); + } +}
--- a/test/XMLTest.java Sat Nov 10 18:51:09 2007 +0900 +++ b/test/XMLTest.java Sat Nov 10 21:06:18 2007 +0900 @@ -1,11 +1,35 @@ package test; +import rep.Editor; +import rep.Session; import rep.SessionList; public class XMLTest { + public static void main(String[] args){ - SessionList list = new SessionList(); - list.addSession(null, "test.txt"); - list.getXML(); + + Editor editor = new Editor(0, null); + editor.setHost("testhost"); + editor.setPort("testport"); + editor.setName("testfilename"); + + Editor editor2 = new Editor(1, null); + editor2.setHost("testhost2"); + editor2.setPort("testport2"); + //editor2.setName("null"); + + //Session session = new Session(0, "file", null); + //session.addEditor(editor); + + Session session = new Session(editor); + session.addEditor(editor); + session.addEditor(editor2); + + SessionList sessionlist = new SessionList(); + sessionlist.addSession(session); + + SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist.getList()); + encoder.sessionListToXML(); + //list.getXML(); } }