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