# HG changeset patch # User pin # Date 1219840281 -32400 # Node ID abaf502e6d8fa1fce29c8bb576977a4b38b20c8d # Parent 6f15a8880ed8a254c4d834c1bc9c35d52e8aafb8 *** empty log message *** diff -r 6f15a8880ed8 -r abaf502e6d8f rep/Editor.java --- 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 myChannel; - private REPSocketChannel nextChannel; private String host; private String port; - //public int getEID; private String file; - private LinkedList undoCommandList = new LinkedList(); - private LinkedList temp = new LinkedList(); + private TranslaterImp1 translater; + private List sentList; public Editor(int editorNo, REPSocketChannel channel){ this.eid = editorNo; this.myChannel = channel; + translater = new TranslaterImp1(eid); + sentList = new LinkedList(); } - + public Editor(REPSocketChannel channel) { this.myChannel = channel; setHostAndPort(myChannel); } + + public LinkedList translate(REPCommand command){ + LinkedList list = new LinkedList(); + 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 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); - } - }