PrintersSocketServer.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package org.robert.printer.ws;
  2. import java.util.concurrent.atomic.AtomicBoolean;
  3. import org.eclipse.jetty.http.HttpVersion;
  4. import org.eclipse.jetty.server.Connector;
  5. import org.eclipse.jetty.server.HttpConfiguration;
  6. import org.eclipse.jetty.server.HttpConnectionFactory;
  7. import org.eclipse.jetty.server.Server;
  8. import org.eclipse.jetty.server.ServerConnector;
  9. import org.eclipse.jetty.server.SslConnectionFactory;
  10. import org.eclipse.jetty.util.ssl.SslContextFactory;
  11. import org.eclipse.jetty.websocket.server.WebSocketHandler;
  12. import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
  13. public class PrintersSocketServer {
  14. private Server server;
  15. private final String host;
  16. private final int port;
  17. private final AtomicBoolean running;
  18. public PrintersSocketServer() {
  19. this.host = "0.0.0.0";
  20. this.port = 8070;
  21. this.running = new AtomicBoolean(false);
  22. }
  23. /**
  24. *
  25. * @return
  26. */
  27. private Server getServer() {
  28. return this.server;
  29. }
  30. /**
  31. *
  32. * @return
  33. */
  34. private String getHost() {
  35. return this.host;
  36. }
  37. /**
  38. *
  39. * @return
  40. */
  41. private int getPort() {
  42. return this.port;
  43. }
  44. /**
  45. *
  46. * @return
  47. */
  48. public boolean isRunning() {
  49. return this.running.get();
  50. }
  51. /**
  52. * @throws Exception
  53. *
  54. */
  55. public void goUp() throws Exception {
  56. this.server = new Server(this.getPort());
  57. SslContextFactory sslContext = new SslContextFactory();
  58. sslContext.setKeyStorePath(this.getClass().getResource("/resources/printers-tray.jks").toExternalForm());
  59. sslContext.setKeyStorePassword("robert2206b");
  60. sslContext.setKeyManagerPassword("robert2206a");
  61. SslConnectionFactory sslConnection = new SslConnectionFactory(sslContext, HttpVersion.HTTP_1_1.asString());
  62. HttpConnectionFactory httpConnection = new HttpConnectionFactory(new HttpConfiguration());
  63. ServerConnector connector = new ServerConnector(this.server, sslConnection, httpConnection);
  64. connector.setHost(this.getHost());
  65. connector.setPort(this.getPort() + 1);
  66. this.server.addConnector(connector);
  67. final WebSocketHandler wsHandler = new WebSocketHandler() {
  68. @Override
  69. public void configure(WebSocketServletFactory factory) {
  70. factory.register(PrintersSocketHandler.class);
  71. factory.getPolicy().setMaxTextMessageSize(Integer.MAX_VALUE);
  72. }
  73. };
  74. this.server.setHandler(wsHandler);
  75. this.server.setStopAtShutdown(true);
  76. this.server.start();
  77. this.running.set(true);
  78. this.server.join();
  79. }
  80. /**
  81. * @throws Exception
  82. *
  83. */
  84. public void goDown() throws Exception {
  85. this.getServer().getServer().stop();
  86. this.getServer().getHandler().destroy();
  87. for (Connector connector : this.getServer().getConnectors()) {
  88. connector.stop();
  89. }
  90. this.getServer().stop();
  91. this.running.set(false);
  92. }
  93. }