Skip to content

Commit

Permalink
#933: Call management method for each LwM2mObjectEnabler on LeshanClient
Browse files Browse the repository at this point in the history
Now it calls `destroy()`, `stop()` and `start()` for each
LwM2mObjectEnabler instance according to the LeshanClient status.
For example, If it calls `LeshanClient#destroy()`, that method
calls for each LwM2mObjectEnabler's `#destroy()`.
And other methods are also similar.

Signed-off-by: moznion <[email protected]>
  • Loading branch information
moznion authored and sbernard31 committed Nov 27, 2020
1 parent fa0f58c commit a275f2b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public LeshanClient(String endpoint, InetSocketAddress localAddress,

/** @since 2.0 */
public LeshanClient(String endpoint, InetSocketAddress localAddress,
List<? extends LwM2mObjectEnabler> objectEnablers, NetworkConfig coapConfig, Builder dtlsConfigBuilder, List<Certificate> trustStore,
EndpointFactory endpointFactory, RegistrationEngineFactory engineFactory,
List<? extends LwM2mObjectEnabler> objectEnablers, NetworkConfig coapConfig, Builder dtlsConfigBuilder,
List<Certificate> trustStore, EndpointFactory endpointFactory, RegistrationEngineFactory engineFactory,
Map<String, String> additionalAttributes, Map<String, String> bsAdditionalAttributes,
LwM2mNodeEncoder encoder, LwM2mNodeDecoder decoder, ScheduledExecutorService sharedExecutor) {

Expand All @@ -102,7 +102,8 @@ public LeshanClient(String endpoint, InetSocketAddress localAddress,
objectTree = createObjectTree(objectEnablers);
observers = createClientObserverDispatcher();
bootstrapHandler = createBoostrapHandler(objectTree);
endpointsManager = createEndpointsManager(localAddress, coapConfig, dtlsConfigBuilder, trustStore, endpointFactory);
endpointsManager = createEndpointsManager(localAddress, coapConfig, dtlsConfigBuilder, trustStore,
endpointFactory);
requestSender = createRequestSender(endpointsManager, sharedExecutor);
engine = engineFactory.createRegistratioEngine(endpoint, objectTree, endpointsManager, requestSender,
bootstrapHandler, observers, additionalAttributes, bsAdditionalAttributes, sharedExecutor);
Expand Down Expand Up @@ -189,8 +190,10 @@ protected CoapResource createBootstrapResource(RegistrationEngine engine, Bootst
}

protected CaliforniumEndpointsManager createEndpointsManager(InetSocketAddress localAddress,
NetworkConfig coapConfig, Builder dtlsConfigBuilder, List<Certificate> trustStore, EndpointFactory endpointFactory) {
return new CaliforniumEndpointsManager(localAddress, coapConfig, dtlsConfigBuilder, trustStore, endpointFactory);
NetworkConfig coapConfig, Builder dtlsConfigBuilder, List<Certificate> trustStore,
EndpointFactory endpointFactory) {
return new CaliforniumEndpointsManager(localAddress, coapConfig, dtlsConfigBuilder, trustStore,
endpointFactory);
}

protected CaliforniumLwM2mRequestSender createRequestSender(CaliforniumEndpointsManager endpointsManager,
Expand All @@ -211,6 +214,8 @@ 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 @@ -221,6 +226,8 @@ public void stop(boolean deregister) {
LOG.info("Stopping Leshan Client ...");
engine.stop(deregister);
endpointsManager.stop();
objectTree.stop();

LOG.info("Leshan client stopped.");
}

Expand All @@ -230,6 +237,8 @@ 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 @@ -21,6 +21,9 @@
import org.eclipse.leshan.client.LwM2mClient;
import org.eclipse.leshan.client.resource.listener.ObjectListener;
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.request.BootstrapDeleteRequest;
import org.eclipse.leshan.core.request.BootstrapDiscoverRequest;
Expand Down Expand Up @@ -55,6 +58,12 @@
* <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 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 LwM2mObjectEnabler {

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 a275f2b

Please sign in to comment.