Mercurial > hg > RemoteEditor > Eclipse
view src/remoteeditor/network/REP.java @ 31:ebbc14dd1a1a
*** empty log message ***
author | pin |
---|---|
date | Tue, 16 Jan 2007 13:55:38 +0900 |
parents | 5723cfd85c95 |
children | 86e04721e463 |
line wrap: on
line source
package remoteeditor.network; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; import java.util.LinkedList; import java.util.List; import org.eclipse.jface.dialogs.InputDialog; import org.eclipse.jface.window.Window; import org.eclipse.swt.widgets.Shell; import sample.merge.Rep_Cmd; import sample.merge.Translate; public class REP implements Runnable{ SocketChannel sc; int cmd; int eid; int eid2; int lineno; int sid; int seqid; int textsiz; public static final int REP_INSERT_CMD = 6; 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; static final int HEADER_SIZE = 24; String string; public RSocketListener socketListener; Shell shell; List <Rep_Cmd> myCmdList = new LinkedList<Rep_Cmd>(); List <Rep_Cmd> othersCmdList = new LinkedList<Rep_Cmd>(); private String filename; public REP() { String host = "localhost"; int port = 8765; InputDialog dialog = new InputDialog(shell, "REP", "host", "localhost", null); if(dialog.open() == Window.OK){ host = dialog.getValue(); } dialog = new InputDialog(shell, "REP", "port", "8756", null); if (dialog.open() == Window.OK) { try { port = Integer.parseInt(dialog.getValue()); } catch (NumberFormatException x) { } } InetSocketAddress addr = new InetSocketAddress(host, port); try { sc = SocketChannel.open(); sc.configureBlocking(true); sc.connect(addr); while(!sc.finishConnect()){ System.out.println("afro"); } join(); } catch (IOException e) { e.printStackTrace(); } dialog = new InputDialog(shell, "repput", "read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string, "hugo", null); if(dialog.open() == Window.OK){ filename = dialog.getValue(); put(); } dialog = new InputDialog(shell, "repselect", "read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string ,Integer.toString(sid), null); if(dialog.open() == Window.OK){ try { sid = (byte) Integer.parseInt(dialog.getValue()); } catch (NumberFormatException x) { } select(); } } public void join() { rWrite(pack( REP_JOIN_CMD, sid, eid, seqid, lineno, "afro")); unpack(); eid2 = eid; System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string); } public void put() { rWrite(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() { rWrite(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) { cmd = REP_INSERT_CMD; seqid = (byte)offset; try { sc.write(pack(cmd, sid, eid, seqid, lineno, text)); } catch (IOException e1) { e1.printStackTrace(); } 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 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+text.length()*2); buffer.clear(); // position = 0 buffer.putInt(cmd); buffer.putInt(sid); buffer.putInt(eid); buffer.putInt(seqid); buffer.putInt(lineno); buffer.putInt(text.length()*2); //buffer.putString(text); //System.out.println("pack:" + cmd +", "+ "length="+ text.length() +" "+ sid +", "+ eid +", "+ seqid +", "+ lineno); for(int i=0;i<text.length();i++) { buffer.putChar(text.charAt(i)); } buffer.flip(); // limit = current position, position = 0 return buffer; } public void unpack() { ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE); long len = 0; header.clear(); try { len = sc.read(header); } catch (IOException e1) { e1.printStackTrace(); } // limit = read length if (len !=HEADER_SIZE) { System.out.println("‚Ä‚·"); // this can't happen } header.rewind(); // position = 0 /*for(int i = 0; i < 24; i++){ readbyte[i] = header.get(); //System.out.println(readbyte[i]); }*/ String text = ""; cmd = header.getInt(); sid = header.getInt(); eid = header.getInt(); seqid = header.getInt(); lineno = header.getInt(); textsiz = header.getInt()/2; ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz*2); //buffer.limit(textsiz*2); //buffer.rewind(); try { len = sc.read(textBuffer); } catch (IOException e1) { e1.printStackTrace(); } // limit = read length if (len !=HEADER_SIZE) { // this can't happen } textBuffer.rewind(); for(int i=0;i<textsiz;i++) { text +=textBuffer.getChar(); } string = text; try { if(eid2 != eid) othersCmdList.add(new Rep_Cmd(cmd, sid, eid, seqid, lineno, textsiz, text)); } catch (Exception e) { e.printStackTrace(); } } public void addSocketListener(RSocketListener socketListener){ Thread th = new Thread(this); th.start(); this.socketListener = socketListener; } public void run() { while(sc.isConnected()){ unpack(); System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string); for(Rep_Cmd othersCmd:othersCmdList){ socketListener.packetReceived(new RSocketEvent(othersCmd.cmd, othersCmd.sid, othersCmd.eid, othersCmd.seq, othersCmd.lineno, othersCmd.string.length(), othersCmd.string)); othersCmdList.remove(othersCmd); } //socketListener.packetReceived(new RSocketEvent(cmd, sid, eid, seqid, lineno, textsiz, string)); // } } public void rRead(ByteBuffer buffer){ try { sc.read(buffer); } catch (IOException e) { e.printStackTrace(); } } public void rWrite(ByteBuffer buffer){ try { sc.write(buffer); } catch (IOException e) { e.printStackTrace(); } } public void dispose() { try { sc.close(); } catch (IOException e) { e.printStackTrace(); } } public void sendCmd(int cmd2, int line, int length, String lineText) { //cmd = REP_INSERT_CMD; //rWrite(pack(cmd2, sid, eid, seqid, line, lineText)); try { myCmdList.add(new Rep_Cmd(cmd2, sid, eid, seqid, line, length, lineText)); //System.out.println("myCmdList : " + myCmdList.toString()); //System.out.println("othersCmdList : " + othersCmdList.toString()); new Translate(myCmdList, othersCmdList); for(Rep_Cmd userCmd:myCmdList){ rWrite(pack(userCmd.cmd, userCmd.sid, userCmd.eid, userCmd.seq, userCmd.lineno, userCmd.string)); myCmdList.remove(userCmd); } } catch (Exception e) { e.printStackTrace(); } } }