From de12dca6892c61eedb65ae4e4358880b96150964 Mon Sep 17 00:00:00 2001 From: moznion Date: Wed, 9 Dec 2020 02:29:14 +0900 Subject: [PATCH] #933: Implement Destroyable, Startable and Stoppable for ObjectEnabler To call each interface method on corresponded `LwM2mObjectTree` method. Signed-off-by: moznion --- .../client/resource/LwM2mObjectEnabler.java | 8 +++++ .../leshan/client/resource/ObjectEnabler.java | 34 ++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/LwM2mObjectEnabler.java b/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/LwM2mObjectEnabler.java index e4f4234e30..cc442619e3 100644 --- a/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/LwM2mObjectEnabler.java +++ b/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/LwM2mObjectEnabler.java @@ -53,6 +53,14 @@ *

* In case you really need the flexibility of this interface you should consider to inherit from * {@link BaseObjectEnabler}. + *

+ * An instance that implements this interface synchronizes with the lifecycle of the LeshanClient. + * This means when {@code LeshanClient#destroy()} is called, {@code LwM2mObjectEnabler#destroy()} is + * also called if it implements the {@link org.eclipse.leshan.core.Destroyable} interface. + * And {@link org.eclipse.leshan.core.Startable} ({@code #start()}) and + * {@link org.eclipse.leshan.core.Stoppable} ({@code #stop()}) are also same as this. + * If you need to restart the instance, please implement {@link org.eclipse.leshan.core.Startable} + * with {@link org.eclipse.leshan.core.Stoppable} together. */ public interface LwM2mObjectEnabler { diff --git a/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/ObjectEnabler.java b/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/ObjectEnabler.java index e0930669ca..9aac8be8b7 100644 --- a/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/ObjectEnabler.java +++ b/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/ObjectEnabler.java @@ -28,7 +28,10 @@ import org.eclipse.leshan.client.LwM2mClient; import org.eclipse.leshan.client.servers.ServerIdentity; import org.eclipse.leshan.client.servers.ServersInfoExtractor; +import org.eclipse.leshan.core.Destroyable; import org.eclipse.leshan.core.LwM2mId; +import org.eclipse.leshan.core.Startable; +import org.eclipse.leshan.core.Stoppable; import org.eclipse.leshan.core.model.ObjectModel; import org.eclipse.leshan.core.node.LwM2mObject; import org.eclipse.leshan.core.node.LwM2mObjectInstance; @@ -61,7 +64,7 @@ * Implementing a {@link LwM2mInstanceEnabler} then creating an {@link ObjectEnabler} with {@link ObjectsInitializer} is * the easier way to implement LWM2M object in Leshan client. */ -public class ObjectEnabler extends BaseObjectEnabler { +public class ObjectEnabler extends BaseObjectEnabler implements Destroyable, Startable, Stoppable { protected Map instances; protected LwM2mInstanceEnablerFactory instanceFactory; @@ -399,4 +402,33 @@ public void setLwM2mClient(LwM2mClient client) { instanceEnabler.setLwM2mClient(client); } } + + @Override + public void destroy() { + for (LwM2mInstanceEnabler instanceEnabler : instances.values()) { + if (instanceEnabler instanceof Destroyable) { + ((Destroyable) instanceEnabler).destroy(); + } else if (instanceEnabler instanceof Stoppable) { + ((Stoppable) instanceEnabler).stop(); + } + } + } + + @Override + public void start() { + for (LwM2mInstanceEnabler instanceEnabler : instances.values()) { + if (instanceEnabler instanceof Startable) { + ((Startable) instanceEnabler).start(); + } + } + } + + @Override + public void stop() { + for (LwM2mInstanceEnabler instanceEnabler : instances.values()) { + if (instanceEnabler instanceof Stoppable) { + ((Stoppable) instanceEnabler).stop(); + } + } + } }