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 shutdownTrigger for registration engine #928

Closed
wants to merge 2 commits into from
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 @@ -58,6 +58,11 @@
public class LeshanClient implements LwM2mClient {

private static final Logger LOG = LoggerFactory.getLogger(LeshanClient.class);
private static final Runnable NOP_SHUTDOWN_TRIGGER = new Runnable() {
@Override
public void run() {
}
};

private final CoapAPI coapApi;
private final CoapServer coapServer;
Expand Down Expand Up @@ -85,15 +90,15 @@ public LeshanClient(String endpoint, InetSocketAddress localAddress,
Map<String, String> additionalAttributes, Map<String, String> bsAdditionalAttributes,
LwM2mNodeEncoder encoder, LwM2mNodeDecoder decoder, ScheduledExecutorService sharedExecutor) {
this(endpoint, localAddress, objectEnablers, coapConfig, dtlsConfigBuilder, null, endpointFactory,
engineFactory, additionalAttributes, bsAdditionalAttributes, encoder, decoder, sharedExecutor);
engineFactory, additionalAttributes, bsAdditionalAttributes, encoder, decoder, sharedExecutor, NOP_SHUTDOWN_TRIGGER);
}

/** @since 2.0 */
public LeshanClient(String endpoint, InetSocketAddress localAddress,
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) {
LwM2mNodeEncoder encoder, LwM2mNodeDecoder decoder, ScheduledExecutorService sharedExecutor, Runnable shutdownTrigger) {

Validate.notNull(endpoint);
Validate.notEmpty(objectEnablers);
Expand All @@ -105,7 +110,7 @@ public LeshanClient(String endpoint, InetSocketAddress localAddress,
endpointsManager = createEndpointsManager(localAddress, coapConfig, dtlsConfigBuilder, trustStore, endpointFactory);
requestSender = createRequestSender(endpointsManager, sharedExecutor);
engine = engineFactory.createRegistratioEngine(endpoint, objectTree, endpointsManager, requestSender,
bootstrapHandler, observers, additionalAttributes, bsAdditionalAttributes, sharedExecutor);
bootstrapHandler, observers, additionalAttributes, bsAdditionalAttributes, sharedExecutor, shutdownTrigger);

coapServer = createCoapServer(coapConfig, sharedExecutor);
coapServer.add(createBootstrapResource(engine, bootstrapHandler));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ public class LeshanClientBuilder {
/** @since 1.1 */
protected Map<String, String> bsAdditionalAttributes;

private Runnable shutdownTrigger = new Runnable() {
@Override
public void run() {
}
};

/**
* Creates a new instance for setting the configuration options for a {@link LeshanClient} instance.
*
Expand Down Expand Up @@ -217,6 +223,16 @@ public LeshanClientBuilder setBootstrapAdditionalAttributes(Map<String, String>
return this;
}

/**
* Set the shutdown trigger that called on unexpected exception has been occurred.
*
* The default value is NOP procedure.
*/
public LeshanClientBuilder setShutdownTrigger(Runnable shutdownTrigger) {
this.shutdownTrigger = shutdownTrigger;
return this;
}

/**
* Set a shared executor. This executor will be used everywhere it is possible. This is generally used when you want
* to limit the number of thread to use or if you want to simulate a lot of clients sharing the same thread pool.
Expand Down Expand Up @@ -324,7 +340,8 @@ protected Connector createSecuredConnector(DtlsConnectorConfig dtlsConfig) {
}

return createLeshanClient(endpoint, localAddress, objectEnablers, coapConfig, dtlsConfigBuilder,
this.trustStore, endpointFactory, engineFactory, additionalAttributes, encoder, decoder, executor);
this.trustStore, endpointFactory, engineFactory, additionalAttributes, encoder, decoder, executor,
shutdownTrigger);
}

/**
Expand Down Expand Up @@ -355,9 +372,9 @@ protected LeshanClient createLeshanClient(String endpoint, InetSocketAddress loc
List<? extends LwM2mObjectEnabler> objectEnablers, NetworkConfig coapConfig, Builder dtlsConfigBuilder,
List<Certificate> trustStore, EndpointFactory endpointFactory, RegistrationEngineFactory engineFactory,
Map<String, String> additionalAttributes, LwM2mNodeEncoder encoder, LwM2mNodeDecoder decoder,
ScheduledExecutorService sharedExecutor) {
ScheduledExecutorService sharedExecutor, Runnable shutdownTrigger) {
return new LeshanClient(endpoint, localAddress, objectEnablers, coapConfig, dtlsConfigBuilder, trustStore,
endpointFactory, engineFactory, additionalAttributes, bsAdditionalAttributes, encoder, decoder,
executor);
executor, shutdownTrigger);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
public class DefaultRegistrationEngine implements RegistrationEngine {

private static final Logger LOG = LoggerFactory.getLogger(DefaultRegistrationEngine.class);
private static final Runnable NOP_SHUTDOWN_TRIGGER = new Runnable() {
@Override
public void run() {
}
};

private static final long NOW = 0;
private static final ServerIdentity ALL = new ServerIdentity(null, null);
Expand Down Expand Up @@ -122,14 +127,17 @@ private static enum Status {
private final ScheduledExecutorService schedExecutor;
private final boolean attachedExecutor;

private final Runnable shutdownTrigger;

public DefaultRegistrationEngine(String endpoint, LwM2mObjectTree objectTree, EndpointsManager endpointsManager,
LwM2mRequestSender requestSender, BootstrapHandler bootstrapState, LwM2mClientObserver observer,
Map<String, String> additionalAttributes, ScheduledExecutorService executor, long requestTimeoutInMs,
long deregistrationTimeoutInMs, int bootstrapSessionTimeoutInSec, int retryWaitingTimeInMs,
Integer communicationPeriodInMs, boolean reconnectOnUpdate, boolean resumeOnConnect) {
this(endpoint, objectTree, endpointsManager, requestSender, bootstrapState, observer, additionalAttributes,
null, executor, requestTimeoutInMs, deregistrationTimeoutInMs, bootstrapSessionTimeoutInSec,
retryWaitingTimeInMs, communicationPeriodInMs, reconnectOnUpdate, resumeOnConnect, false);
retryWaitingTimeInMs, communicationPeriodInMs, reconnectOnUpdate, resumeOnConnect, false,
NOP_SHUTDOWN_TRIGGER);
}

/** @since 1.1 */
Expand All @@ -138,7 +146,8 @@ public DefaultRegistrationEngine(String endpoint, LwM2mObjectTree objectTree, En
Map<String, String> additionalAttributes, Map<String, String> bsAdditionalAttributes,
ScheduledExecutorService executor, long requestTimeoutInMs, long deregistrationTimeoutInMs,
int bootstrapSessionTimeoutInSec, int retryWaitingTimeInMs, Integer communicationPeriodInMs,
boolean reconnectOnUpdate, boolean resumeOnConnect, boolean useQueueMode) {
boolean reconnectOnUpdate, boolean resumeOnConnect, boolean useQueueMode,
Runnable shutdownTrigger) {
this.endpoint = endpoint;
this.objectEnablers = objectTree.getObjectEnablers();
this.bootstrapHandler = bootstrapState;
Expand All @@ -157,6 +166,7 @@ public DefaultRegistrationEngine(String endpoint, LwM2mObjectTree objectTree, En
this.reconnectOnUpdate = reconnectOnUpdate;
this.resumeOnConnect = resumeOnConnect;
this.queueMode = useQueueMode;
this.shutdownTrigger = shutdownTrigger;

if (executor == null) {
schedExecutor = createScheduledExecutor();
Expand All @@ -166,6 +176,7 @@ public DefaultRegistrationEngine(String endpoint, LwM2mObjectTree objectTree, En
attachedExecutor = false;
}


sender = requestSender;
}

Expand Down Expand Up @@ -526,6 +537,7 @@ public void run() {
LOG.info("Bootstrap task interrupted. ");
} catch (RuntimeException e) {
LOG.error("Unexpected exception during bootstrap task", e);
shutdownTrigger.run();
}
}
}
Expand Down Expand Up @@ -564,6 +576,7 @@ public void run() {
LOG.info("Registration task interrupted. ");
} catch (RuntimeException e) {
LOG.error("Unexpected exception during registration task", e);
shutdownTrigger.run();
}
}
}
Expand Down Expand Up @@ -612,6 +625,7 @@ public void run() {
LOG.info("Registration update task interrupted.");
} catch (RuntimeException e) {
LOG.error("Unexpected exception during update registration task", e);
shutdownTrigger.run();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ public DefaultRegistrationEngineFactory() {
public RegistrationEngine createRegistratioEngine(String endpoint, LwM2mObjectTree objectTree,
EndpointsManager endpointsManager, LwM2mRequestSender requestSender, BootstrapHandler bootstrapState,
LwM2mClientObserver observer, Map<String, String> additionalAttributes,
Map<String, String> bsAdditionalAttributes, ScheduledExecutorService sharedExecutor) {
Map<String, String> bsAdditionalAttributes, ScheduledExecutorService sharedExecutor,
Runnable shutdownTrigger) {
return new DefaultRegistrationEngine(endpoint, objectTree, endpointsManager, requestSender, bootstrapState,
observer, additionalAttributes, bsAdditionalAttributes, sharedExecutor, requestTimeoutInMs,
deregistrationTimeoutInMs, bootstrapSessionTimeoutInSec, retryWaitingTimeInMs, communicationPeriodInMs,
reconnectOnUpdate, resumeOnConnect, queueMode);
reconnectOnUpdate, resumeOnConnect, queueMode, shutdownTrigger);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ public interface RegistrationEngineFactory {
RegistrationEngine createRegistratioEngine(String endpoint, LwM2mObjectTree objectTree,
EndpointsManager endpointsManager, LwM2mRequestSender requestSender, BootstrapHandler bootstrapState,
LwM2mClientObserver observer, Map<String, String> additionalAttributes,
Map<String, String> bsAdditionalAttributes, ScheduledExecutorService sharedExecutor);
Map<String, String> bsAdditionalAttributes, ScheduledExecutorService sharedExecutor,
Runnable shutdownTrigger);
}
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,11 @@ public static void createAndStartClient(String endpoint, String localAddress, in
initializer.setInstancesForObject(SERVER, new Server(123, lifetime));
}
}
initializer.setInstancesForObject(DEVICE, new MyDevice());
final MyDevice myDevice = new MyDevice();
initializer.setInstancesForObject(DEVICE, myDevice);
initializer.setInstancesForObject(LOCATION, locationInstance);
initializer.setInstancesForObject(OBJECT_ID_TEMPERATURE_SENSOR, new RandomTemperatureSensor());
final RandomTemperatureSensor randomTemperatureSensor = new RandomTemperatureSensor();
initializer.setInstancesForObject(OBJECT_ID_TEMPERATURE_SENSOR, randomTemperatureSensor);
List<LwM2mObjectEnabler> enablers = initializer.createAll();

// Create CoAP Config
Expand Down Expand Up @@ -743,7 +745,23 @@ public void handshakeFailed(Handshaker handshaker, Throwable error) {
}
builder.setAdditionalAttributes(additionalAttributes);
builder.setBootstrapAdditionalAttributes(bsAdditionalAttributes);

final LeshanClientHolder leshanClientHolder = new LeshanClientHolder();
builder.setShutdownTrigger(new Runnable() {
@Override
public void run() {
LOG.info("shutdown trigger has called");
final LeshanClient client = leshanClientHolder.getClient();
if (client != null) {
client.destroy(true);
}
myDevice.cancel();
randomTemperatureSensor.shutdown();
}
});

final LeshanClient client = builder.build();
leshanClientHolder.setClient(client);

client.getObjectTree().addListener(new ObjectsListenerAdapter() {
@Override
Expand Down Expand Up @@ -872,4 +890,16 @@ public void run() {
}
}
}

private static class LeshanClientHolder {
private LeshanClient client;

public LeshanClient getClient() {
return client;
}

public void setClient(final LeshanClient client) {
this.client = client;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ public class MyDevice extends BaseInstanceEnabler {
private static final List<Integer> supportedResources = Arrays.asList(0, 1, 2, 3, 9, 10, 11, 13, 14, 15, 16, 17, 18,
19, 20, 21);

private final Timer timer;

public MyDevice() {
// notify new date each 5 second
Timer timer = new Timer("Device-Current Time");
timer = new Timer("Device-Current Time");
timer.schedule(new TimerTask() {
@Override
public void run() {
Expand Down Expand Up @@ -210,4 +212,8 @@ private long getMemoryTotal() {
public List<Integer> getAvailableResourceIds(ObjectModel model) {
return supportedResources;
}

public void cancel() {
timer.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,8 @@ private void resetMinMaxMeasuredValues() {
public List<Integer> getAvailableResourceIds(ObjectModel model) {
return supportedResources;
}

public void shutdown() {
scheduler.shutdown();
}
}