Browse Source

Merge pull request #153 from ShaneMcC/parserCallback

Allow parser sub-classes to redefine the callback manager
pull/154/head
Shane Mc Cormack 7 years ago
parent
commit
4105a942d9

+ 22
- 5
common/src/main/java/com/dmdirc/parser/common/BaseParser.java View File

@@ -72,7 +72,7 @@ public abstract class BaseParser extends ThreadedParser {
72 72
     private URI proxy;
73 73
 
74 74
     /** The callback manager to use for this parser. */
75
-    private final CallbackManager callbackManager;
75
+    private CallbackManager callbackManager;
76 76
 
77 77
     /**
78 78
      * Creates a new base parser for the specified URI.
@@ -81,8 +81,6 @@ public abstract class BaseParser extends ThreadedParser {
81 81
      */
82 82
     public BaseParser(final URI uri) {
83 83
         this.uri = uri;
84
-
85
-        callbackManager = new CallbackManager(this::handleCallbackError);
86 84
     }
87 85
 
88 86
     @SuppressWarnings({
@@ -90,7 +88,7 @@ public abstract class BaseParser extends ThreadedParser {
90 88
             "CallToPrintStackTrace",
91 89
             "UseOfSystemOutOrSystemErr"
92 90
     })
93
-    private void handleCallbackError(final PublicationError e) {
91
+    protected void handleCallbackError(final PublicationError e) {
94 92
         if (Thread.holdsLock(errorHandlerLock)) {
95 93
             // ABORT ABORT ABORT - we're publishing an error on the same thread we just tried
96 94
             // to publish an error on. Something in the error reporting pipeline must be
@@ -101,7 +99,7 @@ public abstract class BaseParser extends ThreadedParser {
101 99
         }
102 100
 
103 101
         synchronized (errorHandlerLock) {
104
-            callbackManager.publish(new ParserErrorEvent(this, LocalDateTime.now(), e.getCause()));
102
+            getCallbackManager().publish(new ParserErrorEvent(this, LocalDateTime.now(), e.getCause()));
105 103
         }
106 104
     }
107 105
 
@@ -177,9 +175,28 @@ public abstract class BaseParser extends ThreadedParser {
177 175
 
178 176
     @Override
179 177
     public CallbackManager getCallbackManager() {
178
+        // If setCallbackManager hasn't been called, assume we want to use the default CallbackManager
179
+        if (callbackManager == null) {
180
+            setCallbackManager(new CallbackManager(this::handleCallbackError));
181
+        }
180 182
         return callbackManager;
181 183
     }
182 184
 
185
+    /**
186
+     * Set the {@link CallbackManager} used by this parser.
187
+     * This can only be called once
188
+     *
189
+     * @param manager CallbackManager to use
190
+     */
191
+    protected void setCallbackManager(final CallbackManager manager) {
192
+        if (manager == null) {
193
+            throw new NullPointerException("setCallbackManager can not be called with a null parameter.");
194
+        } else if (callbackManager != null) {
195
+            throw new IllegalStateException("setCallbackManager can only be called once.");
196
+        }
197
+        callbackManager = manager;
198
+    }
199
+
183 200
     @Override
184 201
     public void joinChannel(final String channel) {
185 202
         joinChannels(new ChannelJoinRequest(channel));

+ 5
- 1
common/src/main/java/com/dmdirc/parser/common/CallbackManager.java View File

@@ -36,11 +36,15 @@ import net.engio.mbassy.bus.error.IPublicationErrorHandler;
36 36
 public class CallbackManager extends MBassador<ParserEvent> {
37 37
 
38 38
     public CallbackManager(final IPublicationErrorHandler errorHandler) {
39
-        super(new BusConfiguration().addFeature(Feature.SyncPubSub.Default())
39
+        this(new BusConfiguration().addFeature(Feature.SyncPubSub.Default())
40 40
                 .addFeature(Feature.AsynchronousHandlerInvocation.Default(1, 1))
41 41
                 .addFeature(Feature.AsynchronousMessageDispatch.Default()
42 42
                         .setNumberOfMessageDispatchers(1))
43 43
                 .addPublicationErrorHandler(errorHandler));
44 44
     }
45 45
 
46
+    protected CallbackManager(final BusConfiguration busConfiguration) {
47
+        super(busConfiguration);
48
+    }
49
+
46 50
 }

+ 1
- 0
irc/src/main/java/com/dmdirc/parser/irc/IRCParser.java View File

@@ -274,6 +274,7 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
274 274
      */
275 275
     public IRCParser(final MyInfo myDetails, final URI uri) {
276 276
         super(uri);
277
+        setCallbackManager(new IRCParserCallbackManager(this::handleCallbackError));
277 278
 
278 279
         // TODO: There should be a factory or builder for parsers that can construct the graph
279 280
         final ObjectGraph graph = ObjectGraph.create(new IRCParserModule(this, prefixModes,

+ 61
- 0
irc/src/main/java/com/dmdirc/parser/irc/IRCParserCallbackManager.java View File

@@ -0,0 +1,61 @@
1
+/*
2
+ * Copyright (c) 2006-2017 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.parser.irc;
24
+
25
+import com.dmdirc.parser.common.CallbackManager;
26
+import com.dmdirc.parser.events.ParserEvent;
27
+import net.engio.mbassy.bus.IMessagePublication;
28
+import net.engio.mbassy.bus.config.BusConfiguration;
29
+import net.engio.mbassy.bus.config.Feature;
30
+import net.engio.mbassy.bus.error.IPublicationErrorHandler;
31
+
32
+import java.util.concurrent.TimeUnit;
33
+
34
+/**
35
+ * Parser Callback Manager.
36
+ * Manages adding/removing/calling callbacks.
37
+ *
38
+ * Because IRCParser was designed for synchronous callbacks not async events, we enforce synchronous publish only
39
+ * in this CallbackManager for now.
40
+ *
41
+ * This may change in future.
42
+ */
43
+public class IRCParserCallbackManager extends CallbackManager {
44
+    public IRCParserCallbackManager(final IPublicationErrorHandler errorHandler) {
45
+        super(new BusConfiguration().addFeature(Feature.SyncPubSub.Default())
46
+                .addFeature(Feature.AsynchronousHandlerInvocation.Default(1, 1))
47
+                .addFeature(Feature.AsynchronousMessageDispatch.Default()
48
+                        .setNumberOfMessageDispatchers(0))
49
+                .addPublicationErrorHandler(errorHandler));
50
+    }
51
+
52
+    @Override
53
+    public IMessagePublication publishAsync(final ParserEvent message) {
54
+        throw new UnsupportedOperationException("IRCParser does not support publishAsync");
55
+    }
56
+
57
+    @Override
58
+    public IMessagePublication publishAsync(final ParserEvent message, final long timeout, final TimeUnit unit) {
59
+        throw new UnsupportedOperationException("IRCParser does not support publishAsync");
60
+    }
61
+}

Loading…
Cancel
Save