Mercurial > hg > Applications > TightVNC_orginal
view src/main/java/jp/ac/u_ryukyu/treevnc/server/VncProxyService.java @ 47:f3b775d3592a
create newVps.
author | one |
---|---|
date | Thu, 06 Dec 2012 17:51:29 +0900 |
parents | d2741b80399e |
children | 6ebe870da138 |
line wrap: on
line source
package jp.ac.u_ryukyu.treevnc.server; import java.awt.event.*; import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.util.LinkedList; import java.util.logging.Logger; import jp.ac.u_ryukyu.treevnc.AcceptThread; import jp.ac.u_ryukyu.treevnc.server.state.ChangeHost; import com.glavsoft.exceptions.AuthenticationFailedException; import com.glavsoft.exceptions.FatalException; import com.glavsoft.exceptions.TransportException; import com.glavsoft.exceptions.UnsupportedProtocolVersionException; import com.glavsoft.exceptions.UnsupportedSecurityTypeException; import com.glavsoft.rfb.IChangeSettingsListener; import com.glavsoft.rfb.IRfbSessionListener; import com.glavsoft.rfb.protocol.Protocol; import com.glavsoft.rfb.protocol.ProtocolSettings; import com.glavsoft.transport.Reader; import com.glavsoft.transport.Writer; import com.glavsoft.viewer.ConnectionManager; import com.glavsoft.viewer.Viewer; import com.glavsoft.viewer.cli.Parser; import com.glavsoft.viewer.swing.ClipboardControllerImpl; import com.glavsoft.viewer.swing.ParametersHandler; import com.glavsoft.viewer.swing.Surface; import com.glavsoft.viewer.swing.UiSettings; import com.glavsoft.viewer.swing.ParametersHandler.ConnectionParams; public class VncProxyService extends Viewer implements Runnable, IRfbSessionListener, IChangeSettingsListener { /** * */ private static final long serialVersionUID = 1L; public MyRfbProtoProxy rfb; private ConnectionManager connectionManager; private ConnectionParams connectionParams; boolean isClient = false; public static final int DEFAULT_PORT = 5900; public static Logger logger = Logger.getLogger("com.glavsoft"); private boolean forceReconnection; private String reconnectionReason; private final ProtocolSettings settings; private final UiSettings uiSettings; private AcceptThread acceptThread; private GetBroadCastProxy getCast; public AcceptClient aClient; public int opendPort; private VncProxyService prevVps; static VncProxyService currentVps; // public AcceptClient acc; private void initProxy(Parser parser) { aClient = new AcceptClient(); ParametersHandler.completeSettingsFromCLI(parser, connectionParams, settings, uiSettings); showControls = ParametersHandler.showControls; passwordFromParams = parser.getValueFor(ParametersHandler.ARG_PASSWORD); logger.info("TightVNC Viewer version " + ver()); isApplet = false; } public VncProxyService() { connectionManager = new ConnectionManager(this, isApplet); connectionParams = new ParametersHandler.ConnectionParams(); settings = ProtocolSettings.getDefaultSettings(); uiSettings = super.uiSettings; connectionParams.hostName = "localhost"; } public VncProxyService(VncProxyService vps, String hostName) { this(); connectionParams.hostName = hostName; rfb = vps.rfb; isClient = vps.isClient; forceReconnection = vps.forceReconnection; reconnectionReason = vps.reconnectionReason; acceptThread = vps.acceptThread; getCast = vps.getCast; aClient = vps.aClient; opendPort = vps.opendPort; prevVps = vps; } public static void main(String[] argv) { String[] mainArgs = argv; System.out.println(mainArgs.length); // input into arguments Decision Parser parser = new Parser(); ParametersHandler.completeParserOptions(parser); parser.parse(argv); if (parser.isSet(ParametersHandler.ARG_HELP)) { printUsage(parser.optionsUsage()); System.exit(0); } VncProxyService vps = new VncProxyService(); vps.initProxy1(argv, mainArgs, parser); } private void initProxy1(String[] argv, String[] mainArgs, Parser parser) { initProxy(parser); if (mainArgs.length != 0) { for (int i = 0; i < argv.length; i++) { setArguments(mainArgs); // write later } } else { // getHostData(); } run(); createConnectionAndStart(); getChangeScreenRequest(); // Should be used Client Socket. try { threadSetAndStart(); } catch (UnknownHostException e) { } catch (IOException e) { } } private void setArguments(String[] mainArgs) { } public boolean run1() { rfb = new MyRfbProtoProxy(); // getHostData(); if (forceReconnection) { connectionManager.showReconnectDialog("Connection lost", reconnectionReason); forceReconnection = false; } tryAgain = true; while (tryAgain) { // connectionParams.hostName = arguments[0]; // workingSocket = // connectionManager.connectToTreeHost(connectionParams, settings); workingSocket = connectionManager.connectToHost(connectionParams, settings, rfb); if (null == workingSocket) { closeApp(); break; } logger.info("Connected"); try { workingSocket.setTcpNoDelay(true); // disable Nagle algorithm Reader reader = new Reader(workingSocket.getInputStream()); Writer writer = new Writer(workingSocket.getOutputStream()); // rfb.setStream(reader,writer); workingProtocol = new Protocol(reader, writer, new PasswordChooser(passwordFromParams, connectionParams, containerFrame, this), settings); workingProtocol.handshake(); rfb.setProtocolContext(workingProtocol); // input into change parents ClipboardControllerImpl clipboardController = new ClipboardControllerImpl( workingProtocol, settings.getRemoteCharsetName()); clipboardController.setEnabled(settings .isAllowClipboardTransfer()); settings.addListener(clipboardController); surface = new Surface(workingProtocol, this, uiSettings.getScaleFactor()); // this method settings.addListener(this); uiSettings.addListener(surface); containerFrame = createContainer(); connectionManager.setContainerFrame(containerFrame); updateFrameTitle(); containerFrame.dispose(); containerFrame = null; if (prevVps != null) { prevVps.cleanUpSession(); prevVps = null; } currentVps = this; workingProtocol.startNormalHandling(this, surface, clipboardController, rfb); // rfb.setInitData(workingProtocol.getInitData()); too early // workingProtocol.startNormalHandling(this, surface, // clipboardController); tryAgain = false; } catch (UnsupportedProtocolVersionException e) { connectionManager.showReconnectDialog( "Unsupported Protocol Version", e.getMessage()); logger.severe(e.getMessage()); } catch (UnsupportedSecurityTypeException e) { connectionManager.showReconnectDialog( "Unsupported Security Type", e.getMessage()); logger.severe(e.getMessage()); } catch (AuthenticationFailedException e) { passwordFromParams = null; connectionManager.showReconnectDialog("Authentication Failed", e.getMessage()); logger.severe(e.getMessage()); } catch (TransportException e) { connectionManager.showReconnectDialog("Connection Error", "Connection Error" + ": " + e.getMessage()); logger.severe(e.getMessage()); } catch (IOException e) { connectionManager.showReconnectDialog("Connection Error", "Connection Error" + ": " + e.getMessage()); logger.severe(e.getMessage()); } catch (FatalException e) { connectionManager.showReconnectDialog("Connection Error", "Connection Error" + ": " + e.getMessage()); logger.severe(e.getMessage()); } } return !tryAgain; } private void cleanUpSession() { if (prevVps != null) { prevVps.cleanUpSession(); prevVps = null; } workingProtocol.cleanUpSession(); } public void createConnectionAndStart() { Thread thread; opendPort = rfb.selectPort(5999); acceptThread = new AcceptThread(rfb, opendPort); thread = new Thread(acceptThread); thread.start(); getCast = new GetBroadCastProxy(this, workingProtocol.getRemoteDesktopName(), this.connectionParams.hostName); thread = new Thread(getCast); thread.start(); } public void proxyStart(String[] argv) { String[] mainArgs = argv; System.out.println(mainArgs.length); // input into arguments Decision Parser parser = new Parser(); ParametersHandler.completeParserOptions(parser); parser.parse(argv); if (parser.isSet(ParametersHandler.ARG_HELP)) { printUsage(parser.optionsUsage()); System.exit(0); } initProxy(parser); if (mainArgs.length != 0) { for (int i = 0; i < argv.length; i++) { setArguments(mainArgs); // write later } } else { // getHostData(); } // SwingUtilities.invokeLater(myClient); run(); createConnectionAndStart(); } @Override public void destroy() { } @Override public void windowClosing(WindowEvent e) { if (e != null && e.getComponent() != null) { e.getWindow().setVisible(false); } closeApp(); } private void threadSetAndStart() throws UnknownHostException, IOException { CreateThread createThread = new CreateThread(aClient); Thread thread = new Thread(createThread); thread.start(); Thread thread2 = new Thread(new ChangeHost(this, forceReconnection)); thread2.start(); } public void setConnectionManager(ConnectionManager _connectionManager) { connectionManager = _connectionManager; } private void getChangeScreenRequest() { Thread th = new Thread(new Runnable() { @Override public void run() { while (true) { try { Socket clientSocket = null; ServerSocket echoServer = new ServerSocket(10001); while (true) { clientSocket = echoServer.accept(); BufferedReader is = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); String newHostName = is.readLine(); // String screenSize = is.readLine(); changeVNCServer(newHostName); System.out.println("----------success----------"); clientSocket.close(); } } catch (IOException e) { continue; // log } } } }); th.start(); } protected void socketClose() { try { workingSocket.close(); acceptThread.flag = true; getCast.setStopFlag(true); // createThread.setStopFlag(true); } catch (IOException e) {/* nop */ } } @Override public void rfbSessionStopped(final String reason) { if (workingSocket != null && workingSocket.isConnected()) { try { workingSocket.close(); } catch (IOException e) { /* nop */ } } } private void createSocketAndSend(LinkedList<String> clientList, String port) throws UnknownHostException, IOException { boolean passFlag = false; for (String client : clientList) { if (passFlag) { Socket echoSocket; echoSocket = new Socket(client, 10001); DataOutputStream os = new DataOutputStream( echoSocket.getOutputStream()); os.writeBytes("reconnection\n"); os.writeBytes(port + "\n"); os.close(); } passFlag = true; } } public void changeVNCServer(String hostName) throws UnknownHostException, IOException { // sender and reader stop VncProxyService newVps = new VncProxyService(this,hostName); // run call and change workingProtocol newVps.connectionParams.hostName = hostName; if (newVps.run1()) { newVps.createConnectionAndStart(); newVps.createSocketAndSend(aClient.getList(), String.valueOf(opendPort)); } else { newVps.cleanUpSession(); } } }