# HG changeset patch # User kent # Date 1219995969 -32400 # Node ID d22384c0026c6fd902d459a75c66fa6644db5ac7 # Parent c2c47d7675a8adb14ebb4b700e8732a7518c1295 *** empty log message *** diff -r c2c47d7675a8 -r d22384c0026c rep/REPCommandPacker.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/REPCommandPacker.java Fri Aug 29 16:46:09 2008 +0900 @@ -0,0 +1,114 @@ +package rep; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.channels.SocketChannel; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; + +import rep.channel.REPPack; + + +public class REPCommandPacker implements REPPack { + // JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5 + private final int HEADER_SIZE = 24; + final int CHAR_ORDER = 5; + + + /* (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(HEADER_SIZE+(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(HEADER_SIZE+length); + buffer.rewind(); + + return buffer; + } + + + public REPCommand unpackUConv(SocketChannel sc) throws IOException { + ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE); + long len = 0; + header.clear(); + len = sc.read(header); + if(len <= 0){ + return null; + } + if (len !=HEADER_SIZE) { + throw new IOException(); + } + 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(); + + ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz); + + len = sc.read(textBuffer); + if(len <= 0){ + return null; + } + if (len != textsiz) { + throw new IOException(); + } + 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(); + + REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string); + + return repcommand; + } + + +} diff -r c2c47d7675a8 -r d22384c0026c rep/REPPacketReceive.java --- a/rep/REPPacketReceive.java Fri Aug 29 16:44:39 2008 +0900 +++ b/rep/REPPacketReceive.java Fri Aug 29 16:46:09 2008 +0900 @@ -10,24 +10,19 @@ import rep.channel.REPSocketChannel; -import rep.channel.REPUnpack; public class REPPacketReceive implements REPUnpack { - REPSocketChannel socketchannel; private final int HEADER_SIZE = 24; private boolean debug=false; - public REPPacketReceive(REPSocketChannel sc){ - socketchannel = sc; - } // このクラスはシミュレーションの時には生成されないので、SocketChannelでいいんだよ REPSocketChannelにしない public REPCommand unpackUConv(SocketChannel sc) throws IOException { ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE); long len = 0; header.clear(); - len = socketchannel.read(header); + len = sc.read(header); if(len <= 0){ return null; } @@ -45,7 +40,7 @@ ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz); - len = socketchannel.read(textBuffer); + len = sc.read(textBuffer); if(len <= 0){ return null; } diff -r c2c47d7675a8 -r d22384c0026c rep/REPPacketSend.java --- a/rep/REPPacketSend.java Fri Aug 29 16:44:39 2008 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,74 +0,0 @@ -package rep; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.charset.Charset; -import java.nio.charset.CharsetEncoder; - -import rep.channel.REPPack; -import rep.channel.REPSocketChannel; - - -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 c2c47d7675a8 -r d22384c0026c rep/SessionManager.java --- a/rep/SessionManager.java Fri Aug 29 16:44:39 2008 +0900 +++ b/rep/SessionManager.java Fri Aug 29 16:46:09 2008 +0900 @@ -66,7 +66,7 @@ public void init(int port) throws InterruptedException, IOException { - REPServerSocketChannel ssc = REPServerSocketChannel.open(); + REPServerSocketChannel ssc = REPServerSocketChannel.open(new REPCommandPacker()); ssc.configureBlocking(false); //reuse address 必須 ssc.socket().setReuseAddress(true); ssc.socket().bind(new InetSocketAddress(port)); @@ -460,7 +460,7 @@ port = send_port; InetSocketAddress addr = new InetSocketAddress(host, port); try { - REPSocketChannel sessionchannel = REPSocketChannel.create(); + REPSocketChannel sessionchannel = REPSocketChannel.create(new REPCommandPacker()); sessionchannel.configureBlocking(true); sessionchannel.connect(addr); while(!sessionchannel.finishConnect()){ @@ -491,9 +491,7 @@ command.setString(string); //SM_JOINコマンドを送信。 - REPPacketSend send = new REPPacketSend(channel); - send.send(command); - + channel.write(command); //SessionManagerのListに追加。 smList.add(channel); } @@ -524,8 +522,7 @@ sendCommand.setCMD(REP.SMCMD_JOIN_ACK); sendCommand.setEID(editor.getEID()); sendCommand.setSID(sid); - REPPacketSend sender = new REPPacketSend(channel); - sender.send(sendCommand); + channel.write(sendCommand); }else { REPSocketChannel editorChannel = event.getEditorChannel(); sid = event.getSID();