Mercurial > hg > Applications > TightVNC_orginal
annotate src/main/java/jp/ac/u_ryukyu/treevnc/server/VncProxyService.java @ 48:6ebe870da138
this version resolve change host problem.
author | one |
---|---|
date | Tue, 18 Dec 2012 17:56:49 +0900 |
parents | f3b775d3592a |
children | b0c7fad4c695 |
rev | line source |
---|---|
12 | 1 package jp.ac.u_ryukyu.treevnc.server; |
11 | 2 |
3 import java.awt.event.*; | |
4 import java.io.*; | |
45 | 5 import java.net.ServerSocket; |
38 | 6 import java.net.Socket; |
7 import java.net.UnknownHostException; | |
8 import java.util.LinkedList; | |
11 | 9 import java.util.logging.Logger; |
10 | |
31 | 11 import jp.ac.u_ryukyu.treevnc.AcceptThread; |
40 | 12 import jp.ac.u_ryukyu.treevnc.server.state.ChangeHost; |
31 | 13 |
11 | 14 import com.glavsoft.exceptions.AuthenticationFailedException; |
15 import com.glavsoft.exceptions.FatalException; | |
16 import com.glavsoft.exceptions.TransportException; | |
17 import com.glavsoft.exceptions.UnsupportedProtocolVersionException; | |
18 import com.glavsoft.exceptions.UnsupportedSecurityTypeException; | |
19 import com.glavsoft.rfb.IChangeSettingsListener; | |
20 import com.glavsoft.rfb.IRfbSessionListener; | |
21 import com.glavsoft.rfb.protocol.Protocol; | |
22 import com.glavsoft.rfb.protocol.ProtocolSettings; | |
23 import com.glavsoft.transport.Reader; | |
24 import com.glavsoft.transport.Writer; | |
25 import com.glavsoft.viewer.ConnectionManager; | |
26 import com.glavsoft.viewer.Viewer; | |
27 import com.glavsoft.viewer.cli.Parser; | |
28 import com.glavsoft.viewer.swing.ClipboardControllerImpl; | |
29 import com.glavsoft.viewer.swing.ParametersHandler; | |
16 | 30 import com.glavsoft.viewer.swing.Surface; |
11 | 31 import com.glavsoft.viewer.swing.UiSettings; |
32 import com.glavsoft.viewer.swing.ParametersHandler.ConnectionParams; | |
33 | |
45 | 34 public class VncProxyService extends Viewer implements Runnable, |
35 IRfbSessionListener, IChangeSettingsListener { | |
11 | 36 |
37 /** | |
38 * | |
39 */ | |
40 private static final long serialVersionUID = 1L; | |
41 | |
12 | 42 public MyRfbProtoProxy rfb; |
37 | 43 private ConnectionManager connectionManager; |
46 | 44 private ConnectionParams connectionParams; |
45 | |
46 boolean isClient = false; | |
47 public static final int DEFAULT_PORT = 5900; | |
48 public static Logger logger = Logger.getLogger("com.glavsoft"); | |
49 private boolean forceReconnection; | |
50 private String reconnectionReason; | |
51 private final ProtocolSettings settings; | |
52 private final UiSettings uiSettings; | |
53 private AcceptThread acceptThread; | |
54 private GetBroadCastProxy getCast; | |
40 | 55 public AcceptClient aClient; |
56 public int opendPort; | |
45 | 57 |
47 | 58 private VncProxyService prevVps; |
59 static VncProxyService currentVps; | |
60 | |
13 | 61 // public AcceptClient acc; |
46 | 62 |
63 private void initProxy(Parser parser) { | |
40 | 64 aClient = new AcceptClient(); |
45 | 65 ParametersHandler.completeSettingsFromCLI(parser, connectionParams, |
66 settings, uiSettings); | |
11 | 67 showControls = ParametersHandler.showControls; |
68 passwordFromParams = parser.getValueFor(ParametersHandler.ARG_PASSWORD); | |
69 logger.info("TightVNC Viewer version " + ver()); | |
70 isApplet = false; | |
71 } | |
45 | 72 |
11 | 73 public VncProxyService() { |
37 | 74 connectionManager = new ConnectionManager(this, isApplet); |
11 | 75 connectionParams = new ParametersHandler.ConnectionParams(); |
76 settings = ProtocolSettings.getDefaultSettings(); | |
77 uiSettings = super.uiSettings; | |
45 | 78 connectionParams.hostName = "localhost"; |
11 | 79 } |
45 | 80 |
81 | |
47 | 82 public VncProxyService(VncProxyService vps, String hostName) { |
83 this(); | |
84 connectionParams.hostName = hostName; | |
85 rfb = vps.rfb; | |
86 isClient = vps.isClient; | |
87 forceReconnection = vps.forceReconnection; | |
88 reconnectionReason = vps.reconnectionReason; | |
89 acceptThread = vps.acceptThread; | |
90 getCast = vps.getCast; | |
91 aClient = vps.aClient; | |
92 opendPort = vps.opendPort; | |
93 prevVps = vps; | |
94 } | |
95 | |
11 | 96 public static void main(String[] argv) { |
97 String[] mainArgs = argv; | |
98 System.out.println(mainArgs.length); | |
99 // input into arguments Decision | |
100 | |
101 Parser parser = new Parser(); | |
102 ParametersHandler.completeParserOptions(parser); | |
103 | |
104 parser.parse(argv); | |
105 if (parser.isSet(ParametersHandler.ARG_HELP)) { | |
106 printUsage(parser.optionsUsage()); | |
107 System.exit(0); | |
108 } | |
46 | 109 VncProxyService vps = new VncProxyService(); |
110 vps.initProxy1(argv, mainArgs, parser); | |
111 } | |
112 | |
113 private void initProxy1(String[] argv, String[] mainArgs, | |
114 Parser parser) { | |
115 initProxy(parser); | |
11 | 116 if (mainArgs.length != 0) { |
117 for (int i = 0; i < argv.length; i++) { | |
46 | 118 setArguments(mainArgs); |
11 | 119 // write later |
120 } | |
121 } else { | |
45 | 122 // getHostData(); |
11 | 123 } |
48 | 124 run1(); |
46 | 125 createConnectionAndStart(); |
126 getChangeScreenRequest(); // Should be used Client Socket. | |
127 try { | |
128 threadSetAndStart(); | |
129 } catch (UnknownHostException e) { | |
130 | |
131 } catch (IOException e) { | |
132 | |
133 } | |
11 | 134 } |
45 | 135 |
11 | 136 private void setArguments(String[] mainArgs) { |
137 } | |
47 | 138 |
139 | |
140 public boolean run1() { | |
11 | 141 |
40 | 142 rfb = new MyRfbProtoProxy(); |
45 | 143 // getHostData(); |
11 | 144 if (forceReconnection) { |
45 | 145 connectionManager.showReconnectDialog("Connection lost", |
146 reconnectionReason); | |
11 | 147 forceReconnection = false; |
148 } | |
149 tryAgain = true; | |
150 while (tryAgain) { | |
45 | 151 // connectionParams.hostName = arguments[0]; |
152 // workingSocket = | |
153 // connectionManager.connectToTreeHost(connectionParams, settings); | |
154 workingSocket = connectionManager.connectToHost(connectionParams, | |
155 settings, rfb); | |
11 | 156 if (null == workingSocket) { |
157 closeApp(); | |
158 break; | |
159 } | |
160 logger.info("Connected"); | |
161 try { | |
162 workingSocket.setTcpNoDelay(true); // disable Nagle algorithm | |
163 Reader reader = new Reader(workingSocket.getInputStream()); | |
164 Writer writer = new Writer(workingSocket.getOutputStream()); | |
45 | 165 // rfb.setStream(reader,writer); |
11 | 166 workingProtocol = new Protocol(reader, writer, |
45 | 167 new PasswordChooser(passwordFromParams, |
168 connectionParams, containerFrame, this), | |
11 | 169 settings); |
170 workingProtocol.handshake(); | |
27 | 171 rfb.setProtocolContext(workingProtocol); |
11 | 172 // input into change parents |
45 | 173 ClipboardControllerImpl clipboardController = new ClipboardControllerImpl( |
174 workingProtocol, settings.getRemoteCharsetName()); | |
175 clipboardController.setEnabled(settings | |
176 .isAllowClipboardTransfer()); | |
11 | 177 settings.addListener(clipboardController); |
45 | 178 surface = new Surface(workingProtocol, this, |
179 uiSettings.getScaleFactor()); // this method | |
19 | 180 settings.addListener(this); |
181 uiSettings.addListener(surface); | |
182 containerFrame = createContainer(); | |
183 connectionManager.setContainerFrame(containerFrame); | |
184 updateFrameTitle(); | |
37 | 185 containerFrame.dispose(); |
186 containerFrame = null; | |
47 | 187 if (prevVps != null) { |
188 prevVps.cleanUpSession(); | |
189 prevVps = null; | |
190 } | |
191 currentVps = this; | |
45 | 192 workingProtocol.startNormalHandling(this, surface, |
193 clipboardController, rfb); | |
20
98519d16a8c3
getInitData from ProtocolContext
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
194 // rfb.setInitData(workingProtocol.getInitData()); too early |
45 | 195 // workingProtocol.startNormalHandling(this, surface, |
196 // clipboardController); | |
11 | 197 tryAgain = false; |
198 } catch (UnsupportedProtocolVersionException e) { | |
45 | 199 connectionManager.showReconnectDialog( |
200 "Unsupported Protocol Version", e.getMessage()); | |
11 | 201 logger.severe(e.getMessage()); |
202 } catch (UnsupportedSecurityTypeException e) { | |
45 | 203 connectionManager.showReconnectDialog( |
204 "Unsupported Security Type", e.getMessage()); | |
11 | 205 logger.severe(e.getMessage()); |
206 } catch (AuthenticationFailedException e) { | |
207 passwordFromParams = null; | |
45 | 208 connectionManager.showReconnectDialog("Authentication Failed", |
209 e.getMessage()); | |
11 | 210 logger.severe(e.getMessage()); |
211 } catch (TransportException e) { | |
45 | 212 connectionManager.showReconnectDialog("Connection Error", |
213 "Connection Error" + ": " + e.getMessage()); | |
11 | 214 logger.severe(e.getMessage()); |
215 } catch (IOException e) { | |
45 | 216 connectionManager.showReconnectDialog("Connection Error", |
217 "Connection Error" + ": " + e.getMessage()); | |
11 | 218 logger.severe(e.getMessage()); |
219 } catch (FatalException e) { | |
45 | 220 connectionManager.showReconnectDialog("Connection Error", |
221 "Connection Error" + ": " + e.getMessage()); | |
11 | 222 logger.severe(e.getMessage()); |
223 } | |
224 } | |
47 | 225 return !tryAgain; |
226 } | |
227 | |
228 private void cleanUpSession() { | |
229 if (prevVps != null) { | |
230 prevVps.cleanUpSession(); | |
231 prevVps = null; | |
232 } | |
233 workingProtocol.cleanUpSession(); | |
13 | 234 } |
45 | 235 |
38 | 236 public void createConnectionAndStart() { |
40 | 237 Thread thread; |
238 opendPort = rfb.selectPort(5999); | |
239 acceptThread = new AcceptThread(rfb, opendPort); | |
240 thread = new Thread(acceptThread); | |
38 | 241 thread.start(); |
13 | 242 getCast = new GetBroadCastProxy(this, |
243 workingProtocol.getRemoteDesktopName(), | |
244 this.connectionParams.hostName); | |
45 | 245 thread = new Thread(getCast); |
38 | 246 thread.start(); |
13 | 247 } |
11 | 248 |
34 | 249 public void proxyStart(String[] argv) { |
250 String[] mainArgs = argv; | |
251 System.out.println(mainArgs.length); | |
252 // input into arguments Decision | |
11 | 253 |
34 | 254 Parser parser = new Parser(); |
255 ParametersHandler.completeParserOptions(parser); | |
11 | 256 |
34 | 257 parser.parse(argv); |
258 if (parser.isSet(ParametersHandler.ARG_HELP)) { | |
259 printUsage(parser.optionsUsage()); | |
260 System.exit(0); | |
261 } | |
46 | 262 initProxy(parser); |
34 | 263 if (mainArgs.length != 0) { |
264 for (int i = 0; i < argv.length; i++) { | |
46 | 265 setArguments(mainArgs); |
34 | 266 // write later |
267 } | |
268 } else { | |
45 | 269 // getHostData(); |
34 | 270 } |
45 | 271 // SwingUtilities.invokeLater(myClient); |
46 | 272 run(); |
273 createConnectionAndStart(); | |
34 | 274 } |
275 | |
11 | 276 @Override |
277 public void destroy() { | |
278 | |
279 } | |
280 | |
281 @Override | |
282 public void windowClosing(WindowEvent e) { | |
283 if (e != null && e.getComponent() != null) { | |
284 e.getWindow().setVisible(false); | |
285 } | |
286 closeApp(); | |
287 } | |
45 | 288 |
38 | 289 private void threadSetAndStart() throws UnknownHostException, IOException { |
46 | 290 CreateThread createThread = new CreateThread(aClient); |
38 | 291 Thread thread = new Thread(createThread); |
37 | 292 thread.start(); |
46 | 293 Thread thread2 = new Thread(new ChangeHost(this, forceReconnection)); |
294 thread2.start(); | |
37 | 295 } |
45 | 296 |
37 | 297 public void setConnectionManager(ConnectionManager _connectionManager) { |
298 connectionManager = _connectionManager; | |
299 } | |
45 | 300 |
301 private void getChangeScreenRequest() { | |
302 Thread th = new Thread(new Runnable() { | |
303 @Override | |
304 public void run() { | |
46 | 305 while (true) { |
306 try { | |
307 Socket clientSocket = null; | |
308 ServerSocket echoServer = new ServerSocket(10001); | |
309 while (true) { | |
310 clientSocket = echoServer.accept(); | |
311 BufferedReader is = new BufferedReader( | |
312 new InputStreamReader( | |
313 clientSocket.getInputStream())); | |
314 String newHostName = is.readLine(); | |
315 // String screenSize = is.readLine(); | |
316 changeVNCServer(newHostName); | |
317 System.out.println("----------success----------"); | |
318 clientSocket.close(); | |
319 } | |
320 } catch (IOException e) { | |
321 continue; // log | |
45 | 322 } |
323 } | |
324 } | |
325 }); | |
326 th.start(); | |
327 } | |
328 | |
38 | 329 protected void socketClose() { |
330 try { | |
331 workingSocket.close(); | |
45 | 332 acceptThread.flag = true; |
38 | 333 getCast.setStopFlag(true); |
45 | 334 // createThread.setStopFlag(true); |
335 } catch (IOException e) {/* nop */ | |
336 } | |
38 | 337 } |
45 | 338 |
38 | 339 @Override |
340 public void rfbSessionStopped(final String reason) { | |
341 if (workingSocket != null && workingSocket.isConnected()) { | |
342 try { | |
343 workingSocket.close(); | |
45 | 344 } catch (IOException e) { /* nop */ |
345 } | |
38 | 346 } |
347 } | |
45 | 348 |
349 private void createSocketAndSend(LinkedList<String> clientList, String port) | |
350 throws UnknownHostException, IOException { | |
42 | 351 boolean passFlag = false; |
45 | 352 for (String client : clientList) { |
353 if (passFlag) { | |
38 | 354 Socket echoSocket; |
355 echoSocket = new Socket(client, 10001); | |
45 | 356 DataOutputStream os = new DataOutputStream( |
357 echoSocket.getOutputStream()); | |
38 | 358 os.writeBytes("reconnection\n"); |
48 | 359 System.out.println("port ==== " + port); |
45 | 360 os.writeBytes(port + "\n"); |
38 | 361 os.close(); |
362 } | |
42 | 363 passFlag = true; |
38 | 364 } |
365 } | |
366 | |
45 | 367 public void changeVNCServer(String hostName) throws UnknownHostException, |
368 IOException { | |
42 | 369 // sender and reader stop |
47 | 370 VncProxyService newVps = new VncProxyService(this,hostName); |
45 | 371 // run call and change workingProtocol |
47 | 372 newVps.connectionParams.hostName = hostName; |
373 if (newVps.run1()) { | |
374 newVps.createConnectionAndStart(); | |
48 | 375 newVps.createSocketAndSend(aClient.getList(), String.valueOf(newVps.opendPort)); |
47 | 376 } else { |
377 newVps.cleanUpSession(); | |
378 } | |
38 | 379 } |
45 | 380 |
11 | 381 } |