Skip to content

Commit

Permalink
#933: Implement Destroyable, Startable and Stoppable for ObjectEnabler
Browse files Browse the repository at this point in the history
To call each interface method on corresponded `LwM2mObjectTree` method.

Signed-off-by: moznion <moznion@gmail.com>
  • Loading branch information
moznion authored and sbernard31 committed Dec 10, 2020
1 parent 1e9287f commit 2d21d7c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@
* <p>
* In case you really need the flexibility of this interface you should consider to inherit from
* {@link BaseObjectEnabler}.
* <p>
* 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 {

Expand Down
Original file line number Diff line number Diff line change
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 @@ -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<Integer, LwM2mInstanceEnabler> instances;
protected LwM2mInstanceEnablerFactory instanceFactory;
Expand Down Expand Up @@ -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();
}
}
}
}

0 comments on commit 2d21d7c

Please sign in to comment.