123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package org.robert.printer.ws;
- import java.util.concurrent.atomic.AtomicBoolean;
- import org.eclipse.jetty.http.HttpVersion;
- import org.eclipse.jetty.server.Connector;
- import org.eclipse.jetty.server.HttpConfiguration;
- import org.eclipse.jetty.server.HttpConnectionFactory;
- import org.eclipse.jetty.server.Server;
- import org.eclipse.jetty.server.ServerConnector;
- import org.eclipse.jetty.server.SslConnectionFactory;
- import org.eclipse.jetty.util.ssl.SslContextFactory;
- import org.eclipse.jetty.websocket.server.WebSocketHandler;
- import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
- public class PrintersSocketServer {
- private Server server;
- private final String host;
- private final int port;
- private final AtomicBoolean running;
- public PrintersSocketServer() {
- this.host = "0.0.0.0";
- this.port = 8070;
- this.running = new AtomicBoolean(false);
- }
- /**
- *
- * @return
- */
- private Server getServer() {
- return this.server;
- }
- /**
- *
- * @return
- */
- private String getHost() {
- return this.host;
- }
- /**
- *
- * @return
- */
- private int getPort() {
- return this.port;
- }
- /**
- *
- * @return
- */
- public boolean isRunning() {
- return this.running.get();
- }
- /**
- * @throws Exception
- *
- */
- public void goUp() throws Exception {
- this.server = new Server(this.getPort());
- SslContextFactory sslContext = new SslContextFactory();
- sslContext.setKeyStorePath(this.getClass().getResource("/resources/printers-tray.jks").toExternalForm());
- sslContext.setKeyStorePassword("robert2206b");
- sslContext.setKeyManagerPassword("robert2206a");
- SslConnectionFactory sslConnection = new SslConnectionFactory(sslContext, HttpVersion.HTTP_1_1.asString());
- HttpConnectionFactory httpConnection = new HttpConnectionFactory(new HttpConfiguration());
- ServerConnector connector = new ServerConnector(this.server, sslConnection, httpConnection);
- connector.setHost(this.getHost());
- connector.setPort(this.getPort() + 1);
- this.server.addConnector(connector);
- final WebSocketHandler wsHandler = new WebSocketHandler() {
- @Override
- public void configure(WebSocketServletFactory factory) {
- factory.register(PrintersSocketHandler.class);
- factory.getPolicy().setMaxTextMessageSize(Integer.MAX_VALUE);
- }
- };
- this.server.setHandler(wsHandler);
- this.server.setStopAtShutdown(true);
- this.server.start();
- this.running.set(true);
- this.server.join();
- }
- /**
- * @throws Exception
- *
- */
- public void goDown() throws Exception {
- this.getServer().getServer().stop();
- this.getServer().getHandler().destroy();
- for (Connector connector : this.getServer().getConnectors()) {
- connector.stop();
- }
- this.getServer().stop();
- this.running.set(false);
- }
- }
|