diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/DynamicIPObserveTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/DynamicIPObserveTest.java index 1fed237822..e7cb26b471 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/DynamicIPObserveTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/DynamicIPObserveTest.java @@ -202,8 +202,6 @@ public void can_send_notification_if_ip_changes_using_psk(Protocol givenProtocol assertThat(registrationAfterObserve).isEqualTo(registrationBeforeObserve); } - // TODO Registration is not correctly updated we must fix it. - @Disabled @TestTlsTransport public void update_registration_on_notification_using_psk(Protocol givenProtocol, String givenClientEndpointProvider, String givenServerEndpointProvider) @@ -230,7 +228,7 @@ public void update_registration_on_notification_using_psk(Protocol givenProtocol Registration registrationBeforeObserve = server.getRegistrationFor(client); assertSuccessfulNotificationSendingAfterAddressChanged(registrationBeforeObserve); - // check that client registration is not updated. + // check that client registration is updated. Registration registrationAfterObserve = server.getRegistrationFor(client); assertThat(registrationAfterObserve.getSocketAddress()) .isNotEqualTo(registrationBeforeObserve.getSocketAddress()); diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/server/redis/RedisRegistrationStoreTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/server/redis/RedisRegistrationStoreTest.java index b351557e39..0c3cc6cde3 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/server/redis/RedisRegistrationStoreTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/server/redis/RedisRegistrationStoreTest.java @@ -44,6 +44,7 @@ import org.eclipse.leshan.core.observation.ObservationIdentifier; import org.eclipse.leshan.core.observation.SingleObservation; import org.eclipse.leshan.core.peer.IpPeer; +import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.request.BindingMode; import org.eclipse.leshan.core.request.ContentFormat; import org.eclipse.leshan.core.request.ObserveCompositeRequest; @@ -86,16 +87,17 @@ public void setUp() throws UnknownHostException { observationStore = new LwM2mObservationStore(store, new LwM2mNotificationReceiver() { @Override - public void onNotification(CompositeObservation observation, ClientProfile profile, + public void onNotification(CompositeObservation observation, LwM2mPeer sender, ClientProfile profile, ObserveCompositeResponse response) { } @Override - public void onNotification(SingleObservation observation, ClientProfile profile, ObserveResponse response) { + public void onNotification(SingleObservation observation, LwM2mPeer sender, ClientProfile profile, + ObserveResponse response) { } @Override - public void onError(Observation observation, ClientProfile profile, Exception error) { + public void onError(Observation observation, LwM2mPeer sender, ClientProfile profile, Exception error) { } @Override diff --git a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/endpoint/CaliforniumServerEndpointsProvider.java b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/endpoint/CaliforniumServerEndpointsProvider.java index 3244f538a2..27785f4e9d 100644 --- a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/endpoint/CaliforniumServerEndpointsProvider.java +++ b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/endpoint/CaliforniumServerEndpointsProvider.java @@ -164,14 +164,14 @@ public void onNotification(Request coapRequest, Response coapResponse) { AbstractLwM2mResponse response = messagetranslator.createObservation(observation, coapResponse, toolbox, profile); if (observation instanceof SingleObservation) { - notificatonReceiver.onNotification((SingleObservation) observation, profile, + notificatonReceiver.onNotification((SingleObservation) observation, client, profile, (ObserveResponse) response); } else if (observation instanceof CompositeObservation) { - notificatonReceiver.onNotification((CompositeObservation) observation, profile, + notificatonReceiver.onNotification((CompositeObservation) observation, client, profile, (ObserveCompositeResponse) response); } } catch (Exception e) { - notificatonReceiver.onError(observation, profile, e); + notificatonReceiver.onError(observation, client, profile, e); } } diff --git a/leshan-server-cf/src/test/java/org/eclipse/leshan/server/californium/observation/LwM2mObservationStoreTest.java b/leshan-server-cf/src/test/java/org/eclipse/leshan/server/californium/observation/LwM2mObservationStoreTest.java index 39a66855c2..2a6dbf7d80 100644 --- a/leshan-server-cf/src/test/java/org/eclipse/leshan/server/californium/observation/LwM2mObservationStoreTest.java +++ b/leshan-server-cf/src/test/java/org/eclipse/leshan/server/californium/observation/LwM2mObservationStoreTest.java @@ -43,6 +43,7 @@ import org.eclipse.leshan.core.observation.ObservationIdentifier; import org.eclipse.leshan.core.observation.SingleObservation; import org.eclipse.leshan.core.peer.IpPeer; +import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.request.BindingMode; import org.eclipse.leshan.core.request.ContentFormat; import org.eclipse.leshan.core.request.ObserveCompositeRequest; @@ -80,16 +81,17 @@ public void setUp() throws UnknownHostException { store = new InMemoryRegistrationStore(); observationStore = new LwM2mObservationStore(store, new LwM2mNotificationReceiver() { @Override - public void onNotification(CompositeObservation observation, ClientProfile profile, + public void onNotification(CompositeObservation observation, LwM2mPeer sender, ClientProfile profile, ObserveCompositeResponse response) { } @Override - public void onNotification(SingleObservation observation, ClientProfile profile, ObserveResponse response) { + public void onNotification(SingleObservation observation, LwM2mPeer sender, ClientProfile profile, + ObserveResponse response) { } @Override - public void onError(Observation observation, ClientProfile profile, Exception error) { + public void onError(Observation observation, LwM2mPeer sender, ClientProfile profile, Exception error) { } @Override diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/observation/LwM2mNotificationReceiver.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/observation/LwM2mNotificationReceiver.java index 3651cdf0e4..28a79d310e 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/observation/LwM2mNotificationReceiver.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/observation/LwM2mNotificationReceiver.java @@ -18,6 +18,7 @@ import org.eclipse.leshan.core.observation.CompositeObservation; import org.eclipse.leshan.core.observation.Observation; import org.eclipse.leshan.core.observation.SingleObservation; +import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.response.ObserveCompositeResponse; import org.eclipse.leshan.core.response.ObserveResponse; import org.eclipse.leshan.server.profile.ClientProfile; @@ -44,26 +45,31 @@ public interface LwM2mNotificationReceiver { * Called on new notification. * * @param observation the observation for which new data are received + * @param sender the client which sent the notification * @param profile the client profile concerned by this observation * @param response the lwm2m response received (successful or error response) * */ - void onNotification(SingleObservation observation, ClientProfile profile, ObserveResponse response); + void onNotification(SingleObservation observation, LwM2mPeer sender, ClientProfile profile, + ObserveResponse response); /** * Called on new notification. * * @param observation the composite-observation for which new data are received + * @param sender the client which sent the notification * @param profile the client profile concerned by this observation * @param response the lwm2m observe-composite response received (successful or error response) * */ - void onNotification(CompositeObservation observation, ClientProfile profile, ObserveCompositeResponse response); + void onNotification(CompositeObservation observation, LwM2mPeer sender, ClientProfile profile, + ObserveCompositeResponse response); /** * Called when an error occurs on new notification. * * @param observation the observation for which new data are received + * @param sender the client which sent the notification * @param profile the client profile concerned by this observation * @param error the exception raised when we handle the notification. It can be : * */ - void onError(Observation observation, ClientProfile profile, Exception error); + void onError(Observation observation, LwM2mPeer sender, ClientProfile profile, Exception error); } diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/observation/ObservationServiceImpl.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/observation/ObservationServiceImpl.java index dce9e5f733..e24ff541ce 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/observation/ObservationServiceImpl.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/observation/ObservationServiceImpl.java @@ -200,41 +200,57 @@ public void removeListener(ObservationListener listener) { listeners.remove(listener); } - private void updateRegistrationOnRegistration(Observation observation, ClientProfile profile) { + private Registration updateRegistrationOnRegistration(Observation observation, LwM2mPeer sender, + ClientProfile profile) { if (updateRegistrationOnNotification) { - LwM2mPeer peerData = profile.getTransportData(); - RegistrationUpdate regUpdate = new RegistrationUpdate(observation.getRegistrationId(), peerData, null, null, + RegistrationUpdate regUpdate = new RegistrationUpdate(observation.getRegistrationId(), sender, null, null, null, null, null, null, null, null, null, null); UpdatedRegistration updatedRegistration = registrationStore.updateRegistration(regUpdate); if (updatedRegistration == null || updatedRegistration.getUpdatedRegistration() == null) { - LOG.error("Unexpected error: There is no registration with id {} for this observation {}", + String errorMsg = String.format( + "Unexpected error: There is no registration with id %s for this observation %s", observation.getRegistrationId(), observation); - return; + LOG.error(errorMsg); + throw new IllegalStateException(errorMsg); } - updatedRegistration.getUpdatedRegistration(); + return updatedRegistration.getUpdatedRegistration(); } + return profile.getRegistration(); } // ********** NotificationListener interface **********// @Override - public void onNotification(SingleObservation observation, ClientProfile profile, ObserveResponse response) { - updateRegistrationOnRegistration(observation, profile); - for (ObservationListener listener : listeners) { - listener.onResponse(observation, profile.getRegistration(), response); + public void onNotification(SingleObservation observation, LwM2mPeer sender, ClientProfile profile, + ObserveResponse response) { + try { + Registration updatedRegistration = updateRegistrationOnRegistration(observation, sender, profile); + for (ObservationListener listener : listeners) { + listener.onResponse(observation, updatedRegistration, response); + } + } catch (Exception e) { + for (ObservationListener listener : listeners) { + listener.onError(observation, profile.getRegistration(), e); + } } } @Override - public void onNotification(CompositeObservation observation, ClientProfile profile, + public void onNotification(CompositeObservation observation, LwM2mPeer sender, ClientProfile profile, ObserveCompositeResponse response) { - updateRegistrationOnRegistration(observation, profile); - for (ObservationListener listener : listeners) { - listener.onResponse(observation, profile.getRegistration(), response); + try { + Registration updatedRegistration = updateRegistrationOnRegistration(observation, sender, profile); + for (ObservationListener listener : listeners) { + listener.onResponse(observation, updatedRegistration, response); + } + } catch (Exception e) { + for (ObservationListener listener : listeners) { + listener.onError(observation, profile.getRegistration(), e); + } } } @Override - public void onError(Observation observation, ClientProfile profile, Exception error) { + public void onError(Observation observation, LwM2mPeer sender, ClientProfile profile, Exception error) { for (ObservationListener listener : listeners) { listener.onError(observation, profile.getRegistration(), error); }