}
private void handleOutboundCoapMessage(ChannelHandlerContext ctx, MessageEvent me){
CoapMessage coapMessage = (CoapMessage) me.getMessage();
InetSocketAddress remoteEndpoint = (InetSocketAddress) me.getRemoteAddress();
if(coapMessage.getMessageID() != CoapMessage.UNDEFINED_MESSAGE_ID){
int messageID = coapMessage.getMessageID();
if(coapMessage instanceof CoapResponse && ((CoapResponse) coapMessage).isUpdateNotification()
&& coapMessage.getMessageTypeName() != MessageType.Name.ACK){
if(this.transfers.contains(remoteEndpoint, messageID)){
if(updateConfirmableUpdateNotification(remoteEndpoint, (CoapResponse) coapMessage)){
return;
}
else{
//There was no update notification (which is very unlikely)
coapMessage.setMessageID(CoapMessage.UNDEFINED_MESSAGE_ID);
}
}
}
else{
ctx.sendDownstream(me);
return;
}
}
int messageID = this.messageIDFactory.getNextMessageID(remoteEndpoint);
if(messageID == CoapMessage.UNDEFINED_MESSAGE_ID){
MiscellaneousErrorEvent event = new MiscellaneousErrorEvent(remoteEndpoint, messageID,
coapMessage.getToken(), "No message ID available for remote endpoint: " + remoteEndpoint);
Channels.fireMessageReceived(ctx.getChannel(), event);
return;
}
else if(coapMessage.getMessageTypeName() == MessageType.Name.CON){
coapMessage.setMessageID(messageID);
this.addTransfer(remoteEndpoint, coapMessage, true);
log.debug("DOWNSTREAM AFTER (to {}): {}.", me.getRemoteAddress(), me.getMessage());
ctx.sendDownstream(me);
}
else if(coapMessage.getMessageTypeName() == MessageType.Name.NON){
coapMessage.setMessageID(messageID);
this.addTransfer(remoteEndpoint, coapMessage, false);
log.debug("DOWNSTREAM AFTER (to {}): {}.", me.getRemoteAddress(), me.getMessage());
ctx.sendDownstream(me);
}
else{
coapMessage.setMessageID(messageID);
log.debug("DOWNSTREAM AFTER (to {}): {}.", me.getRemoteAddress(), me.getMessage());
ctx.sendDownstream(me);
}
MessageIDAssignedEvent event = new MessageIDAssignedEvent(remoteEndpoint, messageID, coapMessage.getToken());
Channels.fireMessageReceived(ctx.getChannel(), event);
}