changeset 142:abaf502e6d8f

*** empty log message ***
author pin
date Wed, 27 Aug 2008 21:31:21 +0900
parents 6f15a8880ed8
children 785a3e8ea858
files rep/Editor.java
diffstat 1 files changed, 60 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/rep/Editor.java	Wed Aug 27 20:43:44 2008 +0900
+++ b/rep/Editor.java	Wed Aug 27 21:31:21 2008 +0900
@@ -1,33 +1,75 @@
 package rep;
 
-import java.nio.channels.SocketChannel;
+import java.io.IOException;
+import java.nio.ByteBuffer;
 import java.util.LinkedList;
-import java.util.StringTokenizer;
+import java.util.List;
 
 import rep.channel.REPSocketChannel;
+import rep.translater.TranslaterImp1;
 
 public class Editor {
 	private int eid;
 	private REPSocketChannel<REPCommand> myChannel;
-	private REPSocketChannel<REPCommand> nextChannel;
 	private String host;
 	private String port;
-	//public int getEID;
 	private String file;
-	private LinkedList<REPCommand> undoCommandList = new LinkedList<REPCommand>();
-	private LinkedList<Integer> temp = new LinkedList<Integer>();
+	private TranslaterImp1 translater;
+	private List<REPCommand> sentList;
 
 	public Editor(int editorNo, REPSocketChannel<REPCommand> channel){
 		this.eid = editorNo;
 		this.myChannel = channel;
+		translater = new TranslaterImp1(eid);
+		sentList = new LinkedList<REPCommand>();
 	}
-
+	
 	public Editor(REPSocketChannel<REPCommand> channel) {
 		this.myChannel = channel;
 		setHostAndPort(myChannel);
 	}
+	
+	public LinkedList<REPCommand> translate(REPCommand command){
+		LinkedList<REPCommand> list = new LinkedList<REPCommand>();
+		if(command.eid == eid){
+			if(checkReturnedCommand(command)){
+				REPCommand[] cmds = translater.catchOwnCommand(command);
+				sendMergedCommand(cmds);
+				return null;
+			}else{
+				sentList.add(command);
+				translater.transSendCmd(command);
+				list.add(command);
+			}
+		}else{
+			REPCommand[] cmds = translater.transReceiveCmd(command);
+			for(REPCommand cmd : cmds){
+				list.add(cmd);
+			}
+		}
+		return list;
+	}
+	
+	private void sendMergedCommand(REPCommand[] cmds) {
+		for(int i = 0; i < cmds.length; i++){
+			try {
+				myChannel.write(pack(cmds[i]));
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+		}
+	}
 
-	public Editor() {
+	private boolean checkReturnedCommand(REPCommand command) {
+		if(sentList.size() > 0){
+			if(sentList.get(0).seq == command.seq){
+				sentList.remove(0);
+				return true;
+			}else{
+				System.out.println("error:Editor.checkReturnedCommand()");
+			}
+		}
+		return false;
 	}
 
 	private void setHostAndPort(REPSocketChannel<REPCommand> myChannel2) {
@@ -80,41 +122,20 @@
 	}
 
 	public void send(REPCommand repCmd) {
-		REPPacketSend send = new REPPacketSend(myChannel);
-		send.send(repCmd);
+		try {
+			myChannel.write(pack(repCmd));
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+	private ByteBuffer pack(REPCommand repCmd) {
+		// TODO Auto-generated method stub
+		return null;
 	}
 
 	public void setChannel(REPSocketChannel channel) {
 		myChannel = channel;
 	}
 
-	public void addUndoCommand(REPCommand command) {
-		if(command.cmd == REP.SMCMD_GET_UNDO_ACK){
-			command.setCMD((temp.get(0)).intValue());
-			temp.remove();
-		}
-		
-		undoCommandList.addFirst(command);
-		System.out.println(undoCommandList);
-//		if(undoCommandList.size() > 10){
-//			for(REPCommand undoCommand : undoCommandList){
-//				send(undoCommand);
-//			}
-//			undoCommandList.clear();
-//		}
-	}
-
-	public void undo() {
-		// TODO Auto-generated method stub
-		for(REPCommand undoCommand : undoCommandList){
-			send(undoCommand);
-		}
-		undoCommandList.clear();
-	}
-
-	public void setKindOfUndoCmd(int cmd) {
-		// TODO Auto-generated method stub
-		temp .add(cmd);
-	}
-
 }