view rep/EditorList.java @ 137:1ea856259add

*** empty log message ***
author pin
date Wed, 27 Aug 2008 18:43:09 +0900
parents 70fc1e70652c
children 6a5fe529b192
line wrap: on
line source

package rep;

import java.nio.channels.SocketChannel;
import java.util.LinkedList;

import rep.channel.REPSocketChannel;

public class EditorList {

	private int numberOfEditor;
	private LinkedList<Editor> editorList = new LinkedList<Editor>();

	public void sendJoinAck(REPCommand repCmd) {
		Editor editor = null;
		for(Editor editor2 : editorList){
			error(String.valueOf(editor2.getEID()), String.valueOf(repCmd.eid));
			if(editor2.getEID() == repCmd.eid){
				editor = editor2;
				break;
			}
		}
		error(editor);
		REPPacketSend send = new REPPacketSend(editor.getChannel());
		send.send(repCmd);
	}


	public void sendJoinAck(REPSocketChannel channel, REPCommand repCmd) {
		REPCommand command = repCmd;
		command.setCMD(REP.SMCMD_JOIN_ACK);
		
		REPPacketSend send = new REPPacketSend(channel);
		send.send(command);
	}

	public int addEditor(REPSocketChannel<REPCommand> channel, REPCommand repCmd) {
		numberOfEditor++;
		editorList.add(new Editor(numberOfEditor, channel));
		return numberOfEditor;
	}

	public void addEditor(REPSocketChannel<REPCommand> channel) {
		editorList.add(new Editor(0, channel));
	}

	public void setEID(REPCommand repCmd) {
		for(Editor editor : editorList){
			if(editor.getEID() == 0){
				editor.setEID(repCmd.eid);
				break;
			}
		}
	}
	
	private void error(Object obj) {
		if(obj == null){
			System.out.println("null!");
		}
	}
	private void error(String str1, String str2){
		if(str1.equals(str2)){
			return;
		}else{
			System.out.println("Not equals! str1:str2");
		}
	}


	public void sendPutAck(REPSocketChannel<REPCommand> channel, REPCommand repCmd) {
		REPPacketSend send = new REPPacketSend(channel);
		send.send(repCmd);
	}
	
	public void send(REPSocketChannel<REPCommand> channel, REPCommand command){
		REPPacketSend send = new REPPacketSend(channel);
		send.send(command);
	}


	public void setHost(String myHost) {
		// TODO Auto-generated method stub
		for(Editor editor : editorList) {
			editor.setHost(myHost);
		}
	}


	public Editor getEditor(String hostport) {
		// TODO Auto-generated method stub
		for(Editor editor : editorList){
			String[] splited = hostport.split(":");
			System.out.println(splited[0] + "," + editor.getHost());
			if(splited[0].equals(editor.getHost())){
				return editor;
			}
			
			//String hostandport = editor.getHost() + ":" + editor.getPort();
			//if(hostport.equals(hostandport)){
			//	return editor;
			//}
		}
		return null;
	}


	public int addEditor(Editor editor) {
		numberOfEditor++;
		editorList.add(editor);
		return numberOfEditor;
	}


	public Editor getEditor(REPSocketChannel channel) {
		// TODO Auto-generated method stub
		Editor editor1 = null;
		for(Editor editor: editorList){
			if(channel == editor.getChannel()){
				editor1 = editor;
			}
		}
		return editor1;
	}


	public void undoAllEditors() {
		// TODO Auto-generated method stub
		for(Editor editor : editorList){
			editor.undo();
		}
	}

}