Browse Source

Add a controller for websockets.

The handler can't be dependency injected (as it's created by the
framework) so, in order to minimise the amount of static
horribleness, pass in a controller which can do the actual work
and have proper dependencies.
pull/438/head
Chris Smith 8 years ago
parent
commit
3298500395

+ 85
- 0
ui_web2/src/com/dmdirc/addons/ui_web2/WebSocketController.java View File

@@ -0,0 +1,85 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.ui_web2;
24
+
25
+import java.io.IOException;
26
+import java.util.Collection;
27
+import java.util.concurrent.CopyOnWriteArrayList;
28
+
29
+import javax.inject.Inject;
30
+import javax.inject.Singleton;
31
+
32
+import org.eclipse.jetty.websocket.api.Session;
33
+
34
+/**
35
+ * Manages events raised by the {@link WebSocketHandler}.
36
+ *
37
+ * <p>This serves as a bridge between the {@link WebSocketHandler}, which cannot have dependencies
38
+ * passed in sanely due to the framework, and the rest of the plugin/client.
39
+ */
40
+@Singleton
41
+public class WebSocketController {
42
+
43
+    private final Collection<Session> sessions = new CopyOnWriteArrayList<Session>();
44
+
45
+    @Inject
46
+    public WebSocketController() {
47
+    }
48
+
49
+    void sessionConnected(final Session session) {
50
+        sessions.add(session);
51
+    }
52
+
53
+    void sessionClosed(final Session session, final int statusCode, final String reason) {
54
+        sessions.remove(session);
55
+    }
56
+
57
+    void messageReceived(final Session session, final String message) {
58
+        // Echo the message back for testing
59
+        sendMessage(session, message);
60
+    }
61
+
62
+    /**
63
+     * Sends a message to a specific session.
64
+     *
65
+     * @param session The session to send a message to.
66
+     * @param message The message to be sent.
67
+     */
68
+    public void sendMessage(final Session session, final String message) {
69
+        try {
70
+            WebSocketHandler.sendMessage(session, message);
71
+        } catch (IOException ex) {
72
+            // TODO: Raise an error...
73
+        }
74
+    }
75
+
76
+    /**
77
+     * Sends a message to all connected sessions.
78
+     *
79
+     * @param message The message to be sent.
80
+     */
81
+    public void sendMessage(final String message) {
82
+        sessions.forEach(s -> sendMessage(s, message));
83
+    }
84
+
85
+}

+ 20
- 7
ui_web2/src/com/dmdirc/addons/ui_web2/WebSocketHandler.java View File

@@ -23,8 +23,6 @@
23 23
 package com.dmdirc.addons.ui_web2;
24 24
 
25 25
 import java.io.IOException;
26
-import java.util.Queue;
27
-import java.util.concurrent.ConcurrentLinkedQueue;
28 26
 
29 27
 import org.eclipse.jetty.websocket.api.Session;
30 28
 import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
@@ -38,21 +36,36 @@ import org.eclipse.jetty.websocket.api.annotations.WebSocket;
38 36
 @WebSocket
39 37
 public class WebSocketHandler {
40 38
 
41
-    private static final Queue<Session> sessions = new ConcurrentLinkedQueue<>();
39
+    /** Controller to use. */
40
+    @SuppressWarnings("StaticNonFinalField")
41
+    private static WebSocketController controller;
42
+
43
+    /**
44
+     * Sets the controller that manages ALL instances of {@link WebSocketHandler}.
45
+     *
46
+     * @param controller Controller to use.
47
+     */
48
+    @SuppressWarnings("StaticMethodOnlyUsedInOneClass")
49
+    public static void setController(final WebSocketController controller) {
50
+        WebSocketHandler.controller = controller;
51
+    }
42 52
 
43 53
     @OnWebSocketConnect
44 54
     public void connected(final Session session) {
45
-        sessions.add(session);
55
+        controller.sessionConnected(session);
46 56
     }
47 57
 
48 58
     @OnWebSocketClose
49 59
     public void closed(final Session session, final int statusCode, final String reason) {
50
-        sessions.remove(session);
60
+        controller.sessionClosed(session, statusCode, reason);
51 61
     }
52 62
 
53 63
     @OnWebSocketMessage
54
-    public void message(final Session session, final String message) throws IOException {
55
-        // Echo the message back, for testing.
64
+    public void message(final Session session, final String message) {
65
+        controller.messageReceived(session, message);
66
+    }
67
+
68
+    public static void sendMessage(final Session session, final String message) throws IOException {
56 69
         session.getRemote().sendString(message);
57 70
     }
58 71
 

+ 3
- 1
ui_web2/src/com/dmdirc/addons/ui_web2/WebUiModule.java View File

@@ -68,7 +68,9 @@ public class WebUiModule {
68 68
     @Singleton
69 69
     public WebServer getWebServer(
70 70
             @PluginDomain(WebUiPlugin.class) final String domain,
71
-            @GlobalConfig final AggregateConfigProvider globalConfig) {
71
+            @GlobalConfig final AggregateConfigProvider globalConfig,
72
+            final WebSocketController controller) {
73
+        WebSocketHandler.setController(controller);
72 74
         final int port = globalConfig.getOptionInt(domain, "port");
73 75
         return new WebServer(port);
74 76
     }

Loading…
Cancel
Save