view rep/SessionManagerList.java @ 343:21ad256c25c2

*** empty log message ***
author kono
date Mon, 13 Oct 2008 13:16:31 +0900
parents 90965a3bd4f3
children ef4afcae0c92
line wrap: on
line source

package rep;

import java.util.LinkedList;
import java.util.List;

import rep.channel.REPSocketChannel;
import rep.xml.SessionXMLEncoder;

public class SessionManagerList {

	private List<REPSocketChannel<REPCommand>> list = new LinkedList<REPSocketChannel<REPCommand>>();
	private int mySMID;
	private REPSocketChannel<REPCommand> master;

	public void add(REPSocketChannel<REPCommand> channel) {
		list.add(channel);
	}

	public void sendUpdate(int sessionID, String string) {
		for(REPSocketChannel<REPCommand> channel : list){
			channel.write(new REPCommand(REP.SMCMD_UPDATE, 0, mySMID, 0, 0, string));
		}
	}

	public void sendJoin(REPCommand command) {
		for(REPSocketChannel<REPCommand> channel : list){
			channel.write(command);
		}
	}
	
	public void setMaster(REPSocketChannel<REPCommand> channel){
		this.master = channel;
	}

	public void sendSessionList(SessionList sessionlist, REPCommand command) {		
		SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist);
		command.setString(encoder.sessionListToXML());
		
		for(REPSocketChannel<REPCommand> channel : list){
			channel.write(command);
		}
	}

	public void sendToMaster(REPCommand repCmd) {
		master.write(repCmd);
	}

	public void sendToSlave(REPCommand repCmd) {
		for(REPSocketChannel<REPCommand> channel : list){
			if(channel.equals(master)) continue;
			channel.write(repCmd);
		}
	}

	public void sendExcept(REPSocketChannel<REPCommand> channel2, REPCommand command) {
		for(REPSocketChannel<REPCommand> channel : list){
			if(channel.equals(channel2)) continue;
			channel.write(command);
		}
	}

}