changeset 297:9c553308b918

*** empty log message ***
author kono
date Tue, 30 Sep 2008 19:16:06 +0900
parents c35f01b5060f
children cf9328e66d25
files test/sematest/JoinTester.java test/sematest/PutTester.java test/sematest/TestEditor.java test/sematest/Tester.java
diffstat 4 files changed, 44 insertions(+), 146 deletions(-) [+]
line wrap: on
line diff
--- a/test/sematest/JoinTester.java	Mon Sep 29 14:46:30 2008 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,27 +0,0 @@
-package test.sematest;
-
-import java.util.LinkedList;
-
-import rep.REP;
-import rep.REPCommand;
-
-public class JoinTester extends Tester {
-
-	public JoinTester(String name, String _host, int _port) {
-		super(name, _host, _port);
-	}
-
-	public static void main(String[] args){
-		
-		REPCommand command = new REPCommand();
-		command.setCMD(REP.SMCMD_JOIN);
-		command.setString("JoinTester");
-		LinkedList<REPCommand> commands = new LinkedList<REPCommand>();
-		commands.add(command);
-		
-		Tester tester = new JoinTester("JoinTester", "localhost", 8766);
-		tester.setCommands(commands);
-		tester.start();
-	}
-
-}
--- a/test/sematest/PutTester.java	Mon Sep 29 14:46:30 2008 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,25 +0,0 @@
-package test.sematest;
-
-import java.util.LinkedList;
-
-import rep.REP;
-import rep.REPCommand;
-
-public class PutTester extends Tester {
-
-	public PutTester(String name, String _host, int _port) {
-		super(name, _host, _port);
-		REPCommand command = new REPCommand();
-		command.setCMD(REP.SMCMD_PUT);
-		command.setString("PutTest.txt");
-		LinkedList<REPCommand> commands = new LinkedList<REPCommand>();
-		commands.add(command);
-		setCommands(commands);
-	}
-	
-	public static void main(String[] args){
-		Tester tester = new PutTester("PutTester", "localhost", 8766);
-		tester.start();
-	}
-
-}
--- 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	:
--- a/test/sematest/Tester.java	Mon Sep 29 14:46:30 2008 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,69 +0,0 @@
-package test.sematest;
-
-import java.io.IOException;
-import java.net.InetSocketAddress;
-import java.util.List;
-
-import rep.REP;
-import rep.REPCommand;
-import rep.REPCommandPacker;
-import rep.channel.REPLogger;
-import rep.channel.REPSocketChannel;
-
-public class Tester extends Thread{
-	private InetSocketAddress semaIP;
-	protected REPLogger ns;
-	private boolean running = true;
-	protected REPSocketChannel<REPCommand> channel;
-	protected List<REPCommand> commandList;
-	
-	public Tester(String name, String _host,int _port){
-		super(name);
-
-		// REPServerSocketChannel.isSimulation = false;
-		semaIP = new InetSocketAddress(_host, _port);
-		ns = REPLogger.singleton();
-		ns.setLogLevel(5);
-	}
-
-	public void run(){
-		try {
-			channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
-			channel.configureBlocking(true);
-
-			ns.writeLog("try to connect to SessionManager whose ip is "+semaIP, 1);
-			while (!channel.connect(semaIP)){
-				ns.writeLog("SeMa not listen to socket yet, wait", 1);
-				Thread.yield();
-			}
-			ns.writeLog("successes to connect", 1);
-			
-			sendCommands();
-			
-			while(running){
-				REPCommand packet = channel.read();
-				ns.writeLog("gets return string==> `"+packet+"\'", 1);
-				if(packet.cmd == REP.SMCMD_QUIT){
-					running = false;
-				}
-			}
-
-			ns.writeLog("testEditor exits.", 1);
-			
-			channel.close1();
-			
-		} catch (IOException e) {}
-	}
-
-	public void sendCommands(){
-		for(REPCommand command: commandList){
-			channel.write(command);
-		}
-		
-	}
-	
-	public void setCommands(List<REPCommand> commands){
-		commandList = commands;
-	}
-	
-}