CoapEndpoint implements Endpoint, MessagePostProcessInterceptors, MulticastReceivers { @Override public void sendRequest(final Request request) { if (!started) { request.cancel(); return; } request.prepareDestinationContext(); InetSocketAddress destinationAddress = request.getDestinationContext().getPeerAddress(); if (request.isMulticast()) { if (0 >= multicastBaseMid) { LOGGER.warn("{}multicast messaging to destination {} is not enabled! Please enable it configuring \"" + Keys.MULTICAST_BASE_MID + "\" greater than 0", tag, destinationAddress); request.setSendError(new IllegalArgumentException("multicast is not enabled!")); return; } else if (request.getType() == Type.CON) { LOGGER.warn( "{}CON request to multicast destination {} is not allowed, as per RFC 7252, 8.1, a client MUST use NON message type for multicast requests ", tag, destinationAddress); request.setSendError(new IllegalArgumentException("multicast is not supported for CON!")); return; } else if (request.hasMID() && request.getMID() < multicastBaseMid) { LOGGER.warn( "{}multicast request to group {} has mid {} which is not in the MULTICAST_MID range [{}-65535]", tag, destinationAddress, request.getMID(), multicastBaseMid); request.setSendError( new IllegalArgumentException("multicast mid is not in range [" + multicastBaseMid + "-65535]")); return; } } else if (0 < multicastBaseMid && request.getMID() >= multicastBaseMid) { LOGGER.warn("{}request has mid {}, which is in the MULTICAST_MID range [{}-65535]", tag, destinationAddress, request.getMID(), multicastBaseMid); request.setSendError( new IllegalArgumentException("unicast mid is in multicast range [" + multicastBaseMid + "-65535]")); return; } if (destinationAddress.isUnresolved()) { LOGGER.warn("{}request has unresolved destination address", tag, destinationAddress); request.setSendError( new IllegalArgumentException(destinationAddress + " is a unresolved address!")); return; } final Exchange exchange = new Exchange(request, Origin.LOCAL, executor); exchange.execute(new Runnable() { @Override public void run() { if (requestTransmission != null) { request.addMessageObserver(requestTransmission); } coapstack.sendRequest(exchange, request); } }); } @Deprecated protected  CoapEndpoint(Connector connector, boolean applyConfiguration, NetworkConfig config,
			TokenGenerator tokenGenerator, ObservationStore store, MessageExchangeStore exchangeStore,
			EndpointContextMatcher endpointContextMatcher, String loggingTag, CoapEndpointHealth health,
			CoapStackFactory coapStackFactory, Object customStackArgument); protected  CoapEndpoint(Connector connector, boolean applyConfiguration, NetworkConfig config,
			TokenGenerator tokenGenerator, ObservationStore store, MessageExchangeStore exchangeStore,
			EndpointContextMatcher endpointContextMatcher, String loggingTag,
			CoapStackFactory coapStackFactory, Object customStackArgument); @Override synchronized void start(); @Override synchronized void stop(); @Override synchronized void destroy(); @Override void clear(); @Override boolean isStarted(); @Override void setExecutors(ScheduledExecutorService mainExecutor, ScheduledExecutorService secondaryExecutor); @Override void addNotificationListener(final NotificationListener lis); @Override void removeNotificationListener(final NotificationListener lis); @Override void addObserver(final EndpointObserver observer); @Override void removeObserver(final EndpointObserver observer); @Override void addInterceptor(final MessageInterceptor interceptor); @Override void removeInterceptor(final MessageInterceptor interceptor); @Override List<MessageInterceptor> getInterceptors(); @Override void addPostProcessInterceptor(MessageInterceptor interceptor); @Override void removePostProcessInterceptor(MessageInterceptor interceptor); @Override List<MessageInterceptor> getPostProcessInterceptors(); @Override void addMulticastReceiver(final Connector receiver); @Override void removeMulticastReceiver(Connector receiver); @Override void startMulticastReceivers(); @Override void sendRequest(final Request request); @Override void sendResponse(final Exchange exchange, final Response response); @Override void sendEmptyMessage(final Exchange exchange, final EmptyMessage message); @Override void setMessageDeliverer(MessageDeliverer deliverer); @Override InetSocketAddress getAddress(); @Override URI getUri(); @Override NetworkConfig getConfig(); Connector getConnector(); @Override void cancelObservation(Token token); static synchronized void setDefaultCoapStackFactory(CoapStackFactory newFactory); static final CoapStackFactory STANDARD_COAP_STACK_FACTORY; }
@Test public void testSendRequestCallsMessageCallbackOnSent() throws Exception { Request request = Request.newGet(); request.setDestinationContext(new AddressEndpointContext(InetAddress.getLoopbackAddress(), CoAP.DEFAULT_COAP_PORT)); CountingMessageObserver observer = new CountingMessageObserver(); request.addMessageObserver(observer); endpoint.sendRequest(request); assertTrue(observer.waitForSentCalls(1, 1, TimeUnit.SECONDS)); }