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;
}
}
}
Aggregations