executor.schedule(new Runnable() {
@Override
public void run() {
Token token = coapMessage.getToken();
ObservationCancelledEvent event = new ObservationCancelledEvent(remoteEndpoint, token,
ObservationCancelledEvent.Reason.ACTIVE_CANCELLATION_BY_CLIENT);
Channels.write(ctx.getChannel(), event);
}
}, 0, TimeUnit.SECONDS);
}
//CoAP ping
else if(coapMessage.getMessageTypeName() == MessageType.Name.CON &&
coapMessage.getMessageCodeName() == MessageCode.Name.EMPTY){
Token emptyToken = new Token(new byte[0]);
if(this.clientCallbacks.contains(remoteEndpoint, emptyToken)){
String description = "Empty token for remote endpoint " + remoteEndpoint + " not available.";
clientCallback.processMiscellaneousError(description);
return;
}
else{
coapMessage.setToken(emptyToken);
}
}
else{
//Prepare CoAP request, the response reception and then send the CoAP request
Token token = tokenFactory.getNextToken(remoteEndpoint);
if(token == null){
String description = "No token available for remote endpoint " + remoteEndpoint + ".";
clientCallback.processMiscellaneousError(description);
return;
}
else{
coapMessage.setToken(token);
}
}
//Add the response callback to wait for the inbound response
addResponseCallback(remoteEndpoint, coapMessage.getToken(), clientCallback);
//Send the request
sendCoapMessage(ctx, me.getFuture(), coapMessage, remoteEndpoint);
return;
}
catch (Exception ex) {
log.error("This should never happen!", ex);
removeClientCallback(remoteEndpoint, coapMessage.getToken());
}
}
// else if (me.getMessage() instanceof ApplicationShutdownEvent){
// this.clientCallbacks.clear();
// }
else if(me.getMessage() instanceof ObservationCancelledEvent){
ObservationCancelledEvent message = (ObservationCancelledEvent) me.getMessage();
if(removeClientCallback(message.getRemoteEndpoint(), message.getToken()) == null){
log.error("Could not stop observation (remote endpoints: {}, token: {})! No callback found!",
message.getRemoteEndpoint(), message.getToken());
}
}
ctx.sendDownstream(me);