CoapRetryFilter extends AbstractIoFilter { @Override public void messageReceived(IoSession session, Object in, ReadFilterChainController controller) { LOGGER.debug("Processing a MESSAGE_RECEIVED for session {}", session); CoapMessage coapMsg = (CoapMessage) in; String transmissionId = CoapTransmission.uniqueId(session, coapMsg); switch (coapMsg.getType()) { case NON_CONFIRMABLE: super.messageReceived(session, coapMsg, controller); break; case CONFIRMABLE: CoapMessage ack = processed.get(transmissionId); if (ack != null) { LOGGER.debug("Duplicated messages detected with ID {} in session {}", coapMsg.requestId(), session); controller.callWriteMessageForRead(ack); } else { super.messageReceived(session, coapMsg, controller); } break; case ACK: case RESET: CoapTransmission t = inFlight.get(transmissionId); if (t != null) { t.getRetryFuture().cancel(false); inFlight.remove(transmissionId); } super.messageReceived(session, coapMsg, controller); break; } }  @Override void messageReceived(IoSession session, Object in, ReadFilterChainController controller); @Override void messageWriting(final IoSession session, final WriteRequest message, WriteFilterChainController controller);  }
@Test public void non_confirmable_message_received() { CoapMessage in = new CoapMessage(1, MessageType.NON_CONFIRMABLE, 1, 1234, "token".getBytes(), null, "payload".getBytes()); filter.messageReceived(session, in, readController); verify(readController).callReadNextFilter(in); Mockito.verifyNoMoreInteractions(readController); }
@Test public void first_time_confirmable_message_received() { CoapMessage in = new CoapMessage(1, MessageType.CONFIRMABLE, 1, 1234, "token".getBytes(), null, "payload".getBytes()); filter.messageReceived(session, in, readController); verify(readController).callReadNextFilter(in); Mockito.verifyNoMoreInteractions(readController); }