From 9d5b405f285051173adc0b6e4a2ca9b48cc8640d Mon Sep 17 00:00:00 2001 From: moznion Date: Wed, 9 Dec 2020 02:34:00 +0900 Subject: [PATCH] #933: Add an interface `LwM2mClientObserver2` to handle unexpected error This interface extends `LwM2mClientObserver` interface. And make `LwM2mClientObserverAdapter` and `LwM2mClientObserverDispatcher` implement that new interface. This doesn't break the backward compatibility because `LwM2mClientObserver2` is compatible with `LwM2mClientObserver` and each implementing class conceals the difference between `LwM2mClientObserver` and `LwM2mClientObserver2`. Signed-off-by: moznion --- .../client/observer/LwM2mClientObserver2.java | 14 ++++++++++++++ .../observer/LwM2mClientObserverAdapter.java | 6 +++++- .../observer/LwM2mClientObserverDispatcher.java | 10 +++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 leshan-client-core/src/main/java/org/eclipse/leshan/client/observer/LwM2mClientObserver2.java diff --git a/leshan-client-core/src/main/java/org/eclipse/leshan/client/observer/LwM2mClientObserver2.java b/leshan-client-core/src/main/java/org/eclipse/leshan/client/observer/LwM2mClientObserver2.java new file mode 100644 index 0000000000..6f8a7c6add --- /dev/null +++ b/leshan-client-core/src/main/java/org/eclipse/leshan/client/observer/LwM2mClientObserver2.java @@ -0,0 +1,14 @@ +package org.eclipse.leshan.client.observer; + +/** + * An extended interface of {@link LwM2mClientObserver} with handler for unexpected error. + *

+ * Future plan: Since version 2.0, this interface is going to merge into {@link LwM2mClientObserver}. + * + * @since 1.3 + */ +public interface LwM2mClientObserver2 extends LwM2mClientObserver { + // ============== Unexpected Error Handling ================= + + void onUnexpectedError(Throwable unexpectedError); +} diff --git a/leshan-client-core/src/main/java/org/eclipse/leshan/client/observer/LwM2mClientObserverAdapter.java b/leshan-client-core/src/main/java/org/eclipse/leshan/client/observer/LwM2mClientObserverAdapter.java index 52fc10ba73..fcbd428d7a 100644 --- a/leshan-client-core/src/main/java/org/eclipse/leshan/client/observer/LwM2mClientObserverAdapter.java +++ b/leshan-client-core/src/main/java/org/eclipse/leshan/client/observer/LwM2mClientObserverAdapter.java @@ -26,7 +26,7 @@ * An abstract adapter class for observing registration life cycle. The methods in this class are empty. This class * exists as convenience for creating client observer objects. */ -public class LwM2mClientObserverAdapter implements LwM2mClientObserver { +public class LwM2mClientObserverAdapter implements LwM2mClientObserver2 { @Override public void onBootstrapStarted(ServerIdentity bsserver, BootstrapRequest request) { @@ -95,4 +95,8 @@ public void onDeregistrationFailure(ServerIdentity server, DeregisterRequest req @Override public void onDeregistrationTimeout(ServerIdentity server, DeregisterRequest request) { } + + @Override + public void onUnexpectedError(Throwable unexpectedError) { + } } diff --git a/leshan-client-core/src/main/java/org/eclipse/leshan/client/observer/LwM2mClientObserverDispatcher.java b/leshan-client-core/src/main/java/org/eclipse/leshan/client/observer/LwM2mClientObserverDispatcher.java index 39002d13a7..d5576bc503 100644 --- a/leshan-client-core/src/main/java/org/eclipse/leshan/client/observer/LwM2mClientObserverDispatcher.java +++ b/leshan-client-core/src/main/java/org/eclipse/leshan/client/observer/LwM2mClientObserverDispatcher.java @@ -24,7 +24,7 @@ * A dispatcher for LwM2mClientObserver. It allow several observers on a LwM2mClient. * */ -public class LwM2mClientObserverDispatcher implements LwM2mClientObserver { +public class LwM2mClientObserverDispatcher implements LwM2mClientObserver2 { private CopyOnWriteArrayList observers = new CopyOnWriteArrayList<>(); public void addObserver(LwM2mClientObserver observer) { @@ -151,4 +151,12 @@ public void onDeregistrationTimeout(ServerIdentity server, DeregisterRequest req } } + @Override + public void onUnexpectedError(Throwable unexpectedError) { + for (LwM2mClientObserver observer : observers) { + if (observer instanceof LwM2mClientObserver2) { + ((LwM2mClientObserver2) observer).onUnexpectedError(unexpectedError); + } + } + } }