}
private void handleIncomingCoapResponse(ChannelHandlerContext ctx, MessageEvent me) {
CoapResponse coapResponse = (CoapResponse) me.getMessage();
InetSocketAddress remoteEndpoint = (InetSocketAddress) me.getRemoteAddress();
Token token = coapResponse.getToken();
//Current response is NO update notification or is an error response (which SHOULD implicate the first)
if(!coapResponse.isUpdateNotification() || MessageCode.isErrorMessage(coapResponse.getMessageCode())){
if(observations.contains(remoteEndpoint, token)){
log.info("Stop observation (remote address: {}, token: {}) due to received response: {}",
new Object[]{remoteEndpoint, token, coapResponse});
stopObservation(remoteEndpoint, token);
}
}
else if(coapResponse.isUpdateNotification()){
//current response is update notification but there is no suitable observation
if(!observations.contains(remoteEndpoint, token)){
log.warn("No observation found for update notification (remote endpoint: {}, token: {}).",
remoteEndpoint, token);
}
//Current response is (non-error) update notification and there is a suitable observation
else if(coapResponse.isUpdateNotification() && !MessageCode.isErrorMessage(coapResponse.getMessageCode())){
//Lookup status age of latest update notification
ResourceStatusAge latestStatusAge = observations.get(remoteEndpoint, token);
//Get status age from newly received update notification
long receivedSequenceNo = coapResponse.getObserve();
ResourceStatusAge receivedStatusAge = new ResourceStatusAge(receivedSequenceNo, System.currentTimeMillis());
if(ResourceStatusAge.isReceivedStatusNewer(latestStatusAge, receivedStatusAge)){
updateStatusAge(remoteEndpoint, token, receivedStatusAge);
}