diff --git a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/LeshanClientDemo.java b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/LeshanClientDemo.java index a3c2061238..dac2583bb6 100644 --- a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/LeshanClientDemo.java +++ b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/LeshanClientDemo.java @@ -59,6 +59,7 @@ import org.eclipse.leshan.client.californium.LeshanClientBuilder; import org.eclipse.leshan.client.engine.DefaultRegistrationEngineFactory; import org.eclipse.leshan.client.object.Server; +import org.eclipse.leshan.client.observer.LwM2mClientObserverAdapter; import org.eclipse.leshan.client.resource.LwM2mObjectEnabler; import org.eclipse.leshan.client.resource.ObjectsInitializer; import org.eclipse.leshan.client.resource.listener.ObjectsListenerAdapter; @@ -623,9 +624,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(); + final RandomTemperatureSensor randomTemperatureSensor = new RandomTemperatureSensor(); + initializer.setInstancesForObject(DEVICE, myDevice); initializer.setInstancesForObject(LOCATION, locationInstance); - initializer.setInstancesForObject(OBJECT_ID_TEMPERATURE_SENSOR, new RandomTemperatureSensor()); + initializer.setInstancesForObject(OBJECT_ID_TEMPERATURE_SENSOR, randomTemperatureSensor); List enablers = initializer.createAll(); // Create CoAP Config @@ -745,6 +748,9 @@ public void handshakeFailed(Handshaker handshaker, Throwable error) { builder.setBootstrapAdditionalAttributes(bsAdditionalAttributes); final LeshanClient client = builder.build(); + client.addObserver( + new ClientShutdownOnUnexpectedErrorObserver(client, myDevice, randomTemperatureSensor)); + client.getObjectTree().addListener(new ObjectsListenerAdapter() { @Override public void objectRemoved(LwM2mObjectEnabler object) { @@ -872,4 +878,28 @@ public void run() { } } } + + private static class ClientShutdownOnUnexpectedErrorObserver extends LwM2mClientObserverAdapter { + final LeshanClient client; + final MyDevice myDevice; + final RandomTemperatureSensor randomTemperatureSensor; + + public ClientShutdownOnUnexpectedErrorObserver( + final LeshanClient client, + final MyDevice myDevice, + final RandomTemperatureSensor randomTemperatureSensor + ) { + this.client = client; + this.myDevice = myDevice; + this.randomTemperatureSensor = randomTemperatureSensor; + } + + @Override + public void onUnexpectedErrorOccurred(Throwable unexpectedError) { + LOG.error("unexpected error occurred", unexpectedError); + client.destroy(true); + myDevice.destroy(); + randomTemperatureSensor.destroy(); + } + } } diff --git a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/MyDevice.java b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/MyDevice.java index 207649d7a8..a51c5219fc 100644 --- a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/MyDevice.java +++ b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/MyDevice.java @@ -14,6 +14,7 @@ import org.eclipse.leshan.client.resource.BaseInstanceEnabler; import org.eclipse.leshan.client.servers.ServerIdentity; +import org.eclipse.leshan.core.Destroyable; import org.eclipse.leshan.core.model.ObjectModel; import org.eclipse.leshan.core.model.ResourceModel.Type; import org.eclipse.leshan.core.node.LwM2mResource; @@ -23,7 +24,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class MyDevice extends BaseInstanceEnabler { +public class MyDevice extends BaseInstanceEnabler implements Destroyable { private static final Logger LOG = LoggerFactory.getLogger(MyDevice.class); @@ -31,9 +32,11 @@ public class MyDevice extends BaseInstanceEnabler { private static final List 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"); + this.timer = new Timer("Device-Current Time"); timer.schedule(new TimerTask() { @Override public void run() { @@ -210,4 +213,9 @@ private long getMemoryTotal() { public List getAvailableResourceIds(ObjectModel model) { return supportedResources; } + + @Override + public void destroy() { + timer.cancel(); + } } diff --git a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/RandomTemperatureSensor.java b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/RandomTemperatureSensor.java index 7719a55f53..8a1fa3f565 100644 --- a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/RandomTemperatureSensor.java +++ b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/RandomTemperatureSensor.java @@ -11,6 +11,7 @@ import org.eclipse.leshan.client.resource.BaseInstanceEnabler; import org.eclipse.leshan.client.servers.ServerIdentity; +import org.eclipse.leshan.core.Destroyable; import org.eclipse.leshan.core.model.ObjectModel; import org.eclipse.leshan.core.response.ExecuteResponse; import org.eclipse.leshan.core.response.ReadResponse; @@ -18,7 +19,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class RandomTemperatureSensor extends BaseInstanceEnabler { +public class RandomTemperatureSensor extends BaseInstanceEnabler implements Destroyable { private static final Logger LOG = LoggerFactory.getLogger(RandomTemperatureSensor.class); @@ -113,4 +114,9 @@ private void resetMinMaxMeasuredValues() { public List getAvailableResourceIds(ObjectModel model) { return supportedResources; } + + @Override + public void destroy() { + scheduler.shutdown(); + } }