Mercurial > hg > Applications > TreeVNC
diff src/main/java/ac/ryukyu/treevnc/server/VncProxyService.java @ 11:57ae9fbb1245
add files
author | Taninari YU <you@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 21 Aug 2012 14:02:05 +0900 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/ac/ryukyu/treevnc/server/VncProxyService.java Tue Aug 21 14:02:05 2012 +0900 @@ -0,0 +1,179 @@ +package ac.ryukyu.treevnc.server; + +import java.awt.event.*; +import java.io.*; +import java.util.logging.Logger; + + +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 VncProxyService(Parser parser) { + this(); + 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() { + connectionParams = new ParametersHandler.ConnectionParams(); + settings = ProtocolSettings.getDefaultSettings(); + uiSettings = super.uiSettings; + + } + + 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 ConnectionParams connectionParams; + private final ProtocolSettings settings; + private final UiSettings uiSettings; + private String[] arguments; + + + 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(parser); + if (mainArgs.length != 0) { + for (int i = 0; i < argv.length; i++) { + vps.setArguments(mainArgs); + // write later + } + } else { + //getHostData(); + } + //SwingUtilities.invokeLater(myClient); + Thread th = new Thread(vps); + th.start(); + } + + private void setArguments(String[] mainArgs) { + arguments = mainArgs; + } + + @Override + public void run() { + // getHostData(); + ConnectionManager connectionManager = new ConnectionManager(this, isApplet); + if (forceReconnection) { + connectionManager.showReconnectDialog("Connection lost", reconnectionReason); + forceReconnection = false; + } + tryAgain = true; + while (tryAgain) { + connectionParams.hostName = arguments[0]; + // workingSocket = connectionManager.connectToTreeHost(connectionParams, settings); + workingSocket = connectionManager.connectToTreeHost(connectionParams, settings); + 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()); + + workingProtocol = new Protocol(reader, writer, + new PasswordChooser(passwordFromParams, connectionParams, containerFrame, this), + settings); + + workingProtocol.handshake(); + // 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()); + settings.addListener(this); + uiSettings.addListener(surface); + containerFrame = createContainer(); + connectionManager.setContainerFrame(containerFrame); + updateFrameTitle(); + + 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()); + } + } + + } + + + + @Override + public void destroy() { + + } + + + + @Override + public void windowClosing(WindowEvent e) { + if (e != null && e.getComponent() != null) { + e.getWindow().setVisible(false); + } + closeApp(); + } +}