12
|
1 package jp.ac.u_ryukyu.treevnc.server;
|
11
|
2
|
|
3 import java.awt.event.*;
|
|
4 import java.io.*;
|
|
5 import java.util.logging.Logger;
|
|
6
|
|
7
|
|
8 import com.glavsoft.exceptions.AuthenticationFailedException;
|
|
9 import com.glavsoft.exceptions.FatalException;
|
|
10 import com.glavsoft.exceptions.TransportException;
|
|
11 import com.glavsoft.exceptions.UnsupportedProtocolVersionException;
|
|
12 import com.glavsoft.exceptions.UnsupportedSecurityTypeException;
|
|
13 import com.glavsoft.rfb.IChangeSettingsListener;
|
|
14 import com.glavsoft.rfb.IRfbSessionListener;
|
|
15 import com.glavsoft.rfb.protocol.Protocol;
|
|
16 import com.glavsoft.rfb.protocol.ProtocolSettings;
|
|
17 import com.glavsoft.transport.Reader;
|
|
18 import com.glavsoft.transport.Writer;
|
|
19 import com.glavsoft.viewer.ConnectionManager;
|
|
20 import com.glavsoft.viewer.Viewer;
|
|
21 import com.glavsoft.viewer.cli.Parser;
|
|
22 import com.glavsoft.viewer.swing.ClipboardControllerImpl;
|
|
23 import com.glavsoft.viewer.swing.ParametersHandler;
|
|
24 import com.glavsoft.viewer.swing.Surface;
|
|
25 import com.glavsoft.viewer.swing.UiSettings;
|
|
26 import com.glavsoft.viewer.swing.ParametersHandler.ConnectionParams;
|
|
27
|
|
28 public class VncProxyService extends Viewer implements Runnable,
|
|
29 IRfbSessionListener, IChangeSettingsListener {
|
|
30
|
|
31 /**
|
|
32 *
|
|
33 */
|
|
34 private static final long serialVersionUID = 1L;
|
|
35
|
12
|
36 public MyRfbProtoProxy rfb;
|
11
|
37 public VncProxyService(Parser parser) {
|
|
38 this();
|
12
|
39 try {
|
|
40 rfb = new MyRfbProtoProxy();
|
|
41 } catch (IOException e) {
|
|
42 e.printStackTrace();
|
|
43 }
|
11
|
44 ParametersHandler.completeSettingsFromCLI(parser, connectionParams, settings, uiSettings);
|
|
45 showControls = ParametersHandler.showControls;
|
|
46 passwordFromParams = parser.getValueFor(ParametersHandler.ARG_PASSWORD);
|
|
47 logger.info("TightVNC Viewer version " + ver());
|
|
48 isApplet = false;
|
|
49 }
|
|
50
|
|
51 public VncProxyService() {
|
|
52 connectionParams = new ParametersHandler.ConnectionParams();
|
|
53 settings = ProtocolSettings.getDefaultSettings();
|
|
54 uiSettings = super.uiSettings;
|
|
55
|
|
56 }
|
|
57
|
|
58 boolean isClient = false;
|
|
59 public static final int DEFAULT_PORT = 5900;
|
|
60 public static Logger logger = Logger.getLogger("com.glavsoft");
|
|
61 private boolean forceReconnection;
|
|
62 private String reconnectionReason;
|
|
63 private ConnectionParams connectionParams;
|
|
64 private final ProtocolSettings settings;
|
|
65 private final UiSettings uiSettings;
|
|
66 private String[] arguments;
|
|
67
|
|
68
|
|
69 public static void main(String[] argv) {
|
|
70 String[] mainArgs = argv;
|
|
71 System.out.println(mainArgs.length);
|
|
72 // input into arguments Decision
|
|
73
|
|
74 Parser parser = new Parser();
|
|
75 ParametersHandler.completeParserOptions(parser);
|
|
76
|
|
77 parser.parse(argv);
|
|
78 if (parser.isSet(ParametersHandler.ARG_HELP)) {
|
|
79 printUsage(parser.optionsUsage());
|
|
80 System.exit(0);
|
|
81 }
|
|
82 VncProxyService vps = new VncProxyService(parser);
|
|
83 if (mainArgs.length != 0) {
|
|
84 for (int i = 0; i < argv.length; i++) {
|
|
85 vps.setArguments(mainArgs);
|
|
86 // write later
|
|
87 }
|
|
88 } else {
|
|
89 //getHostData();
|
|
90 }
|
|
91 //SwingUtilities.invokeLater(myClient);
|
|
92 Thread th = new Thread(vps);
|
|
93 th.start();
|
|
94 }
|
|
95
|
|
96 private void setArguments(String[] mainArgs) {
|
|
97 arguments = mainArgs;
|
|
98 }
|
|
99
|
|
100 @Override
|
|
101 public void run() {
|
|
102 // getHostData();
|
|
103 ConnectionManager connectionManager = new ConnectionManager(this, isApplet);
|
|
104 if (forceReconnection) {
|
|
105 connectionManager.showReconnectDialog("Connection lost", reconnectionReason);
|
|
106 forceReconnection = false;
|
|
107 }
|
|
108 tryAgain = true;
|
|
109 while (tryAgain) {
|
|
110 connectionParams.hostName = arguments[0];
|
|
111 // workingSocket = connectionManager.connectToTreeHost(connectionParams, settings);
|
12
|
112 workingSocket = connectionManager.connectToHost(connectionParams, settings);
|
11
|
113 if (null == workingSocket) {
|
|
114 closeApp();
|
|
115 break;
|
|
116 }
|
|
117 logger.info("Connected");
|
|
118
|
|
119 try {
|
|
120 workingSocket.setTcpNoDelay(true); // disable Nagle algorithm
|
|
121 Reader reader = new Reader(workingSocket.getInputStream());
|
|
122 Writer writer = new Writer(workingSocket.getOutputStream());
|
|
123
|
|
124 workingProtocol = new Protocol(reader, writer,
|
|
125 new PasswordChooser(passwordFromParams, connectionParams, containerFrame, this),
|
|
126 settings);
|
|
127
|
|
128 workingProtocol.handshake();
|
|
129 // input into change parents
|
|
130 ClipboardControllerImpl clipboardController =
|
|
131 new ClipboardControllerImpl(workingProtocol, settings.getRemoteCharsetName());
|
|
132 clipboardController.setEnabled(settings.isAllowClipboardTransfer());
|
|
133 settings.addListener(clipboardController);
|
|
134
|
|
135 surface = new Surface(workingProtocol, this, uiSettings.getScaleFactor());
|
|
136 settings.addListener(this);
|
|
137 uiSettings.addListener(surface);
|
|
138 containerFrame = createContainer();
|
|
139 connectionManager.setContainerFrame(containerFrame);
|
|
140 updateFrameTitle();
|
|
141
|
|
142 workingProtocol.startNormalHandling(this, surface, clipboardController);
|
|
143 tryAgain = false;
|
|
144 } catch (UnsupportedProtocolVersionException e) {
|
|
145 connectionManager.showReconnectDialog("Unsupported Protocol Version", e.getMessage());
|
|
146 logger.severe(e.getMessage());
|
|
147 } catch (UnsupportedSecurityTypeException e) {
|
|
148 connectionManager.showReconnectDialog("Unsupported Security Type", e.getMessage());
|
|
149 logger.severe(e.getMessage());
|
|
150 } catch (AuthenticationFailedException e) {
|
|
151 passwordFromParams = null;
|
|
152 connectionManager.showReconnectDialog("Authentication Failed", e.getMessage());
|
|
153 logger.severe(e.getMessage());
|
|
154 } catch (TransportException e) {
|
|
155 connectionManager.showReconnectDialog("Connection Error", "Connection Error" + ": " + e.getMessage());
|
|
156 logger.severe(e.getMessage());
|
|
157 } catch (IOException e) {
|
|
158 connectionManager.showReconnectDialog("Connection Error", "Connection Error" + ": " + e.getMessage());
|
|
159 logger.severe(e.getMessage());
|
|
160 } catch (FatalException e) {
|
|
161 connectionManager.showReconnectDialog("Connection Error", "Connection Error" + ": " + e.getMessage());
|
|
162 logger.severe(e.getMessage());
|
|
163 }
|
|
164 }
|
|
165
|
|
166 }
|
|
167
|
|
168
|
|
169
|
|
170 @Override
|
|
171 public void destroy() {
|
|
172
|
|
173 }
|
|
174
|
|
175
|
|
176
|
|
177 @Override
|
|
178 public void windowClosing(WindowEvent e) {
|
|
179 if (e != null && e.getComponent() != null) {
|
|
180 e.getWindow().setVisible(false);
|
|
181 }
|
|
182 closeApp();
|
|
183 }
|
|
184 }
|