super.sendRequest(exchange, request);
}
@Override
public void sendResponse(final Exchange exchange, final Response response) {
final ObserveRelation relation = exchange.getRelation();
if (relation != null && relation.isEstablished()) {
if (exchange.getRequest().isAcknowledged() || exchange.getRequest().getType()==Type.NON) {
// Transmit errors as CON
if (!ResponseCode.isSuccess(response.getCode())) {
LOGGER.fine("Response has error code "+response.getCode()+" and must be sent as CON");
response.setType(Type.CON);
relation.cancel();
} else {
// Make sure that every now and than a CON is mixed within
if (relation.check()) {
LOGGER.fine("The observe relation requires the notification to be sent as CON");
response.setType(Type.CON);
// By default use NON, but do not override resource decision
} else if (response.getType()==null) {
response.setType(Type.NON);
}
}
}
// This is a notification
response.setLast(false);
/*
* Only one Confirmable message is allowed to be in transit. A CON
* is in transit as long as it has not been acknowledged, rejected,
* or timed out. All further notifications are postponed here. If a
* former CON is acknowledged or timeouts, it starts the youngest
* notification (In case of a timeout, it keeps the retransmission
* counter). When a fresh/younger notification arrives but must be
* postponed we forget any former notification.
*/
if (response.getType() == Type.CON) {
prepareSelfReplacement(exchange, response);
}
// The decision whether to postpone this notification or not and the
// decision which notification is the youngest to send next must be
// synchronized
synchronized (exchange) {
Response current = relation.getCurrentControlNotification();
if (current != null && isInTransit(current)) {
LOGGER.fine("A former notification is still in transit. Postpone " + response);
relation.setNextControlNotification(response);
return;
} else {
relation.setCurrentControlNotification(response);
relation.setNextControlNotification(null);
}
}
} // else no observe was requested or the resource does not allow it
super.sendResponse(exchange, response);