Skip to content

Commit

Permalink
#933: Implement Destroyable, Startable and Stoppable on ObjectEnabler
Browse files Browse the repository at this point in the history
To call `LwM2mInstanceEnabler#stop()`, `LwM2mInstanceEnabler#start()`
and `LwM2mInstanceEnabler#destroy()` on the associated method, for each
enabler if implemented the interface.

Signed-off-by: moznion <moznion@gmail.com>
  • Loading branch information
moznion authored and sbernard31 committed Dec 7, 2020
1 parent a1bacb5 commit 1da2600
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Expand Up @@ -22,6 +22,9 @@

import org.eclipse.leshan.client.LwM2mClient;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.core.Destroyable;
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.LwM2mObjectInstance;
import org.eclipse.leshan.core.node.LwM2mResource;
Expand All @@ -45,6 +48,12 @@
* <p>
* Implementations of this interface should adhere to the definition of the implemented LWM2M Object type regarding
* acceptable resource IDs for the <code>read, write</code> and <code>execute</code> methods.
* <p>
* An instance that implements this interface synchronizes with the lifecycle of the LeshanClient. This means when
* {@code LeshanClient#destroy()} is called, {@code LwM2mInstanceEnabler#destroy()} is also called if it implements the
* {@link Destroyable} interface. And {@link Startable} ({@code #start()}) and {@link Stoppable} ({@code #stop()}) are
* also same as this. If you need to restart the instance, please implement {@link Startable} with {@link Stoppable}
* together.
*/
public interface LwM2mInstanceEnabler {

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -62,7 +65,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<Integer, LwM2mInstanceEnabler> instances;
protected LwM2mInstanceEnablerFactory instanceFactory;
Expand Down Expand Up @@ -411,4 +414,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();
}
}
}
}

0 comments on commit 1da2600

Please sign in to comment.