Mercurial > hg > RemoteEditor > REPSessionManager
view rep/Editor.java @ 170:30cf7747d134
*** empty log message ***
author | pin |
---|---|
date | Thu, 28 Aug 2008 18:56:46 +0900 |
parents | 3841cc75b808 |
children | a097b1d619a1 |
line wrap: on
line source
package rep; import java.io.IOException; import java.nio.ByteBuffer; import java.util.LinkedList; import java.util.List; import rep.channel.REPSocketChannel; import rep.translater.TranslaterImp1; public class Editor { private int eid; private REPSocketChannel<REPCommand> myChannel; private String host; private String port; private String file; private TranslaterImp1 translater; private List<REPCommand> sentList; public Editor(){ setHostAndPort(myChannel); translater = new TranslaterImp1(eid); sentList = new LinkedList<REPCommand>(); } public Editor(int editorNo, REPSocketChannel<REPCommand> channel){ this.eid = editorNo; this.myChannel = channel; translater = new TranslaterImp1(eid); sentList = new LinkedList<REPCommand>(); setHostAndPort(myChannel); } public Editor(REPSocketChannel<REPCommand> channel) { this.myChannel = channel; setHostAndPort(myChannel); translater = new TranslaterImp1(eid); sentList = new LinkedList<REPCommand>(); } 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 { REPCommand mergeCommand = cmds[i]; mergeCommand.setEID(REP.MERGE_EID); myChannel.write(pack(mergeCommand)); } catch (IOException e) { e.printStackTrace(); } } } 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) { String socketString = myChannel2.socket().getRemoteSocketAddress().toString(); String[] split = socketString.split("/"); int length = split.length; String hostAndPort = split[length-1]; split = hostAndPort.split(":"); host = split[0]; port = split[1]; } public REPSocketChannel<REPCommand> getChannel() { return myChannel; } public void setHost(String host){ this.host = host; } public void setPort(String port){ this.port = port; } public String getHost(){ return host; } public String getPort(){ return port; } public int getEID() { return eid; } public void setEID(int eid) { this.eid = eid; } public String toString(){ return (host + ":" + port + ":" + file); } public String getName() { return file; } public void setName(String string) { file = string; } public void send(REPCommand 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 boolean isMerging() { return translater.isMerging(); } }