view rep/Session.java @ 323:1e605880d49e

*** empty log message ***
author kono
date Fri, 10 Oct 2008 18:04:09 +0900
parents 5893fd8c0f50
children b18c24dcc5d2
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 int eidSeed = 0;
	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 int newEid() {
		int eid=0;
		boolean flag = true;
		while(flag) {
			eid=eidSeed++;
			for(EditorPlus e:editorList) {
				if((flag = (eid==e.eid))) break;
			}
		}
		return eid;
	}
	
	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;
	}
	

}