Skip to content

Commit

Permalink
Fix code about resource instance deletion notification.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Dec 12, 2023
1 parent 82045e2 commit 5bf2cd7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void onNotification(Request coapRequest, Response coapResponse) {

// create Observe Response
try {
AbstractLwM2mResponse response = messagetranslator.createObservation(observation,
AbstractLwM2mResponse response = messagetranslator.createObserveResponse(observation,
coapResponse, toolbox, profile);
if (observation instanceof SingleObservation) {
notificatonReceiver.onNotification((SingleObservation) observation, client, profile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.List;
import java.util.Map;

import org.eclipse.californium.core.coap.CoAP;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.core.server.resources.Resource;
Expand Down Expand Up @@ -84,15 +83,8 @@ public List<Resource> createResources(UplinkRequestReceiver receiver, ServerEndp
identityHandlerProvider));
}

public AbstractLwM2mResponse createObservation(Observation observation, Response coapResponse,
public AbstractLwM2mResponse createObserveResponse(Observation observation, Response coapResponse,
ServerEndpointToolbox toolbox, ClientProfile profile) {
// CHANGED response is supported for backward compatibility with old spec.
if (coapResponse.getCode() != CoAP.ResponseCode.CHANGED
&& coapResponse.getCode() != CoAP.ResponseCode.CONTENT) {
throw new InvalidResponseException("Unexpected response code [%s] for %s", coapResponse.getCode(),
observation);
}

// get content format
ContentFormat contentFormat = null;
if (coapResponse.getOptions().hasContentFormat()) {
Expand All @@ -107,25 +99,34 @@ public AbstractLwM2mResponse createObservation(Observation observation, Response
if (observation instanceof SingleObservation) {
SingleObservation singleObservation = (SingleObservation) observation;

List<TimestampedLwM2mNode> timestampedNodes = toolbox.getDecoder().decodeTimestampedData(
coapResponse.getPayload(), contentFormat, singleObservation.getPath(), profile.getModel());

// create lwm2m response
if (timestampedNodes.size() == 1 && !timestampedNodes.get(0).isTimestamped()) {
return new ObserveResponse(responseCode, timestampedNodes.get(0).getNode(), null, singleObservation,
null, coapResponse);
} else {
return new ObserveResponse(responseCode, null, timestampedNodes, singleObservation, null,
if (responseCode.isError()) {
return new ObserveResponse(responseCode, null, null, null, coapResponse.getPayloadString(),
coapResponse);
} else {
List<TimestampedLwM2mNode> timestampedNodes = toolbox.getDecoder().decodeTimestampedData(
coapResponse.getPayload(), contentFormat, singleObservation.getPath(), profile.getModel());

// create lwm2m response
if (timestampedNodes.size() == 1 && !timestampedNodes.get(0).isTimestamped()) {
return new ObserveResponse(responseCode, timestampedNodes.get(0).getNode(), null,
singleObservation, null, coapResponse);
} else {
return new ObserveResponse(responseCode, null, timestampedNodes, singleObservation, null,
coapResponse);
}
}
} else if (observation instanceof CompositeObservation) {

CompositeObservation compositeObservation = (CompositeObservation) observation;

Map<LwM2mPath, LwM2mNode> nodes = toolbox.getDecoder().decodeNodes(coapResponse.getPayload(),
contentFormat, compositeObservation.getPaths(), profile.getModel());

return new ObserveCompositeResponse(responseCode, nodes, null, coapResponse, compositeObservation);
if (responseCode.isError()) {
return new ObserveCompositeResponse(responseCode, null, coapResponse.getPayloadString(),
coapResponse, null);
} else {
Map<LwM2mPath, LwM2mNode> nodes = toolbox.getDecoder().decodeNodes(coapResponse.getPayload(),
contentFormat, compositeObservation.getPaths(), profile.getModel());
return new ObserveCompositeResponse(responseCode, nodes, null, coapResponse, compositeObservation);
}
}

throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,35 @@ private void sendObservation(CoapRequest observeRequest, Service<CoapRequest, Co

private static SeparateResponse toSeparateResponse(CoapResponse obsResponse, int currentObserveSequence,
CoapRequest subscribeRequest) {
obsResponse.options().setObserve(currentObserveSequence);
if (obsResponse.getCode() == Code.C205_CONTENT) {
// TODO would be better to check if this is a success instead of 205 content
obsResponse.options().setObserve(currentObserveSequence);
}
return obsResponse.toSeparate(subscribeRequest.getToken(), subscribeRequest.getPeerAddress());
}

private void sendObservation(CoapRequest observeRequest, SeparateResponse separateResponse) {
InetSocketAddress peerAddress = separateResponse.getPeerAddress();

notificationSender.apply(separateResponse).whenComplete((result, exception) -> {
if (exception != null) {
observersStore.remove(observeRequest);
LOGGER.warn("[{}#{}] Removed observation relation, got exception: {}", peerAddress,
separateResponse.getToken(), exception.toString());
} else if (!result) {
try {
notificationSender.apply(separateResponse).whenComplete((result, exception) -> {
if (exception != null) {
observersStore.remove(observeRequest);
LOGGER.warn("[{}#{}] Removed observation relation, got exception: {}", peerAddress,
separateResponse.getToken(), exception.toString());
} else if (!result) {
observersStore.remove(observeRequest);
LOGGER.info("[{}#{}] Removed observation relation, got reset", peerAddress,
separateResponse.getToken());
}
});

if (separateResponse.getCode() != Code.C205_CONTENT) {
observersStore.remove(observeRequest);
LOGGER.info("[{}#{}] Removed observation relation, got reset", peerAddress,
separateResponse.getToken());
}
});

if (separateResponse.getCode() != Code.C205_CONTENT) {
observersStore.remove(observeRequest);
} catch (RuntimeException e) {
LOGGER.warn("Unexpected Exception when sending Notification(%s) to %s", peerAddress,
separateResponse.getToken(), e);
}
}

Expand Down

0 comments on commit 5bf2cd7

Please sign in to comment.