view src/sample/merge/TestEditor.java @ 58:0eaf3f3ecadb

*** empty log message ***
author pin
date Wed, 25 Jul 2007 14:49:02 +0900
parents f055e65c7e3c
children aa47ea5bdac9
line wrap: on
line source

package sample.merge;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

import remoteeditor.command.REPCommand;
import remoteeditor.command.REPCommandEvent;
import remoteeditor.command.REPCommandListener;
import remoteeditor.network.REP;
import remoteeditor.network.REPPacketReceive;
import remoteeditor.network.REPPacketSend;

public class TestEditor implements REPCommandListener, Runnable{
	private SocketChannel sc;
	private REPPacketReceive repreceive;
	private REPPacketSend repsend;
	private String myEditorName;
	private int myseq;
	private int mysid;
	private int myeid;
	List <REPCommand> userCmdList = new LinkedList<REPCommand>();
	List <REPCommand> tokenCmdList = new LinkedList<REPCommand>();
	Translate trans = new Translate(userCmdList, tokenCmdList);
	private String myString = " ";
	
	List <String> myText = new LinkedList<String>();
	private TestSessionManager sessionManager;
	
		
	public static void main(String[] args){
		TestEditor editorA = new TestEditor("EditorA");
		editorA.init();
		editorA.join();
		editorA.put();
		editorA.select();
		
		TestEditor editorB = new TestEditor("EditorB");
		editorB.init();
		editorB.join();
		editorB.select();
		
		//editorA.send(REP.REP_INSERT_CMD);
		//editorB.send(REP.REP_INSERT_CMD);
		//editorA.send(REP.REP_INSERT_CMD);
		//editorB.send(REP.REP_INSERT_CMD);
		//editorA.send(REP.REP_INSERT_CMD);
		//editorB.send(REP.REP_INSERT_CMD);
		
		Integer[] cmdkind = {REP.REP_INSERT_CMD, REP.REP_DELETE_CMD, REP.REP_REPLACE_CMD};
		Enumeration e = new PermEnum(cmdkind);
		while(e.hasMoreElements()){
			Integer[] a = (Integer[])e.nextElement();
//			System.out.print("{" + a[0].intValue());
//			for(int i=1; i<a.length; i++) System.out.print(", "+a[i]);
//			System.out.println("}");
			for(int i = 0; i<a.length; i++){
				editorA.send(a[i].intValue());
			}
			System.out.println("");
		}
	}
	
	public TestEditor(String str){
		myEditorName = str;
	}

	public void init(){
		int port = 8765;
		String host = "localhost";
		InetSocketAddress addr = new InetSocketAddress(host, port);
		try {
			sc = SocketChannel.open();
			sc.configureBlocking(true);
			sc.connect(addr);
			while(!sc.finishConnect()){
				System.out.println("afro");
			}
		}catch (IOException e) {
			e.printStackTrace();
		}
		repreceive = new REPPacketReceive(sc);
		repsend = new REPPacketSend(sc);
		
	}
	
	public void join(){
		repsend.send(new REPCommand(REP.REP_JOIN_CMD, 0, 0, myseq, 0, 0, "")); myseq++;
		REPCommand command = repreceive.unpack();
		myeid = command.eid; 
		trans.setMyEID(myeid);
	}
	
	public void put(){
		repsend.send(new REPCommand(REP.REP_PUT_CMD, 0, myeid, myseq, 0, 4, "afro")); myseq++;
		REPCommand command = repreceive.unpack();
		mysid = command.sid;
	}
	
	public void select(){
		mysid = 1;
		repsend.send(new REPCommand(REP.REP_SELECT_CMD, mysid, myeid, myseq, 0, 0, "")); myseq++;
		REPCommand command = repreceive.unpack();
		repreceive.addCommandListener(this);
	}
	
	public void send(int cmdKind){
		REPCommand sendCommand = new REPCommand(cmdKind, mysid, myeid, myseq, 10, myEditorName.length(), myEditorName);
		myString = sendCommand.string + myString;
		//System.out.println(myEditorName + " : " + myString);
		trans.addUserList(sendCommand);
		repsend.send(sendCommand);
		myseq++;
	}
	
	public void send(REPCommand command){
		System.out.println(command.toString());
	}

	public void CommandReceived(REPCommandEvent event) {
		trans.addTokenList(event.getCommand());
		//System.out.println(myEditorName + " : userCmdList : " + userCmdList.toString());
		//System.out.println(myEditorName + " : tokenCmdList : " + tokenCmdList.toString());
		trans.merge();
		REPCommand tokenCmd = null;
		if (tokenCmdList.size() != 0) {
			tokenCmd = tokenCmdList.get(0);
			repsend.send(tokenCmd);
			if(tokenCmd.lineno == 10){
				myString = tokenCmd.string + myString;
			}else if(tokenCmd.lineno == 11){
				myString = myString + tokenCmd.string;
			}
			tokenCmdList.remove(0);
		}
		System.out.println(myEditorName + " : " + myString);
	}

	public void run() {
		Integer[] cmdkind = {REP.REP_INSERT_CMD, REP.REP_DELETE_CMD, REP.REP_REPLACE_CMD};
		//Integer[] cmdkind = {REP.REP_INSERT_CMD, REP.REP_DELETE_CMD};
		Enumeration e = new PermEnum(cmdkind);
		//TestEditor editorA = new TestEditor("EditorA");
		while(e.hasMoreElements()){
			Integer[] a = (Integer[])e.nextElement();
			for(int i = 0; i<a.length; i++){
				REPCommand command = new REPCommand(a[i].intValue(), 1, 1, myseq, 10, 1, myEditorName + myseq); myseq++;
				send(command);
			}
		}
	}

	public int getEID() {
		return myeid;		
	}
	
	public void changeText(REPCommand command){
		switch(command.cmd){
		case REP.REP_INSERT_CMD:
			myText.add(command.lineno, command.string);
			break;
		}
	}

	public void setSessionManager(TestSessionManager sessionManager) {
		// TODO Auto-generated method stub
		this.sessionManager = sessionManager;
	}

	public String getEditorName() {
		// TODO Auto-generated method stub
		return myEditorName;
	}
}