Skip to content

Commit

Permalink
GH-1421: Fix leshanServerBuilder.setUpdateRegistrationOnNotification
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jul 17, 2023
1 parent b825100 commit b57049a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,32 +45,37 @@ 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 :
* <ul>
* <li>InvalidResponseException if the response received is malformed.</li>
* <li>or any other RuntimeException for unexpected issue.
* </ul>
*/
void onError(Observation observation, ClientProfile profile, Exception error);
void onError(Observation observation, LwM2mPeer sender, ClientProfile profile, Exception error);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit b57049a

Please sign in to comment.