annotate src/fdl/PSX.java @ 25:330fa49bc4fd

*** empty log message ***
author kono
date Wed, 20 Aug 2008 14:12:15 +0900
parents 35375016b2f0
children d7d70edc9c7c
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
23
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
15 import java.io.IOException;
24
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
16 import java.net.InetAddress;
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
17 import java.net.InetSocketAddress;
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
18 import java.net.UnknownHostException;
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
19 import java.nio.ByteBuffer;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
20 import java.nio.ByteOrder;
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
21 import java.nio.CharBuffer;
23
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
22 import java.nio.channels.SelectionKey;
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
23 import java.nio.channels.SocketChannel;
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
24 import java.nio.charset.CharacterCodingException;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
25 import java.nio.charset.Charset;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
26 import java.nio.charset.CharsetDecoder;
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
27
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 /**
18
2cbd98257d61 *** empty log message ***
kono
parents: 17
diff changeset
31 PSX Tuple Command Protocol Format
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
32
24
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
33 All PSX offset command operation should be here.
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
34 Some public library
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
35 */
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
36
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
37 public class PSX {
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
38 static final int PSX_IN = 'i';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
39 static final int PSX_OUT = 'o';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
40 static final int PSX_UPDATE = 'u';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
41 static final int PSX_RD = 'r';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
42 static final int PSX_CHECK = 'c';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
43 static final int PSX_REPLY = '?';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
44 static final int PSX_WAIT_RD = 'w';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
45 static final int PSX_ANSWER = 'a';
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
46 static final int PSX_HTTP_ANSWER = 'P'; // Put
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
47 static final int PSX_HTTP_REQUEST = 'G'; // Get
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
48 static final int PSX_COM_DEBUG = 'D'; //Communication DEBUG
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
49
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
50 static final int LINDA_PACKET_LENGTH_OFFSET =0;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
51 static final int LINDA_MODE_OFFSET =0+4;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
52 static final int LINDA_ID_OFFSET =1+4;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
53 static final int LINDA_SEQ_OFFSET =3+4;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
54 static final int LINDA_DATA_LENGTH_OFFSET =7+4;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
55 static final int LINDA_HEADER_SIZE =12+4;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
56 static final int INT_SIZE =4;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
57 static final int SHORT_SIZE =2;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
58
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
59 static final int PRIVILEGED_ID_START = 32768;
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
60 static final int PRIVILEGED_ID_END = 36864;
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
61 static final int META_STOP = PRIVILEGED_ID_START;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
62 static final int META_MONITOR = PRIVILEGED_ID_START+1;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
63 static final int META_MONITOR_DATA = PRIVILEGED_ID_START+2;
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
64
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
65
25
330fa49bc4fd *** empty log message ***
kono
parents: 24
diff changeset
66 static void printCommand(String comment, ByteBuffer command, ByteBuffer data) {
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
67 char id = (char)command.getShort(LINDA_ID_OFFSET);
25
330fa49bc4fd *** empty log message ***
kono
parents: 24
diff changeset
68 System.out.println(comment+" LENGTH:"+command.getInt(LINDA_PACKET_LENGTH_OFFSET)+" "+
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
69 "MODE:"+(char)command.get(LINDA_MODE_OFFSET)+" "+
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
70 "ID:"+(int)id+" "+
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
71 "SEQ:"+command.getInt(LINDA_SEQ_OFFSET)+" "+
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
72 "DATA LENGTH:"+command.getInt(LINDA_DATA_LENGTH_OFFSET)+" ");
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
73 System.out.println("DATA:"+data);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
74 command.rewind();
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
75 }
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
76
25
330fa49bc4fd *** empty log message ***
kono
parents: 24
diff changeset
77 static void printData(String comment,ByteBuffer command) {
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
78 /*** print data ***/
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
79 char id = (char)command.getShort(LINDA_ID_OFFSET);
25
330fa49bc4fd *** empty log message ***
kono
parents: 24
diff changeset
80 System.out.println(comment+" LENGTH:"+command.getInt(LINDA_PACKET_LENGTH_OFFSET)+" "+
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
81 "MODE:"+(char)command.get(LINDA_MODE_OFFSET)+" "+
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
82 "ID:"+(int)id+" "+
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
83 "SEQ:"+command.getInt(LINDA_SEQ_OFFSET)+" ");
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
84 command.rewind();
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
85 }
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
86
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
87
22
56e015e8f5dc Testing TestLindaServer
kono
parents: 19
diff changeset
88 static ByteBuffer setCommand(int _mode, int _id, int _seq, int _datalen) {
56e015e8f5dc Testing TestLindaServer
kono
parents: 19
diff changeset
89 ByteBuffer command = ByteBuffer.allocate(LINDA_HEADER_SIZE);
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
90 command.order(ByteOrder.BIG_ENDIAN);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
91
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
92 command.putInt(LINDA_PACKET_LENGTH_OFFSET,
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
93 _datalen+LINDA_HEADER_SIZE-INT_SIZE);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
94 command.put(LINDA_MODE_OFFSET,(byte)_mode);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
95 command.putShort(LINDA_ID_OFFSET,(short)_id);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
96 command.putInt(LINDA_SEQ_OFFSET,_seq);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
97 command.putInt(LINDA_DATA_LENGTH_OFFSET,_datalen);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
98 command.rewind();
22
56e015e8f5dc Testing TestLindaServer
kono
parents: 19
diff changeset
99 return command;
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
100 }
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
101
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
102 static void setAnserCommand(ByteBuffer command, int seq) {
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
103 command.put(LINDA_MODE_OFFSET, (byte)'a');
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
104 command.putInt(LINDA_SEQ_OFFSET, seq);
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
105 command.rewind();
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
106 }
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
107
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
108 public static ByteBuffer string2ByteBuffer(String log) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
109 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
110 for(int i=0;i<log.length();i++) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
111 blog.putChar(log.charAt(i));
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
112 }
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
113 blog.flip();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
114 return blog;
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
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
117 public static String getdataString(ByteBuffer data) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
118 String sendtext;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
119 data.rewind();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
120 //Decode UTF-8 to System Encoding(UTF-16)
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
121 Charset charset = Charset.forName("UTF-8");
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
122 CharsetDecoder decoder = charset.newDecoder();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
123 CharBuffer cb = null;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
124 try {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
125 cb = decoder.decode(data);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
126 } catch (CharacterCodingException e) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
127 e.printStackTrace();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
128 }
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
129 cb.rewind();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
130
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
131 sendtext = cb.toString();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
132 return sendtext;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 18
diff changeset
133 }
23
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
134
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
135 public static void send(SocketChannel ch, ByteBuffer command, ByteBuffer data) {
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
136 int send_size = LINDA_HEADER_SIZE;
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
137 int count = 0;
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
138
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
139 if (command.position()!=0||command.limit()!=PSX.LINDA_HEADER_SIZE)
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
140 System.err.println("command length erron send");
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
141 try {
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
142 //command Send
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
143 while(send_size > 0){
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
144 count = ch.write(command);
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
145 if(count < 0) throw new IOException();
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
146 send_size -= count;
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
147 }
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
148
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
149 if (data==null) return;
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
150 //data Send
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
151 while(data.remaining() > 0){
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
152 count = ch.write(data);
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
153 if(count < 0) throw new IOException();
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
154 }
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
155 } catch (IOException e) {
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
156 System.out.println("Write Falied on:"+ch);
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
157 return;
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
158 }
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
159 }
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
160
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
161 public static void send(SelectionKey key, ByteBuffer command, ByteBuffer data) {
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
162 SocketChannel ch = (SocketChannel)key.channel();
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
163 send(ch,command,data);
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
164 }
24
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
165
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
166 static String getRemoteHostAndPort(SocketChannel ch) {
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
167 String socketString = ch.socket().getRemoteSocketAddress().toString();
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
168 String[] split = socketString.split("/");
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
169 int length = split.length;
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
170 String hostAndPort = split[length-1];
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
171 split = hostAndPort.split(":");
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
172 String host = split[0];
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
173 String port = split[1];
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
174 int portnum = Integer.parseInt(port);
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
175 try {
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
176 InetSocketAddress address = new InetSocketAddress(InetAddress.getByName(host), portnum);
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
177 host = address.getHostName().toString();
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
178 return (host +":"+port);
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
179 }
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
180 catch( UnknownHostException e ){
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
181 return hostAndPort;
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
182 }
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
183 }
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
184
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
185 static String getLocalHostAndPort(SocketChannel ch) {
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
186 String socketString = ch.socket().getLocalSocketAddress().toString();
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
187 String[] split = socketString.split("/");
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
188 int length = split.length;
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
189 String hostAndPort = split[length-1];
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
190 split = hostAndPort.split(":");
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
191 String host = split[0];
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
192 String port = split[1];
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
193 try {
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
194 InetAddress localhost = InetAddress.getLocalHost();
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
195 host = localhost.getHostName();
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
196 return (host +":"+port);
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
197 }
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
198 catch( UnknownHostException e ){
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
199 return (host +":"+port);
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
200 }
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
201 }
17
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
202
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
203
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
204 }
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
205
609b288f47f9 *** empty log message ***
kono
parents:
diff changeset
206 /* end */