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