Mercurial > hg > RemoteEditor > Eclipse
changeset 206:563057fe244e
source code clean up
author | one |
---|---|
date | Sat, 18 Dec 2010 18:01:07 +0900 |
parents | 540d7a8a9e33 |
children | fe375c84d5d0 a853f36c238c |
files | src/remoteeditor/command/REPCommandEvent.java src/remoteeditor/network/REPPacketReceive.java src/remoteeditor/network/REPPacketSend.java src/remoteeditor/ui/REPSelectWindow.java |
diffstat | 4 files changed, 35 insertions(+), 203 deletions(-) [+] |
line wrap: on
line diff
--- a/src/remoteeditor/command/REPCommandEvent.java Sat Dec 18 17:35:25 2010 +0900 +++ b/src/remoteeditor/command/REPCommandEvent.java Sat Dec 18 18:01:07 2010 +0900 @@ -1,5 +1,7 @@ package remoteeditor.command; +import rep.REPCommand; + public class REPCommandEvent { REPCommand repcommand; public REPCommandEvent(REPCommand cmd){
--- a/src/remoteeditor/network/REPPacketReceive.java Sat Dec 18 17:35:25 2010 +0900 +++ b/src/remoteeditor/network/REPPacketReceive.java Sat Dec 18 18:01:07 2010 +0900 @@ -1,152 +1,23 @@ package remoteeditor.network; import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.channels.SelectionKey; import java.nio.channels.SocketChannel; -import java.nio.charset.CharacterCodingException; -import java.nio.charset.Charset; -import java.nio.charset.CharsetDecoder; -import remoteeditor.command.REPCommand; import remoteeditor.command.REPCommandEvent; import remoteeditor.command.REPCommandListener; +import rep.REPCommand; +import rep.REPCommandPacker; public class REPPacketReceive implements Runnable{ SocketChannel socketchannel; - private int HEADER_SIZE = 24; private REPCommandListener commandlistener; - + REPCommandPacker p = new REPCommandPacker(); public REPPacketReceive(SocketChannel sc){ socketchannel = sc; } - - private REPCommand unpack() { - //System.out.println("test1"); - ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE); - long len = 0; - header.clear(); - try { - len = socketchannel.read(header); - } catch (IOException e1) { - e1.printStackTrace(); - } // limit = read length - if (len !=HEADER_SIZE) { - System.out.println("this can't happen"); - // this can't happen - } - header.rewind(); // position = 0 - String text = ""; - int cmd = header.getInt(); - int sid = header.getInt(); - int eid = header.getInt(); - int seqid = header.getInt(); - int lineno = header.getInt(); - int textsiz = header.getInt()/2; - - ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz*2); - - try { - len = socketchannel.read(textBuffer); - } catch (IOException e1) { - e1.printStackTrace(); - } // limit = read length - if (len != textsiz * 2) { - // this can't happen - System.out.println("����"); - } - textBuffer.rewind(); - for(int i=0;i<textsiz;i++) { - text +=textBuffer.getChar(); - } - String string = text; - //System.out.println(string); - REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string); - System.out.println("received command: " + repcommand.toString()); - return repcommand; - } - - public REPCommand unpackUConv() { - ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE); - long len = 0; - header.clear(); - try { - len = socketchannel.read(header); - if(len == -1){ - socketchannel.close(); - return null; - }else if(len == 0){ - return null; - } - } catch (IOException e1) { - e1.printStackTrace(); - } // limit = read length - if (len !=HEADER_SIZE) { - System.out.println("てす"); - // this can't happen - } - header.rewind(); // position = 0 - - int cmd = header.getInt(); - int sid = header.getInt(); - int eid = header.getInt(); - int seqid = header.getInt(); - int lineno = header.getInt(); - int textsiz = header.getInt(); - //int tmptextsiz = header.getInt(); - //int textsiz = (tmptextsiz /5) + (tmptextsiz % 5); - - ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz); - - try { - len = socketchannel.read(textBuffer); - if(len == -1){ - socketchannel.close(); - return null; - } - } catch (IOException e1) { - e1.printStackTrace(); - } // limit = read length - if (len != textsiz) { - // this can't happen - System.out.println("あと"); - } - textBuffer.rewind(); - - //Decode UTF-8 to System Encoding(UTF-16) - Charset charset = Charset.forName("UTF-8"); - CharsetDecoder decoder = charset.newDecoder(); - CharBuffer cb = null; - try { - cb = decoder.decode(textBuffer); - } catch (CharacterCodingException e) { - e.printStackTrace(); - } - cb.rewind(); - - String string = cb.toString(); - - textsiz = string.length(); - if(textsiz > 2){ - System.out.println(string); - } - //System.out.println("CharBuffer size: " +cb.length()); - - //System.out.println("textsize: " +textsiz); - - //System.out.println(cb.toString()); - - REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string); - System.out.println("UnPack Packet: => cmd:"+cmd+" sid:"+sid+" eid:"+eid+"seqid:"+seqid+" lineno:"+lineno+" textsiz:" +textsiz+" text: "+string); - System.out.println("received command: " + repcommand.toString()); - - return repcommand; - } - public void addCommandListener(REPCommandListener listener){ commandlistener = listener; Thread th = new Thread(this); @@ -154,11 +25,19 @@ } public void run() { + while(socketchannel.isConnected()){ - //unpack(); -// commandlistener.CommandReceived(new REPCommandEvent(unpack())); - commandlistener.CommandReceived(new REPCommandEvent(unpackUConv())); + try { + commandlistener.CommandReceived(new REPCommandEvent(p.unpackUConv(socketchannel))); + } catch (IOException e) { + return; + } } } + + + public REPCommand unpackUConv() throws IOException { + return p.unpackUConv(socketchannel); + } }
--- a/src/remoteeditor/network/REPPacketSend.java Sat Dec 18 17:35:25 2010 +0900 +++ b/src/remoteeditor/network/REPPacketSend.java Sat Dec 18 18:01:07 2010 +0900 @@ -1,77 +1,25 @@ package remoteeditor.network; import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.CharBuffer; + import java.nio.channels.SocketChannel; -import java.nio.charset.Charset; -import java.nio.charset.CharsetEncoder; -import remoteeditor.command.REPCommand; +import rep.REPCommand; +import rep.REPCommandPacker; public class REPPacketSend { SocketChannel socketchannel; - private int CHAR_ORDER = 5; + REPCommandPacker p; public REPPacketSend(SocketChannel sc){ socketchannel = sc; } - private ByteBuffer pack(REPCommand command){ - //command.setString(command.string + ":temp:123456"); - System.out.println("send command: " + command.toString()); - ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*2); - buffer.clear(); // position = 0 - buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid); - buffer.putInt(command.seq); buffer.putInt(command.lineno); - buffer.putInt(command.string.length()*2); - for(int i=0;i<command.string.length();i++) { - buffer.putChar(command.string.charAt(i)); - } - buffer.flip(); // limit = current position, position = 0 - return buffer; - } - - private ByteBuffer packUConv(REPCommand command){ - System.out.println("send command byUTF8: " + command.toString()); - if(command.string == null){ - command.setString("test"); - } - ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string.length()*CHAR_ORDER )); - buffer.clear(); // position = 0 - buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid); - buffer.putInt(command.seq); buffer.putInt(command.lineno); - - int pos = buffer.position(); - buffer.putInt(0); - - //Encode to UTF8 - CharBuffer cb = CharBuffer.wrap(command.string); - Charset charset = Charset.forName("UTF-8"); - CharsetEncoder encoder = charset.newEncoder(); - try { - encoder.encode(cb, buffer, true); - } catch (IllegalStateException e) { - e.printStackTrace(); - } - - //Encoded string length set - int length = (buffer.position() -pos) -4; - System.out.println("UTF-8: Set REPComand textlen(Byte) : " + (buffer.position() - pos-4)); - if(length < 0) { - length = 0; - } - buffer.putInt(pos, length); - - buffer.limit(24+length); - buffer.rewind(); - - return buffer; - } public void send(REPCommand command){ + try { - socketchannel.write(packUConv(command)); + socketchannel.write(p.packUConv(command)); } catch (IOException e) { e.printStackTrace(); }
--- a/src/remoteeditor/ui/REPSelectWindow.java Sat Dec 18 17:35:25 2010 +0900 +++ b/src/remoteeditor/ui/REPSelectWindow.java Sat Dec 18 18:01:07 2010 +0900 @@ -1,7 +1,6 @@ package remoteeditor.ui; import java.io.IOException; -import java.net.InetSocketAddress; import java.nio.channels.SocketChannel; import java.util.StringTokenizer; @@ -13,10 +12,9 @@ import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; -import remoteeditor.command.REPCommand; +import rep.REPCommand; import rep.REP; import remoteeditor.network.REPPacketReceive; import remoteeditor.network.REPPacketSend; @@ -25,7 +23,6 @@ private Shell shell; private Combo combo; private Display display; - private SocketChannel channel; private REPPacketReceive repreceive; private REPPacketSend repsend; private String filename; @@ -107,12 +104,15 @@ } private void select() { - repsend.send(new REPCommand(REP.SMCMD_SELECT, mysid, myeid, myseq, 0, 0, "")); + repsend.send(new REPCommand(REP.SMCMD_SELECT, mysid, myeid, myseq, 0, "")); } private void put() { - repsend.send(new REPCommand(REP.SMCMD_PUT, mysid, myeid, myseq, 0, filename.length(), filename)); - command = repreceive.unpackUConv(); + repsend.send(new REPCommand(REP.SMCMD_PUT, mysid, myeid, myseq, 0, filename)); + try { + command = repreceive.unpackUConv(); + } catch (IOException e) { + } mysid = command.sid; } @@ -126,13 +126,16 @@ } public void setChannel(SocketChannel sc) { - this.channel = sc; repreceive = new REPPacketReceive(sc); repsend = new REPPacketSend(sc); } public void join(){ - repsend.send(new REPCommand(REP.SMCMD_JOIN, 0, 0, 0, 0, 0, "")); - command = repreceive.unpackUConv(); + repsend.send(new REPCommand(REP.SMCMD_JOIN, 0, 0, 0, 0, "")); + try { + command = repreceive.unpackUConv(); + } catch (IOException e) { + + } myeid = command.eid; //System.out.println(command.toString()); StringTokenizer tokenizer = new StringTokenizer(command.string, ",{} ");