annotate src/main/java/com/glavsoft/rfb/protocol/SenderTask.java @ 9:3c0f262384dc

send EncodingType.DESKTOP_SIZE AliceVNCMessage
author YU
date Tue, 23 Sep 2014 16:15:59 +0900
parents daa24f8a557b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
daa24f8a557b TightVNC original
YU
parents:
diff changeset
1 // Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
2 // All rights reserved.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
3 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
4 //-------------------------------------------------------------------------
daa24f8a557b TightVNC original
YU
parents:
diff changeset
5 // This file is part of the TightVNC software. Please visit our Web site:
daa24f8a557b TightVNC original
YU
parents:
diff changeset
6 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
7 // http://www.tightvnc.com/
daa24f8a557b TightVNC original
YU
parents:
diff changeset
8 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
9 // This program is free software; you can redistribute it and/or modify
daa24f8a557b TightVNC original
YU
parents:
diff changeset
10 // it under the terms of the GNU General Public License as published by
daa24f8a557b TightVNC original
YU
parents:
diff changeset
11 // the Free Software Foundation; either version 2 of the License, or
daa24f8a557b TightVNC original
YU
parents:
diff changeset
12 // (at your option) any later version.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
13 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
14 // This program is distributed in the hope that it will be useful,
daa24f8a557b TightVNC original
YU
parents:
diff changeset
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
daa24f8a557b TightVNC original
YU
parents:
diff changeset
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
daa24f8a557b TightVNC original
YU
parents:
diff changeset
17 // GNU General Public License for more details.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
18 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
19 // You should have received a copy of the GNU General Public License along
daa24f8a557b TightVNC original
YU
parents:
diff changeset
20 // with this program; if not, write to the Free Software Foundation, Inc.,
daa24f8a557b TightVNC original
YU
parents:
diff changeset
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
22 //-------------------------------------------------------------------------
daa24f8a557b TightVNC original
YU
parents:
diff changeset
23 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
24
daa24f8a557b TightVNC original
YU
parents:
diff changeset
25 package com.glavsoft.rfb.protocol;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
26
daa24f8a557b TightVNC original
YU
parents:
diff changeset
27 import com.glavsoft.exceptions.TransportException;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
28 import com.glavsoft.rfb.client.ClientToServerMessage;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
29 import com.glavsoft.transport.Writer;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
30
daa24f8a557b TightVNC original
YU
parents:
diff changeset
31 import java.io.PrintWriter;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
32 import java.io.StringWriter;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
33 import java.util.logging.Logger;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
34
daa24f8a557b TightVNC original
YU
parents:
diff changeset
35 public class SenderTask implements Runnable {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
36
daa24f8a557b TightVNC original
YU
parents:
diff changeset
37 private final MessageQueue queue;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
38 private final Writer writer;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
39 private final ProtocolContext protocolContext;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
40 private volatile boolean isRunning = false;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
41
daa24f8a557b TightVNC original
YU
parents:
diff changeset
42 /**
daa24f8a557b TightVNC original
YU
parents:
diff changeset
43 * Create sender task
daa24f8a557b TightVNC original
YU
parents:
diff changeset
44 * Task runs as thread, receive messages from queue and sends them to writer.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
45 * When no messages appears in queue longer than timeout period, sends FramebufferUpdate
daa24f8a557b TightVNC original
YU
parents:
diff changeset
46 * request
daa24f8a557b TightVNC original
YU
parents:
diff changeset
47 * @param messageQueue queue to poll messages
daa24f8a557b TightVNC original
YU
parents:
diff changeset
48 * @param writer writer to send messages out
daa24f8a557b TightVNC original
YU
parents:
diff changeset
49 * @param protocolContext protocol
daa24f8a557b TightVNC original
YU
parents:
diff changeset
50 */
daa24f8a557b TightVNC original
YU
parents:
diff changeset
51 public SenderTask(MessageQueue messageQueue, Writer writer, ProtocolContext protocolContext) {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
52 this.queue = messageQueue;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
53 this.writer = writer;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
54 this.protocolContext = protocolContext;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
55 }
daa24f8a557b TightVNC original
YU
parents:
diff changeset
56
daa24f8a557b TightVNC original
YU
parents:
diff changeset
57 @Override
daa24f8a557b TightVNC original
YU
parents:
diff changeset
58 public void run() {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
59 isRunning = true;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
60 while (isRunning) {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
61 ClientToServerMessage message;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
62 try {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
63 message = queue.get();
daa24f8a557b TightVNC original
YU
parents:
diff changeset
64 if (message != null) {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
65 message.send(writer);
daa24f8a557b TightVNC original
YU
parents:
diff changeset
66 }
daa24f8a557b TightVNC original
YU
parents:
diff changeset
67 } catch (InterruptedException e) {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
68 // nop
daa24f8a557b TightVNC original
YU
parents:
diff changeset
69 } catch (TransportException e) {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
70 Logger.getLogger(getClass().getName()).severe("Close session: " + e.getMessage());
daa24f8a557b TightVNC original
YU
parents:
diff changeset
71 if (isRunning) {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
72 protocolContext.cleanUpSession("Connection closed");
daa24f8a557b TightVNC original
YU
parents:
diff changeset
73 }
daa24f8a557b TightVNC original
YU
parents:
diff changeset
74 stopTask();
daa24f8a557b TightVNC original
YU
parents:
diff changeset
75 } catch (Throwable te) {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
76 StringWriter sw = new StringWriter();
daa24f8a557b TightVNC original
YU
parents:
diff changeset
77 PrintWriter pw = new PrintWriter(sw);
daa24f8a557b TightVNC original
YU
parents:
diff changeset
78 te.printStackTrace(pw);
daa24f8a557b TightVNC original
YU
parents:
diff changeset
79 if (isRunning) {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
80 protocolContext.cleanUpSession(te.getMessage() + "\n" + sw.toString());
daa24f8a557b TightVNC original
YU
parents:
diff changeset
81 }
daa24f8a557b TightVNC original
YU
parents:
diff changeset
82 stopTask();
daa24f8a557b TightVNC original
YU
parents:
diff changeset
83 }
daa24f8a557b TightVNC original
YU
parents:
diff changeset
84 }
daa24f8a557b TightVNC original
YU
parents:
diff changeset
85 }
daa24f8a557b TightVNC original
YU
parents:
diff changeset
86
daa24f8a557b TightVNC original
YU
parents:
diff changeset
87 public void stopTask() {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
88 isRunning = false;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
89 }
daa24f8a557b TightVNC original
YU
parents:
diff changeset
90
daa24f8a557b TightVNC original
YU
parents:
diff changeset
91 }