Mercurial > hg > trac > TreeVNC > TreeVNC
view src/main/java/jp/ac/u_ryukyu/treevnc/server/VncProxyService.java @ 94:75879c316796
synchronized wait for Rfb initialization in change server.
author | oc |
---|---|
date | Mon, 19 May 2014 19:57:32 +0900 |
parents | 7b8d3411270a |
children | 76e49dd9c9c5 |
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.core.SettingsChangedEvent; import com.glavsoft.rfb.IChangeSettingsListener; import com.glavsoft.rfb.IRfbSessionListener; import com.glavsoft.rfb.protocol.ProtocolSettings; import com.glavsoft.viewer.Viewer; import com.glavsoft.viewer.cli.Parser; import com.glavsoft.viewer.swing.ConnectionParams; import com.glavsoft.viewer.swing.ParametersHandler; public class VncProxyService extends Viewer implements Runnable, IRfbSessionListener, IChangeSettingsListener { /** * */ private static final long serialVersionUID = 1L; public static Logger logger = Logger.getLogger("com.glavsoft"); private boolean forceReconnection; private String reconnectionReason; private AcceptThread acceptThread; private GetBroadCastProxy getCast; private AcceptClient clients; public int opendPort; static VncProxyService currentVps; private int fbWidth = 0; private boolean firstTime = true; private ServerSocket echoServerSocket; private int vncport = ConnectionParams.DEFAULT_RFB_PORT; public VncProxyService() { ProtocolSettings.getDefaultSettings(); uiSettings = super.uiSettings; } public VncProxyService(VncProxyService vps, String hostName) { this(); connectionParams.hostName = hostName; myRfb = vps.myRfb; forceReconnection = vps.forceReconnection; reconnectionReason = vps.reconnectionReason; acceptThread = vps.acceptThread; getCast = vps.getCast; clients = vps.clients; opendPort = vps.opendPort; } 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("localhost"); } private void initProxy1(String hostName) { myRfb = new MyRfbProtoProxy(); clients = new AcceptClient(); isApplet = false; setIsTreeVNC(true); setConnectionParam(hostName,vncport); isApplet = true; run(); createConnectionAndStart(); getChangeScreenRequest(); // Should be used Client Socket. try { threadSetAndStart(); } catch (UnknownHostException e) { } catch (IOException e) { } } public void createConnectionAndStart() { Thread thread; opendPort = myRfb.selectPort(5999); acceptThread = new AcceptThread(myRfb, opendPort); thread = new Thread(acceptThread, "TreeVNC-accept"); thread.start(); if(firstTime) { getCast = new GetBroadCastProxy(this, this.connectionParams.getHostName(), this.connectionParams.getHostName()); thread = new Thread(getCast); thread.start(); } } public void proxyStart(String[] argv, int width, int height) { fbWidth = width; proxyStart(argv); } public void proxyStart(String[] argv) { // input into arguments Decision Parser parser = new Parser(); ParametersHandler.completeParserOptions(parser); if (fbWidth == 0) parser.parse(argv); if (parser.isSet(ParametersHandler.ARG_HELP)) { printUsage(parser.optionsUsage()); System.exit(0); } initProxy1("localhost"); } @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(clients); Thread thread = new Thread(createThread); thread.start(); Thread thread2 = new Thread(new ChangeHost(this, forceReconnection)); thread2.start(); } private void getChangeScreenRequest() { Thread th = new Thread(new Runnable() { @Override public void run() { while (true) { try { Socket clientSocket = null; echoServerSocket = new ServerSocket(10002); while (true) { clientSocket = echoServerSocket.accept(); BufferedReader is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String newHostName = is.readLine(); if("checkdelay".equals(newHostName)) { Long delay = System.currentTimeMillis()-Long.parseLong(is.readLine()); DataOutputStream os = new DataOutputStream(clientSocket.getOutputStream()); os.writeBytes(String.valueOf(delay)+"\n"); } else { System.out.println(newHostName); if (permitChangeScreen()) { // please remove these numbers. changeVNCServer(newHostName, 3200, 1980); } else { continue; } clientSocket.close(); } } } catch (IOException e) { continue; // log } catch (InterruptedException e) { continue; } } } }); th.start(); } private boolean permitChangeScreen() { return true; } protected void socketClose() { } @Override public void rfbSessionStopped(final String reason) { } private void sendReconnection(LinkedList<String> clientList, String host, String port) throws UnknownHostException, IOException { boolean passFlag = false; int counter = 0; for (String client : clientList) { if (passFlag) { Socket echoSocket = new Socket(client, 10001); DataOutputStream os = new DataOutputStream( echoSocket.getOutputStream()); os.writeBytes("reconnection\n"); if(counter++ < clients.getTreeBranch()) os.writeBytes(port + "\n"); else os.writeBytes(5999 + "\n"); os.close(); echoSocket.close(); } passFlag = true; } } /** * chnageVNCServer is called when host change. * * @param hostName * HostAddress * @param width * FrameWidth * @param height * FrameHeight * @throws InterruptedException */ public void changeVNCServer(String hostName, int width, int height) throws UnknownHostException, IOException, InterruptedException { // sender and reader stop VncProxyService newVps = new VncProxyService(this, hostName); newVps.setFirstTime(false); // newVps.fbWidth = width; // newVps.fbHeight = height; // run call and change workingProtocol newVps.setConnectionParam(hostName, vncport); newVps.initProxy1(hostName); orderRecconection(newVps ,hostName); socketClose(); } @Override public void settingsChanged(SettingsChangedEvent event) { } public void setFirstTime(boolean flag) { firstTime = flag; } public void orderRecconection(VncProxyService vps,String hostName) throws UnknownHostException, IOException, InterruptedException { vps.myRfb.waitForReady(this); vps.sendReconnection(clients.getList(), hostName, String.valueOf(vps.opendPort)); } public LinkedList<String> clientList() { return clients.getList(); } }