Mercurial > hg > Applications > TreeVNC
annotate src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java @ 94:75879c316796
synchronized wait for Rfb initialization in change server.
author | oc |
---|---|
date | Mon, 19 May 2014 19:57:32 +0900 |
parents | 1503a373ab73 |
children | 76e49dd9c9c5 |
rev | line source |
---|---|
32 | 1 package jp.ac.u_ryukyu.treevnc; |
28 | 2 |
45 | 3 import java.io.DataOutputStream; |
28 | 4 import java.io.IOException; |
5 import java.net.Socket; | |
6 import java.nio.ByteBuffer; | |
7 import java.util.LinkedList; | |
8 import java.util.concurrent.atomic.AtomicInteger; | |
9 | |
35 | 10 import jp.ac.u_ryukyu.treevnc.client.EchoClient; |
28 | 11 import jp.ac.u_ryukyu.treevnc.server.RequestScreenThread; |
94
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
12 import jp.ac.u_ryukyu.treevnc.server.VncProxyService; |
28 | 13 |
14 import com.glavsoft.exceptions.TransportException; | |
15 import com.glavsoft.rfb.protocol.Protocol; | |
16 import com.glavsoft.rfb.protocol.ProtocolContext; | |
17 import com.glavsoft.transport.Reader; | |
18 import com.glavsoft.transport.Writer; | |
65 | 19 import com.glavsoft.viewer.ViewerImpl; |
28 | 20 |
21 public class MyRfbProto { | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
22 final static int FramebufferUpdateRequest = 3; |
28 | 23 final static int CheckDelay = 11; |
24 final static int FramebufferUpdate = 0; | |
25 private ProtocolContext context; | |
54 | 26 //final static String versionMsg_3_855 = "RFB 003.855\n"; |
27 final static String versionMsg_3_856 = "RFB 003.856\n"; | |
28 | 28 private int clients; |
32 | 29 protected MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>(); |
28 | 30 private RequestScreenThread rThread; |
31 private boolean proxyFlag = true; | |
35 | 32 private EchoClient echo; |
45 | 33 private String proxyAddr; |
60 | 34 public int acceptPort; |
94
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
35 protected boolean readyReconnect = false; |
65 | 36 private boolean cuiVersion; |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
37 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
38 public MyRfbProto() { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
39 rThread = new RequestScreenThread(this); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
40 } |
28 | 41 |
42 | |
43 public void newClient(AcceptThread acceptThread, final Socket newCli, | |
44 final Writer os, final Reader is) throws IOException { | |
45 // createBimgFlag = true; | |
46 // rfb.addSockTmp(newCli); | |
47 // addSock(newCli); | |
48 final int myId = clients; | |
49 final MulticastQueue.Client<LinkedList<ByteBuffer>> c = multicastqueue.newClient(); | |
50 final AtomicInteger writerRunning = new AtomicInteger(); | |
51 writerRunning.set(1); | |
52 /** | |
53 * Timeout thread. If a client is suspended, it has top of queue | |
54 * indefinitely, which caused memory overflow. After the timeout, we | |
55 * poll the queue and discard it. Start long wait if writer is running. | |
56 */ | |
57 final Runnable timer = new Runnable() { | |
58 public void run() { | |
59 int count = 0; | |
60 for (;;) { | |
61 long timeout = 50000 / 8; | |
62 try { | |
63 synchronized (this) { | |
64 int state, flag; | |
65 writerRunning.set(0); | |
66 wait(timeout); | |
67 flag = 0; | |
68 while ((state = writerRunning.get()) == 0) { | |
69 c.poll(); // discard, should be timeout | |
70 count++; | |
71 if (flag == 0) { | |
72 System.out.println("Discarding " + myId | |
73 + " count=" + count); | |
74 flag = 1; | |
75 } | |
76 wait(10); // if this is too short, writer cannot | |
77 // take the poll, if this is too | |
78 // long, memory will overflow... | |
79 } | |
80 if (flag == 1) | |
81 System.out.println("Resuming " + myId | |
82 + " count=" + count); | |
83 if (state != 1) { | |
84 System.out.println("Client died " + myId); | |
85 break; | |
86 } | |
87 } | |
88 } catch (InterruptedException e) { | |
89 } | |
90 } | |
91 } | |
92 }; | |
88 | 93 new Thread(timer, "timer-discard-multicastqueue").start(); |
28 | 94 /** |
95 * discard all incoming from clients | |
96 */ | |
97 final Runnable reader = new Runnable() { | |
98 public void run() { | |
99 byte b[] = new byte[4096]; | |
100 for (;;) { | |
101 try { | |
102 int c = is.readByte(b); | |
103 if (c <= 0) | |
104 throw new IOException(); | |
105 // System.out.println("client read "+c); | |
89 | 106 } catch (Exception e) { |
28 | 107 try { |
108 writerRunning.set(2); | |
109 os.close(); | |
110 is.close(); | |
36 | 111 break; |
28 | 112 } catch (IOException e1) { |
113 } catch (TransportException e1) { | |
114 e1.printStackTrace(); | |
115 } | |
116 return; | |
117 } | |
118 } | |
119 } | |
120 }; | |
121 /** | |
122 * send packets to a client | |
123 */ | |
124 Runnable sender = new Runnable() { | |
125 public void run() { | |
126 writerRunning.set(1); | |
127 try { | |
128 requestThreadNotify(); | |
129 | |
130 /** | |
131 * initial connection of RFB protocol | |
132 */ | |
133 sendRfbVersion(os); | |
134 // readVersionMsg(is); | |
135 readVersionMsg(is, os); | |
136 sendSecurityType(os); | |
137 readSecType(is); | |
138 sendSecResult(os); | |
139 readClientInit(is); | |
140 sendInitData(os); | |
88 | 141 // after this, we discard upward packet. |
142 new Thread(reader, "discard-upward-comm").start(); | |
28 | 143 // writeFramebufferUpdateRequest(0,0, framebufferWidth, |
144 // framebufferHeight, false ); | |
145 for (;;) { | |
146 LinkedList<ByteBuffer> bufs = c.poll(); | |
147 int inputIndex = 0; | |
148 ByteBuffer header = bufs.get(inputIndex); | |
149 if (header == null) | |
150 continue; | |
151 else if (header.get(0) == CheckDelay) { | |
152 writeToClient(os, bufs, inputIndex); | |
153 continue; | |
154 } else if (header.get(0) == FramebufferUpdate) { | |
63 | 155 //System.out.println("client "+ myId); |
28 | 156 } |
157 /* | |
158 * if(i%20==0){ sendDataCheckDelay(); } i++; | |
159 */ | |
160 writeToClient(os, bufs, inputIndex); | |
161 writerRunning.set(1); // yes my client is awaking. | |
162 } | |
89 | 163 } catch (Exception e) { |
28 | 164 try { |
165 writerRunning.set(2); | |
166 os.close(); | |
167 } catch (IOException e1) { | |
168 } | |
169 /* if socket closed cliList.remove(newCli); */ | |
170 } | |
171 } | |
172 | |
173 public void writeToClient(final Writer os, | |
174 LinkedList<ByteBuffer> bufs, int inputIndex) | |
175 throws TransportException { | |
176 while (inputIndex < bufs.size()) { | |
177 ByteBuffer b = bufs.get(inputIndex++); | |
178 os.write(b.array(), b.position(), b.limit()); | |
179 } | |
180 os.flush(); | |
74 | 181 bufs = null; |
182 multicastqueue.heapAvailable(); | |
28 | 183 } |
184 }; | |
185 clients++; | |
88 | 186 new Thread(sender, "writer-to-lower-node").start(); |
28 | 187 |
188 } | |
189 | |
88 | 190 public void requestThreadNotify() { |
28 | 191 rThread.reStart(); |
192 } | |
193 | |
194 private void sendRfbVersion(Writer writer) throws IOException, TransportException { | |
195 // os.write(versionMsg_3_8.getBytes()); | |
54 | 196 writer.write(versionMsg_3_856.getBytes()); |
28 | 197 } |
198 | |
199 private int readVersionMsg(Reader reader, Writer writer) throws IOException, TransportException { | |
200 | |
201 byte[] b = new byte[12]; | |
202 | |
203 reader.readBytes(b); | |
204 | |
205 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') | |
206 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') | |
207 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') | |
208 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9') | |
209 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) { | |
210 throw new IOException("this is not an RFB server"); | |
211 } | |
212 | |
213 int rfbMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); | |
214 int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); | |
215 | |
216 if (rfbMajor < 3) { | |
217 throw new IOException( | |
218 "RFB server does not support protocol version 3"); | |
219 } | |
220 | |
221 if (rfbMinor == 855) { | |
222 sendProxyFlag(writer); | |
223 if (proxyFlag) | |
224 sendPortNumber(writer); | |
225 } | |
226 return rfbMinor; | |
227 } | |
45 | 228 |
229 public void screenChangeRequest() throws IOException { | |
64
e73e2c30c9a6
before update for build CUI version.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
230 Socket echoSocket = new Socket(proxyAddr, 10002); |
45 | 231 DataOutputStream os = new DataOutputStream(echoSocket.getOutputStream()); |
232 os.writeBytes(echo.getMyAddress()+"\n"); | |
59 | 233 //os.writeBytes(String.valueOf(echo.client.getFrameWidth())+"\n"); temp comment out for rebuild |
234 //os.writeBytes(String.valueOf(echo.client.getFrameHeight())+"\n"); temp comment out for rebuild | |
45 | 235 os.close(); |
77 | 236 echoSocket.close(); |
45 | 237 } |
28 | 238 |
239 private void sendProxyFlag(Writer writer) throws TransportException { | |
240 if (proxyFlag) | |
241 writer.writeInt(1); | |
242 else | |
243 writer.writeInt(0); | |
244 } | |
245 | |
246 private void sendPortNumber(Writer writer) throws TransportException { | |
247 byte[] b = new byte[4]; | |
248 //b = castIntByte(getHost.getPort()); | |
249 b = castIntByte(9999); | |
250 writer.write(b); | |
251 } | |
252 | |
253 private byte[] castIntByte(int len) { | |
254 byte[] b = new byte[4]; | |
255 b[0] = (byte) ((len >>> 24) & 0xFF); | |
256 b[1] = (byte) ((len >>> 16) & 0xFF); | |
257 b[2] = (byte) ((len >>> 8) & 0xFF); | |
258 b[3] = (byte) ((len >>> 0) & 0xFF); | |
259 return b; | |
260 } | |
261 | |
45 | 262 |
28 | 263 private void readSecType(Reader reader) throws TransportException { |
264 byte[] b = new byte[1]; | |
265 reader.read(b); | |
266 } | |
267 | |
268 private void sendSecurityType(Writer os) throws TransportException { | |
269 // number-of-security-types | |
270 os.writeInt(1); | |
271 // security-types | |
272 // 1:None | |
273 os.writeInt(1); | |
274 | |
275 /* | |
276 * os.write(4); os.write(30); os.write(31); os.write(32); os.write(35); | |
277 * os.flush(); | |
278 */ | |
279 } | |
280 | |
281 private void sendSecResult(Writer os) throws TransportException { | |
282 byte[] b = castIntByte(0); | |
283 os.write(b); | |
284 } | |
285 | |
286 private void readClientInit(Reader in) throws TransportException { | |
287 byte[] b = new byte[0]; | |
288 in.readBytes(b); | |
289 } | |
290 | |
85 | 291 byte initData[] = {7, -128, 4, 56, 32, 24, 0, 1, 0, -1, 0, -1, 0, -1, 16, 8, 0, 0, 0, 0, 0, 0, 0, 7, 102, 105, 114, 101, 102, 108, 121}; |
94
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
292 protected VncProxyService waiter; |
28 | 293 private void sendInitData(Writer os) throws TransportException { |
85 | 294 // In case of "-d" we have no context |
295 if (context != null){ | |
296 os.write(context.getInitData()); | |
297 } else { | |
298 // Send dummy data | |
299 os.write(initData); | |
300 | |
301 } | |
28 | 302 } |
303 | |
304 public void setProtocolContext(Protocol workingProtocol) { | |
305 context = workingProtocol; | |
306 } | |
29 | 307 |
308 | |
309 public void readSendData(int dataLen, Reader reader) throws TransportException { | |
310 | |
311 } | |
31 | 312 |
313 public Socket accept() throws IOException { | |
314 return null; | |
315 } | |
316 | |
38 | 317 public int selectPort(int port) { |
318 return port; | |
31 | 319 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
320 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
321 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
322 public void writeFramebufferUpdateRequest(int x, int y, int w, int h, |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
323 boolean incremental) throws TransportException { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
324 byte[] b = new byte[10]; |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
325 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
326 b[0] = (byte) FramebufferUpdateRequest; // 3 is FrameBufferUpdateRequest |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
327 b[1] = (byte) (incremental ? 1 : 0); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
328 b[2] = (byte) ((x >> 8) & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
329 b[3] = (byte) (x & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
330 b[4] = (byte) ((y >> 8) & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
331 b[5] = (byte) (y & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
332 b[6] = (byte) ((w >> 8) & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
333 b[7] = (byte) (w & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
334 b[8] = (byte) ((h >> 8) & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
335 b[9] = (byte) (h & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
336 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
337 // os.write(b); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
338 } |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
339 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
340 public void notProxy() { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
341 proxyFlag = false; |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
342 } |
35 | 343 |
344 public void setEcho(EchoClient _echo) { | |
345 echo = _echo; | |
346 } | |
347 | |
65 | 348 public void setViewer(ViewerImpl v) { |
36 | 349 echo.setViewer(v); |
350 } | |
351 | |
65 | 352 public ViewerImpl getViewer() { |
353 return echo.getViewer(); | |
354 } | |
355 | |
35 | 356 public EchoClient getEcho() { |
357 return echo; | |
358 } | |
43 | 359 |
360 public void setTerminationType(boolean setType) { | |
361 /*nop*/ | |
362 } | |
363 | |
364 public boolean getTerminationType() { | |
365 /*nop*/ | |
366 return true; | |
367 } | |
45 | 368 |
369 public void setProxyAddr(String proxyAddr) { | |
370 this.proxyAddr = proxyAddr; | |
371 } | |
52 | 372 |
373 | |
374 public void close() { | |
375 //nothing | |
376 } | |
45 | 377 |
60 | 378 public int getAcceptPort() { |
379 return 0; | |
380 } | |
381 | |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
382 public boolean getReadyReconnect() { |
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
383 return readyReconnect; |
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
384 } |
65 | 385 |
386 | |
387 public boolean getCuiVersion() { | |
388 return cuiVersion; | |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
389 } |
65 | 390 |
391 public void setCuiVersion(boolean flag) { | |
392 cuiVersion = flag; | |
393 } | |
66 | 394 |
395 public void readCheckDelay(Reader reader) throws TransportException { | |
396 | |
397 } | |
398 | |
399 public String getProxyAddr() { | |
400 return proxyAddr; | |
401 } | |
65 | 402 |
94
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
403 public synchronized void setReadyReconnect(boolean ready) { |
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
404 readyReconnect = ready; |
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
405 if (ready) { |
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
406 notifyAll(); |
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
407 } |
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
408 } |
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
409 |
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
410 public synchronized void waitForReady(VncProxyService vncProxyService) throws InterruptedException { |
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
411 while (!readyReconnect) { |
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
412 wait(); |
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
413 } |
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
414 } |
75879c316796
synchronized wait for Rfb initialization in change server.
oc
parents:
89
diff
changeset
|
415 |
28 | 416 } |