0
|
1 package rep;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.nio.ByteBuffer;
|
33
|
5 import java.nio.CharBuffer;
|
124
|
6 import java.nio.channels.SelectableChannel;
|
27
|
7 import java.nio.channels.SelectionKey;
|
0
|
8 import java.nio.channels.SocketChannel;
|
33
|
9 import java.nio.charset.CharacterCodingException;
|
|
10 import java.nio.charset.Charset;
|
|
11 import java.nio.charset.CharsetDecoder;
|
13
|
12 import java.util.LinkedList;
|
|
13 import java.util.StringTokenizer;
|
0
|
14
|
124
|
15 import rep.channel.ChannelSimulator;
|
131
|
16 import rep.channel.REPSocketChannel;
|
124
|
17
|
0
|
18 public class REPPacketReceive {
|
|
19
|
131
|
20 REPSocketChannel socketchannel;
|
17
|
21 private final int HEADER_SIZE = 24;
|
27
|
22 SelectionKey key;
|
0
|
23
|
131
|
24 public REPPacketReceive(REPSocketChannel sc){
|
0
|
25 socketchannel = sc;
|
|
26 }
|
27
|
27
|
|
28 public void setkey(SelectionKey key) {
|
|
29 this.key = key;
|
|
30 }
|
0
|
31
|
49
|
32
|
33
|
33 public REPCommand unpackUConv() {
|
|
34 ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
|
|
35 long len = 0;
|
|
36 header.clear();
|
|
37 try {
|
|
38 len = socketchannel.read(header);
|
|
39 if(len == -1){
|
|
40 if(key != null){
|
|
41 key.cancel();
|
|
42 }
|
|
43 socketchannel.close();
|
|
44 return null;
|
|
45 }else if(len == 0){
|
|
46 return null;
|
|
47 }
|
|
48 } catch (IOException e1) {
|
|
49 e1.printStackTrace();
|
|
50 } // limit = read length
|
|
51 if (len !=HEADER_SIZE) {
|
124
|
52 System.out.println("error.");
|
33
|
53 // this can't happen
|
|
54 }
|
|
55 header.rewind(); // position = 0
|
|
56
|
|
57 int cmd = header.getInt();
|
|
58 int sid = header.getInt();
|
|
59 int eid = header.getInt();
|
|
60 int seqid = header.getInt();
|
|
61 int lineno = header.getInt();
|
46
|
62 int textsiz = header.getInt();
|
33
|
63
|
46
|
64 ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz);
|
|
65
|
33
|
66 try {
|
|
67 len = socketchannel.read(textBuffer);
|
46
|
68 if(len == -1){
|
|
69 if(key != null){
|
|
70 key.cancel();
|
|
71 }
|
|
72 socketchannel.close();
|
|
73 return null;
|
|
74 }
|
33
|
75 } catch (IOException e1) {
|
|
76 e1.printStackTrace();
|
|
77 } // limit = read length
|
46
|
78 if (len != textsiz) {
|
33
|
79 // this can't happen
|
124
|
80 System.out.println("error.");
|
33
|
81 }
|
|
82 textBuffer.rewind();
|
|
83
|
|
84 //Decode UTF-8 to System Encoding(UTF-16)
|
|
85 Charset charset = Charset.forName("UTF-8");
|
|
86 CharsetDecoder decoder = charset.newDecoder();
|
|
87 CharBuffer cb = null;
|
|
88 try {
|
|
89 cb = decoder.decode(textBuffer);
|
|
90 } catch (CharacterCodingException e) {
|
|
91 e.printStackTrace();
|
|
92 }
|
46
|
93 cb.rewind();
|
33
|
94
|
|
95 String string = cb.toString();
|
|
96
|
51
|
97 textsiz = string.length();
|
33
|
98
|
|
99 REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string);
|
46
|
100 System.out.println("UnPack Packet: => cmd:"+cmd+" sid:"+sid+" eid:"+eid+"seqid:"+seqid+" lineno:"+lineno+" textsiz:" +textsiz+" text: "+string);
|
33
|
101 System.out.println("received command: " + repcommand.toString());
|
49
|
102
|
33
|
103 return repcommand;
|
|
104 }
|
0
|
105 }
|