-
Notifications
You must be signed in to change notification settings - Fork 408
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 callback mechanism into LwM2mClientObserver for when the unexpected error occurred #941
Changes from 3 commits
2e3a324
7e32bf5
e3aad0d
16c6cb7
3f73020
7e6d029
b4a89c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<LwM2mObjectEnabler> 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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be done on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's a point. I think it would be better to implement this shutdown observer mechanism on What do you think about this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For For WDYT ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'm on the same page. One more thing. Would it useful to support a method to prevent shutting down even if an unexpected error has occurred on the LeshanClient? For example There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would not add it for now. Let's see if someone need it later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still don't see what could be the use case. As tasks in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, my suggestion was for just in case. It looks there is no reason to support that. Thank you. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed at 7e6d029 |
||
|
||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to do the same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. I'll do this 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed at 3f73020 |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe
onUnexpectedError
is enough ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the advice, I've not had confidence in this honestly :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed at 16c6cb7