Mercurial > hg > RemoteEditor > REPSessionManager
view rep/Session.java @ 356:b18c24dcc5d2
Before chaning put/join scheme for ditributed select.
author | kono |
---|---|
date | Sat, 18 Oct 2008 19:01:40 +0900 |
parents | 1e605880d49e |
children | 034acadc0cdc |
line wrap: on
line source
package rep; import java.util.Iterator; import java.util.LinkedList; import rep.channel.REPSocketChannel; public class Session { private Forwarder masterEditor; private int sessionID; private String sessionName; private LinkedList<EditorPlus> editorList = new LinkedList<EditorPlus>(); private boolean isOwner = false; private Forwarder firstForwarder; public Session(int sid, String name, Forwarder editor) { this(sid, editor); sessionName = name; } public Session(int sid, Forwarder editor) { sessionID = sid; if (editor!=null) { // we have a master masterEditor = editor; editor.setSID(sid); editorList.add(editor); if(editor.channel!=null) { firstForwarder = editor; masterEditor.setNext(masterEditor); } } } 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); 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); } public LinkedList<EditorPlus> getEditorList() { return editorList; } public String toString(){ return sessionName; } public int getSID() { return sessionID; } public Forwarder getOwner() { return masterEditor; } public String getName() { return sessionName; } public boolean deleteEditor(REPSocketChannel<REPCommand> channel) { boolean flag = false; for (Iterator<EditorPlus> it = editorList.iterator();it.hasNext(); ) { Forwarder e = (Forwarder)it.next(); if (e.getChannel()==channel) { unconnect(e); it.remove(); // to avoid concurrent modification flag = true; } } return flag; } private void unconnect(Forwarder e) { for(EditorPlus e1:editorList) { Forwarder f = (Forwarder)e1; if(f.next==e) f.next=e.next; } if(firstForwarder==e) firstForwarder=null; if(masterEditor==e) masterEditor=null; // if no masterEditor we should delete session also... } public void setSID(int sid) { sessionID = sid; } public boolean hasOwner() { return isOwner; } public void hasOwner(boolean b) { isOwner = b; } public Editor getEditor(REPSocketChannel<REPCommand> channel) { for(EditorPlus editor : editorList){ if(editor.getChannel() == channel) { return (Editor)editor; } } return null; } Forwarder getNextEditor(Forwarder editor) { return editor.getNextForwarder(); } public void closeSession() { EditorPlus e = editorList.getFirst(); Forwarder first = (Forwarder)e; REPCommand command = new REPCommand(REP.REPCMD_CLOSE, sessionID, REP.SM_EID.id, 0, 0, ""); if (first!=null) first.send(command); } public boolean deleteForwarder(Forwarder editor) { unconnect(editor); return editorList.remove(editor); } public Forwarder getFirstForwarder() { return firstForwarder; } }