You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2020/07/13 07:32:24 UTC
[plc4x] branch feature/knx-read-write-support updated: Added
ContextHandler to allow to Cancel running Registered Handlers.
This is an automated email from the ASF dual-hosted git repository.
jfeinauer pushed a commit to branch feature/knx-read-write-support
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/feature/knx-read-write-support by this push:
new deef47c Added ContextHandler to allow to Cancel running Registered Handlers.
deef47c is described below
commit deef47cb9c6694a040129cd131de63f921632b8c
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Mon Jul 13 09:29:36 2020 +0200
Added ContextHandler to allow to Cancel running Registered Handlers.
---
.../apache/plc4x/java/spi/ConversationContext.java | 12 ++++++++--
.../apache/plc4x/java/spi/Plc4xNettyWrapper.java | 9 ++++++++
.../java/spi/internal/DefaultContextHandler.java | 26 ++++++++++++++++++++++
.../spi/internal/DefaultExpectRequestContext.java | 7 ++++--
.../spi/internal/DefaultSendRequestContext.java | 6 +++--
.../java/spi/internal/HandlerRegistration.java | 20 +++++++++++++++++
6 files changed, 74 insertions(+), 6 deletions(-)
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/ConversationContext.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/ConversationContext.java
index bdfa6cc..27ffbdf 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/ConversationContext.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/ConversationContext.java
@@ -47,7 +47,7 @@ public interface ConversationContext<T> {
SendRequestContext<T> check(Predicate<T> checker);
- void handle(Consumer<T> packetConsumer);
+ ContextHandler handle(Consumer<T> packetConsumer);
SendRequestContext<T> onTimeout(Consumer<TimeoutException> packetConsumer);
@@ -64,7 +64,7 @@ public interface ConversationContext<T> {
ExpectRequestContext<T> check(Predicate<T> checker);
- void handle(Consumer<T> packetConsumer);
+ ContextHandler handle(Consumer<T> packetConsumer);
ExpectRequestContext<T> onTimeout(Consumer<TimeoutException> packetConsumer);
@@ -79,4 +79,12 @@ public interface ConversationContext<T> {
}
}
+ interface ContextHandler {
+
+ boolean isDone();
+
+ void cancel();
+
+ }
+
}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java
index ca8dcce..849641d 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java
@@ -123,6 +123,13 @@ public class Plc4xNettyWrapper<T> extends MessageToMessageCodec<T, Object> {
for (Iterator<HandlerRegistration> iter = this.registeredHandlers.iterator(); iter.hasNext(); ) {
HandlerRegistration registration = iter.next();
// Check if the handler can still be used or should be removed
+ // Was cancelled?
+ if (registration.isCancelled()) {
+ logger.debug("Removing {} as it was cancelled!", registration);
+ iter.remove();
+ continue;
+ }
+ // Timeout?
if (registration.getTimeout().isBefore(Instant.now())) {
logger.debug("Removing {} as its timed out (was set till {})", registration, registration.getTimeout());
iter.remove();
@@ -152,6 +159,8 @@ public class Plc4xNettyWrapper<T> extends MessageToMessageCodec<T, Object> {
this.registeredHandlers.remove(registration);
Consumer handler = registration.getPacketConsumer();
handler.accept(instance);
+ // Confirm that it was handled!
+ registration.confirmHandled();
return;
}
}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultContextHandler.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultContextHandler.java
new file mode 100644
index 0000000..2249759
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultContextHandler.java
@@ -0,0 +1,26 @@
+package org.apache.plc4x.java.spi.internal;
+
+import org.apache.plc4x.java.spi.ConversationContext;
+
+import java.util.function.BooleanSupplier;
+
+class DefaultContextHandler implements ConversationContext.ContextHandler {
+
+ private final BooleanSupplier getDone;
+ private final Runnable cancel;
+
+ public DefaultContextHandler(BooleanSupplier getDone, Runnable cancel) {
+ this.getDone = getDone;
+ this.cancel = cancel;
+ }
+
+ @Override
+ public boolean isDone() {
+ return this.getDone.getAsBoolean();
+ }
+
+ @Override
+ public void cancel() {
+ this.cancel.run();
+ }
+}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultExpectRequestContext.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultExpectRequestContext.java
index 80700fb..716da55 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultExpectRequestContext.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultExpectRequestContext.java
@@ -48,6 +48,7 @@ public class DefaultExpectRequestContext<T> implements ConversationContext.Expec
protected BiConsumer<?, ? extends Throwable> errorConsumer;
protected Duration timeout;
+ private HandlerRegistration registration;
public DefaultExpectRequestContext(Consumer<HandlerRegistration> finisher, Class<T> expectClazz, Duration timeout, ConversationContext context) {
this.finisher = finisher;
@@ -74,12 +75,14 @@ public class DefaultExpectRequestContext<T> implements ConversationContext.Expec
}
@Override
- public void handle(Consumer<T> packetConsumer) {
+ public ConversationContext.ContextHandler handle(Consumer<T> packetConsumer) {
if (this.packetConsumer != null) {
throw new ConversationContext.PlcWiringException("can't handle multiple consumers");
}
this.packetConsumer = packetConsumer;
- finisher.accept(new HandlerRegistration(commands, expectClazz, packetConsumer, onTimeoutConsumer, errorConsumer, Instant.now().plus(timeout)));
+ registration = new HandlerRegistration(commands, expectClazz, packetConsumer, onTimeoutConsumer, errorConsumer, Instant.now().plus(timeout));
+ finisher.accept(registration);
+ return new DefaultContextHandler(registration::hasHandled, registration::cancel);
}
@Override
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultSendRequestContext.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultSendRequestContext.java
index 2b61368..376c0fd 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultSendRequestContext.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultSendRequestContext.java
@@ -88,13 +88,15 @@ public class DefaultSendRequestContext<T> implements ConversationContext.SendReq
}
@Override
- public void handle(Consumer<T> packetConsumer) {
+ public DefaultContextHandler handle(Consumer<T> packetConsumer) {
if (this.packetConsumer != null) {
throw new ConversationContext.PlcWiringException("can't handle multiple consumers");
}
this.packetConsumer = packetConsumer;
- finisher.accept(new HandlerRegistration(commands, expectClazz, packetConsumer, onTimeoutConsumer, errorConsumer, Instant.now().plus(timeout)));
+ final HandlerRegistration registration = new HandlerRegistration(commands, expectClazz, packetConsumer, onTimeoutConsumer, errorConsumer, Instant.now().plus(timeout));
+ finisher.accept(registration);
context.sendToWire(request);
+ return new DefaultContextHandler(() -> registration.hasHandled(), () -> registration.cancel());
}
@Override
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/HandlerRegistration.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/HandlerRegistration.java
index 38f5eed..876ad91 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/HandlerRegistration.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/HandlerRegistration.java
@@ -24,6 +24,7 @@ import io.vavr.control.Either;
import java.time.Instant;
import java.util.Deque;
import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
@@ -46,6 +47,9 @@ public class HandlerRegistration {
private final BiConsumer<?, ? extends Throwable> errorConsumer;
private final Instant timeout;
+ private volatile boolean cancelled = false;
+ private volatile boolean handled = false;
+
public HandlerRegistration(Deque<Either<Function<?, ?>, Predicate<?>>> commands, Class<?> expectClazz, Consumer<?> packetConsumer, Consumer<TimeoutException> onTimeoutConsumer, BiConsumer<?, ? extends Throwable> errorConsumer, Instant timeout) {
this.commands = commands;
this.expectClazz = expectClazz;
@@ -79,6 +83,22 @@ public class HandlerRegistration {
return timeout;
}
+ public void cancel() {
+ this.cancelled = true;
+ }
+
+ public boolean isCancelled() {
+ return this.cancelled;
+ }
+
+ public void confirmHandled() {
+ this.handled = true;
+ }
+
+ public boolean hasHandled() {
+ return this.handled;
+ }
+
@Override
public String toString() {
return "HandlerRegistration#" + id;