Mercurial > hg > RemoteEditor > REPSessionManager
changeset 359:fa041bae35f1
all code written for distributed session except gather.
author | kono |
---|---|
date | Sun, 19 Oct 2008 19:24:38 +0900 |
parents | 034acadc0cdc |
children | b25f832f875d |
files | rep/EditorPlus.java rep/FirstConnector.java rep/Session.java rep/SessionList.java rep/SessionManager.java rep/xml/SessionXMLDecoder.java rep/xml/SessionXMLEncoder.java test/XMLTest.java |
diffstat | 8 files changed, 101 insertions(+), 74 deletions(-) [+] |
line wrap: on
line diff
--- a/rep/EditorPlus.java Sun Oct 19 16:54:37 2008 +0900 +++ b/rep/EditorPlus.java Sun Oct 19 19:24:38 2008 +0900 @@ -68,4 +68,10 @@ public void setChannel(REPSocketChannel<REPCommand> channel) { this.channel = channel; } + + public void merge(EditorPlus editor) { + if (sid==-1) sid = editor.sid; + if (file==null) file = editor.file; + if (host==null) host = editor.host; + } }
--- a/rep/FirstConnector.java Sun Oct 19 16:54:37 2008 +0900 +++ b/rep/FirstConnector.java Sun Oct 19 19:24:38 2008 +0900 @@ -30,24 +30,22 @@ case SMCMD_JOIN: { //どのSessionにも属さないエディタをリストに追加 - //エディタとchannelは1対1 (ではない) + //エディタとchannelは1対1 (ではないかも) //エディタが新しくputする場合は新しくソケットを作る // 1対1でない場合は、multiplexerを挿めば良い - // ここのeditorList はsessionのとは別物 - Editor editor = new Editor(manager,-1,channel); + Editor editor = manager.newEditor(channel); editor.setHost(manager.myHost); - manager.editorList.add(editor); - manager.updateGUI(); fw = editor; break; } case SMCMD_PUT: { - // put の場合でも、eid は、masterまで聞きにいく必要が - // ある。 - Editor editor = new Editor(manager,0,channel); + // 新しいeditorとsessionをここで作る。eid,sidは、 + // session manager IDが付いているので、global unique + Editor editor = manager.newEditor(channel); + Session session = manager.newSession(editor); + editor.setSID(session.getSID()); editor.setHost(manager.myHost); - manager.editorList.add(editor); fw = editor; break; }
--- a/rep/Session.java Sun Oct 19 16:54:37 2008 +0900 +++ b/rep/Session.java Sun Oct 19 19:24:38 2008 +0900 @@ -1,16 +1,22 @@ package rep; -import java.util.Iterator; +import java.util.Collection; +import java.util.HashMap; import java.util.LinkedList; import rep.channel.REPSocketChannel; -public class Session { +public class Session extends HashMap<Integer,EditorPlus> { + /** + * + */ + private static final long serialVersionUID = 1L; private Forwarder masterEditor; private int sessionID; private String sessionName; - private LinkedList<EditorPlus> editorList = new LinkedList<EditorPlus>(); + // isOnwer means this session has active channels(forwarders). private boolean isOwner = false; private Forwarder firstForwarder; + private Forwarder last; public Session(int sid, String name, Forwarder editor) { this(sid, editor); @@ -22,33 +28,35 @@ sessionID = sid; if (editor!=null) { // we have a master - masterEditor = editor; + masterEditor = last = editor; editor.setSID(sid); - editorList.add(editor); + put(editor.eid,editor); if(editor.channel!=null) { firstForwarder = editor; masterEditor.setNext(masterEditor); + isOwner = true; } } } public void addForwarder(Forwarder forwarder) { // add a forwarder and connect this to the session - Forwarder last = (Forwarder)editorList.getLast(); forwarder.setNext(last.getNextForwarder()); last.setNext(forwarder); - editorList.add(forwarder); + last = forwarder; + put(forwarder.eid,forwarder); + isOwner = true; if(firstForwarder==null) firstForwarder = forwarder; } public void addEditor(Editor editor) { // add a not-connected editor in a sassion // the editor is outside of this manager - editorList.add(editor); + put(editor.eid,editor); } - public LinkedList<EditorPlus> getEditorList() { - return editorList; + public Collection<EditorPlus> getEditorList() { + return values(); } public String toString(){ @@ -66,33 +74,37 @@ public boolean deleteEditor(REPSocketChannel<REPCommand> channel) { - boolean flag = false; - - for (Iterator<EditorPlus> it = editorList.iterator();it.hasNext(); ) { - Forwarder e = (Forwarder)it.next(); + LinkedList<EditorPlus> toBeRemoved = new LinkedList<EditorPlus>(); + for (EditorPlus e:values() ) { if (e.getChannel()==channel) { - unconnect(e); - it.remove(); // to avoid concurrent modification - flag = true; + unconnect((Forwarder)e); + toBeRemoved.add(e); } } - return flag; + for(EditorPlus e:toBeRemoved) { + remove(e); + } + return !toBeRemoved.isEmpty(); } public boolean deleteForwarder(Forwarder editor) { unconnect(editor); - return editorList.remove(editor); + return remove(editor)!=null; } private void unconnect(Forwarder e) { - for(EditorPlus e1:editorList) { + boolean hasOwner = false; + for(EditorPlus e1:values()) { Forwarder f = (Forwarder)e1; - if(f.next==e) + if(f.next==e) { f.next=e.next; + } else { + if (f.channel!=null) hasOwner=true; + } } if(firstForwarder==e) firstForwarder=null; if(masterEditor==e) masterEditor=null; - // if no masterEditor we should delete session also... + isOwner = hasOwner; } public void setSID(int sid) { @@ -103,12 +115,9 @@ return isOwner; } - public void hasOwner(boolean b) { - isOwner = b; - } public Editor getEditor(REPSocketChannel<REPCommand> channel) { - for(EditorPlus editor : editorList){ + for(EditorPlus editor : values()){ if(editor.getChannel() == channel) { return (Editor)editor; } @@ -122,8 +131,7 @@ } public void closeSession() { - EditorPlus e = editorList.getFirst(); - Forwarder first = (Forwarder)e; + Forwarder first = firstForwarder; REPCommand command = new REPCommand(REP.REPCMD_CLOSE, sessionID, REP.SM_EID.id, 0, 0, ""); if (first!=null) first.send(command); @@ -137,12 +145,24 @@ public void remove(SessionManager manager) { - for(EditorPlus editor : editorList){ + for(EditorPlus editor : values()){ if(editor.getChannel() !=null) unconnect((Forwarder)editor); manager.editorList.remove(editor); } } + + + public void merge(Session s) { + for(EditorPlus editor : s.values()){ + EditorPlus mine = get(editor.eid); + if (mine==null) { + put(editor.eid,editor); + } else { + mine.merge(editor); + } + } + } }
--- a/rep/SessionList.java Sun Oct 19 16:54:37 2008 +0900 +++ b/rep/SessionList.java Sun Oct 19 19:24:38 2008 +0900 @@ -83,13 +83,6 @@ // return str.toString(); // }*/ - - public int addSession(Session session) { - int sid; - session.setSID(sid=newSessionID()); - put(sid,session); - return sid; - } public Session getSession(int sid) { return get(sid); @@ -100,8 +93,14 @@ } public void merge(SessionList receivedSessionList) { - // TODO Auto-generated method stub - + for(Session s:receivedSessionList.values()) { + int sid = s.getSID(); + if (containsKey(sid)) { + get(sid).merge(s); + } else { + put(sid,s); + } + } }
--- a/rep/SessionManager.java Sun Oct 19 16:54:37 2008 +0900 +++ b/rep/SessionManager.java Sun Oct 19 19:24:38 2008 +0900 @@ -343,7 +343,7 @@ session.addForwarder(editor); REPCommand sendCommand = new REPCommand(); if (editor.isDirect()&&editor.getEID()==eid) { - sendUpdate(); + sendUpdate(session.getSID()); sendCommand.setCMD(REP.SMCMD_JOIN_ACK); } else { // SELECT_ACK is sent to the session ring to @@ -369,11 +369,26 @@ } } - private void sendUpdate() { - // TODO Auto-generated method stub - + private void sendUpdate(int sid) { + REPCommand command = makeREPCommandWithSessionList(REP.SMCMD_UPDATE); + command.setSID(sid); + command.setEID(REP.SM_EID.id); + smList.sendToMaster(command); } + public Editor newEditor(REPSocketChannel<REPCommand> channel) { + int eid = makeID(editorList.newEid()); + Editor editor = new Editor(this, eid, channel); + editorList.add(editor); + return editor; + } + + + public Session newSession(Forwarder master) { + int sid= makeID(sessionList.newSessionID()); + Session session = new Session(sid, master); + return session; + } public void addWaitingCommand(PacketSet set) { waitingCommandInMerge.add(set); @@ -432,7 +447,7 @@ private void removeSession(Session s0) { s0.remove(this); sessionList.remove(s0); - sendUpdate(); + sendUpdate(s0.getSID()); } public void setParentPort(int port) { @@ -456,10 +471,6 @@ case SMCMD_JOIN: { // first connection or forwarded command - if (forwarder.isDirect()) { - // direct linked editor なので、ここでIDを作成する - command.setEID(makeID(editorList.newEid())); - } if(isMaster()) { REPCommand ackCommand = new REPCommand(); ackCommand.setCMD(REP.SMCMD_JOIN_ACK); @@ -484,13 +495,6 @@ case SMCMD_PUT: { // first connection or forwarded command - if (forwarder.isDirect()) { - // direct link, make new ID - int eid = makeID(editorList.newEid()); - int sid = makeID(sessionList.newSessionID()); - command.setEID(eid); - command.setSID(sid); - } if(isMaster()) { command.setCMD(REP.SMCMD_PUT_ACK); command.string = command.string; @@ -559,11 +563,14 @@ break; case SMCMD_UPDATE: - command.setString(mergeUpdate(command)); - // 上に知らせる - smList.sendToMaster(command); - break; - + if (!isMaster()) { + command.setString(mergeUpdate(command)); + // 上に知らせる + smList.sendToMaster(command); + break; + } + // fall thru + command.setCMD(REP.SMCMD_UPDATE_ACK); case SMCMD_UPDATE_ACK: command.setString(mergeUpdate(command)); // 下に知らせる
--- a/rep/xml/SessionXMLDecoder.java Sun Oct 19 16:54:37 2008 +0900 +++ b/rep/xml/SessionXMLDecoder.java Sun Oct 19 19:24:38 2008 +0900 @@ -87,7 +87,7 @@ editor.setHost(host);/* editor.setPort(port)*/; editor.setName(file); editor.setEID(Integer.parseInt(eid)); session = new Session(sid, editor); session.addEditor(editor); - sessionlist.addSession(session); + sessionlist.put(sid,session); }else { Editor editor = new Editor(null, false, 0);
--- a/rep/xml/SessionXMLEncoder.java Sun Oct 19 16:54:37 2008 +0900 +++ b/rep/xml/SessionXMLEncoder.java Sun Oct 19 19:24:38 2008 +0900 @@ -1,8 +1,6 @@ package rep.xml; import java.io.StringWriter; -import java.util.LinkedList; - import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -66,8 +64,7 @@ root.appendChild(elementSession); elementSession.setAttribute("sid", Integer.toString(session.getSID())); - LinkedList <EditorPlus> editorlist = session.getEditorList(); - for(EditorPlus editor : editorlist){ + for(EditorPlus editor : session.getEditorList()){ Element elementEditor = doc.createElement("Editor"); elementEditor.setAttribute("eid", Integer.toString(editor.getEID())); elementSession.appendChild(elementEditor);
--- a/test/XMLTest.java Sun Oct 19 16:54:37 2008 +0900 +++ b/test/XMLTest.java Sun Oct 19 19:24:38 2008 +0900 @@ -27,7 +27,7 @@ SessionList sessionlist = new SessionList(); //sessionlist.setMaxHost("naha.ie.u-ryukyu.ac.jp"); - sessionlist.addSession(session); + sessionlist.put(session.getSID(),session); /*** SessionList から XML へ ***/ //SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist.getList());