comparison src/viewer_swing/java/com/glavsoft/viewer/swing/SwingNetworkConnectionWorker.java @ 52:472a9bcacb21 draft default tip

TightVNC 2.7.1.0
author you@cr.ie.u-ryukyu.ac.jp
date Wed, 07 Aug 2013 19:01:17 +0900
parents
children
comparison
equal deleted inserted replaced
0:4689cc86d6cb 52:472a9bcacb21
1 // Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC.
2 // All rights reserved.
3 //
4 //-------------------------------------------------------------------------
5 // This file is part of the TightVNC software. Please visit our Web site:
6 //
7 // http://www.tightvnc.com/
8 //
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License along
20 // with this program; if not, write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 //-------------------------------------------------------------------------
23 //
24
25 package com.glavsoft.viewer.swing;
26
27 import com.glavsoft.viewer.*;
28 import com.glavsoft.viewer.swing.ssh.SshConnectionManager;
29
30 import javax.swing.*;
31 import java.io.IOException;
32 import java.net.Socket;
33 import java.net.UnknownHostException;
34 import java.security.AccessControlException;
35 import java.util.List;
36 import java.util.concurrent.CancellationException;
37 import java.util.concurrent.ExecutionException;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
40
41
42 public class SwingNetworkConnectionWorker extends SwingWorker<Socket, String> implements NetworkConnectionWorker {
43 public static final int MAX_HOSTNAME_LENGTH_FOR_MESSAGES = 40;
44 private final JFrame parentWindow;
45 private Logger logger;
46 private boolean hasSshSupport;
47 private ConnectionParams connectionParams;
48 private ConnectionPresenter presenter;
49
50
51 public SwingNetworkConnectionWorker(JFrame parentWindow) {
52 this.parentWindow = parentWindow;
53 logger = Logger.getLogger(getClass().getName());
54 }
55
56 @Override
57 public Socket doInBackground() throws Exception {
58 String s = "<b>" +connectionParams.hostName + "</b>:" + connectionParams.getPortNumber();
59 if (connectionParams.useSsh()) {
60 s += " <i>(via ssh://" + connectionParams.sshUserName + "@" + connectionParams.sshHostName + ":" + connectionParams.getSshPortNumber() + ")</i>";
61 }
62
63 String message = "<html>Trying to connect to " + s + "</html>";
64 logger.info(message.replaceAll("<[^<>]+?>", ""));
65 publish(message);
66 int port;
67 String host;
68 if (hasSshSupport && connectionParams.useSsh()) {
69 SshConnectionManager sshConnectionManager = new SshConnectionManager(parentWindow);
70 message = "Creating SSH tunnel to " + connectionParams.sshHostName + ":" + connectionParams.getSshPortNumber();
71 logger.info(message);
72 publish(message);
73 port = sshConnectionManager.connect(connectionParams);
74 if (sshConnectionManager.isConnected() ) {
75 host = "127.0.0.1";
76 message = "SSH tunnel established: " + host + ":" + port;
77 logger.info(message);
78 publish(message);
79 } else {
80 throw new ConnectionErrorException("Could not create SSH tunnel: " + sshConnectionManager.getErrorMessage());
81 }
82 } else {
83 host = connectionParams.hostName;
84 port = connectionParams.getPortNumber();
85 }
86
87 message = "Connecting to host " + host + ":" + port + (connectionParams.useSsh() ? " (tunneled)" : "");
88 logger.info(message);
89 publish(message);
90
91 return new Socket(host, port);
92 }
93
94 private String formatHostString(String hostName) {
95 if (hostName.length() <= MAX_HOSTNAME_LENGTH_FOR_MESSAGES) {
96 return hostName;
97 } else {
98 return hostName.substring(0, MAX_HOSTNAME_LENGTH_FOR_MESSAGES) + "...";
99 }
100 }
101
102 @Override
103 protected void process(List<String> strings) { // EDT
104 String message = strings.get(strings.size() - 1); // get last
105 presenter.showMessage(message);
106 }
107
108 @Override
109 protected void done() { // EDT
110 try {
111 final Socket socket = get();
112 presenter.successfulNetworkConnection(socket);
113 } catch (CancellationException e) {
114 logger.info("Cancelled");
115 presenter.showMessage("Cancelled");
116 presenter.connectionFailed();
117 } catch (InterruptedException e) {
118 logger.info("Interrupted");
119 presenter.showMessage("Interrupted");
120 presenter.connectionFailed();
121 } catch (ExecutionException e) {
122 String errorMessage = null;
123 try {
124 throw e.getCause();
125 } catch (UnknownHostException uhe) {
126 logger.severe("Unknown host: " + connectionParams.hostName);
127 errorMessage = "Unknown host: '" + formatHostString(connectionParams.hostName) + "'";
128 } catch (IOException ioe) {
129 logger.severe("Couldn't connect to '" + connectionParams.hostName +
130 ":" + connectionParams.getPortNumber() + "':\n" + ioe.getMessage());
131 logger.log(Level.FINEST, "Couldn't connect to '" + connectionParams.hostName +
132 ":" + connectionParams.getPortNumber() + "':\n" + ioe.getMessage(), ioe);
133 errorMessage = "Couldn't connect to '" + formatHostString(connectionParams.hostName) +
134 ":" + connectionParams.getPortNumber() + "':\n" + ioe.getMessage();
135 } catch (CancelConnectionException cce) {
136 logger.severe("Cancelled: " + cce.getMessage());
137 } catch (AccessControlException ace) {
138 logger.severe("Couldn't connect to: " +
139 connectionParams.hostName + ":" + connectionParams.getPortNumber() +
140 ": " + ace.getMessage());
141 logger.log(Level.FINEST, "Couldn't connect to: " +
142 connectionParams.hostName + ":" + connectionParams.getPortNumber() +
143 ": " + ace.getMessage(), ace);
144 errorMessage = "Access control error";
145 } catch (ConnectionErrorException cee) {
146 logger.severe(cee.getMessage() + " host: " +
147 connectionParams.hostName + ":" + connectionParams.getPortNumber());
148 errorMessage = cee.getMessage() + "\nHost: " +
149 formatHostString(connectionParams.hostName) + ":" + connectionParams.getPortNumber();
150 } catch (Throwable throwable) {
151 logger.log(Level.FINEST, "Couldn't connect to '" + formatHostString(connectionParams.hostName) +
152 ":" + connectionParams.getPortNumber() + "':\n" + throwable.getMessage(), throwable);
153 errorMessage = "Couldn't connect to '" + formatHostString(connectionParams.hostName) +
154 ":" + connectionParams.getPortNumber() + "':\n" + throwable.getMessage();
155 }
156 presenter.showConnectionErrorDialog(errorMessage);
157 presenter.clearMessage();
158 presenter.connectionFailed();
159 }
160 }
161
162 @Override
163 public void setConnectionParams(ConnectionParams connectionParams) {
164 this.connectionParams = connectionParams;
165 }
166
167 @Override
168 public void setPresenter(ConnectionPresenter presenter) {
169 this.presenter = presenter;
170 }
171
172 @Override
173 public void setHasSshSupport(boolean hasSshSupport) {
174 this.hasSshSupport = hasSshSupport;
175 }
176
177 @Override
178 public boolean cancel() {
179 return super.cancel(true);
180 }
181 }