annotate rep/REPPacketSend.java @ 76:97ca5f5447cd

*** empty log message ***
author pin
date Tue, 04 Dec 2007 10:33:06 +0900
parents afddc6fe8d8d
children 04a1ab625121
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
1 package rep;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
2
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
3 import java.io.IOException;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
4 import java.nio.ByteBuffer;
34
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
5 import java.nio.CharBuffer;
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
6 import java.nio.channels.SocketChannel;
34
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
7 import java.nio.charset.Charset;
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
8 import java.nio.charset.CharsetEncoder;
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
9
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
10
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
11 public class REPPacketSend {
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
12 SocketChannel socketchannel;
48
b4991de8e83a UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 47
diff changeset
13 // JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5
50
6f23795f61c9 UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 49
diff changeset
14 final int CHAR_ORDER = 5;
48
b4991de8e83a UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 47
diff changeset
15
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
16 public REPPacketSend(SocketChannel sc){
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
17 socketchannel = sc;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
18 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
19
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
20 public ByteBuffer pack(REPCommand command){
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
21 System.out.println("send command: " + command.toString());
45
10f1adf6d74d *** empty log message ***
pin
parents: 44
diff changeset
22 if(command.string == null){
10f1adf6d74d *** empty log message ***
pin
parents: 44
diff changeset
23 command.setString("test");
10f1adf6d74d *** empty log message ***
pin
parents: 44
diff changeset
24 }
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
25 ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*2);
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
26 buffer.clear(); // position = 0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
27 buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid);
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
28 buffer.putInt(command.seq); buffer.putInt(command.lineno);
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
29 buffer.putInt(command.string.length()*2);
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
30 for(int i=0;i<command.string.length();i++) {
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
31 buffer.putChar(command.string.charAt(i));
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
32 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
33 buffer.flip(); // limit = current position, position = 0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
34 return buffer;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
35 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
36
34
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
37 public ByteBuffer packUConv(REPCommand command){
44
618d76c06b9e *** empty log message ***
pin
parents: 35
diff changeset
38 System.out.println("send command byUTF8: " + command.toString());
618d76c06b9e *** empty log message ***
pin
parents: 35
diff changeset
39 if(command.string == null){
618d76c06b9e *** empty log message ***
pin
parents: 35
diff changeset
40 command.setString("test");
618d76c06b9e *** empty log message ***
pin
parents: 35
diff changeset
41 }
51
e9fba549803e UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 50
diff changeset
42 ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string.length()*CHAR_ORDER));
34
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
43 buffer.clear(); // position = 0
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
44 buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid);
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
45 buffer.putInt(command.seq); buffer.putInt(command.lineno);
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
46
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
47 int pos = buffer.position();
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
48 buffer.putInt(0);
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
49
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
50 //Encode to UTF8
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
51 CharBuffer cb = CharBuffer.wrap(command.string);
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
52 Charset charset = Charset.forName("UTF-8");
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
53 CharsetEncoder encoder = charset.newEncoder();
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
54 try {
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
55 encoder.encode(cb, buffer, true);
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
56 } catch (IllegalStateException e) {
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
57 e.printStackTrace();
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
58 }
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
59
52
afddc6fe8d8d UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 51
diff changeset
60 //Encoded string length set
51
e9fba549803e UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 50
diff changeset
61 int length = (buffer.position() -pos) -4;
47
f4eb7fd098c4 UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 45
diff changeset
62 System.out.println("UTF-8: Set REPComand textlen(Byte) : " + (buffer.position() - pos-4));
52
afddc6fe8d8d UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 51
diff changeset
63 if(length < 0) {
afddc6fe8d8d UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 51
diff changeset
64 length = 0;
afddc6fe8d8d UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 51
diff changeset
65 }
51
e9fba549803e UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 50
diff changeset
66 buffer.putInt(pos, length);
e9fba549803e UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 50
diff changeset
67
e9fba549803e UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 50
diff changeset
68 buffer.limit(24+length);
e9fba549803e UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 50
diff changeset
69 buffer.rewind();
34
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
70
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
71 return buffer;
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
72 }
689622193437 add UTF-8 pack method -> packUConv()
fuchita
parents: 0
diff changeset
73
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
74 public void send(REPCommand command){
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
75 try {
49
f8b4101746d2 UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 48
diff changeset
76 socketchannel.write(pack(command));
f8b4101746d2 UTF-8 Pack Method packUConv()/unpackUConv() add
fuchita
parents: 48
diff changeset
77 //socketchannel.write(packUConv(command));
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
78 //System.out.println(command.toString());
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
79 } catch (IOException e) {
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
80 e.printStackTrace();
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
81 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
82 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
83 }