comparison src/main/java/jp/ac/u_ryukyu/treevnc/server/VncProxyService.java @ 45:20326a4b9d88

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