Mercurial > hg > RemoteEditor > REPSessionManager
changeset 38:de8638eb0edd
*** empty log message ***
author | pin |
---|---|
date | Sat, 10 Nov 2007 18:51:09 +0900 |
parents | 30e46b8e4a87 |
children | c873ee0318f8 |
files | rep/Editor.java rep/Session.java rep/SessionList.java rep/SessionManager.java rep/SessionManagerList.java |
diffstat | 5 files changed, 131 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/rep/Editor.java Sat Nov 10 18:50:44 2007 +0900 +++ b/rep/Editor.java Sat Nov 10 18:51:09 2007 +0900 @@ -44,4 +44,8 @@ return ("eid:" + eid + ":" + channel.socket().getLocalSocketAddress().toString()); } + public String getName() { + return null; + } + }
--- a/rep/Session.java Sat Nov 10 18:50:44 2007 +0900 +++ b/rep/Session.java Sat Nov 10 18:51:09 2007 +0900 @@ -5,7 +5,7 @@ import java.util.List; public class Session { - Editor masterEditor; + private Editor masterEditor; private int sessionID; private String sessionName; private LinkedList<Editor> editorList = new LinkedList<Editor>(); @@ -24,4 +24,13 @@ public String toString(){ return sessionName; } + public String getSID() { + return String.valueOf(sessionID); + } + public Editor getMaster() { + return masterEditor; + } + public String getName() { + return sessionName; + } }
--- a/rep/SessionList.java Sat Nov 10 18:50:44 2007 +0900 +++ b/rep/SessionList.java Sat Nov 10 18:51:09 2007 +0900 @@ -1,15 +1,40 @@ package rep; +import java.beans.XMLEncoder; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.StringWriter; import java.nio.channels.SocketChannel; import java.util.Hashtable; import java.util.LinkedList; import java.util.List; +import javax.swing.JButton; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Result; +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 org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + public class SessionList { //List<LinkedList<SocketChannel>> sessions = new LinkedList<LinkedList<SocketChannel>>(); //Hashtable<Integer, LinkedList<SocketChannel>> sessions2 = new Hashtable<Integer, LinkedList<SocketChannel>>(); Hashtable<Integer, Session> session3 = new Hashtable<Integer, Session>(); + LinkedList<Session> session4 = new LinkedList<Session>(); + LinkedList<String> stringlist = new LinkedList<String>(); private int sessionID = 0; private int editorCount = 0; @@ -27,6 +52,7 @@ public void makeDefaultSession(){ session3.put(0, new Session(0, null, null)); + session4.add(new Session(0, "defaul", smchannel)); } public int getEditorNumber() { @@ -45,6 +71,7 @@ //return sessions2.size(); session3.put(sessionID, new Session(sessionID, string, channel)); //addEditor(channel, sid, command); + session4.add(new Session(sessionID, string, channel)); return sessionID; } @@ -150,7 +177,7 @@ } public void sendSelect(int sid) { - SocketChannel channel = session3.get(sid).masterEditor.getChannel(); + SocketChannel channel = session3.get(sid).getMaster().getChannel(); REPPacketSend send = new REPPacketSend(channel); send.send(new REPCommand(REP.SMCMD_SELECT, sid, 0, 0, 0, 0, "")); } @@ -159,4 +186,70 @@ 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); +// +// 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(); + } + }
--- a/rep/SessionManager.java Sat Nov 10 18:50:44 2007 +0900 +++ b/rep/SessionManager.java Sat Nov 10 18:51:09 2007 +0900 @@ -55,7 +55,7 @@ System.out.println("sessionManagerNet()"); ServerSocketChannel ssc = ServerSocketChannel.open(); - ssc.configureBlocking(false); //should be true + ssc.configureBlocking(false); ssc.socket().bind(new InetSocketAddress(port)); ssc.register(selector, SelectionKey.OP_ACCEPT); @@ -122,6 +122,7 @@ private void manager(SocketChannel channel, REPCommand repCmd) { if(repCmd == null) return; switch(repCmd.cmd){ + case REP.SMCMD_JOIN: if(isMaster){ int eid = editorList.addEditor(channel, repCmd); @@ -133,13 +134,8 @@ smList.sendJoin(repCmd); //sessionmanagerGUI.setComboEditor(repCmd.eid, channel); } + break; - /*** 各エディタのテスト用 ***/ - repCmd.setCMD(REP.SMCMD_JOIN_ACK); - REPPacketSend sendJoinAckTest = new REPPacketSend(channel); - sendJoinAckTest.send(repCmd); - - break; case REP.SMCMD_JOIN_ACK: // editorList.setEID(repCmd); // editorList.sendJoinAck(repCmd); @@ -153,13 +149,8 @@ int sid = sessionlist.addSession(channel, repCmd.string); sessionlist.addEditor(channel, sid, 0); } + break; - /*** 各エディタのテスト用 ***/ - repCmd.setCMD(REP.SMCMD_PUT_ACK); - REPPacketSend sendPutAckTest = new REPPacketSend(channel); - sendPutAckTest.send(repCmd); - - break; // case REP.SMCMD_PUT_ACK: // break; @@ -169,40 +160,45 @@ REPPacketSend repSend3 = new REPPacketSend(channel); repSend3.send(repCmd); //ACKを返す break; + case REP.SMCMD_SELECT_ACK: break; + case REP.SMCMD_SESSION: break; + case REP.SMCMD_SM_JOIN: - //this.isMaster = true; - //myHost = repCmd.host; if(isMaster){ - //REPPacketSend send = new REPPacketSend(channel); + smList.add(channel); repCmd.setCMD(REP.SMCMD_SM_JOIN_ACK); smList.sendSessionList(sessionlist, repCmd); } - sendSessionManagerJoinAck(channel, repCmd); break; + case REP.SMCMD_SM_JOIN_ACK: - this.isMaster = false; - myHost = repCmd.host; - //smList.add(channel); //SessionManagerのリストへ追加 + if(isMaster){ + smList.send(channel, repCmd); + } + break; + case REP.SMCMD_UPDATE: int sessionID2 = sessionlist.addSession(channel, repCmd.string); //Sessionを作成 sessionlist.addEditor(channel, sessionID2, repCmd); sessionmanagerGUI.setComboSession(sessionID2, repCmd.string); //ComboBoxにSessionを追加 break; + case REP.SMCMD_UPDATE_ACK: break; + case REP.REPCMD_READ: //sessionlist.sendCmd(channel, repCmd); break; + default: //sessionlist.sendCmd(channel, repCmd); sessionlist.sendToNextEditor(channel, repCmd); break; - } }
--- a/rep/SessionManagerList.java Sat Nov 10 18:50:44 2007 +0900 +++ b/rep/SessionManagerList.java Sat Nov 10 18:51:09 2007 +0900 @@ -48,7 +48,8 @@ } public void sendSessionList(SessionList sessionlist, REPCommand command) { - command.setCMD(REP.SMCMD_SM_JOIN_ACK); + String xml = sessionlist.getXML(); + command.setString(xml); for(SocketChannel channel : list){ REPPacketSend send = new REPPacketSend(channel); @@ -56,4 +57,8 @@ } } + public void send(SocketChannel channel, REPCommand repCmd) { + + } + }