Pārlūkot izejas kodu

Merge pull request #439 from csmith/web2

Pass initial window state to new clients.
pull/440/head
Greg Holmes 8 gadus atpakaļ
vecāks
revīzija
3729eb15da

+ 1
- 0
ui_web2/build.gradle Parādīt failu

@@ -1,3 +1,4 @@
1 1
 dependencies {
2 2
   bundle group: 'com.sparkjava', name: 'spark-core', version: '2.3'
3
+  bundle group: 'com.google.code.gson', name: 'gson', 'version': '2.5'
3 4
 }

+ 74
- 0
ui_web2/src/com/dmdirc/addons/ui_web2/InitialStateProducer.java Parādīt failu

@@ -0,0 +1,74 @@
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 com.dmdirc.addons.ui_web2.serialisers.BackBufferSerializer;
26
+import com.dmdirc.addons.ui_web2.serialisers.WindowModelSerialiser;
27
+import com.dmdirc.interfaces.WindowModel;
28
+import com.dmdirc.ui.WindowManager;
29
+import com.dmdirc.ui.messages.BackBuffer;
30
+
31
+import com.google.gson.Gson;
32
+import com.google.gson.GsonBuilder;
33
+
34
+import java.util.Collection;
35
+import java.util.Collections;
36
+
37
+import javax.inject.Inject;
38
+
39
+/**
40
+ * Handles compiling the initial burst of state that will be sent to new web clients.
41
+ */
42
+public class InitialStateProducer {
43
+
44
+    private final Gson serialiser;
45
+    private final WindowManager windowManager;
46
+
47
+    @Inject
48
+    public InitialStateProducer(final WindowManager windowManager) {
49
+        serialiser = new GsonBuilder()
50
+                .registerTypeHierarchyAdapter(WindowModel.class, new WindowModelSerialiser())
51
+                .registerTypeAdapter(BackBuffer.class, new BackBufferSerializer())
52
+                .create();
53
+        this.windowManager = windowManager;
54
+    }
55
+
56
+    public String getInitialState() {
57
+        final InitialState state = new InitialState(windowManager.getRootWindows());
58
+        return serialiser.toJson(state);
59
+    }
60
+
61
+    private static class InitialState {
62
+
63
+        private final Collection<WindowModel> windows;
64
+
65
+        private InitialState(final Collection<WindowModel> windows) {
66
+            this.windows = windows;
67
+        }
68
+
69
+        public Collection<WindowModel> getWindows() {
70
+            return Collections.unmodifiableCollection(windows);
71
+        }
72
+    }
73
+
74
+}

+ 5
- 2
ui_web2/src/com/dmdirc/addons/ui_web2/WebSocketController.java Parādīt failu

@@ -40,14 +40,17 @@ import org.eclipse.jetty.websocket.api.Session;
40 40
 @Singleton
41 41
 public class WebSocketController {
42 42
 
43
-    private final Collection<Session> sessions = new CopyOnWriteArrayList<Session>();
43
+    private final Collection<Session> sessions = new CopyOnWriteArrayList<>();
44
+    private final InitialStateProducer initialStateProducer;
44 45
 
45 46
     @Inject
46
-    public WebSocketController() {
47
+    public WebSocketController(final InitialStateProducer initialStateProducer) {
48
+        this.initialStateProducer = initialStateProducer;
47 49
     }
48 50
 
49 51
     void sessionConnected(final Session session) {
50 52
         sessions.add(session);
53
+        sendMessage(session, initialStateProducer.getInitialState());
51 54
     }
52 55
 
53 56
     void sessionClosed(final Session session, final int statusCode, final String reason) {

+ 52
- 0
ui_web2/src/com/dmdirc/addons/ui_web2/serialisers/BackBufferSerializer.java Parādīt failu

@@ -0,0 +1,52 @@
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.serialisers;
24
+
25
+import com.dmdirc.ui.messages.BackBuffer;
26
+import com.dmdirc.ui.messages.IRCDocument;
27
+
28
+import com.google.gson.JsonArray;
29
+import com.google.gson.JsonElement;
30
+import com.google.gson.JsonSerializationContext;
31
+import com.google.gson.JsonSerializer;
32
+
33
+import java.lang.reflect.Type;
34
+
35
+/**
36
+ * Serializes {@link BackBuffer}s.
37
+ */
38
+public class BackBufferSerializer implements JsonSerializer<BackBuffer> {
39
+
40
+    @Override
41
+    public JsonElement serialize(final BackBuffer src, final Type typeOfSrc,
42
+            final JsonSerializationContext context) {
43
+        final JsonArray res = new JsonArray();
44
+        final IRCDocument document = src.getDocument();
45
+        for (int i = 0; i < document.getNumLines(); i++) {
46
+            // TODO: Pass on foreground and background colours
47
+            res.add(document.getLine(i).getText());
48
+        }
49
+        return res;
50
+    }
51
+
52
+}

+ 53
- 0
ui_web2/src/com/dmdirc/addons/ui_web2/serialisers/WindowModelSerialiser.java Parādīt failu

@@ -0,0 +1,53 @@
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.serialisers;
24
+
25
+import com.dmdirc.interfaces.WindowModel;
26
+
27
+import com.google.gson.JsonElement;
28
+import com.google.gson.JsonObject;
29
+import com.google.gson.JsonSerializationContext;
30
+import com.google.gson.JsonSerializer;
31
+
32
+import java.lang.reflect.Type;
33
+
34
+/**
35
+ * Serialises {@link WindowModel}s.
36
+ */
37
+public class WindowModelSerialiser implements JsonSerializer<WindowModel> {
38
+
39
+    @Override
40
+    public JsonElement serialize(final WindowModel src, final Type typeOfSrc,
41
+            final JsonSerializationContext context) {
42
+        final JsonObject res = new JsonObject();
43
+        res.addProperty("name", src.getName());
44
+        res.addProperty("icon", src.getIcon());
45
+        res.addProperty("title", src.getTitle());
46
+        res.addProperty("writable", src.isWritable());
47
+        res.add("children", context.serialize(src.getChildren()));
48
+        res.add("components", context.serialize(src.getComponents()));
49
+        res.add("backbuffer", context.serialize(src.getBackBuffer()));
50
+        return res;
51
+    }
52
+
53
+}

Notiek ielāde…
Atcelt
Saglabāt