diff test/sematest/TestEditor.java @ 297:9c553308b918

*** empty log message ***
author kono
date Tue, 30 Sep 2008 19:16:06 +0900
parents d93b062eadaa
children cf9328e66d25
line wrap: on
line diff
--- a/test/sematest/TestEditor.java	Mon Sep 29 14:46:30 2008 +0900
+++ b/test/sematest/TestEditor.java	Tue Sep 30 19:16:06 2008 +0900
@@ -14,13 +14,17 @@
 import test.Text;
 
 
+/**
+ * @author kono
+ *	Basic Temote Editor client implementation
+ *     should support multi-session
+ *     currently multi-session requires new channel, that is
+ *     only one session for this editor.
+ */
 public class TestEditor extends Thread{
 	private InetSocketAddress semaIP;
 	private REPLogger ns;
-	private boolean master;
-	private int myid ;
 	private int seq = 0;
-	public static int editorNo = 0;
 	public Text text;
 	public LinkedList<REPCommand> cmds;
 	private int eid = 0;
@@ -28,6 +32,7 @@
 	REPSocketChannel<REPCommand> channel;
 	boolean running = true;
 	long timeout = 1;
+	private String name;
 
 	static private String[] text1d = {
 		"aaa", "bbb", "ccc", "ddd", "eee",
@@ -37,19 +42,20 @@
 		super(name);
 		semaIP = new InetSocketAddress(_host, _port);
 		ns = REPLogger.singleton();
-		this.master = master;
-		myid = editorNo++;
+		this.name = name;
 		cmds = new LinkedList<REPCommand>();
 		if (master) {
 			text = new Text(text1d);
+			cmds.add(new REPCommand(REP.SMCMD_PUT,0,0,seq++,0,name+"-file"));
 			cmds.add(new REPCommand(REP.REPCMD_INSERT,0,0,0,0,"m0"));
 			cmds.add(new REPCommand(REP.REPCMD_DELETE,0,0,0,0,"m0"));
 			cmds.add(new REPCommand(REP.SMCMD_QUIT,0,0,0,0,""));
 		} else {
 			text = new Text(new String[0]);
+			cmds.add(new REPCommand(REP.SMCMD_JOIN,0,0,seq++,0,name));
 			cmds.add(new REPCommand(REP.REPCMD_INSERT,0,0,0,0,"c0"));
 			cmds.add(new REPCommand(REP.REPCMD_DELETE,0,0,0,0,"c0"));
-			cmds.add(new REPCommand(REP.SMCMD_QUIT,0,0,0,0,""));
+			//cmds.add(new REPCommand(REP.SMCMD_QUIT,0,0,0,0,""));
 		}
 	}
 
@@ -58,23 +64,16 @@
 			channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
 		} catch (IOException e) {	return;	}
 
-		ns.writeLog("try to connect to SessionManager whose ip is "+semaIP+" editor-"+myid, 1);
+		ns.writeLog("try to connect to SessionManager whose ip is "+semaIP+" "+name, 1);
 		try {
 			while (!channel.connect(semaIP)){
 				ns.writeLog("SeMa not listen to socket yet, wait", 1);
 			}
 		} catch (IOException e) { return; }
-		ns.writeLog("successes to connect editor-"+myid, 1);
-		REPCommand command;
+		ns.writeLog("successes to connect "+name);
 		/*
 		 *         public REPCommand(REP cmd,int sid,int eid, int seq, int lineno,  String string) 
 		 */
-		if (master)
-			command = new REPCommand(REP.SMCMD_PUT,0,0,seq++,0,"master-file");
-		else				
-			command = new REPCommand(REP.SMCMD_JOIN,0,0,seq++,0,"editor-"+myid);
-		channel.write(command);
-
 		try {
 			mainloop();
 		} catch (IOException e) {
@@ -98,8 +97,23 @@
 	private void userInput() {
 		REPCommand cmd = cmds.poll();
 		if (cmd!=null) {
-			text.edit(cmd);
-			sendCommand(cmd);
+			switch(cmd.cmd) {
+			case REPCMD_INSERT:
+				text.insert(cmd.lineno, cmd.string);
+				sendCommand(cmd);
+				break;
+			case REPCMD_DELETE:
+				String del = text.delete(cmd.lineno);
+				cmd.setString(del);
+				sendCommand(cmd);
+				break;
+			case SMCMD_QUIT:
+				cmds.clear();
+				sendCommand(cmd);
+				break;
+			default:
+				assert(false);
+			}
 		} else {
 			// no more command to send
 			timeout = 0;
@@ -111,19 +125,24 @@
 		cmd.setSEQID(seq++);
 		cmd.setEID(eid);
 		cmd.setSID(sid);
-		ns.writeLog("editor-"+myid+" send "+cmd);
+		ns.writeLog(name +" send "+cmd);
 		channel.write(cmd);
 	}
 
 	private void handle(REPCommand cmd) {
-		ns.writeLog("editor-"+myid+" read "+cmd);
+		ns.writeLog(name +": read "+cmd);
 		switch(cmd.cmd) {
-		 case REPCMD_INSERT	:
-			 break;
-		 case REPCMD_INSERT_ACK	:
-			 break;
-		 case REPCMD_DELETE	:
-			 break;
+		case REPCMD_INSERT	:
+			text.insert(cmd.lineno, cmd.string);
+			sendCommand(cmd);
+			break;
+		case REPCMD_INSERT_ACK	:
+			break;
+		case REPCMD_DELETE	:
+			String del = text.delete(cmd.lineno);
+			cmd.setString(del);
+			sendCommand(cmd);
+			break;
 		 case REPCMD_DELETE_ACK	:
 			 break;
 		 case REPCMD_CLOSE	: