|
@@ -0,0 +1,104 @@
|
|
|
+package org.robert.printer.ws;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.Reader;
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
+import org.codehaus.jettison.json.JSONException;
|
|
|
+import org.codehaus.jettison.json.JSONObject;
|
|
|
+import org.eclipse.jetty.websocket.api.Session;
|
|
|
+import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
|
|
|
+import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
|
|
|
+import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
|
|
|
+import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
|
|
|
+import org.eclipse.jetty.websocket.api.annotations.WebSocket;
|
|
|
+import org.robert.printer.common.PrintersDiscovery;
|
|
|
+
|
|
|
+@WebSocket
|
|
|
+public class PrintersSocketHandler {
|
|
|
+
|
|
|
+ private final HashMap<Integer, SocketConnection> connections = new HashMap<Integer, SocketConnection>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public HashMap<Integer, SocketConnection> getConnections() {
|
|
|
+ return this.connections;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param session
|
|
|
+ */
|
|
|
+ @OnWebSocketConnect
|
|
|
+ public void onConnect(Session session) {
|
|
|
+ this.getConnections().put(session.getRemoteAddress().getPort(), new SocketConnection());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param session
|
|
|
+ * @param code
|
|
|
+ * @param reason
|
|
|
+ */
|
|
|
+ @OnWebSocketClose
|
|
|
+ public void onClose(Session session, int code, String reason) {
|
|
|
+ int port = session.getRemoteAddress().getPort();
|
|
|
+ this.getConnections().remove(port);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param session
|
|
|
+ * @param error
|
|
|
+ */
|
|
|
+ @OnWebSocketError
|
|
|
+ public void onError(Session session, Throwable error) {
|
|
|
+ System.out.println(error.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnWebSocketMessage
|
|
|
+ public void onMessage(Session session, Reader reader) throws IOException, JSONException {
|
|
|
+ String message = IOUtils.toString(reader);
|
|
|
+
|
|
|
+ if (message == null || message.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject json = new JSONObject(message);
|
|
|
+
|
|
|
+ int port = session.getRemoteAddress().getPort();
|
|
|
+ SocketConnection connection = this.getConnections().get(port);
|
|
|
+
|
|
|
+ this.processMessage(session, json, connection);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @throws JSONException
|
|
|
+ *
|
|
|
+ */
|
|
|
+ private void processMessage(Session session, JSONObject json, SocketConnection connection) throws JSONException {
|
|
|
+ String action = json.optString("action");
|
|
|
+
|
|
|
+
|
|
|
+ if (action.equals("list")) {
|
|
|
+ JSONObject printerNames = PrintersDiscovery.getPrinterNames();
|
|
|
+ this.send(session, printerNames);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param session
|
|
|
+ * @param reply
|
|
|
+ */
|
|
|
+ private synchronized void send(Session session, JSONObject reply) {
|
|
|
+ try {
|
|
|
+ session.getRemote().sendString(reply.toString());
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|