0
|
1 package rep;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.nio.ByteBuffer;
|
|
5 import java.nio.channels.SocketChannel;
|
13
|
6 import java.util.LinkedList;
|
|
7 import java.util.StringTokenizer;
|
0
|
8
|
|
9 public class REPPacketReceive {
|
|
10
|
|
11 SocketChannel socketchannel;
|
17
|
12 private final int HEADER_SIZE = 24;
|
|
13 //private String host;
|
|
14 //private int port;
|
0
|
15
|
|
16 public REPPacketReceive(SocketChannel sc){
|
|
17 socketchannel = sc;
|
|
18 }
|
|
19
|
|
20
|
|
21 public REPCommand unpack() {
|
|
22
|
|
23 ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
|
|
24 long len = 0;
|
|
25 header.clear();
|
|
26 try {
|
|
27 len = socketchannel.read(header);
|
|
28 if(len == -1){
|
|
29 socketchannel.close();
|
|
30 return null;
|
|
31 }else if(len == 0){
|
|
32 return null;
|
|
33 }
|
|
34 } catch (IOException e1) {
|
|
35 e1.printStackTrace();
|
|
36 } // limit = read length
|
|
37 if (len !=HEADER_SIZE) {
|
|
38 System.out.println("‚Ä‚·");
|
|
39 // this can't happen
|
|
40 }
|
|
41 header.rewind(); // position = 0
|
|
42
|
|
43 String text = "";
|
|
44 int cmd = header.getInt();
|
|
45 int sid = header.getInt();
|
|
46 int eid = header.getInt();
|
|
47 int seqid = header.getInt();
|
|
48 int lineno = header.getInt();
|
|
49 int textsiz = header.getInt()/2;
|
|
50
|
|
51 ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz*2);
|
|
52
|
|
53 try {
|
|
54 len = socketchannel.read(textBuffer);
|
|
55 } catch (IOException e1) {
|
|
56 e1.printStackTrace();
|
|
57 } // limit = read length
|
|
58 if (len != textsiz * 2) {
|
|
59 // this can't happen
|
|
60 System.out.println("‚ ‚Æ");
|
|
61 }
|
|
62 textBuffer.rewind();
|
|
63 for(int i=0;i<textsiz;i++) {
|
|
64 text +=textBuffer.getChar();
|
|
65 }
|
|
66 String string = text;
|
|
67 //System.out.println(string);
|
14
|
68 //getSocket(string);
|
0
|
69 REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string);
|
19
|
70 System.out.println("received command: " + repcommand.toString());
|
15
|
71 getSocket(repcommand);
|
20
|
72 System.out.println("received command: " + repcommand.toString());
|
0
|
73 return repcommand;
|
|
74 }
|
13
|
75
|
|
76
|
15
|
77 private void getSocket(REPCommand command) {
|
18
|
78 if(command.cmd != REP.SMCMD_JOIN){
|
15
|
79 String string = command.string;
|
|
80 StringTokenizer st2 = new StringTokenizer(string, ":");
|
20
|
81 LinkedList<String> list = new LinkedList<String>();
|
|
82 while (st2.hasMoreTokens()){
|
|
83 list.add(st2.nextToken());
|
|
84 }
|
|
85 String port = list.getLast();
|
|
86 list.removeLast();
|
|
87 String host = list.getLast();
|
|
88 int socketInfoLength = host.length() + port.length() + 2;
|
|
89 System.out.println(host.length() + ":" + port.length() + ":" + socketInfoLength);
|
15
|
90 command.setString(string.substring(0, string.length() - socketInfoLength));
|
17
|
91 command.setHost(host);
|
20
|
92 command.setPort(port);
|
18
|
93 }
|
13
|
94 }
|
0
|
95 }
|