# HG changeset patch # User kent # Date 1219847901 -32400 # Node ID 4ff68518e9ca5d04f20e855aedb6b4f8447f4bca # Parent 20beee6ca31a3590d8c717135452e85bfb52a61d *** empty log message *** diff -r 20beee6ca31a -r 4ff68518e9ca rep/REPPacketReceive.java --- a/rep/REPPacketReceive.java Wed Aug 27 23:14:39 2008 +0900 +++ b/rep/REPPacketReceive.java Wed Aug 27 23:38:21 2008 +0900 @@ -3,54 +3,36 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; -import java.nio.channels.SelectableChannel; 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 java.util.LinkedList; -import java.util.StringTokenizer; + -import rep.channel.ChannelSimulator; import rep.channel.REPSocketChannel; +import rep.channel.REPUnpack; -public class REPPacketReceive { +public class REPPacketReceive implements REPUnpack { REPSocketChannel socketchannel; private final int HEADER_SIZE = 24; - SelectionKey key; + private boolean debug=false; public REPPacketReceive(REPSocketChannel sc){ socketchannel = sc; } - public void setkey(SelectionKey key) { - this.key = key; - } - - public REPCommand unpackUConv() { + public REPCommand unpackUConv(REPSocketChannel sc) throws IOException { ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE); long len = 0; header.clear(); - try { - len = socketchannel.read(header); - if(len == -1){ - if(key != null){ - key.cancel(); - } - socketchannel.close(); - return null; - }else if(len == 0){ - return null; - } - } catch (IOException e1) { - e1.printStackTrace(); - } // limit = read length + len = socketchannel.read(header); + if(len <= 0){ + return null; + } if (len !=HEADER_SIZE) { - System.out.println("error."); - // this can't happen + throw new IOException(); } header.rewind(); // position = 0 @@ -63,21 +45,12 @@ ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz); - try { - len = socketchannel.read(textBuffer); - if(len == -1){ - if(key != null){ - key.cancel(); - } - socketchannel.close(); - return null; - } - } catch (IOException e1) { - e1.printStackTrace(); - } // limit = read length + len = socketchannel.read(textBuffer); + if(len <= 0){ + return null; + } if (len != textsiz) { - // this can't happen - System.out.println("error."); + throw new IOException(); } textBuffer.rewind(); @@ -97,8 +70,11 @@ textsiz = string.length(); 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()); + + if (debug){ + 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; } diff -r 20beee6ca31a -r 4ff68518e9ca rep/REPPacketSend.java --- a/rep/REPPacketSend.java Wed Aug 27 23:14:39 2008 +0900 +++ b/rep/REPPacketSend.java Wed Aug 27 23:38:21 2008 +0900 @@ -3,14 +3,14 @@ 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 rep.channel.REPPack; import rep.channel.REPSocketChannel; -public class REPPacketSend { +public class REPPacketSend implements REPPack { REPSocketChannel socketchannel; // JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5 final int CHAR_ORDER = 5; @@ -19,23 +19,9 @@ socketchannel = channel; } - private ByteBuffer pack(REPCommand command){ - System.out.println("send command: " + command.toString()); - if(command.string == null){ - command.setString("test"); - } - 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 { + + public abstract ByteBuffer packUConv(P command); + + public abstract void send(P command); + +} \ No newline at end of file diff -r 20beee6ca31a -r 4ff68518e9ca rep/channel/REPPacketSend.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/channel/REPPacketSend.java Wed Aug 27 23:38:21 2008 +0900 @@ -0,0 +1,74 @@ +package rep.channel; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; + +import rep.REPCommand; +import rep.channel.REPPack; + + +public class REPPacketSend implements REPPack { + REPSocketChannel socketchannel; + // JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5 + final int CHAR_ORDER = 5; + + public REPPacketSend(REPSocketChannel channel){ + socketchannel = channel; + } + + /* (non-Javadoc) + * @see rep.REPPack#packUConv(rep.REPCommand) + */ + public 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; + } + + /* (non-Javadoc) + * @see rep.REPPack#send(rep.REPCommand) + */ + public void send(REPCommand command){ + try { + //socketchannel.write(pack(command)); + socketchannel.write(packUConv(command)); + //System.out.println(command.toString()); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff -r 20beee6ca31a -r 4ff68518e9ca rep/channel/REPSocketChannel.java --- a/rep/channel/REPSocketChannel.java Wed Aug 27 23:14:39 2008 +0900 +++ b/rep/channel/REPSocketChannel.java Wed Aug 27 23:38:21 2008 +0900 @@ -14,10 +14,20 @@ public class REPSocketChannel

extends SelectableChannel{ private SocketChannel sc; + private REPUnpack

unpack; + private REPPack

pack; public REPSocketChannel(SocketChannel channel) { sc = channel; } + + public void setUnpack(REPUnpack

_unpack){ + unpack = _unpack; + } + public void setPack(REPPack

_pack){ + pack = _pack; + } + @Override public Object blockingLock() { @@ -84,14 +94,19 @@ return null; } - public P read(){ - // TODO - return null; - + public P read() throws IOException{ + return unpack.unpackUConv(this); } public boolean write(P p){ - return false; - // + ByteBuffer bb = pack.packUConv(p); + try { + while (bb.remaining() > 0 ){ + sc.write(bb); + } + return true; + } catch (IOException e) { + return false; + } } public static REPSocketChannel create() throws IOException { diff -r 20beee6ca31a -r 4ff68518e9ca rep/channel/REPUnpack.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/channel/REPUnpack.java Wed Aug 27 23:38:21 2008 +0900 @@ -0,0 +1,9 @@ +package rep.channel; + +import java.io.IOException; + +public interface REPUnpack

{ + + public P unpackUConv(REPSocketChannel

sc) throws IOException; + +} \ No newline at end of file