Mercurial > hg > RemoteEditor > REPSessionManager
annotate rep/REPCommandPacker.java @ 382:4b87f89b3afd
REP Session Manager (Java version)
new structure
author | one@firefly.cr.ie.u-ryukyu.ac.jp |
---|---|
date | Mon, 10 Nov 2008 22:07:45 +0900 |
parents | 537a9c6c6d9c |
children | 1fca50ce3508 |
rev | line source |
---|---|
186 | 1 package rep; |
2 | |
3 import java.io.IOException; | |
4 import java.nio.ByteBuffer; | |
5 import java.nio.CharBuffer; | |
6 import java.nio.channels.SocketChannel; | |
7 import java.nio.charset.CharacterCodingException; | |
8 import java.nio.charset.Charset; | |
9 import java.nio.charset.CharsetDecoder; | |
10 import java.nio.charset.CharsetEncoder; | |
11 | |
12 import rep.channel.REPPack; | |
13 | |
224 | 14 /* |
15 //+-------+--------+--------+-------+--------+---------+------+ | |
16 //| cmd | session| editor | seqid | lineno | textsiz | text | | |
17 //| | id | id | | | | | | |
18 //+-------+--------+--------+-------+--------+---------+------+ | |
19 //o-------header section (network order)-------------o | |
20 int cmd; // command | |
21 int sid; // session ID : uniqu to editing file | |
22 int eid; // editor ID : owner editor ID = 1。Session に対して unique | |
23 int seqno; // Sequence number : sequence number はエディタごとに管理 | |
24 int lineno; // line number | |
25 int textsize; // textsize : bytesize | |
26 byte[] text; | |
27 */ | |
28 | |
186 | 29 |
30 public class REPCommandPacker implements REPPack<REPCommand> { | |
311 | 31 private static final int TEXTSIZELIMIT = 0x7000000; |
312 | 32 private static final int HEADER_SIZE = 24; |
186 | 33 // JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5 |
312 | 34 private static final int CHARSIZE = 5; |
186 | 35 |
311 | 36 Charset charset = Charset.forName("UTF-8"); |
37 CharsetEncoder encoder = charset.newEncoder(); | |
38 CharsetDecoder decoder = charset.newDecoder(); | |
186 | 39 |
40 /* (non-Javadoc) | |
41 * @see rep.REPPack#packUConv(rep.REPCommand) | |
42 */ | |
312 | 43 public ByteBuffer packUConv(REPCommand command){ |
382
4b87f89b3afd
REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
380
diff
changeset
|
44 ByteBuffer buffer = ByteBuffer.allocateDirect(HEADER_SIZE+(command.string.length()*CHARSIZE)); |
186 | 45 buffer.clear(); // position = 0 |
271 | 46 buffer.putInt(command.cmd.id); |
224 | 47 buffer.putInt(command.sid); |
48 buffer.putInt(command.eid); | |
49 buffer.putInt(command.seq); | |
50 buffer.putInt(command.lineno); | |
186 | 51 |
52 int pos = buffer.position(); | |
224 | 53 buffer.putInt(0); |
311 | 54 int pos1 = buffer.position(); |
337 | 55 |
56 if (command.string!=null) { | |
57 //Encode to UTF8 | |
58 CharBuffer cb = CharBuffer.wrap(command.string); | |
59 try { | |
60 encoder.encode(cb, buffer, true); | |
61 } catch (IllegalStateException e) { | |
62 buffer.position(pos1); | |
63 } | |
64 } | |
186 | 65 |
66 //Encoded string length set | |
311 | 67 int length = buffer.position() -pos1 ; |
186 | 68 buffer.putInt(pos, length); |
69 buffer.limit(HEADER_SIZE+length); | |
70 buffer.rewind(); | |
71 | |
72 return buffer; | |
73 } | |
74 | |
75 | |
76 public REPCommand unpackUConv(SocketChannel sc) throws IOException { | |
77 ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE); | |
78 header.clear(); | |
311 | 79 |
224 | 80 while(header.remaining()>0){ |
267 | 81 if (sc.read(header)<0) throw new IOException(); |
312 | 82 } |
224 | 83 |
186 | 84 header.rewind(); // position = 0 |
85 | |
86 int cmd = header.getInt(); | |
87 int sid = header.getInt(); | |
88 int eid = header.getInt(); | |
89 int seqid = header.getInt(); | |
90 int lineno = header.getInt(); | |
91 int textsiz = header.getInt(); | |
92 | |
311 | 93 if (textsiz>TEXTSIZELIMIT||textsiz<0) { |
94 // corrupted packet | |
95 throw new IOException(); | |
96 } | |
186 | 97 ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz); |
224 | 98 |
99 while(textBuffer.remaining()>0){ | |
267 | 100 if (sc.read(textBuffer)<0) throw new IOException(); |
186 | 101 } |
102 textBuffer.rewind(); | |
103 | |
104 //Decode UTF-8 to System Encoding(UTF-16) | |
311 | 105 String string; |
186 | 106 try { |
312 | 107 CharBuffer cb; |
186 | 108 cb = decoder.decode(textBuffer); |
311 | 109 cb.rewind(); |
110 string = cb.toString(); | |
186 | 111 } catch (CharacterCodingException e) { |
311 | 112 string = ""; |
186 | 113 } |
114 textsiz = string.length(); | |
115 REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string); | |
337 | 116 //if (isLogging) |
117 //System.out.println("Reading: "+repcommand); | |
186 | 118 return repcommand; |
119 } | |
120 | |
121 } |