annotate src/rep/REPCommandPacker.java @ 203:fce2776071d4

package rep copy
author one
date Sat, 18 Dec 2010 13:18:54 +0900
parents 3133040ee4f4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
193
3133040ee4f4 (no commit message)
one
parents:
diff changeset
1 package rep;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
2
3133040ee4f4 (no commit message)
one
parents:
diff changeset
3 import java.io.IOException;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
4 import java.nio.ByteBuffer;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
5 import java.nio.CharBuffer;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
6 import java.nio.channels.SocketChannel;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
7 import java.nio.charset.CharacterCodingException;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
8 import java.nio.charset.Charset;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
9 import java.nio.charset.CharsetDecoder;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
10 import java.nio.charset.CharsetEncoder;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
11
3133040ee4f4 (no commit message)
one
parents:
diff changeset
12 import rep.channel.REPPack;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
13
3133040ee4f4 (no commit message)
one
parents:
diff changeset
14 /*
3133040ee4f4 (no commit message)
one
parents:
diff changeset
15 //+-------+--------+--------+-------+--------+---------+------+
3133040ee4f4 (no commit message)
one
parents:
diff changeset
16 //| cmd | session| editor | seqid | lineno | textsiz | text |
3133040ee4f4 (no commit message)
one
parents:
diff changeset
17 //| | id | id | | | | |
3133040ee4f4 (no commit message)
one
parents:
diff changeset
18 //+-------+--------+--------+-------+--------+---------+------+
3133040ee4f4 (no commit message)
one
parents:
diff changeset
19 //o-------header section (network order)-------------o
3133040ee4f4 (no commit message)
one
parents:
diff changeset
20 int cmd; // command
3133040ee4f4 (no commit message)
one
parents:
diff changeset
21 int sid; // session ID : uniqu to editing file
3133040ee4f4 (no commit message)
one
parents:
diff changeset
22 int eid; // editor ID : owner editor ID = 1。Session に対して unique
3133040ee4f4 (no commit message)
one
parents:
diff changeset
23 int seqno; // Sequence number : sequence number はエディタごとに管理
3133040ee4f4 (no commit message)
one
parents:
diff changeset
24 int lineno; // line number
3133040ee4f4 (no commit message)
one
parents:
diff changeset
25 int textsize; // textsize : bytesize
3133040ee4f4 (no commit message)
one
parents:
diff changeset
26 byte[] text;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
27 */
3133040ee4f4 (no commit message)
one
parents:
diff changeset
28
3133040ee4f4 (no commit message)
one
parents:
diff changeset
29
3133040ee4f4 (no commit message)
one
parents:
diff changeset
30 public class REPCommandPacker implements REPPack<REPCommand> {
3133040ee4f4 (no commit message)
one
parents:
diff changeset
31 private static final int TEXTSIZELIMIT = 0x7000000;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
32 private static final int HEADER_SIZE = 24;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
33 // JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5
3133040ee4f4 (no commit message)
one
parents:
diff changeset
34 private static final int CHARSIZE = 5;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
35
3133040ee4f4 (no commit message)
one
parents:
diff changeset
36 Charset charset = Charset.forName("UTF-8");
3133040ee4f4 (no commit message)
one
parents:
diff changeset
37 CharsetEncoder encoder = charset.newEncoder();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
38 CharsetDecoder decoder = charset.newDecoder();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
39
3133040ee4f4 (no commit message)
one
parents:
diff changeset
40 /* (non-Javadoc)
3133040ee4f4 (no commit message)
one
parents:
diff changeset
41 * @see rep.REPPack#packUConv(rep.REPCommand)
3133040ee4f4 (no commit message)
one
parents:
diff changeset
42 */
3133040ee4f4 (no commit message)
one
parents:
diff changeset
43 public ByteBuffer packUConv(REPCommand command){
3133040ee4f4 (no commit message)
one
parents:
diff changeset
44 int size = 0;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
45 if (command.string!=null) size = command.string.length()*CHARSIZE;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
46 ByteBuffer buffer = ByteBuffer.allocateDirect(HEADER_SIZE+size);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
47 buffer.clear(); // position = 0
3133040ee4f4 (no commit message)
one
parents:
diff changeset
48 buffer.putInt(command.cmd.id);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
49 buffer.putInt(command.sid);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
50 buffer.putInt(command.eid);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
51 buffer.putInt(command.seq);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
52 buffer.putInt(command.lineno);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
53
3133040ee4f4 (no commit message)
one
parents:
diff changeset
54 int pos = buffer.position();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
55 buffer.putInt(0);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
56 int pos1 = buffer.position();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
57
3133040ee4f4 (no commit message)
one
parents:
diff changeset
58 if (command.string!=null) {
3133040ee4f4 (no commit message)
one
parents:
diff changeset
59 //Encode to UTF8
3133040ee4f4 (no commit message)
one
parents:
diff changeset
60 CharBuffer cb = CharBuffer.wrap(command.string);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
61 try {
3133040ee4f4 (no commit message)
one
parents:
diff changeset
62 encoder.encode(cb, buffer, true);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
63 } catch (IllegalStateException e) {
3133040ee4f4 (no commit message)
one
parents:
diff changeset
64 buffer.position(pos1);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
65 }
3133040ee4f4 (no commit message)
one
parents:
diff changeset
66 }
3133040ee4f4 (no commit message)
one
parents:
diff changeset
67
3133040ee4f4 (no commit message)
one
parents:
diff changeset
68 //Encoded string length set
3133040ee4f4 (no commit message)
one
parents:
diff changeset
69 int length = buffer.position() -pos1 ;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
70 buffer.putInt(pos, length);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
71 buffer.limit(HEADER_SIZE+length);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
72 buffer.rewind();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
73
3133040ee4f4 (no commit message)
one
parents:
diff changeset
74 return buffer;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
75 }
3133040ee4f4 (no commit message)
one
parents:
diff changeset
76
3133040ee4f4 (no commit message)
one
parents:
diff changeset
77
3133040ee4f4 (no commit message)
one
parents:
diff changeset
78 public REPCommand unpackUConv(SocketChannel sc) throws IOException {
3133040ee4f4 (no commit message)
one
parents:
diff changeset
79 ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
80 header.clear();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
81
3133040ee4f4 (no commit message)
one
parents:
diff changeset
82 while(header.remaining()>0){
3133040ee4f4 (no commit message)
one
parents:
diff changeset
83 if (sc.read(header)<0) throw new IOException();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
84 }
3133040ee4f4 (no commit message)
one
parents:
diff changeset
85
3133040ee4f4 (no commit message)
one
parents:
diff changeset
86 header.rewind(); // position = 0
3133040ee4f4 (no commit message)
one
parents:
diff changeset
87
3133040ee4f4 (no commit message)
one
parents:
diff changeset
88 int cmd = header.getInt();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
89 int sid = header.getInt();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
90 int eid = header.getInt();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
91 int seqid = header.getInt();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
92 int lineno = header.getInt();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
93 int textsiz = header.getInt();
203
fce2776071d4 package rep copy
one
parents: 193
diff changeset
94
fce2776071d4 package rep copy
one
parents: 193
diff changeset
95 /**
fce2776071d4 package rep copy
one
parents: 193
diff changeset
96 * We should avoid large reading here. Large command should be
fce2776071d4 package rep copy
one
parents: 193
diff changeset
97 * broke in smaller one. It should be easy.
fce2776071d4 package rep copy
one
parents: 193
diff changeset
98 */
193
3133040ee4f4 (no commit message)
one
parents:
diff changeset
99 if (textsiz>TEXTSIZELIMIT||textsiz<0) {
3133040ee4f4 (no commit message)
one
parents:
diff changeset
100 // corrupted packet
3133040ee4f4 (no commit message)
one
parents:
diff changeset
101 throw new IOException();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
102 }
3133040ee4f4 (no commit message)
one
parents:
diff changeset
103 ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
104
3133040ee4f4 (no commit message)
one
parents:
diff changeset
105 while(textBuffer.remaining()>0){
3133040ee4f4 (no commit message)
one
parents:
diff changeset
106 if (sc.read(textBuffer)<0) throw new IOException();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
107 }
3133040ee4f4 (no commit message)
one
parents:
diff changeset
108 textBuffer.rewind();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
109
3133040ee4f4 (no commit message)
one
parents:
diff changeset
110 //Decode UTF-8 to System Encoding(UTF-16)
3133040ee4f4 (no commit message)
one
parents:
diff changeset
111 String string;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
112 try {
3133040ee4f4 (no commit message)
one
parents:
diff changeset
113 CharBuffer cb;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
114 cb = decoder.decode(textBuffer);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
115 cb.rewind();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
116 string = cb.toString();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
117 } catch (CharacterCodingException e) {
3133040ee4f4 (no commit message)
one
parents:
diff changeset
118 string = "";
3133040ee4f4 (no commit message)
one
parents:
diff changeset
119 }
3133040ee4f4 (no commit message)
one
parents:
diff changeset
120 textsiz = string.length();
3133040ee4f4 (no commit message)
one
parents:
diff changeset
121 REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
122 //if (isLogging)
3133040ee4f4 (no commit message)
one
parents:
diff changeset
123 //System.out.println("Reading: "+repcommand);
3133040ee4f4 (no commit message)
one
parents:
diff changeset
124 return repcommand;
3133040ee4f4 (no commit message)
one
parents:
diff changeset
125 }
3133040ee4f4 (no commit message)
one
parents:
diff changeset
126
3133040ee4f4 (no commit message)
one
parents:
diff changeset
127 }