/** * Validate before sending that there is a destination set. */ private void validateBeforeSending() { if (getDestination() == null) throw new NullPointerException("Destination is null"); if (getDestinationPort() == 0) throw new NullPointerException("Destination port is 0"); }
@Override public void sendRequest(Request request) { LOGGER.log(Level.INFO, "{0}:{1} <== req {2}", new Object[]{request.getDestination(), request.getDestinationPort(), request}); }
/** * Validate before sending that there is a destination set. */ private void validateBeforeSending() { if (getDestination() == null) throw new NullPointerException("Destination is null"); if (getDestinationPort() == 0) throw new NullPointerException("Destination port is 0"); }
@Override public void sendRequest(Request request) { LOGGER.info(String.format("%s:%d <== req %s", request.getDestination(), request.getDestinationPort(), request)); }
@Test public void testSetURISetsDestination() { InetSocketAddress dest = InetSocketAddress.createUnresolved("192.168.0.1", 12000); Request req = Request.newGet().setURI("coap://192.168.0.1:12000"); assertThat(req.getDestination().getHostAddress(), is(dest.getHostString())); assertThat(req.getDestinationPort(), is(dest.getPort())); }
@Test public void testSetURISetsDestinationPortBasedOnUriScheme() { Request req = Request.newGet().setURI("coap://127.0.0.1"); assertThat(req.getDestinationPort(), is(CoAP.DEFAULT_COAP_PORT)); req = Request.newGet().setURI("coaps://127.0.0.1"); assertThat(req.getDestinationPort(), is(CoAP.DEFAULT_COAP_SECURE_PORT)); req = Request.newGet().setURI("coap+tcp://127.0.0.1"); assertThat(req.getDestinationPort(), is(CoAP.DEFAULT_COAP_PORT)); req = Request.newGet().setURI("coaps+tcp://127.0.0.1"); assertThat(req.getDestinationPort(), is(CoAP.DEFAULT_COAP_SECURE_PORT)); }
/** * Serializes the specified request. Message identifier, message code, * token, options and payload are converted into a byte array and wrapped in * a {@link RawData} object. The request's destination address and port are * stored as address and port in the RawData object. * * @param request * the request * @return the request as raw data */ public RawData serialize(Request request) { byte[] bytes = request.getBytes(); if (bytes == null) bytes = new DataSerializer().serializeRequest(request); request.setBytes(bytes); return new RawData(bytes, request.getDestination(), request.getDestinationPort()); }
@Test public void testSetOptionsSetsUriHostOption() { Request req = Request.newGet(); req.setDestination(InetAddress.getLoopbackAddress()); req.setOptions(URI.create("coap://iot.eclipse.org")); assertThat(req.getDestinationPort(), is(CoAP.DEFAULT_COAP_PORT)); assertThat(req.getOptions().getUriHost(), is("iot.eclipse.org")); }
/** * Returns the endpoint responsible for the given exchange. * @param exchange the exchange * @return the endpoint for the exchange */ public RemoteEndpoint getRemoteEndpoint(Exchange exchange){ //int remotePort, InetAddress remoteAddress){ InetAddress remoteAddress = exchange.getRequest().getDestination(); int remotePort = exchange.getRequest().getDestinationPort(); // TODO: One IP-Address is considered to be a destination endpoint, for higher granularity (portnumber) changes are necessary if (!remoteEndpointsList.containsKey(remoteAddress)){ RemoteEndpoint unusedRemoteEndpoint = new RemoteEndpoint(remotePort, remoteAddress, config); remoteEndpointsList.put(remoteAddress,unusedRemoteEndpoint); //System.out.println("Number of RemoteEndpoint objects stored:" + remoteEndpointsList.size()); } return remoteEndpointsList.get(remoteAddress); }
/** * Returns the endpoint responsible for the given exchange. * @param exchange the exchange * @return the endpoint for the exchange */ public RemoteEndpoint getRemoteEndpoint(Exchange exchange){ //int remotePort, InetAddress remoteAddress){ InetAddress remoteAddress = exchange.getRequest().getDestination(); int remotePort = exchange.getRequest().getDestinationPort(); // TODO: One IP-Address is considered to be a destination endpoint, for higher granularity (portnumber) changes are necessary if (!remoteEndpointsList.containsKey(remoteAddress)){ RemoteEndpoint unusedRemoteEndpoint = new RemoteEndpoint(remotePort, remoteAddress, config); remoteEndpointsList.put(remoteAddress,unusedRemoteEndpoint); //System.out.println("Number of RemoteEndpoint objects stored:" + remoteEndpointsList.size()); } return remoteEndpointsList.get(remoteAddress); }
@Override public void run() { if (!endpoint.getNonConfirmableQueue().isEmpty()) { endpoint.setProcessingNON(true); Exchange exchange = endpoint.getNonConfirmableQueue().poll(); if (getRemoteEndpoint(exchange).getNonConfirmableCounter() <= MAX_SUCCESSIVE_NONS) { getRemoteEndpoint(exchange).increaseNonConfirmableCounter(); if (exchange.getCurrentRequest().getDestinationPort() != 0) { // it's a response sendBucketRequest(exchange, exchange.getCurrentRequest()); } else if (exchange.getCurrentResponse() != null) { // it's a request sendBucketResponse(exchange, exchange.getCurrentResponse()); } } // schedule next transmission of a NON based on the RTO value (rate = 1/RTO) executor.schedule( new BucketThread(getRemoteEndpoint(exchange)), getRemoteEndpoint(exchange).getRTO(), TimeUnit.MILLISECONDS); } else { endpoint.setProcessingNON(false); } } }
@Override public void run() { if (!endpoint.getNonConfirmableQueue().isEmpty()) { endpoint.setProcessingNON(true); Exchange exchange = endpoint.getNonConfirmableQueue().poll(); if (getRemoteEndpoint(exchange).getNonConfirmableCounter() <= MAX_SUCCESSIVE_NONS) { getRemoteEndpoint(exchange).increaseNonConfirmableCounter(); if (exchange.getCurrentRequest().getDestinationPort() != 0) { // it's a response sendBucketRequest(exchange, exchange.getCurrentRequest()); } else if (exchange.getCurrentResponse() != null) { // it's a request sendBucketResponse(exchange, exchange.getCurrentResponse()); } } // schedule next transmission of a NON based on the RTO value (rate = 1/RTO) executor.schedule( new bucketThread(getRemoteEndpoint(exchange)), getRemoteEndpoint(exchange).getRTO(), TimeUnit.MILLISECONDS); } else { endpoint.setProcessingNON(false); } } }
/** * Verifies that the URI examples from <a href="https://tools.ietf.org/html/rfc7252#section-6.3"> * RFC 7252, Section 6.3</a> result in the same option values. * @throws URISyntaxException */ @Test public void testSetOptionsCompliesWithRfcExample() throws URISyntaxException { String[] exampleUris = new String[]{ "coap://example.com:5683/~sensors/temp.xml", "coap://EXAMPLE.com/%7Esensors/temp.xml", "coap://EXAMPLE.com:/%7esensors/temp.xml" }; for (String uriString : exampleUris) { URI uri = new URI(uriString); Request req = Request.newGet(); // explicitly set destination address so that we do not rely on working DNS req.setDestination(InetAddress.getLoopbackAddress()); req.setOptions(uri); assertThat(req.getOptions().getUriHost(), is("example.com")); assertThat(req.getDestinationPort(), is(5683)); assertThat(req.getOptions().getUriPort(), is(nullValue())); assertThat(req.getOptions().getUriPathString(), is("~sensors/temp.xml")); } }
@Test public void testSetURISetsUriHostOptionToHostName() { assumeTrue(dnsIsWorking()); Request req = Request.newGet().setURI("coaps://localhost"); assertNotNull(req.getDestination()); assertThat(req.getDestinationPort(), is(CoAP.DEFAULT_COAP_SECURE_PORT)); assertThat(req.getOptions().getUriHost(), is("localhost")); }
/** * Send request with option "cancel observe" (GET with Observe=1). */ private void sendCancelObserve() { Request request = this.request; Request cancel = Request.newGet(); cancel.setDestination(request.getDestination()); cancel.setDestinationPort(request.getDestinationPort()); // use same Token cancel.setToken(request.getToken()); // copy options, but set Observe to cancel cancel.setOptions(request.getOptions()); cancel.setObserveCancel(); // dispatch final response to the same message observers for (MessageObserver mo : request.getMessageObservers()) { cancel.addMessageObserver(mo); } endpoint.sendRequest(cancel); }
private static Request getNextRequestBlock(final Request request, final BlockwiseStatus status) { int num = status.getCurrentNum(); int szx = status.getCurrentSzx(); Request block = new Request(request.getCode()); // do not enforce CON, since NON could make sense over SMS or similar transports block.setType(request.getType()); block.setDestination(request.getDestination()); block.setDestinationPort(request.getDestinationPort()); // copy options block.setOptions(new OptionSet(request.getOptions())); // copy message observers so that a failing blockwise request also notifies observers registered with // the original request block.addMessageObservers(request.getMessageObservers()); int currentSize = 1 << (4 + szx); int from = num * currentSize; int to = Math.min((num + 1) * currentSize, request.getPayloadSize()); int length = to - from; byte[] blockPayload = new byte[length]; System.arraycopy(request.getPayload(), from, blockPayload, 0, length); block.setPayload(blockPayload); boolean m = (to < request.getPayloadSize()); block.getOptions().setBlock1(szx, m, num); status.setComplete(!m); return block; }
/** * Send request with option "cancel observe" (GET with Observe=1). */ private void sendCancelObserve() { Request request = this.request; Request cancel = Request.newGet(); cancel.setDestination(request.getDestination()); cancel.setDestinationPort(request.getDestinationPort()); // use same Token cancel.setToken(request.getToken()); // copy options, but set Observe to cancel cancel.setOptions(request.getOptions()); cancel.setObserveCancel(); // dispatch final response to the same message observers for (MessageObserver mo: request.getMessageObservers()) { cancel.addMessageObserver(mo); } endpoint.sendRequest(cancel); }
private static Request getNextRequestBlock(final Request request, final BlockwiseStatus status) { int num = status.getCurrentNum(); int szx = status.getCurrentSzx(); Request block = new Request(request.getCode()); // do not enforce CON, since NON could make sense over SMS or similar transports block.setType(request.getType()); block.setDestination(request.getDestination()); block.setDestinationPort(request.getDestinationPort()); // copy options block.setOptions(new OptionSet(request.getOptions())); // copy message observers so that a failing blockwise request also notifies observers registered with // the original request block.addMessageObservers(request.getMessageObservers()); int currentSize = 1 << (4 + szx); int from = num * currentSize; int to = Math.min((num + 1) * currentSize, request.getPayloadSize()); int length = to - from; byte[] blockPayload = new byte[length]; System.arraycopy(request.getPayload(), from, blockPayload, 0, length); block.setPayload(blockPayload); boolean m = (to < request.getPayloadSize()); block.getOptions().setBlock1(szx, m, num); status.setComplete(!m); return block; }
private static Response responseFor(final Request request) { Response response = new Response(ResponseCode.CONTENT); response.setMID(request.getMID()); response.setToken(request.getToken()); response.setBytes(new byte[]{}); response.setSource(request.getDestination()); response.setSourcePort(request.getDestinationPort()); response.setDestination(request.getSource()); response.setDestinationPort(request.getSourcePort()); return response; } }
private Response responseFor(final Request request) { Response response = new Response(ResponseCode.CONTENT); response.setMID(request.getMID()); response.setToken(request.getToken()); response.setBytes(new byte[]{}); response.setSource(request.getDestination()); response.setSourcePort(request.getDestinationPort()); response.setDestination(request.getSource()); response.setDestinationPort(request.getSourcePort()); return response; } }