Mercurial > hg > FederatedLinda
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 |
rev | line source |
---|---|
17 | 1 |
2 /* | |
3 * @(#)PSXQueueInterface.java 1.1 06/04/01 | |
4 * | |
5 * Copyright 2006 Shinji KONO | |
6 * | |
7 | |
8 PSX Lidna | |
9 Trasport layer of PSX Linda library | |
10 | |
11 */ | |
12 | |
13 package fdl; | |
14 | |
23 | 15 import java.io.IOException; |
24 | 16 import java.net.InetAddress; |
17 import java.net.InetSocketAddress; | |
18 import java.net.UnknownHostException; | |
17 | 19 import java.nio.ByteBuffer; |
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 | 22 import java.nio.channels.SelectionKey; |
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 | 27 |
28 | |
29 | |
30 /** | |
18 | 31 PSX Tuple Command Protocol Format |
17 | 32 |
24 | 33 All PSX offset command operation should be here. |
34 Some public library | |
17 | 35 */ |
36 | |
37 public class PSX { | |
38 static final int PSX_IN = 'i'; | |
39 static final int PSX_OUT = 'o'; | |
40 static final int PSX_UPDATE = 'u'; | |
41 static final int PSX_RD = 'r'; | |
42 static final int PSX_CHECK = 'c'; | |
43 static final int PSX_REPLY = '?'; | |
44 static final int PSX_WAIT_RD = 'w'; | |
45 static final int PSX_ANSWER = 'a'; | |
46 static final int PSX_HTTP_ANSWER = 'P'; // Put | |
47 static final int PSX_HTTP_REQUEST = 'G'; // Get | |
48 static final int PSX_COM_DEBUG = 'D'; //Communication DEBUG | |
49 | |
50 static final int LINDA_PACKET_LENGTH_OFFSET =0; | |
51 static final int LINDA_MODE_OFFSET =0+4; | |
52 static final int LINDA_ID_OFFSET =1+4; | |
53 static final int LINDA_SEQ_OFFSET =3+4; | |
54 static final int LINDA_DATA_LENGTH_OFFSET =7+4; | |
55 static final int LINDA_HEADER_SIZE =12+4; | |
56 static final int INT_SIZE =4; | |
57 static final int SHORT_SIZE =2; | |
58 | |
59 static final int PRIVILEGED_ID_START = 32768; | |
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 | 64 |
65 | |
25 | 66 static void printCommand(String comment, ByteBuffer command, ByteBuffer data) { |
17 | 67 char id = (char)command.getShort(LINDA_ID_OFFSET); |
25 | 68 System.out.println(comment+" LENGTH:"+command.getInt(LINDA_PACKET_LENGTH_OFFSET)+" "+ |
17 | 69 "MODE:"+(char)command.get(LINDA_MODE_OFFSET)+" "+ |
70 "ID:"+(int)id+" "+ | |
71 "SEQ:"+command.getInt(LINDA_SEQ_OFFSET)+" "+ | |
72 "DATA LENGTH:"+command.getInt(LINDA_DATA_LENGTH_OFFSET)+" "); | |
73 System.out.println("DATA:"+data); | |
74 command.rewind(); | |
75 } | |
76 | |
25 | 77 static void printData(String comment,ByteBuffer command) { |
17 | 78 /*** print data ***/ |
79 char id = (char)command.getShort(LINDA_ID_OFFSET); | |
25 | 80 System.out.println(comment+" LENGTH:"+command.getInt(LINDA_PACKET_LENGTH_OFFSET)+" "+ |
17 | 81 "MODE:"+(char)command.get(LINDA_MODE_OFFSET)+" "+ |
82 "ID:"+(int)id+" "+ | |
83 "SEQ:"+command.getInt(LINDA_SEQ_OFFSET)+" "); | |
84 command.rewind(); | |
85 } | |
86 | |
87 | |
22 | 88 static ByteBuffer setCommand(int _mode, int _id, int _seq, int _datalen) { |
89 ByteBuffer command = ByteBuffer.allocate(LINDA_HEADER_SIZE); | |
17 | 90 command.order(ByteOrder.BIG_ENDIAN); |
91 | |
92 command.putInt(LINDA_PACKET_LENGTH_OFFSET, | |
93 _datalen+LINDA_HEADER_SIZE-INT_SIZE); | |
94 command.put(LINDA_MODE_OFFSET,(byte)_mode); | |
95 command.putShort(LINDA_ID_OFFSET,(short)_id); | |
96 command.putInt(LINDA_SEQ_OFFSET,_seq); | |
97 command.putInt(LINDA_DATA_LENGTH_OFFSET,_datalen); | |
98 command.rewind(); | |
22 | 99 return command; |
17 | 100 } |
101 | |
102 static void setAnserCommand(ByteBuffer command, int seq) { | |
103 command.put(LINDA_MODE_OFFSET, (byte)'a'); | |
104 command.putInt(LINDA_SEQ_OFFSET, seq); | |
105 command.rewind(); | |
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 | 134 |
135 public static void send(SocketChannel ch, ByteBuffer command, ByteBuffer data) { | |
136 int send_size = LINDA_HEADER_SIZE; | |
137 int count = 0; | |
138 | |
139 if (command.position()!=0||command.limit()!=PSX.LINDA_HEADER_SIZE) | |
140 System.err.println("command length erron send"); | |
141 try { | |
142 //command Send | |
143 while(send_size > 0){ | |
144 count = ch.write(command); | |
145 if(count < 0) throw new IOException(); | |
146 send_size -= count; | |
147 } | |
148 | |
149 if (data==null) return; | |
150 //data Send | |
151 while(data.remaining() > 0){ | |
152 count = ch.write(data); | |
153 if(count < 0) throw new IOException(); | |
154 } | |
155 } catch (IOException e) { | |
156 System.out.println("Write Falied on:"+ch); | |
157 return; | |
158 } | |
159 } | |
160 | |
161 public static void send(SelectionKey key, ByteBuffer command, ByteBuffer data) { | |
162 SocketChannel ch = (SocketChannel)key.channel(); | |
163 send(ch,command,data); | |
164 } | |
24 | 165 |
166 static String getRemoteHostAndPort(SocketChannel ch) { | |
167 String socketString = ch.socket().getRemoteSocketAddress().toString(); | |
168 String[] split = socketString.split("/"); | |
169 int length = split.length; | |
170 String hostAndPort = split[length-1]; | |
171 split = hostAndPort.split(":"); | |
172 String host = split[0]; | |
173 String port = split[1]; | |
174 int portnum = Integer.parseInt(port); | |
175 try { | |
176 InetSocketAddress address = new InetSocketAddress(InetAddress.getByName(host), portnum); | |
177 host = address.getHostName().toString(); | |
178 return (host +":"+port); | |
179 } | |
180 catch( UnknownHostException e ){ | |
181 return hostAndPort; | |
182 } | |
183 } | |
184 | |
185 static String getLocalHostAndPort(SocketChannel ch) { | |
186 String socketString = ch.socket().getLocalSocketAddress().toString(); | |
187 String[] split = socketString.split("/"); | |
188 int length = split.length; | |
189 String hostAndPort = split[length-1]; | |
190 split = hostAndPort.split(":"); | |
191 String host = split[0]; | |
192 String port = split[1]; | |
193 try { | |
194 InetAddress localhost = InetAddress.getLocalHost(); | |
195 host = localhost.getHostName(); | |
196 return (host +":"+port); | |
197 } | |
198 catch( UnknownHostException e ){ | |
199 return (host +":"+port); | |
200 } | |
201 } | |
17 | 202 |
203 | |
204 } | |
205 | |
206 /* end */ |