view src/viewer_swing/java/com/glavsoft/viewer/ViewerInterface.java @ 605:b1ace1c5d37a

fix wait
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 18 Feb 2020 21:47:17 +0900
parents 668ff8c17f46
children
line wrap: on
line source

package com.glavsoft.viewer;

import com.glavsoft.rfb.protocol.ProtocolContext;
import com.glavsoft.transport.Reader;
import com.glavsoft.transport.Writer;
import com.glavsoft.viewer.swing.ConnectionParams;
import com.glavsoft.viewer.swing.SwingViewerWindow;
import jp.ac.u_ryukyu.treevnc.*;

import java.awt.*;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;

public interface ViewerInterface extends Runnable {

    public boolean getCuiVersion();

	public TreeRFBProto getRfb();

	public void closeApp();

	public void setSocket(Socket soc);

	public void run();

	public void setTerminationType(boolean b);

    void setCuiVersion(boolean flag);

    public void startTreeViewer(String hostName, boolean b, boolean addSerialNum);

    public void connectToParenet(int port, String hostname) throws IOException;

    public void inhelitClients(String hostName, short newVNCServerId, int x, int y, int width, int height, int scale);

    public void proxyStart(String[] args, String hostName, int width, int height, boolean showTree, boolean checkDelay, boolean addSerialNum,
                           boolean fixingSize, boolean filterSingleDisplay, boolean hasViewer,boolean connect);

    public void setNoConnection(boolean noConnection);

	public void setVisible(boolean b);

	public Socket getVNCSocket();

    public boolean getShowTree();

    public void setWidth(int width);

    public void setHeight(int height);

    public void setFixingSize(int width, int height);
    
    public void setFitScreen();

    public ArrayList<FbRectangle> getScreenRectangles();

    public Point getScreenOffset(ArrayList<Rectangle> rectangles);

    public int retinaScale(int shareScreenNumber);

    public ConnectionPresenter getConnectionPresenter();

    public void setConnectionPresenter(ConnectionPresenter connectionPresenter);

    void changeToDirectConnectedServer(String hostName, Reader is, Writer os, int x, int y, int width, int height, int scale);

    public void setUseMulticast(boolean useMulticast) ;

    public boolean getUseMulticast();

    BroadcastRFBListener getRfbBroadcastListener();

    public boolean getIsRetinaDisplay(int shareScreenNumber);

    default void changeVncServer(int x, int y, int width, int height, int scale, short id) {
        String localhost = "127.0.0.1"; // InetAddress.getLocalHost().getHostName()
        try {
            getRfb().changeVNCServer(this, localhost, ConnectionParams.DEFAULT_RFB_PORT, x, y, width, height, scale, id, null, null);
        } catch (Exception e1) {
            System.out.println("can't change server :" + e1.getMessage());
        }
    }

    default void screenChangeRequest(ProtocolContext context, int shareScreenNumber) {
        ArrayList<FbRectangle> rectangles = getScreenRectangles();
        FbRectangle rectangle1 = rectangles.get(shareScreenNumber);
        int singleWidth = (int) (rectangle1.getWidth());
        int singleHeight = (int) (rectangle1.getHeight());
        int x = rectangle1.getXfb();
        int y = rectangle1.getYfb();
        int scale = rectangle1.getRetinaScale();
        // System.out.println("request scrren change rectangles = "+rectangle1);
        // showScreenInfo("request screen change id = " + viewer.getRfb().getId(), 0, x, y, singleWidth, singleHeight, scale);
        if (getRfb().isTreeManager() || context == null) {
            changeVncServer(x, y, singleWidth * scale, singleHeight * scale, scale, getRfb().getId());
        }
        if (getRfb().hasParent() && context != null ) {
            String adr = getRfb().getMyAddress();
            if (scanPort(adr, ConnectionParams.DEFAULT_RFB_PORT)) {
                // -1 means request to reverse direct connection socket
                short id = getRfb().isTreeManager() ? (short) -1 : getRfb().getId();
                context.sendMessage(new ScreenChangeRequest(adr, ConnectionParams.DEFAULT_VNC_ROOT, id, x, y, singleWidth * scale, singleHeight * scale, scale));
            }
        }
    }

    // scan port is not enough to check VNC server. Root server should send error message to the requested
    // node when authentication failures are happened
    default boolean scanPort(String adr, int port) {
        try {
            Socket socket = new Socket(adr, port);
            socket.close();
            return true;
        } catch (Exception e) {
            String message =  "Please screen sharing settings";
            // show error panel
            getConnectionPresenter().showPortErrorDialog(message);
            getConnectionPresenter().clearMessage();
            return false;
        }
    }

    default void createScreenSelectionPanel(ProtocolContext context) {
        ArrayList<FbRectangle> rectangles = getScreenRectangles();
        if (rectangles.size() == 1) { // single display
            screenChangeRequest(context, 0);
        } else if (rectangles.size() > 1){ // dual display
            ScreenChangeSelectionPanel selectionPanel = new ScreenChangeSelectionPanel(this, context);
            for (int i = 0; i < rectangles.size(); i++) {
                Rectangle rectangle = rectangles.get(i);
                int screenWidth = rectangle.width;
                int screenHeight = rectangle.height;
                selectionPanel.checkBox(screenWidth+" X "+screenHeight);
            }
            selectionPanel.setButton();
            selectionPanel.visible();
        }
    }

    default void findTreeVncRoot(TreeRFBProto rfb,CreateConnectionParam cp) {
        FindRoot getBcast  = new FindRoot(rfb.acceptPort, cp);
        rfb.createRootSelectionPanel(cp, getBcast);
        getBcast.findRoot();
        // wait for RootSelection
        try {
            cp.waitTreeVNCServer();
        } catch (InterruptedException e) {
            System.out.println("any thread interrupt when wait for FindRoot " + e.getMessage());
        }
    }
}