annotate src/fdl/PSX.java @ 19:0243987383b7

Meta Protocol Engine and sample implementation of event logger. ComDebug_Client needs fixes.
author kono
date Tue, 19 Aug 2008 05:33:32 +0900
parents 2cbd98257d61
children 56e015e8f5dc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
1
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
2 /*
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
3 * @(#)PSXQueueInterface.java 1.1 06/04/01
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
4 *
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
5 * Copyright 2006 Shinji KONO
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
6 *
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
7
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
8 PSX Lidna
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
9 Trasport layer of PSX Linda library
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
10
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
11 */
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
12
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
13 package fdl;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
14
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
15 import java.nio.ByteBuffer;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
16 import java.nio.ByteOrder;
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
17 import java.nio.CharBuffer;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
18 import java.nio.charset.CharacterCodingException;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
19 import java.nio.charset.Charset;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
20 import java.nio.charset.CharsetDecoder;
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
21
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
22
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
23
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
24 /**
18
2cbd98257d61 *** empty log message ***
kono
parents: 17
diff changeset
25 PSX Tuple Command Protocol Format
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
26
18
2cbd98257d61 *** empty log message ***
kono
parents: 17
diff changeset
27 All PSX offseted command operation should be here.
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
28 */
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
29
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
30 public class PSX {
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
31 static final int PSX_IN = 'i';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
32 static final int PSX_OUT = 'o';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
33 static final int PSX_UPDATE = 'u';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
34 static final int PSX_RD = 'r';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
35 static final int PSX_CHECK = 'c';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
36 static final int PSX_REPLY = '?';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
37 static final int PSX_WAIT_RD = 'w';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
38 static final int PSX_ANSWER = 'a';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
39 static final int PSX_HTTP_ANSWER = 'P'; // Put
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
40 static final int PSX_HTTP_REQUEST = 'G'; // Get
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
41 static final int PSX_COM_DEBUG = 'D'; //Communication DEBUG
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
42
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
43 static final int LINDA_PACKET_LENGTH_OFFSET =0;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
44 static final int LINDA_MODE_OFFSET =0+4;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
45 static final int LINDA_ID_OFFSET =1+4;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
46 static final int LINDA_SEQ_OFFSET =3+4;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
47 static final int LINDA_DATA_LENGTH_OFFSET =7+4;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
48 static final int LINDA_HEADER_SIZE =12+4;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
49 static final int INT_SIZE =4;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
50 static final int SHORT_SIZE =2;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
51
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
52 static final int PRIVILEGED_ID_START = 32768;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
53 static final int PRIVILEGED_ID_END = 36864;
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
54 static final int META_STOP = PRIVILEGED_ID_START;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
55 static final int META_MONITOR = PRIVILEGED_ID_START+1;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
56 static final int META_MONITOR_DATA = PRIVILEGED_ID_START+2;
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
57
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
58 // this method should be removed
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
59 static void setReportCommand(ByteBuffer command, String report_txt) {
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
60 command.order(ByteOrder.BIG_ENDIAN);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
61 command.putInt(LINDA_PACKET_LENGTH_OFFSET,(report_txt).length()*2+LINDA_HEADER_SIZE-INT_SIZE);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
62 command.put(LINDA_MODE_OFFSET,(byte)'D');
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
63 command.putShort(LINDA_ID_OFFSET,(short) 0);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
64 command.putInt(LINDA_SEQ_OFFSET,0);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
65 command.putInt(LINDA_DATA_LENGTH_OFFSET,(report_txt).length()*2);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
66 command.rewind();
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
67 }
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
68
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
69 static void printCommand(ByteBuffer command, ByteBuffer data) {
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
70 char id = (char)command.getShort(LINDA_ID_OFFSET);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
71 System.out.println("LENGTH:"+command.getInt(LINDA_PACKET_LENGTH_OFFSET)+" "+
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
72 "MODE:"+(char)command.get(LINDA_MODE_OFFSET)+" "+
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
73 "ID:"+(int)id+" "+
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
74 "SEQ:"+command.getInt(LINDA_SEQ_OFFSET)+" "+
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
75 "DATA LENGTH:"+command.getInt(LINDA_DATA_LENGTH_OFFSET)+" ");
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
76 System.out.println("DATA:"+data);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
77 command.rewind();
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
78 }
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
79
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
80 static void printData(ByteBuffer command) {
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
81 /*** print data ***/
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
82 char id = (char)command.getShort(LINDA_ID_OFFSET);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
83 System.out.println("LENGTH:"+command.getInt(LINDA_PACKET_LENGTH_OFFSET)+" "+
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
84 "MODE:"+(char)command.get(LINDA_MODE_OFFSET)+" "+
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
85 "ID:"+(int)id+" "+
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
86 "SEQ:"+command.getInt(LINDA_SEQ_OFFSET)+" ");
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
87 command.rewind();
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
88 }
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
89
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
90
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
91 static void setCommand(ByteBuffer command, int _mode, int _id, int _seq, int _datalen) {
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
92 command = ByteBuffer.allocate(LINDA_HEADER_SIZE);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
93 command.order(ByteOrder.BIG_ENDIAN);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
94
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
95 command.putInt(LINDA_PACKET_LENGTH_OFFSET,
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
96 _datalen+LINDA_HEADER_SIZE-INT_SIZE);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
97 command.put(LINDA_MODE_OFFSET,(byte)_mode);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
98 command.putShort(LINDA_ID_OFFSET,(short)_id);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
99 command.putInt(LINDA_SEQ_OFFSET,_seq);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
100 command.putInt(LINDA_DATA_LENGTH_OFFSET,_datalen);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
101 command.rewind();
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
102 }
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
103
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
104 static void setAnserCommand(ByteBuffer command, int seq) {
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
105 command.put(LINDA_MODE_OFFSET, (byte)'a');
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
106 command.rewind();
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
107 command.putInt(LINDA_SEQ_OFFSET, seq);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
108 command.rewind();
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
109 }
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
110
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
111 public static ByteBuffer string2ByteBuffer(String log) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
112 ByteBuffer blog = ByteBuffer.allocate(log.length()*2); // this is incorrect...
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
113 for(int i=0;i<log.length();i++) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
114 blog.putChar(log.charAt(i));
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
115 }
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
116 blog.flip();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
117 return blog;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
118 }
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
119
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
120 public static String getdataString(ByteBuffer data) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
121 String sendtext;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
122 data.rewind();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
123 //Decode UTF-8 to System Encoding(UTF-16)
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
124 Charset charset = Charset.forName("UTF-8");
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
125 CharsetDecoder decoder = charset.newDecoder();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
126 CharBuffer cb = null;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
127 try {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
128 cb = decoder.decode(data);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
129 } catch (CharacterCodingException e) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
130 e.printStackTrace();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
131 }
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
132 cb.rewind();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
133
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
134 sendtext = cb.toString();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
135 return sendtext;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
136 }
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
137
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
138
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
139 }
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
140
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
141 /* end */