annotate rep/REPPacketReceive.java @ 20:83088d943612

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