view rep/EditorList.java @ 31:593f915dd6ff JOINandPUT

for JOIN and PUT Testing
author pin
date Sat, 10 Nov 2007 12:52:24 +0900
parents 7012a944e58f
children 97ca5f5447cd
line wrap: on
line source

package rep;

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

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(SocketChannel channel, REPCommand repCmd) {
		REPCommand command = repCmd;
		command.setCMD(REP.SMCMD_JOIN_ACK);
		
		REPPacketSend send = new REPPacketSend(channel);
		send.send(command);
	}

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

	public void addEditor(SocketChannel 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(SocketChannel channel, REPCommand repCmd) {
		REPPacketSend send = new REPPacketSend(channel);
		send.send(repCmd);
	}
	
	public void send(SocketChannel channel, REPCommand command){
		REPPacketSend send = new REPPacketSend(channel);
		send.send(command);
	}

}