Mercurial > hg > Members > riono > TreeVNC_ja_comment
annotate src/main/java/jp/ac/u_ryukyu/treevnc/client/MyRfbProtoClient.java @ 31:872d9bcbfe56
Before changing the Viewer.
author | one |
---|---|
date | Sat, 01 Sep 2012 21:23:30 +0900 |
parents | 57eb5575e6c4 |
children | 758d025ee24b |
rev | line source |
---|---|
12 | 1 package jp.ac.u_ryukyu.treevnc.client; |
3 | 2 |
3 import java.io.DataInputStream; | |
4 import java.io.IOException; | |
5 import java.io.InputStream; | |
6 import java.io.OutputStream; | |
4 | 7 import java.net.BindException; |
8 import java.net.ServerSocket; | |
3 | 9 import java.net.Socket; |
10 import java.nio.ByteBuffer; | |
4 | 11 import java.util.LinkedList; |
12 import java.util.concurrent.atomic.AtomicInteger; | |
13 import java.util.zip.DataFormatException; | |
14 import java.util.zip.Deflater; | |
15 import java.util.zip.Inflater; | |
3 | 16 |
12 | 17 import jp.ac.u_ryukyu.treevnc.MulticastQueue; |
28 | 18 import jp.ac.u_ryukyu.treevnc.test.MyRfbProto; |
12 | 19 |
3 | 20 import com.glavsoft.exceptions.TransportException; |
4 | 21 import com.glavsoft.rfb.encoding.EncodingType; |
20
98519d16a8c3
getInitData from ProtocolContext
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
12
diff
changeset
|
22 import com.glavsoft.rfb.protocol.ProtocolContext; |
3 | 23 import com.glavsoft.transport.Reader; |
24 import com.glavsoft.viewer.ContainerManager; | |
25 import com.glavsoft.viewer.swing.ParametersHandler.ConnectionParams; | |
26 | |
28 | 27 public class MyRfbProtoClient extends MyRfbProto { |
4 | 28 final static int FramebufferUpdate = 0; |
29 final static int CheckDelay = 11; | |
30 final static String versionMsg_3_855 = "RFB 003.855\n"; | |
29 | 31 private static final int INFLATE_BUFSIZE = 1024 * 100; |
3 | 32 private Reader reader; |
33 private EchoClient echoValue = new EchoClient(); | |
29 | 34 private String host, treenum, parent, pHost, leaderflag; |
35 private int echoPort, port, acceptPort; | |
36 Socket clientSocket, sock; | |
3 | 37 DataInputStream is; |
38 OutputStream os; | |
4 | 39 private ServerSocket servSock; |
20
98519d16a8c3
getInitData from ProtocolContext
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
12
diff
changeset
|
40 private ProtocolContext context; |
4 | 41 private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>(); |
42 boolean proxyFlag = false; | |
43 int serverMajor, serverMinor; | |
44 int clientMajor, clientMinor; | |
29 | 45 |
4 | 46 private Inflater inflater = new Inflater(); |
47 private Deflater deflater = new Deflater(); | |
31 | 48 |
49 public MyRfbProtoClient() { | |
50 | |
51 } | |
29 | 52 |
53 public MyRfbProtoClient(Reader reader, String host, String port) { | |
3 | 54 this.reader = reader; |
55 } | |
29 | 56 |
3 | 57 public void setParam(ConnectionParams connectionParams) { |
58 pHost = connectionParams.hostName; | |
59 echoPort = connectionParams.portNumber; | |
60 } | |
29 | 61 |
3 | 62 public boolean readProxyFlag() throws TransportException { |
63 int flag = reader.readUInt8(); | |
29 | 64 if (flag == 1) |
3 | 65 return true; |
66 else | |
67 return false; | |
68 } | |
29 | 69 |
3 | 70 public byte[] readEchoPort() throws Exception { |
71 byte[] b = new byte[4]; | |
72 reader.readBytes(b, 0, b.length); | |
29 | 73 // readFully(b); |
3 | 74 return b; |
75 } | |
29 | 76 |
3 | 77 public void getParentName() { |
78 if (echoValue == null) { | |
79 | |
80 if (clientSocket == null) { | |
81 | |
82 // echo = new EchoClient(pHost, this); | |
83 echoValue = new EchoClient(pHost, echoPort); | |
84 echoValue.openport(); | |
85 | |
86 echoValue = echoValue.requestHostName("1"); | |
87 } else { | |
88 echoValue = new EchoClient(); | |
89 echoValue = echoValue.Interruption(clientSocket); | |
90 } | |
91 } | |
92 // proxyからの返信で接続先を決定する | |
93 host = echoValue.responseLine; | |
94 parent = echoValue.parent; | |
95 if (echoValue.treenum != null) { | |
96 treenum = echoValue.treenum; | |
97 } else { | |
98 treenum = echoValue.treenum; | |
99 } | |
29 | 100 |
3 | 101 if (echoValue.leaderflag != null) { |
102 leaderflag = echoValue.leaderflag; | |
103 } else { | |
104 leaderflag = echoValue.leaderflag; | |
105 } | |
106 } | |
29 | 107 |
3 | 108 int castByteInt(byte[] b) { |
109 ByteBuffer bb = ByteBuffer.wrap(b); | |
110 int value = bb.getInt(); | |
111 return value; | |
112 } | |
29 | 113 |
3 | 114 Socket changeParent(String host, int port) throws IOException { |
115 sock = new Socket(host, port); | |
116 return sock; | |
117 } | |
29 | 118 |
31 | 119 @Override |
4 | 120 public Socket accept() throws IOException { |
121 return servSock.accept(); | |
122 } | |
29 | 123 |
4 | 124 void initServSock(int port) throws IOException { |
125 servSock = new ServerSocket(port); | |
126 acceptPort = port; | |
127 } | |
29 | 128 |
31 | 129 @Override |
4 | 130 public void selectPort(int p) { |
131 int port = p; | |
132 while (true) { | |
133 try { | |
134 initServSock(port); | |
135 break; | |
136 } catch (BindException e) { | |
137 port++; | |
138 continue; | |
139 } catch (IOException e) { | |
140 | |
141 } | |
142 } | |
143 System.out.println("accept port = " + port); | |
144 } | |
29 | 145 |
146 | |
4 | 147 void sendRfbVersion(OutputStream os) throws IOException { |
148 os.write(versionMsg_3_855.getBytes()); | |
29 | 149 // os.write(versionMsg_3_8.getBytes()); |
4 | 150 } |
29 | 151 |
4 | 152 int readVersionMsg(InputStream is, OutputStream os) throws IOException { |
153 | |
154 byte[] b = new byte[12]; | |
155 | |
156 is.read(b); | |
157 | |
158 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') | |
159 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') | |
160 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') | |
161 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9') | |
162 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) { | |
163 throw new IOException("Host " + host + " port " + port | |
164 + " is not an RFB server"); | |
165 } | |
166 | |
167 int rfbMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); | |
168 int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); | |
169 | |
170 if (rfbMajor < 3) { | |
171 throw new IOException( | |
29 | 172 "RFB server does not support protocol version 3"); |
4 | 173 } |
174 | |
175 if (rfbMinor == 855) { | |
176 sendProxyFlag(os); | |
29 | 177 // if(proxyFlag)sendPortNumber(os); |
4 | 178 } |
179 return rfbMinor; | |
29 | 180 |
181 } | |
182 | |
183 void readVersionMsg(InputStream is) throws IOException { | |
4 | 184 |
185 byte[] b = new byte[12]; | |
186 | |
187 is.read(b); | |
188 | |
189 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') | |
190 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') | |
191 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') | |
192 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9') | |
193 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) { | |
194 throw new IOException("Host " + host + " port " + port | |
195 + " is not an RFB server"); | |
196 } | |
197 | |
198 serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); | |
199 serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); | |
200 | |
201 if (serverMajor < 3) { | |
202 throw new IOException( | |
203 "RFB server does not support protocol version 3"); | |
204 } | |
205 | |
206 } | |
29 | 207 |
4 | 208 void sendSecurityType(OutputStream os) throws IOException { |
209 // number-of-security-types | |
210 os.write(1); | |
211 // security-types | |
212 // 1:None | |
213 os.write(1); | |
214 } | |
29 | 215 |
4 | 216 void readSecType(InputStream is) throws IOException { |
217 byte[] b = new byte[1]; | |
218 is.read(b); | |
219 | |
220 } | |
29 | 221 |
4 | 222 void sendSecResult(OutputStream os) throws IOException { |
223 byte[] b = castIntByte(0); | |
224 os.write(b); | |
225 } | |
29 | 226 |
4 | 227 void readClientInit(InputStream in) throws IOException { |
228 byte[] b = new byte[0]; | |
229 in.read(b); | |
230 } | |
29 | 231 |
4 | 232 void sendInitData(OutputStream os) throws IOException { |
20
98519d16a8c3
getInitData from ProtocolContext
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
12
diff
changeset
|
233 os.write(context.getInitData()); |
4 | 234 } |
29 | 235 |
4 | 236 void sendProxyFlag(OutputStream os) throws IOException { |
29 | 237 if (proxyFlag) |
238 os.write(1); | |
239 else | |
240 os.write(0); | |
4 | 241 } |
29 | 242 |
4 | 243 byte[] castIntByte(int len) { |
244 byte[] b = new byte[4]; | |
245 b[0] = (byte) ((len >>> 24) & 0xFF); | |
246 b[1] = (byte) ((len >>> 16) & 0xFF); | |
247 b[2] = (byte) ((len >>> 8) & 0xFF); | |
248 b[3] = (byte) ((len >>> 0) & 0xFF); | |
249 return b; | |
250 } | |
29 | 251 |
4 | 252 /** |
253 * gzip byte arrays | |
29 | 254 * |
4 | 255 * @param deflater |
256 * @param inputs | |
257 * byte data[] | |
29 | 258 * @param inputIndex |
4 | 259 * @param outputs |
260 * byte data[] | |
29 | 261 * @return byte length in last byte array |
4 | 262 * @throws IOException |
263 */ | |
29 | 264 public int zip(Deflater deflater, LinkedList<ByteBuffer> inputs, |
265 int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException { | |
4 | 266 int len = 0; |
29 | 267 ByteBuffer c1 = ByteBuffer.allocate(INFLATE_BUFSIZE); |
268 while (inputIndex < inputs.size()) { | |
4 | 269 ByteBuffer b1 = inputs.get(inputIndex++); |
29 | 270 deflater.setInput(b1.array(), b1.position(), b1.remaining()); |
4 | 271 /** |
29 | 272 * If we finish() stream and reset() it, Deflater start new gzip |
273 * stream, this makes continuous zlib reader unhappy. if we remove | |
274 * finish(), Deflater.deflate() never flushes its output. The | |
275 * original zlib deflate has flush flag. I'm pretty sure this a kind | |
276 * of bug of Java library. | |
4 | 277 */ |
29 | 278 if (inputIndex == inputs.size()) |
4 | 279 deflater.finish(); |
280 int len1 = 0; | |
281 do { | |
29 | 282 len1 = deflater.deflate(c1.array(), c1.position(), |
283 c1.remaining()); | |
284 if (len1 > 0) { | |
4 | 285 len += len1; |
29 | 286 c1.position(c1.position() + len1); |
287 if (c1.remaining() == 0) { | |
288 c1.flip(); | |
289 outputs.addLast(c1); | |
4 | 290 c1 = ByteBuffer.allocate(INFLATE_BUFSIZE); |
291 } | |
292 } | |
29 | 293 } while (len1 > 0 || !deflater.needsInput()); // &&!deflater.finished()); |
4 | 294 } |
29 | 295 if (c1.position() != 0) { |
296 c1.flip(); | |
297 outputs.addLast(c1); | |
4 | 298 } |
299 deflater.reset(); | |
300 return len; | |
301 } | |
29 | 302 |
4 | 303 /** |
304 * gunzip byte arrays | |
29 | 305 * |
4 | 306 * @param inflater |
307 * @param inputs | |
308 * byte data[] | |
309 * @param outputs | |
310 * byte data[] | |
29 | 311 * @return number of total bytes |
4 | 312 * @throws IOException |
313 */ | |
314 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, | |
315 int inputIndex, LinkedList<ByteBuffer> outputs, int bufSize) | |
316 throws DataFormatException { | |
317 int len = 0; | |
318 ByteBuffer buf = ByteBuffer.allocate(bufSize); | |
319 while (inputIndex < inputs.size()) { | |
320 ByteBuffer input = inputs.get(inputIndex++); | |
321 inflater.setInput(input.array(), input.position(), input.limit()); | |
322 // if (inputIndex==inputs.size()) if inflater/deflater has symmetry, | |
323 // we need this | |
324 // inflater.end(); but this won't work | |
325 do { | |
326 int len0 = inflater.inflate(buf.array(), buf.position(), | |
327 buf.remaining()); | |
328 if (len0 > 0) { | |
329 buf.position(buf.position() + len0); | |
330 len += len0; | |
331 if (buf.remaining() == 0) { | |
332 buf.flip(); | |
333 outputs.addLast(buf); | |
334 buf = ByteBuffer.allocate(bufSize); | |
335 } | |
336 } | |
337 } while (!inflater.needsInput()); | |
338 } | |
339 if (buf.position() != 0) { | |
340 buf.flip(); | |
341 outputs.addLast(buf); | |
342 } | |
343 return len; | |
344 } | |
29 | 345 |
346 public void readSendData(int dataLen, Reader reader) | |
347 throws TransportException { | |
348 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>(); | |
4 | 349 ByteBuffer header = ByteBuffer.allocate(16); |
29 | 350 reader.readBytes(header.array(), 0, 16); |
4 | 351 header.limit(16); |
29 | 352 if (header.get(0) == FramebufferUpdate) { |
4 | 353 int encoding = header.getInt(12); |
29 | 354 if (encoding == EncodingType.ZRLE.getId() |
355 || encoding == EncodingType.ZLIB.getId()) { // ZRLEE is | |
356 // already | |
357 // recompressed | |
4 | 358 ByteBuffer len = ByteBuffer.allocate(4); |
29 | 359 reader.readBytes(len.array(), 0, 4); |
360 len.limit(4); | |
361 ByteBuffer inputData = ByteBuffer.allocate(dataLen - 20); | |
362 reader.readBytes(inputData.array(), 0, inputData.capacity()); | |
363 inputData.limit(dataLen - 20); | |
364 LinkedList<ByteBuffer> inputs = new LinkedList<ByteBuffer>(); | |
4 | 365 inputs.add(inputData); |
366 | |
29 | 367 header.putInt(12, EncodingType.ZRLEE.getId()); // means |
368 // recompress | |
369 // every time | |
370 // using new Deflecter every time is incompatible with the | |
371 // protocol, clients have to be modified. | |
4 | 372 Deflater nDeflater = deflater; // new Deflater(); |
373 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); | |
29 | 374 try { |
375 unzip(inflater, inputs, 0, out, INFLATE_BUFSIZE); | |
376 // dump32(inputs); | |
377 int len2 = zip(nDeflater, out, 0, bufs); | |
378 ByteBuffer blen = ByteBuffer.allocate(4); | |
379 blen.putInt(len2); | |
380 blen.flip(); | |
381 bufs.addFirst(blen); | |
4 | 382 |
29 | 383 bufs.addFirst(header); |
384 multicastqueue.put(bufs); | |
385 is.reset(); | |
386 } catch (DataFormatException e) { | |
387 throw new TransportException(e); | |
388 } catch (IOException e) { | |
389 throw new TransportException(e); | |
390 } | |
391 return; | |
4 | 392 } |
29 | 393 } |
4 | 394 bufs.add(header); |
29 | 395 if (dataLen > 16) { |
396 ByteBuffer b = ByteBuffer.allocate(dataLen - 16); | |
397 reader.readBytes(b.array(), 0, dataLen - 16); | |
398 b.limit(dataLen - 16); | |
4 | 399 bufs.add(b); |
400 } | |
401 multicastqueue.put(bufs); | |
29 | 402 try { |
403 is.reset(); | |
404 } catch (IOException e) { | |
405 throw new TransportException(e); | |
406 } | |
4 | 407 |
29 | 408 // It may be compressed. We can inflate here to avoid repeating clients |
409 // decompressing here, | |
410 // but it may generate too many large data. It is better to do it in | |
411 // each client. | |
412 // But we have do inflation for all input data, so we have to do it | |
413 // here. | |
4 | 414 } |
415 | |
3 | 416 } |