annotate rep/REPCommandPacker.java @ 295:6deb6de8d0eb

*** empty log message ***
author pin
date Mon, 29 Sep 2008 14:13:19 +0900
parents 5b7abc22e61a
children 7107faaf3feb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
186
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
1 package rep;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
2
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
3 import java.io.IOException;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
4 import java.nio.ByteBuffer;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
5 import java.nio.CharBuffer;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
6 import java.nio.channels.SocketChannel;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
7 import java.nio.charset.CharacterCodingException;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
8 import java.nio.charset.Charset;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
9 import java.nio.charset.CharsetDecoder;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
10 import java.nio.charset.CharsetEncoder;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
11
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
12 import rep.channel.REPPack;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
13
224
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
14 /*
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
15 //+-------+--------+--------+-------+--------+---------+------+
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
16 //| cmd | session| editor | seqid | lineno | textsiz | text |
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
17 //| | id | id | | | | |
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
18 //+-------+--------+--------+-------+--------+---------+------+
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
19 //o-------header section (network order)-------------o
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
20 int cmd; // command
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
21 int sid; // session ID : uniqu to editing file
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
22 int eid; // editor ID : owner editor ID = 1。Session に対して unique
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
23 int seqno; // Sequence number : sequence number はエディタごとに管理
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
24 int lineno; // line number
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
25 int textsize; // textsize : bytesize
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
26 byte[] text;
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
27 */
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
28
186
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
29
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
30 public class REPCommandPacker implements REPPack<REPCommand> {
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
31 // JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
32 private final int HEADER_SIZE = 24;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
33 final int CHAR_ORDER = 5;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
34
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
35
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
36 /* (non-Javadoc)
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
37 * @see rep.REPPack#packUConv(rep.REPCommand)
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
38 */
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
39 public ByteBuffer packUConv(REPCommand command){
295
6deb6de8d0eb *** empty log message ***
pin
parents: 271
diff changeset
40 //System.out.println("send command byUTF8: " + command.toString());
186
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
41 if(command.string == null){
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
42 command.setString("test");
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
43 }
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
44 ByteBuffer buffer = ByteBuffer.allocateDirect(HEADER_SIZE+(command.string.length()*CHAR_ORDER));
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
45 buffer.clear(); // position = 0
271
kono
parents: 267
diff changeset
46 buffer.putInt(command.cmd.id);
224
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
47 buffer.putInt(command.sid);
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
48 buffer.putInt(command.eid);
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
49 buffer.putInt(command.seq);
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
50 buffer.putInt(command.lineno);
186
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
51
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
52 int pos = buffer.position();
224
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
53 buffer.putInt(0);
186
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
54
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
55 //Encode to UTF8
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
56 CharBuffer cb = CharBuffer.wrap(command.string);
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
57 Charset charset = Charset.forName("UTF-8");
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
58 CharsetEncoder encoder = charset.newEncoder();
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
59 try {
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
60 encoder.encode(cb, buffer, true);
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
61 } catch (IllegalStateException e) {
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
62 e.printStackTrace();
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
63 }
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
64
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
65 //Encoded string length set
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
66 int length = (buffer.position() -pos) -4;
295
6deb6de8d0eb *** empty log message ***
pin
parents: 271
diff changeset
67 //System.out.println("UTF-8: Set REPComand textlen(Byte) : " + (buffer.position() - pos-4));
186
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
68 if(length < 0) {
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
69 length = 0;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
70 }
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
71 buffer.putInt(pos, length);
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
72
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
73 buffer.limit(HEADER_SIZE+length);
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
74 buffer.rewind();
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
75
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
76 return buffer;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
77 }
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
78
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
79
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
80 public REPCommand unpackUConv(SocketChannel sc) throws IOException {
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
81 ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
82 header.clear();
224
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
83 /*
186
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
84 len = sc.read(header);
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
85 if(len <= 0){
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
86 return null;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
87 }
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
88 if (len !=HEADER_SIZE) {
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
89 throw new IOException();
224
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
90 } 下のwhileループで OK ? */
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
91 while(header.remaining()>0){
267
c513cf1ce9cc call handle.cancel() on socket channel close.
kono
parents: 252
diff changeset
92 if (sc.read(header)<0) throw new IOException();
224
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
93 } // 壊れたパケットに対してはブロックする可能性あり まぁTCPだしいいか?
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
94
186
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
95 header.rewind(); // position = 0
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
96
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
97 int cmd = header.getInt();
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
98 int sid = header.getInt();
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
99 int eid = header.getInt();
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
100 int seqid = header.getInt();
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
101 int lineno = header.getInt();
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
102 int textsiz = header.getInt();
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
103
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
104 ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz);
224
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
105
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
106 /*
186
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
107 len = sc.read(textBuffer);
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
108 if(len <= 0){
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
109 return null;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
110 }
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
111 if (len != textsiz) {
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
112 throw new IOException();
224
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
113 }*/
6b0dd92b8e45 add optimizer to Editor
kent
parents: 186
diff changeset
114 while(textBuffer.remaining()>0){
267
c513cf1ce9cc call handle.cancel() on socket channel close.
kono
parents: 252
diff changeset
115 if (sc.read(textBuffer)<0) throw new IOException();
186
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
116 }
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
117 textBuffer.rewind();
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
118
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
119 //Decode UTF-8 to System Encoding(UTF-16)
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
120 Charset charset = Charset.forName("UTF-8");
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
121 CharsetDecoder decoder = charset.newDecoder();
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
122 CharBuffer cb = null;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
123 try {
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
124 cb = decoder.decode(textBuffer);
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
125 } catch (CharacterCodingException e) {
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
126 e.printStackTrace();
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
127 }
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
128 cb.rewind();
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
129
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
130 String string = cb.toString();
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
131
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
132 textsiz = string.length();
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
133
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
134 REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string);
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
135
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
136 return repcommand;
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
137 }
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
138
d22384c0026c *** empty log message ***
kent
parents:
diff changeset
139 }