Skip to content

Commit

Permalink
eclipse-leshan#933: Implement Destroyable, Startable and Stoppable fo…
Browse files Browse the repository at this point in the history
…r LwM2mObjectTree

And call each interface's method at related method of `LeshanClient`;
i.e.  `start()`, `stop()` and `destroy()`.

Signed-off-by: moznion <[email protected]>
  • Loading branch information
moznion committed Dec 8, 2020
1 parent c830d08 commit a3585e6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ public void start() {
LOG.info("Starting Leshan client ...");
endpointsManager.start();
engine.start();
objectTree.start();
if (LOG.isInfoEnabled()) {
LOG.info("Leshan client[endpoint:{}] started.", engine.getEndpoint());
}
Expand All @@ -217,6 +218,7 @@ public void stop(boolean deregister) {
LOG.info("Stopping Leshan Client ...");
engine.stop(deregister);
endpointsManager.stop();
objectTree.stop();
LOG.info("Leshan client stopped.");
}

Expand All @@ -226,6 +228,7 @@ public void destroy(boolean deregister) {
engine.destroy(deregister);
endpointsManager.destroy();
requestSender.destroy();
objectTree.destroy();
LOG.info("Leshan client destroyed.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
* <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>
* {@code LeshanClient#destroy()} is called, {@code LwM2mInstanceEnabler#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 LwM2mInstanceEnabler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
import org.eclipse.leshan.client.LwM2mClient;
import org.eclipse.leshan.client.resource.listener.ObjectListener;
import org.eclipse.leshan.client.resource.listener.ObjectsListener;
import org.eclipse.leshan.core.Destroyable;
import org.eclipse.leshan.core.Startable;
import org.eclipse.leshan.core.Stoppable;

/**
* The LWM2M Object Tree.
* <p>
* It contains all the {@link LwM2mObjectEnabler} which are the implementation of each LWM2M object supported by the
* client.
*/
public class LwM2mObjectTree {
public class LwM2mObjectTree implements Startable, Stoppable, Destroyable {

protected ObjectListener dispatcher = new ObjectListenerDispatcher();
protected CopyOnWriteArrayList<ObjectsListener> listeners = new CopyOnWriteArrayList<>();
Expand Down Expand Up @@ -94,6 +97,35 @@ public void removeObjectEnabler(int objectId) {
}
}

@Override
public void destroy() {
for (LwM2mObjectEnabler objectEnabler : objectEnablers.values()) {
if (objectEnabler instanceof Destroyable) {
((Destroyable) objectEnabler).destroy();
} else if (objectEnabler instanceof Stoppable) {
((Stoppable) objectEnabler).stop();
}
}
}

@Override
public void start() {
for (LwM2mObjectEnabler objectEnabler : objectEnablers.values()) {
if (objectEnabler instanceof Startable) {
((Startable) objectEnabler).start();
}
}
}

@Override
public void stop() {
for (LwM2mObjectEnabler objectEnabler : objectEnablers.values()) {
if (objectEnabler instanceof Stoppable) {
((Stoppable) objectEnabler).stop();
}
}
}

protected class ObjectListenerDispatcher implements ObjectListener {
@Override
public void objectInstancesAdded(LwM2mObjectEnabler object, int... instanceIds) {
Expand Down

0 comments on commit a3585e6

Please sign in to comment.