Search in sources :

Example 1 with TrayManager

use of qz.common.TrayManager in project tray by qzind.

the class PrintSocketServer method runServer.

public static void runServer(CertificateManager certManager, boolean headless) throws InterruptedException, InvocationTargetException {
    SwingUtilities.invokeAndWait(() -> {
        PrintSocketServer.setTrayManager(new TrayManager(headless));
    });
    server = findAvailableSecurePort(certManager);
    Connector secureConnector = null;
    if (server.getConnectors().length > 0 && !server.getConnectors()[0].isFailed()) {
        secureConnector = server.getConnectors()[0];
    }
    while (!running.get() && insecurePortIndex.get() < INSECURE_PORTS.size()) {
        try {
            ServerConnector connector = new ServerConnector(server);
            connector.setPort(getInsecurePortInUse());
            if (secureConnector != null) {
                // setup insecure connector before secure
                server.setConnectors(new Connector[] { connector, secureConnector });
            } else {
                server.setConnectors(new Connector[] { connector });
            }
            ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
            // Handle WebSocket connections
            WebSocketUpgradeFilter filter = WebSocketUpgradeFilter.configure(context);
            filter.addMapping(new ServletPathSpec("/"), (req, resp) -> new PrintSocketClient(server));
            filter.getFactory().getPolicy().setMaxTextMessageSize(MAX_MESSAGE_SIZE);
            // Handle HTTP landing page
            ServletHolder httpServlet = new ServletHolder(new HttpAboutServlet(certManager));
            httpServlet.setInitParameter("resourceBase", "/");
            context.addServlet(httpServlet, "/");
            context.addServlet(httpServlet, "/json");
            server.setHandler(context);
            server.setStopAtShutdown(true);
            server.start();
            trayManager.setReloadThread(new Thread(() -> {
                try {
                    trayManager.setDangerIcon();
                    running.set(false);
                    securePortIndex.set(0);
                    insecurePortIndex.set(0);
                    server.stop();
                } catch (Exception e) {
                    log.error("Failed to reload", e);
                    trayManager.displayErrorMessage("Error stopping print socket: " + e.getLocalizedMessage());
                }
            }));
            running.set(true);
            log.info("Server started on port(s) " + getPorts(server));
            trayManager.setServer(server, insecurePortIndex.get());
            server.join();
        } catch (IOException | MultiException e) {
            // order of getConnectors is the order we added them -> insecure first
            if (server.getConnectors()[0].isFailed()) {
                insecurePortIndex.incrementAndGet();
            }
            // explicitly stop the server, because if only 1 port has an exception the other will still be opened
            try {
                server.stop();
            } catch (Exception stopEx) {
                stopEx.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            trayManager.displayErrorMessage(e.getLocalizedMessage());
            break;
        }
    }
}
Also used : ServletPathSpec(org.eclipse.jetty.http.pathmap.ServletPathSpec) WebSocketUpgradeFilter(org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) TrayManager(qz.common.TrayManager) IOException(java.io.IOException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MultiException(org.eclipse.jetty.util.MultiException) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) MultiException(org.eclipse.jetty.util.MultiException)

Aggregations

IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ServletPathSpec (org.eclipse.jetty.http.pathmap.ServletPathSpec)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 MultiException (org.eclipse.jetty.util.MultiException)1 WebSocketUpgradeFilter (org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter)1 TrayManager (qz.common.TrayManager)1