view rep/Session.java @ 317:c83a3faec487

*** empty log message ***
author kono
date Tue, 07 Oct 2008 21:48:31 +0900
parents 77f443f6dc9f
children dc57e24ea3df
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 Forwarder next;
	private int sessionID;
	private String sessionName;
	private LinkedList<Editor> editorList = new LinkedList<Editor>();
	private boolean isOwner = false;
	
	public Session(int sid, String name, Editor editor) {
		this(sid, editor);
		sessionName = name;
	}
	public Session(Editor editor) {
		masterEditor = editor;
		this.sessionID = 0;
		this.sessionName = editor.getName();
	}

	public Session(int sid, Editor editor) {
		sessionID = sid;
		masterEditor = editor;
		editorList.add(editor);
	}
	public void addEditor(SessionManager manager,int editorID, REPSocketChannel<REPCommand> channel) {
		editorList.add(new Editor(manager,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 Forwarder getOwner() {
		return masterEditor;
	}
	public String getName() {
		return sessionName;
	}
	public void addEditor(Editor editor) {
		int eid = editorList.size();
		editor.setEID(eid);
		editor.setSID(sessionID);
		editorList.add(editor);
	}
	
	public boolean deleteEditor(REPSocketChannel<REPCommand> channel) {
		boolean flag = false;

	    for (Iterator<Editor> it = editorList.iterator();it.hasNext(); ) {
	        Forwarder e = it.next();
			if (e.getChannel()==channel) {
				it.remove(); // to avoid concurrent modification
				flag = true;
			}
		}
		return flag;
	}
	
	public void setSID(int sid) {
		sessionID = sid;
	}
	
	public boolean hasOwner() {
		return isOwner;
	}
	public void hasOwner(boolean b) {
		isOwner = true;
	}
	public void sendToEditor(REPCommand repCmd) {
		for(Forwarder editor : editorList){
			editor.getChannel().write(repCmd);
		}
	}
	public Editor getEditor(REPSocketChannel<REPCommand> channel) {
		for(Editor editor : editorList){
			if(editor.getChannel() == channel) return editor;
		}
		return null;
	}

	
	Forwarder getNextEditor(Forwarder editor) {
		int eid = editor.getEID();
		int neid = (eid+1)%editorList.size();
		Forwarder nextEditor = editorList.get(neid);
		return nextEditor;
	}
	public Forwarder getPrevEditor(Forwarder editor) {
		int eid = editor.getEID();
		int peid = (eid + editorList.size() - 1)%editorList.size();
		Forwarder prevEditor = editorList.get(peid);
		return prevEditor;		
	}
	public void closeSession() {
		REPCommand command = new REPCommand();
		command.setCMD(REP.REPCMD_CLOSE);
		for(Forwarder editor : editorList){
			command.setEID(editor.getEID());
			editor.send(command);
		}
	}
	public void sendToNextEditor(REPSocketChannel<REPCommand>channel,REPCommand command) {
		Forwarder next = getNextEditor(getEditor(channel));
		next.send(command);
	}
	
	public void setForwarder(Forwarder next) {
		this.next = next;
	}
	
	/**
	 * Dispatch REPCommand in this session
	 * @param eid
	 * @return
	 */
	public Forwarder getForwarder(int eid) {
		Forwarder f = editorList.get(eid);
		if (f==null) return next;
		return f;
	}
}