Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of Startable, Stoppable and Destroyable to LwM2mObjectEnabler. #940

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
import org.eclipse.leshan.client.resource.listener.ObjectListener;
import org.eclipse.leshan.client.resource.listener.ObjectsListenerAdapter;
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.californium.EndpointFactory;
import org.eclipse.leshan.core.node.codec.LwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.server;
package org.eclipse.leshan.core;

public interface Destroyable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.server;
package org.eclipse.leshan.core;

public interface Startable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.server;
package org.eclipse.leshan.core;

public interface Stoppable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
import org.eclipse.leshan.core.response.LwM2mResponse;
import org.eclipse.leshan.core.response.ResponseCallback;
import org.eclipse.leshan.core.util.Validate;
import org.eclipse.leshan.server.Destroyable;
import org.eclipse.leshan.server.Startable;
import org.eclipse.leshan.server.Stoppable;
import org.eclipse.leshan.core.Destroyable;
import org.eclipse.leshan.core.Startable;
import org.eclipse.leshan.core.Stoppable;
import org.eclipse.leshan.server.californium.observation.ObservationServiceImpl;
import org.eclipse.leshan.server.californium.registration.CaliforniumRegistrationStore;
import org.eclipse.leshan.server.californium.registration.RegisterResource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.eclipse.leshan.core.response.ErrorCallback;
import org.eclipse.leshan.core.response.LwM2mResponse;
import org.eclipse.leshan.core.response.ResponseCallback;
import org.eclipse.leshan.server.Destroyable;
import org.eclipse.leshan.core.Destroyable;
import org.eclipse.leshan.server.bootstrap.BootstrapSession;
import org.eclipse.leshan.server.bootstrap.LwM2mBootstrapRequestSender;
import org.eclipse.leshan.server.californium.request.RequestSender;
Expand Down Expand Up @@ -134,4 +134,4 @@ public void cancelOngoingRequests(BootstrapSession session) {
public void destroy() {
sender.destroy();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import org.eclipse.leshan.core.node.codec.LwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
import org.eclipse.leshan.core.util.Validate;
import org.eclipse.leshan.server.Destroyable;
import org.eclipse.leshan.server.Startable;
import org.eclipse.leshan.server.Stoppable;
import org.eclipse.leshan.core.Destroyable;
import org.eclipse.leshan.core.Startable;
import org.eclipse.leshan.core.Stoppable;
import org.eclipse.leshan.server.bootstrap.BootstrapConfigurationStore;
import org.eclipse.leshan.server.bootstrap.BootstrapHandler;
import org.eclipse.leshan.server.bootstrap.BootstrapHandlerFactory;
Expand Down Expand Up @@ -245,4 +245,4 @@ public CoapEndpoint getUnsecuredEndpoint() {
return unsecuredEndpoint;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
import org.eclipse.californium.elements.EndpointContext;
import org.eclipse.leshan.core.observation.Observation;
import org.eclipse.leshan.core.util.NamedThreadFactory;
import org.eclipse.leshan.server.Destroyable;
import org.eclipse.leshan.server.Startable;
import org.eclipse.leshan.server.Stoppable;
import org.eclipse.leshan.core.Destroyable;
import org.eclipse.leshan.core.Startable;
import org.eclipse.leshan.core.Stoppable;
import org.eclipse.leshan.server.californium.observation.ObserveUtil;
import org.eclipse.leshan.server.registration.Deregistration;
import org.eclipse.leshan.server.registration.ExpirationListener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.eclipse.leshan.core.response.ObserveResponse;
import org.eclipse.leshan.core.response.ResponseCallback;
import org.eclipse.leshan.core.util.Validate;
import org.eclipse.leshan.server.Destroyable;
import org.eclipse.leshan.core.Destroyable;
import org.eclipse.leshan.server.californium.observation.ObservationServiceImpl;
import org.eclipse.leshan.server.model.LwM2mModelProvider;
import org.eclipse.leshan.server.registration.Registration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.eclipse.leshan.core.request.exception.ClientSleepingException;
import org.eclipse.leshan.core.request.exception.TimeoutException;
import org.eclipse.leshan.core.response.ErrorCallback;
import org.eclipse.leshan.server.Destroyable;
import org.eclipse.leshan.core.Destroyable;
import org.eclipse.leshan.server.queue.PresenceServiceImpl;
import org.eclipse.leshan.server.queue.QueueModeLwM2mRequestSender;
import org.eclipse.leshan.server.registration.Registration;
Expand Down Expand Up @@ -136,4 +136,4 @@ public void destroy() {
((Destroyable) delegatedSender).destroy();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import org.eclipse.leshan.core.response.ResponseCallback;
import org.eclipse.leshan.core.util.NamedThreadFactory;
import org.eclipse.leshan.core.util.Validate;
import org.eclipse.leshan.server.Destroyable;
import org.eclipse.leshan.core.Destroyable;
import org.eclipse.leshan.server.request.LowerLayerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.leshan.core.util.NamedThreadFactory;
import org.eclipse.leshan.server.Destroyable;
import org.eclipse.leshan.core.Destroyable;
import org.eclipse.leshan.server.registration.Registration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
import org.eclipse.leshan.core.observation.Observation;
import org.eclipse.leshan.core.util.NamedThreadFactory;
import org.eclipse.leshan.core.util.Validate;
import org.eclipse.leshan.server.Destroyable;
import org.eclipse.leshan.server.Startable;
import org.eclipse.leshan.server.Stoppable;
import org.eclipse.leshan.core.Destroyable;
import org.eclipse.leshan.core.Startable;
import org.eclipse.leshan.core.Stoppable;
import org.eclipse.leshan.server.californium.observation.ObserveUtil;
import org.eclipse.leshan.server.californium.registration.CaliforniumRegistrationStore;
import org.eclipse.leshan.server.redis.serialization.ObservationSerDes;
Expand Down