Mercurial > hg > RemoteEditor > Eclipse
changeset 18:e87cae12907e
*** empty log message ***
author | pin |
---|---|
date | Tue, 31 Oct 2006 12:11:11 +0900 |
parents | b409b85ab73f |
children | cc68a00f9850 |
files | bin/remoteeditor/editors/RemoteEditor$1.class bin/remoteeditor/editors/RemoteEditor.class bin/remoteeditor/network/REP.class src/remoteeditor/editors/RemoteEditor.java src/remoteeditor/network/REP.java src/sample/merge/Rep_Cmd.java |
diffstat | 6 files changed, 99 insertions(+), 47 deletions(-) [+] |
line wrap: on
line diff
--- a/src/remoteeditor/editors/RemoteEditor.java Tue Oct 24 19:14:11 2006 +0900 +++ b/src/remoteeditor/editors/RemoteEditor.java Tue Oct 31 12:11:11 2006 +0900 @@ -1,5 +1,7 @@ package remoteeditor.editors; +import java.io.IOException; + import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextListener; @@ -49,7 +51,7 @@ textOffset = event.getOffset(); System.out.println("replace = " + replacedText); - System.out.println("input = " + inputText); + System.out.println("input = " + inputText + " : " + inputText.length()); int line = 0; int offset; @@ -63,16 +65,33 @@ } catch (BadLocationException e1) { e1.printStackTrace(); } - - try { - rep.insert(line, length, lineText); //insert, delete replace を 行数で判断 - } catch (Exception e) { - e.printStackTrace(); + if(inputText.contains("\n")){ + try { + rep.insert(line+1, length, "\n"); //insert, delete, replace を 行数で判断 + } catch (Exception e) { + e.printStackTrace(); + } + }else if(inputText.length() != 0){ + try { + rep.replace(line, length, lineText); + } catch (IOException e) { + e.printStackTrace(); + } + }else if(!replacedText.equals("\n")){ + try { + rep.replace(line, length, lineText); + } catch (IOException e) { + e.printStackTrace(); + } + }else { + try { + rep.delete(line, length, lineText); + } catch (IOException e) { + e.printStackTrace(); + } } - } - public void packetReceived(RSocketEvent evt) { final int offset = evt.getLineNo(); final int length = evt.getLength();
--- a/src/remoteeditor/network/REP.java Tue Oct 24 19:14:11 2006 +0900 +++ b/src/remoteeditor/network/REP.java Tue Oct 31 12:11:11 2006 +0900 @@ -26,18 +26,22 @@ int textsiz; public static final int REP_INSERT_CMD = 6; - static final int REP_INSERT_ACK_CMD = 7; - static final int REP_JOIN_CMD = 41; - static final int REP_JOIN_ACK_CMD = 42; - static final int REP_PUT_CMD = 45; - static final int REP_PUT_ACK_CMD = 46; - static final int REP_SELECT_CMD = 47; - static final int REP_SELECT_ACK_CMD = 48; - static final int REP_QUIT_CMD = 53; - static final int REP_QUIT_ACK_CMD = 54; + public static final int REP_INSERT_ACK_CMD = 7; + public static final int REP_DELETE_CMD = 9; + public static final int REP_DELETE_ACK_CMD = 10; + public static final int REP_REPLACE_CMD = 13; + public static final int REP_REPLACE_ACK_CMD = 14; + public static final int REP_JOIN_CMD = 41; + public static final int REP_JOIN_ACK_CMD = 42; + public static final int REP_PUT_CMD = 45; + public static final int REP_PUT_ACK_CMD = 46; + public static final int REP_SELECT_CMD = 47; + public static final int REP_SELECT_ACK_CMD = 48; + public static final int REP_QUIT_CMD = 53; + public static final int REP_QUIT_ACK_CMD = 54; - ByteBuffer buffer = ByteBuffer.allocateDirect(1024); - ByteBuffer read_buffer = ByteBuffer.allocateDirect(1024); + //ByteBuffer buffer = ByteBuffer.allocateDirect(1024); + //ByteBuffer read_buffer = ByteBuffer.allocateDirect(1024); String string; private RSocketListener socketListener; @@ -65,7 +69,6 @@ InetSocketAddress addr = new InetSocketAddress(host, port); - //SocketChannel sc; sc = SocketChannel.open(); sc.configureBlocking(true); sc.connect(addr); @@ -90,38 +93,62 @@ } public void join() throws IOException{ - sc.write(pack(buffer, REP_JOIN_CMD, sid, eid, seqid, lineno, "afro")); - unpack(read_buffer); + sc.write(pack( REP_JOIN_CMD, sid, eid, seqid, lineno, "afro")); + unpack(); System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string); } public void put() throws Exception { - sc.write(pack(buffer, REP_PUT_CMD, sid, eid, seqid, lineno, "filename")); - unpack(read_buffer); + sc.write(pack(REP_PUT_CMD, sid, eid, seqid, lineno, "filename")); + unpack(); System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string); } public void select() throws Exception { - sc.write(pack(buffer, REP_SELECT_CMD, sid, eid, seqid, lineno, "afro")); - unpack(read_buffer); + sc.write(pack(REP_SELECT_CMD, sid, eid, seqid, lineno, "afro")); + unpack(); System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string); } public void insert(int offset, int length, String text) throws IOException { + cmd = REP_INSERT_CMD; seqid = (byte)offset; - sc.write(pack(buffer, REP_INSERT_CMD, sid, eid, seqid, lineno, text)); + sc.write(pack(cmd, sid, eid, seqid, lineno, text)); try { - myCmdList.add(new Rep_Cmd(cmd, sid, eid, seqid, lineno, textsiz, text)); + myCmdList.add(new Rep_Cmd(cmd, sid, eid, seqid, lineno, length, text)); + System.out.println("myCmdList : " + myCmdList.toString()); } catch (Exception e) { e.printStackTrace(); } } - public ByteBuffer pack(ByteBuffer buffer,int cmd, int sid, int eid, int seqid, int lineno, String text ) { - + public void replace(int offset, int length, String text) throws IOException { + cmd = REP_REPLACE_CMD; + seqid = (byte)offset; + sc.write(pack(cmd, sid, eid, seqid, lineno, text)); + try { + myCmdList.add(new Rep_Cmd(cmd, sid, eid, seqid, lineno, length, text)); + System.out.println("myCmdList : " + myCmdList.toString()); + } catch (Exception e) { + e.printStackTrace(); + } + } + public void delete(int offset, int length, String text) throws IOException { + cmd = REP_DELETE_CMD; + seqid = (byte)offset; + sc.write(pack(cmd, sid, eid, seqid, lineno, text)); + try { + myCmdList.add(new Rep_Cmd(cmd, sid, eid, seqid, lineno, length, text)); + System.out.println("myCmdList : " + myCmdList.toString()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public ByteBuffer pack(int cmd, int sid, int eid, int seqid, int lineno, String text ) { - //ByteBuffer buffer = ByteBuffer.allocateDirect(24+textsiz); + ByteBuffer buffer = ByteBuffer.allocateDirect(24+text.length()*2); buffer.clear(); // position = 0 buffer.putInt(cmd); buffer.putInt(sid); buffer.putInt(eid); buffer.putInt(seqid); buffer.putInt(lineno); @@ -140,12 +167,13 @@ static final int HEADER_SIZE = 24; //ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE); - public void unpack(ByteBuffer buffer) throws IOException{ + public void unpack() throws IOException{ ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE); long len; header.clear(); len = sc.read(header); // limit = read length if (len !=HEADER_SIZE) { + System.out.println("てす"); // this can't happen } header.rewind(); // position = 0 @@ -161,24 +189,20 @@ lineno = header.getInt(); textsiz = header.getInt()/2; - ByteBuffer testBuffer = ByteBuffer.allocateDirect(textsiz*2); + ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz*2); //buffer.limit(textsiz*2); //buffer.rewind(); - len = sc.read(testBuffer); // limit = read length + len = sc.read(textBuffer); // limit = read length if (len !=HEADER_SIZE) { // this can't happen } - testBuffer.rewind(); + textBuffer.rewind(); for(int i=0;i<textsiz;i++) { - text +=testBuffer.getChar(); + text +=textBuffer.getChar(); } - /*for(int i = 0; i < textsiz; i++){ - readbyte[i] = buffer.get(); - } - string = new String(readbyte, 0, textsiz);*/ string = text; try { - othersCmdList.add(new Rep_Cmd(cmd, sid, eid, seqid, lineno, textsiz, textBuffer)); + othersCmdList.add(new Rep_Cmd(cmd, sid, eid, seqid, lineno, textsiz, text)); } catch (Exception e) { e.printStackTrace(); } @@ -194,12 +218,12 @@ while(sc.isConnected()){ try { - unpack(buffer); + unpack(); } catch (IOException e) { e.printStackTrace(); } System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string); - socketListener.packetReceived(new RSocketEvent(cmd, sid, eid, seqid, lineno, textsiz, string)); // + //socketListener.packetReceived(new RSocketEvent(cmd, sid, eid, seqid, lineno, textsiz, string)); // } } @@ -211,4 +235,5 @@ e.printStackTrace(); } } + }
--- a/src/sample/merge/Rep_Cmd.java Tue Oct 24 19:14:11 2006 +0900 +++ b/src/sample/merge/Rep_Cmd.java Tue Oct 31 12:11:11 2006 +0900 @@ -1,5 +1,7 @@ package sample.merge; +import java.nio.ByteBuffer; + public class Rep_Cmd { public int cmd; public int sid; @@ -10,19 +12,25 @@ public int stat; public String string; + private int textsiz; - public Rep_Cmd(int cmd,int sid,int eid, int seq, int len, int lineno, String string) throws Exception { + public Rep_Cmd(int cmd,int sid,int eid, int seq, int lineno, int textsiz, String string) throws Exception { this.cmd = cmd; this.sid = sid; this.eid = eid; this.seq = seq; - this.len = len; + this.textsiz = textsiz; this.lineno = lineno; this.string = string; } - + public String toString(){ - return string; + String[] cmdString = new String[14]; + cmdString[6] = "REP_INSERT_CMD"; + cmdString[9] = "REP_DELETE_CMD"; + cmdString[13] = "REP_REPLACE_CMD"; + String repCmdString = new String(cmdString[cmd] + "," + sid + "," + eid + "," + seq + "," + lineno + "," + textsiz + "," + string); + return repCmdString; }