private void assertMessageHasDestinationAddress(final Message message) { if (message.getDestination() == null) { throw new IllegalArgumentException("Message has no destination address"); } else if (message.getDestinationPort() == 0) { throw new IllegalArgumentException("Message has no destination port"); } } }
private KeyToken createUnusedToken(final Message message) { byte[] address = message.getDestination().getAddress(); byte[] token = new byte[tokenSizeLimit]; KeyToken result; // TODO what to do when there are no more unused tokens left? do { rng.nextBytes(token); result = KeyToken.fromValues(token, address, message.getDestinationPort()); } while (!usedTokens.add(result)); return result; } }
/** * Creates a key from an outbound CoAP message. * * @param message the message. * @return the key derived from the message. The key's <em>mid</em> is * scoped to the message's destination address and port. */ public static KeyMID fromOutboundMessage(Message message) { return new KeyMID(message.getMID(), message.getDestination().getAddress(), message.getDestinationPort()); } }
/** * Creates a new key for an outbound CoAP message. * <p> * The key will be scoped to the message's destination endpoint. * * @param msg the message. * @return the key. */ public static KeyToken fromOutboundMessage(final Message msg) { return new KeyToken(msg.getToken(), msg.getDestination().getAddress(), msg.getDestinationPort()); }
@Override public int assignMessageId(final Message message) { int mid = message.getMID(); if (Message.NONE == mid) { InetSocketAddress dest = new InetSocketAddress(message.getDestination(), message.getDestinationPort()); mid = messageIdProvider.getNextMessageId(dest); if (Message.NONE == mid) { LOGGER.log(Level.WARNING, "Cannot send message to {0}, all MIDs are in use", dest); } else { message.setMID(mid); } } return mid; }