Mercurial > hg > RemoteEditor > REPSessionManager
view rep/Session.java @ 170:30cf7747d134
*** empty log message ***
author | pin |
---|---|
date | Thu, 28 Aug 2008 18:56:46 +0900 |
parents | 3dc194f5e28f |
children | a097b1d619a1 |
line wrap: on
line source
package rep; import java.nio.channels.SocketChannel; import java.util.LinkedList; import java.util.List; import rep.channel.REPSocketChannel; public class Session { private Editor masterEditor; private int sessionID; private String sessionName; private LinkedList<Editor> editorList = new LinkedList<Editor>(); private String masterHost; private String masterPort; private LinkedList<REPNode> routingTable = new LinkedList<REPNode>(); private int incrementEID; private boolean isOwner = false; public Session(int sessionID, String string, REPSocketChannel channel) { masterEditor = new Editor(sessionID, channel); this.sessionID = sessionID; this.sessionName = string; } public Session(Editor editor) { masterEditor = editor; masterHost = editor.getHost(); masterPort = editor.getPort(); this.sessionID = 0; this.sessionName = editor.getName(); } public Session(int sid, Editor editor) { sessionID = sid; masterEditor = editor; editorList.add(editor); } public void addEditor(int editorID, REPSocketChannel channel) { editorList.add(new Editor(editorID, channel)); } public LinkedList<Editor> getEditorList() { if(editorList == null) System.out.println("null!"); return editorList; } public String toString(){ return sessionName; } public int getSID() { return sessionID; } public Editor getOwner() { return masterEditor; } public String getName() { return sessionName; } public void addEditor(Editor editor) { int eid = editorList.size(); editor.setEID(eid); editorList.add(editor); } public void setSID(int sessionID2) { sessionID = sessionID2; } public void addToRoutingTable(Editor editor) { routingTable.add(new REPNode(editor)); } public boolean hasOwner() { return isOwner; } public void hasOwner(boolean b) { isOwner = true; } public void sendToEditor(REPCommand repCmd) { for(Editor editor : editorList){ REPPacketSend send = new REPPacketSend(editor.getChannel()); send.send(repCmd); } } public Editor getEditor(REPSocketChannel<REPCommand> channel) { // TODO Auto-generated method stub for(Editor editor : editorList){ if(editor.getChannel() == channel) return editor; } return null; } public void translate(REPSocketChannel<REPCommand> channel, REPCommand command) { Editor editor = getEditor(channel); LinkedList<REPCommand> commandList = editor.translate(command); Editor nextEditor = getNextEditor(editor); for(REPCommand cmd: commandList){ nextEditor.send(cmd); } } private Editor getNextEditor(Editor editor) { int eid = editor.getEID(); int neid = (eid+1)%editorList.size(); Editor nextEditor = editorList.get(neid); return nextEditor; } public Editor getPrevEditor(Editor editor) { int eid = editor.getEID(); int peid = (eid + editorList.size() - 1)%editorList.size(); Editor prevEditor = editorList.get(peid); return prevEditor; } }