Mercurial > hg > RemoteEditor > REPSessionManager
view rep/SessionList.java @ 83:9381b4734a0b
*** empty log message ***
author | pin |
---|---|
date | Fri, 21 Dec 2007 13:55:24 +0900 |
parents | 4bb583553a42 |
children | 612cb62151ce |
line wrap: on
line source
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; import rep.xml.SessionXMLEncoder; public class SessionList { // Hashtable<Integer, Session> session3 = new Hashtable<Integer, Session>(); LinkedList<Session> sessionLinkedList = new LinkedList<Session>(); LinkedList<String> stringlist = new LinkedList<String>(); private int sessionID = 0; private int editorCount = 0; private SocketChannel smchannel; private boolean channelequals = false; private LinkedList<Session> tmpSessionList = new LinkedList<Session>(); private String maxHost; public SessionList(){ makeDefaultSession(); } public void add(SocketChannel channel) { } public void makeDefaultSession(){ // session3.put(0, new Session(0, null, null)); // session4.add(0, new Session(0, null, null)); } public int getEditorNumber() { return 0; } public void add(SocketChannel channel, int sid) { } public int addSession(SocketChannel channel, String string) { sessionID++; // session3.put(sessionID, new Session(sessionID, string, channel)); sessionLinkedList.add(new Session(sessionID, string, channel)); return sessionID; } public void addEditor(SocketChannel channel, int sid, REPCommand repCmd) { int editorID = repCmd.eid; // if(session3.get(sid) == null) System.out.println("ぬるぽ!"); // session3.get(sid).addEditor(editorID, channel); sessionLinkedList.get(sid-1).addEditor(editorID, channel); //本当はforループで検索しないといけないよ。 } public int getSessionID(SocketChannel channel) { return 0; } public int getNumberOfEditor() { editorCount++; return editorCount; } public void sendCmd(SocketChannel channel2, REPCommand repCmd) { // LinkedList <Editor> editorList = session3.get(repCmd.sid).getEditorList(); LinkedList <Editor> editorList = sessionLinkedList.get(repCmd.sid).getEditorList(); //ここもforループで検索しないといけないよ。 for(Editor editor : editorList){ SocketChannel channel = editor.getChannel(); if(channel.equals(channel2)) { System.out.println("equals"); continue; } REPPacketSend repSend = new REPPacketSend(channel); repSend.send(repCmd); } } public void sendCmd(SocketChannel channel, REPCommand repCmd, boolean ring){ // LinkedList <Editor> editorList = session3.get(repCmd.sid).getEditorList(); LinkedList <Editor> editorList = sessionLinkedList.get(repCmd.sid).getEditorList(); //ここもforループで検索しないといけないよ。 REPPacketSend repsend = null; for(Editor editor : editorList){ SocketChannel channel2 = editor.getChannel(); if(channel.equals(channel2)){ System.out.println("equals"); channelequals = true; }else if(channelequals){ repsend = new REPPacketSend(channel2); repsend.send(repCmd); } } } public void addSessionManager(SocketChannel channel, REPCommand repCmd) { smchannel = channel; } // public void sendAddedSession(REPCommand repCmd) { // repCmd.setCMD(REP.SMCMD_SM_JOIN); // REPPacketSend repSend = new REPPacketSend(smchannel); // } public String getSessionList() { // if (session3 != null) { // System.out.println(session3.toString()); // return session3.toString(); // } if (sessionLinkedList != null) { System.out.println(sessionLinkedList.toString()); return sessionLinkedList.toString(); } return "{}"; } public void sendToNextEditor(SocketChannel channel, REPCommand repCmd) { Editor editor2 = null; Editor editor3; // LinkedList <Editor> editorList = session3.get(repCmd.sid).getEditorList(); LinkedList <Editor> editorList = sessionLinkedList.get(repCmd.sid-1).getEditorList(); //ここもforループで検索しないといけないよ。 for(Editor editor : editorList){ SocketChannel channel2 = editor.getChannel(); if(channel.equals(channel2)){ editor2 = editor; } } int size = editorList.size(); Editor lastEditor = (Editor) editorList.getLast(); if(size == 0){ editor3 = null; }else if(editor2 == lastEditor){ editor3 = editorList.get(0); }else{ editor3 = editorList.get(editorList.indexOf(editor2)+1); } if(editor3 != null){ REPPacketSend send = new REPPacketSend(editor3.getChannel()); send.send(repCmd); } } public void addEditor(SocketChannel editorChannel, int sid, int eid) { // session3.get(sid).addEditor(eid, editorChannel); sessionLinkedList.get(sid-1).addEditor(eid, editorChannel); } public void sendSelect(int sid) { // SocketChannel channel = session3.get(sid).getMaster().getChannel(); SocketChannel channel = sessionLinkedList.get(sid-1).getMaster().getChannel(); REPPacketSend send = new REPPacketSend(channel); send.send(new REPCommand(REP.SMCMD_SELECT, sid, 0, 0, 0, 0, "")); } public void addSession(SocketChannel channel, String string, int i) { 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(); // }*/ public LinkedList<Session> getList() { return sessionLinkedList; } public String toXML() { SessionXMLEncoder encoder = new SessionXMLEncoder(sessionLinkedList); encoder.sessionListToXML(); return null; } public int addSession(Session session) { sessionID++; // session3.put(sessionID, session); session.setSID(sessionID); sessionLinkedList.add(session); return sessionID; } public void addToRoutingTable(SocketChannel channel) { } public Session getSession(int sid) { return sessionLinkedList.get(sid - 1); } public void setMaxHost(String myHost) { // TODO Auto-generated method stub maxHost = myHost; } public String getMaxHost() { // TODO Auto-generated method stub return maxHost; } public void setList(LinkedList<Session> list) { // TODO Auto-generated method stub sessionLinkedList = list; } public void update(SocketChannel channel, SessionList receivedSessionList) { LinkedList<Session> list = receivedSessionList.getList(); int i = 0; for(Session session : list){ ++i; int sid = addSession(session); setTranslationTable(channel, i, sid); } } private void setTranslationTable(SocketChannel channel, int i, int sid) { } }