changeset 271:5b7abc22e61a

enum
author kono
date Thu, 11 Sep 2008 17:23:27 +0900
parents 5e0e4877465d
children 4d9058761c90
files rep/Editor.java rep/REP.java rep/REPCommand.java rep/REPCommandPacker.java rep/SessionManager.java rep/optimizers/DeleteInsertOptimizer.java rep/translater/TranslaterImp1.java test/RepCommandOptimizeTest.java test/TestUTF8.java
diffstat 9 files changed, 130 insertions(+), 92 deletions(-) [+]
line wrap: on
line diff
--- a/rep/Editor.java	Thu Sep 11 15:08:06 2008 +0900
+++ b/rep/Editor.java	Thu Sep 11 17:23:27 2008 +0900
@@ -66,7 +66,7 @@
 				translater.transSendCmd(command);
 				list.add(command);
 			}
-		}else if(eid == REP.MERGE_EID){
+		}else if(eid == REP.MERGE_EID.id){
 			//マージコマンドが返ってきた
 			if(translater.checkMergeConflict(command)){
 				//マージ中にエディタからの割り込みがあった場合
@@ -89,7 +89,7 @@
 	
 	private void sendMergedCommand(ArrayList<REPCommand> cmds) {
 		for(REPCommand mergeCommand : cmds){
-			mergeCommand.setEID(REP.MERGE_EID);
+			mergeCommand.setEID(REP.MERGE_EID.id);
 			myChannel.write(mergeCommand);
 		}
 	}
--- a/rep/REP.java	Thu Sep 11 15:08:06 2008 +0900
+++ b/rep/REP.java	Thu Sep 11 17:23:27 2008 +0900
@@ -1,56 +1,82 @@
 package rep;
 
-public class REP {
-	public static final int REPCMD_OPEN		= 1;
-	public static final int REPCMD_OPEN_ACK	= 2;
-	public static final int REPCMD_READ		= 3;
-	public static final int REPCMD_READ_ACK	= 4;
-	public static final int REPCMD_INSERT	= 6;
-	public static final int REPCMD_INSERT_ACK	= 7;
-	public static final int REPCMD_DELETE	= 9;
-	public static final int REPCMD_DELETE_ACK	= 10;
-	public static final int REPCMD_CLOSE	= 11;
-	public static final int REPCMD_CLOSE_2	= 12;
-	public static final int REPCMD_REPLACE	= 13;
-	public static final int REPCMD_REPLACE_ACK	= 14;
-	public static final int REPCMD_NOP		= 15;
-	public static final int SMCMD_JOIN		= 41;
-	public static final int SMCMD_JOIN_ACK	= 42;
-	public static final int SMCMD_GET		= 43;
-	public static final int SMCMD_GET_ACK	= 44;
-	public static final int SMCMD_PUT		= 45;
-	public static final int SMCMD_PUT_ACK	= 46;
-	public static final int SMCMD_SELECT	= 47;
-	public static final int SMCMD_SELECT_ACK	= 48;
-	public static final int SMCMD_REGISTER	= 49;
-	public static final int SMCMD_REGISTER_ACK	= 50;
-	public static final int SMCMD_DEREGISTER	= 51;
-	public static final int SMCMD_DEREGISTER_ACK= 52;
-	public static final int SMCMD_QUIT		= 53;
-	public static final int SMCMD_QUIT_ACK	= 54;
-	public static final int SMCMD_SESSION   = 60;
-	public static final int SMCMD_SESSION_ACK    = 61;
-	public static final int SMCMD_SM_JOIN   = 62;
-	public static final int SMCMD_SM_JOIN_ACK = 63;
-	public static final int SMCMD_UPDATE = 65;
-	public static final int SMCMD_UPDATE_ACK = 66;
-	public static final int SMCMD_GET_UNDO = 71;
-	public static final int SMCMD_GET_UNDO_ACK = 72;
+public enum REP {
+	 REPCMD_OPEN		( 1),
+	 REPCMD_OPEN_ACK	( 2),
+	 REPCMD_READ		( 3),
+	 REPCMD_READ_ACK	( 4),
+	 REPCMD_INSERT	( 6),
+	 REPCMD_INSERT_ACK	( 7),
+	 REPCMD_DELETE	( 9),
+	 REPCMD_DELETE_ACK	( 10),
+	 REPCMD_CLOSE	( 11),
+	 REPCMD_CLOSE_2	( 12),
+	 REPCMD_REPLACE	( 13),
+	 REPCMD_REPLACE_ACK	( 14),
+	 REPCMD_NOP		( 15),
+	 SMCMD_JOIN		( 41),
+	 SMCMD_JOIN_ACK	( 42),
+	 SMCMD_GET		( 43),
+	 SMCMD_GET_ACK	( 44),
+	 SMCMD_PUT		( 45),
+	 SMCMD_PUT_ACK	( 46),
+	 SMCMD_SELECT	( 47),
+	 SMCMD_SELECT_ACK	( 48),
+	 SMCMD_REGISTER	( 49),
+	 SMCMD_REGISTER_ACK	( 50),
+	 SMCMD_DEREGISTER	( 51),
+	 SMCMD_DEREGISTER_ACK( 52),
+	 SMCMD_QUIT		( 53),
+	 SMCMD_QUIT_ACK	( 54),
+	 SMCMD_SESSION   ( 60),
+	 SMCMD_SESSION_ACK    ( 61),
+	 SMCMD_SM_JOIN   ( 62),
+	 SMCMD_SM_JOIN_ACK ( 63),
+	 SMCMD_UPDATE ( 65),
+	 SMCMD_UPDATE_ACK ( 66),
+	 SMCMD_GET_UNDO ( 71),
+	 SMCMD_GET_UNDO_ACK ( 72),
 
-	public static final int SMCMD_UNDO_REPLACE = 73;
-	public static final int SMCMD_START_MERGE = 75;
-	public static final int SMCMD_START_MERGE_ACK = 76;
-	public static final int SMCMD_END_MERGE = 77;
-	public static final int SMCMD_QUIT_2 = 78;
+	 SMCMD_UNDO_REPLACE ( 73),
+	 SMCMD_START_MERGE ( 75),
+	 SMCMD_START_MERGE_ACK ( 76),
+	 SMCMD_END_MERGE ( 77),
+	 SMCMD_QUIT_2 ( 78),
 
 
-	public static final int MERGE_EID = -2;
-	public static final int SMCMD_CH_MASTER = 79;
-	public static final int SMCMD_UPDATE_UP = 80;
-	public static final int SMCMD_UPDATE_DOWN = 81;
+	 MERGE_EID ( -2),
+	 SMCMD_CH_MASTER ( 79),
+	 SMCMD_UPDATE_UP ( 80),
+	 SMCMD_UPDATE_DOWN ( 81),
+
+	 SMCMD_SYNC ( 82),
+	 SMCMD_SYNC_ACK ( 83);
+	 
+	 public final int id;
+	 
+	 REP(int id) {
+		 this.id = id;
+	 }
+
+	 static int max = 0;
+	 static int min = 100;
 
-	public static final int SMCMD_SYNC = 82;
-	public static final int SMCMD_SYNC_ACK = 83;
-
+     static REP rep[] ;
+     static {
+    	 // Certainly this is ridiculous...
+         for (REP r : REP.values()) {
+        	 if (max<r.id) max = r.id;
+        	 if (min>r.id) min = r.id;
+         }
+         rep = new REP[max-min+1];
+         for (REP r : REP.values()) {
+        	 rep[r.id-min] = r;
+         }
+     }
+     
+     public static REP newREP(int id) {
+    	 //  return new REP(id); this does not work...
+    	 return rep[id-min];
+     }
 
 }
--- a/rep/REPCommand.java	Thu Sep 11 15:08:06 2008 +0900
+++ b/rep/REPCommand.java	Thu Sep 11 17:23:27 2008 +0900
@@ -4,7 +4,7 @@
 
 public class REPCommand {
 	public static REPCommand SMCMD_SESSION_JOIN = new REPCommand(REP.SMCMD_SM_JOIN, 0, 0, 0, 0, 0, "");
-	public int cmd;
+	public REP cmd;
 	public int sid;
 	public int eid;
 	public int seq;
@@ -18,7 +18,7 @@
 	String host;
 	String port;
 	
-	public REPCommand(int cmd,int sid,int eid, int seq, int lineno, int textsiz, String string) {
+	public REPCommand(REP cmd,int sid,int eid, int seq, int lineno, int textsiz, String string) {
 		this.cmd = cmd;
 		this.sid = sid;
 		this.eid = eid;
@@ -35,6 +35,18 @@
 	public REPCommand() {
 		// TODO Auto-generated constructor stub
 	}
+	public REPCommand(int cmd, int sid, int eid, int seq, int lineno,
+			int textsiz, String string) {
+		this.cmd = REP.newREP(cmd);
+		this.sid = sid;
+		this.eid = eid;
+		this.seq = seq;
+		this.textsiz = textsiz;
+		this.lineno = lineno;
+		this.string = string;
+		this.throughMaster = false;
+	}
+	
 	public String toString(){
 		String repCmdString = new String(cmd + "," + sid + "," + eid + "," + seq + "," + lineno + "," + textsiz + "," + string);
 		return repCmdString;
@@ -45,7 +57,7 @@
 		this.eid = eid2;
 	}
 
-	public void setCMD(int cmd2) {
+	public void setCMD(REP cmd2) {
 		// TODO Auto-generated method stub
 		this.cmd = cmd2;
 	}
--- a/rep/REPCommandPacker.java	Thu Sep 11 15:08:06 2008 +0900
+++ b/rep/REPCommandPacker.java	Thu Sep 11 17:23:27 2008 +0900
@@ -43,7 +43,7 @@
     	}
     	ByteBuffer buffer = ByteBuffer.allocateDirect(HEADER_SIZE+(command.string.length()*CHAR_ORDER));
     	buffer.clear();  // position = 0 
-    	buffer.putInt(command.cmd);
+    	buffer.putInt(command.cmd.id);
     	buffer.putInt(command.sid);
     	buffer.putInt(command.eid);
     	buffer.putInt(command.seq);
--- a/rep/SessionManager.java	Thu Sep 11 15:08:06 2008 +0900
+++ b/rep/SessionManager.java	Thu Sep 11 17:23:27 2008 +0900
@@ -159,7 +159,7 @@
 
 		switch(receivedCommand.cmd){
 
-		case REP.SMCMD_JOIN:
+		case SMCMD_JOIN:
 		{
 			//どのSessionにも属さないエディタをリストに追加
 			//エディタとchannelは1対1
@@ -174,11 +174,11 @@
 
 		break;
 
-		case REP.SMCMD_JOIN_ACK:
+		case SMCMD_JOIN_ACK:
 			assert (false);
 			break;
 
-		case REP.SMCMD_PUT:
+		case SMCMD_PUT:
 		{
 			//エディタのリストに追加
 			Editor editor = new Editor(editorList.size(), channel);
@@ -213,7 +213,7 @@
 
 		break;
 
-		case REP.SMCMD_SELECT:
+		case SMCMD_SELECT:
 		{
 			//他のSessionManagerをエディタとしてSessionに追加
 			Editor editor = new Editor(channel);
@@ -234,7 +234,7 @@
 
 		break;
 
-		case REP.SMCMD_SELECT_ACK:
+		case SMCMD_SELECT_ACK:
 		{
 			String hostport = receivedCommand.string;
 			Editor editor = getEditor(hostport);
@@ -255,7 +255,7 @@
 
 		break;
 
-		case REP.SMCMD_SM_JOIN:
+		case SMCMD_SM_JOIN:
 
 		{
 			//SessionManagerのリストへ追加
@@ -294,7 +294,7 @@
 		}
 		break;
 
-		case REP.SMCMD_SM_JOIN_ACK:
+		case SMCMD_SM_JOIN_ACK:
 
 			//XMLからSessionListオブジェクトを生成。
 			SessionXMLDecoder decoder2 = new SessionXMLDecoder();
@@ -310,7 +310,7 @@
 
 			break;
 
-		case REP.SMCMD_UPDATE:
+		case SMCMD_UPDATE:
 		{
 			SessionXMLDecoder decoder3 = new SessionXMLDecoder();
 			SessionList receivedSessionList3 = decoder3.decode(receivedCommand.string);
@@ -334,7 +334,7 @@
 		}
 			break;
 
-		case REP.SMCMD_UPDATE_ACK:
+		case SMCMD_UPDATE_ACK:
 		{
 			if(receivedCommand.sid > sessionList.size()){
 				Editor editor = new Editor(channel);
@@ -356,7 +356,7 @@
 		}
 			break;
 
-		case REP.SMCMD_CH_MASTER:
+		case SMCMD_CH_MASTER:
 		{
 			//maxHost を設定。
 			if(setMaxHost(channel, receivedCommand.string)){
@@ -368,8 +368,8 @@
 		}
 			break;
 
-		case REP.REPCMD_DELETE:
-		case REP.REPCMD_INSERT:
+		case REPCMD_DELETE:
+		case REPCMD_INSERT:
 		{
 			//sid から Session を取得
 			Session session = getSession(receivedCommand.sid);
@@ -542,7 +542,7 @@
 		smList.add(channel);
 	}
 
-	private String getLocalHostName(REPSocketChannel channel) {
+	private String getLocalHostName(REPSocketChannel<?> channel) {
 		String host = null;
 		host = channel.socket().getLocalAddress().getHostName();
 		return host;
--- a/rep/optimizers/DeleteInsertOptimizer.java	Thu Sep 11 15:08:06 2008 +0900
+++ b/rep/optimizers/DeleteInsertOptimizer.java	Thu Sep 11 17:23:27 2008 +0900
@@ -21,9 +21,9 @@
 		for(int i = 0; i < output.size(); i++){
 			REPCommand r = output.get(i);
 			switch(r.cmd){
-			case REP.REPCMD_INSERT:
+			case REPCMD_INSERT:
 				break;
-			case REP.REPCMD_DELETE:
+			case REPCMD_DELETE:
 				optimizedAdd(output,r,i);
 				break;
 			}
--- a/rep/translater/TranslaterImp1.java	Thu Sep 11 15:08:06 2008 +0900
+++ b/rep/translater/TranslaterImp1.java	Thu Sep 11 17:23:27 2008 +0900
@@ -86,7 +86,7 @@
 		String str = new String(cmd.string);
 		REPCommand retCmd = new REPCommand(cmd.cmd, cmd.sid, cmd.eid, cmd.seq, cmd.lineno, str.length(), str);
 		
-		retCmd.eid = REP.MERGE_EID;
+		retCmd.eid = REP.MERGE_EID.id;
 		
 		if (cmd.cmd==REP.REPCMD_INSERT) retCmd.cmd=REP.REPCMD_DELETE;
 		else if (cmd.cmd==REP.REPCMD_DELETE) retCmd.cmd=REP.REPCMD_INSERT;
@@ -259,13 +259,13 @@
 			returnCommand.add(createUndo(mergeAgainList.get(mergeAgainList.size() - i -1)));
 		}
 		for(REPCommand command : mergeAgainList){
-			if(command.eid == REP.MERGE_EID){
+			if(command.eid == REP.MERGE_EID.id){
 				returnCommand.add(command);
 			}
 		}
 		for(REPCommand command : mergeAgainList){
 			if(command.eid == eid){
-				command.eid = REP.MERGE_EID;
+				command.eid = REP.MERGE_EID.id;
 				returnCommand.add(command);
 			}
 		}
--- a/test/RepCommandOptimizeTest.java	Thu Sep 11 15:08:06 2008 +0900
+++ b/test/RepCommandOptimizeTest.java	Thu Sep 11 17:23:27 2008 +0900
@@ -14,24 +14,24 @@
 public class RepCommandOptimizeTest {
 	//テストコマンド (command,string,lineno,eid)
 	static String[] test1 = {
-		Integer.toString(REP.REPCMD_DELETE),"d","1","1",
-		Integer.toString(REP.REPCMD_INSERT),"B","3","2",
-		Integer.toString(REP.REPCMD_INSERT),"B","1","3",
-		Integer.toString(REP.REPCMD_INSERT),"C","3","4",
-		Integer.toString(REP.REPCMD_DELETE),"d","13","5",
-		Integer.toString(REP.REPCMD_DELETE),"d","3","6",
-		Integer.toString(REP.REPCMD_DELETE),"d","1","7",
-		Integer.toString(REP.REPCMD_INSERT),"A","5","8",
-		Integer.toString(REP.REPCMD_DELETE),"d","1","9",
-		Integer.toString(REP.REPCMD_DELETE),"d","1","10",
-		Integer.toString(REP.REPCMD_INSERT),"B","10","11",
-		Integer.toString(REP.REPCMD_INSERT),"B","1","12",
-		Integer.toString(REP.REPCMD_INSERT),"C","3","13",
-		Integer.toString(REP.REPCMD_DELETE),"d","2","14",
-		Integer.toString(REP.REPCMD_DELETE),"d","3","15",
-		Integer.toString(REP.REPCMD_DELETE),"d","1","16",
-		Integer.toString(REP.REPCMD_INSERT),"A","3","17",
-		Integer.toString(REP.REPCMD_DELETE),"d","1","18"
+		Integer.toString(REP.REPCMD_DELETE.id),"d","1","1",
+		Integer.toString(REP.REPCMD_INSERT.id),"B","3","2",
+		Integer.toString(REP.REPCMD_INSERT.id),"B","1","3",
+		Integer.toString(REP.REPCMD_INSERT.id),"C","3","4",
+		Integer.toString(REP.REPCMD_DELETE.id),"d","13","5",
+		Integer.toString(REP.REPCMD_DELETE.id),"d","3","6",
+		Integer.toString(REP.REPCMD_DELETE.id),"d","1","7",
+		Integer.toString(REP.REPCMD_INSERT.id),"A","5","8",
+		Integer.toString(REP.REPCMD_DELETE.id),"d","1","9",
+		Integer.toString(REP.REPCMD_DELETE.id),"d","1","10",
+		Integer.toString(REP.REPCMD_INSERT.id),"B","10","11",
+		Integer.toString(REP.REPCMD_INSERT.id),"B","1","12",
+		Integer.toString(REP.REPCMD_INSERT.id),"C","3","13",
+		Integer.toString(REP.REPCMD_DELETE.id),"d","2","14",
+		Integer.toString(REP.REPCMD_DELETE.id),"d","3","15",
+		Integer.toString(REP.REPCMD_DELETE.id),"d","1","16",
+		Integer.toString(REP.REPCMD_INSERT.id),"A","3","17",
+		Integer.toString(REP.REPCMD_DELETE.id),"d","1","18"
 	};
 	
 	static private String[] text1d = {
--- a/test/TestUTF8.java	Thu Sep 11 15:08:06 2008 +0900
+++ b/test/TestUTF8.java	Thu Sep 11 17:23:27 2008 +0900
@@ -17,7 +17,7 @@
     	System.out.println("send command: " + command.toString());
     	ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*2);
     	buffer.clear();  // position = 0 
-    	buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid);
+    	buffer.putInt(command.cmd.id); buffer.putInt(command.sid); buffer.putInt(command.eid);
     	buffer.putInt(command.seq); buffer.putInt(command.lineno); 
     	System.out.println("Plane: Set REPComand textlen(Byte) : " + command.string.length()*2);  
     	buffer.putInt(command.string.length()*2); 
@@ -31,7 +31,7 @@
 	public static ByteBuffer packUConv(REPCommand command){		
     	ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*3);
     	buffer.clear();  // position = 0 
-    	buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid);
+    	buffer.putInt(command.cmd.id); buffer.putInt(command.sid); buffer.putInt(command.eid);
     	buffer.putInt(command.seq); buffer.putInt(command.lineno); 
 
     	int pos = buffer.position();