view src/sample/merge/TestEditor.java @ 128:75fc44fda583

*** empty log message ***
author pin
date Tue, 08 Jan 2008 14:39:08 +0900
parents 403272ec470c
children
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 extends Thread 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>();
	List <REPCommand> userQueue = new LinkedList<REPCommand>();
	List <String> myTextList = new LinkedList<String>();
	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.rep_join();
		editorA.put();
		editorA.select();
		
		TestEditor editorB = new TestEditor("EditorB");
		editorB.init();
		editorB.rep_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.REPCMD_INSERT, REP.REPCMD_DELETE, REP.REPCMD_REPLACE};
		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 TestEditor(int sid, int eid, String string) {
		mysid = sid; myeid = eid; myEditorName = string;
		for(int i = 0; i < 20; i++){
			myTextList.add(" ");
		}
	}

	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 rep_join(){
		repsend.send(new REPCommand(REP.SMCMD_JOIN, 0, 0, myseq, 0, 0, "")); myseq++;
		REPCommand command = repreceive.unpackUConv();
		myeid = command.eid; 
		trans.setMyEID(myeid);
	}
	
	public void put(){
		repsend.send(new REPCommand(REP.SMCMD_PUT, 0, myeid, myseq, 0, 4, "afro")); myseq++;
		REPCommand command = repreceive.unpackUConv();
		mysid = command.sid;
	}
	
	public void select(){
		mysid = 1;
		repsend.send(new REPCommand(REP.SMCMD_SELECT, mysid, myeid, myseq, 0, 0, "")); myseq++;
		REPCommand command = repreceive.unpackUConv();
		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());
		//sessionManager.send(command, myeid);
		sessionManager.send(command, this);
	}

	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 void run() {
		while(true){
			if(userQueue.size() > 0){
				REPCommand command = userQueue.get(0);
				myTextList.add(command.lineno, command.string);
				if(command.string == "exit") {
					System.out.println(myTextList.toString());
					break;
				}
				send(command);
				userCmdList.add(command);
				userQueue.remove(0);
			}
		}
	}

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

	public void setSessionManager(TestSessionManager sessionManager) {
		this.sessionManager = sessionManager;
	}

	public String getEditorName() {
		return myEditorName;
	}

	public synchronized void changeText(int kind, int lineno, String string) {
		userQueue.add(new REPCommand(REP.REPCMD_INSERT, mysid, myeid, getMyseq(), lineno, string.length(), string));
	}

	private int getMyseq() {
		myseq++;
		return myseq;
	}

	public void CommandReceived(REPCommand command) {
		if(command.eid != myeid){
			myTextList.add(command.string);
		}
	}

}