@Override public void sendRequest(Request request) { LOGGER.log(Level.INFO, "{0}:{1} <== req {2}", new Object[]{request.getDestination(), request.getDestinationPort(), request}); }
private void assignClientUriIfEmpty(Request request) { // request.getUri() is a computed getter and never returns null so checking destination if (request.getDestination() == null) { request.setURI(uri); } }
/** * 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"); }
/** * 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)); }
/** * 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()); }
/** * Serializes a request and caches the result on the request object to skip future serializations. * <p> * NB: The byte array cached in the message is encoded according to the specific serializer implementation's * supported wire format. Any subsequent invocation of this method with the same request object will therefore * simply return the cached byte array. This may cause problems when the first invocation was done on a different * type of serializer than the second. * <p> * Clients should use the {@link #getByteArray(Request)} method in order to prevent caching of the resulting * byte array. * * @param request The request to serialize. * @param outboundCallback The callback to invoke once the message's correlation context * has been established. * @return The object containing the serialized request and the callback. */ public final RawData serializeRequest(final Request request, final MessageCallback outboundCallback) { if (request.getBytes() == null) { request.setBytes(getByteArray(request)); } return RawData.outbound( request.getBytes(), new InetSocketAddress(request.getDestination(), request.getDestinationPort()), outboundCallback, false); }
/** * 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 sendRequest(Exchange exchange, Request request) { if (request.getDestination() == null) throw new NullPointerException("Request has no destination address"); if (request.getDestinationPort() == 0)
@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())); }
/** * 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); }
/** * 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); }
@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")); }
refresh.setDestination(request.getDestination()); refresh.setDestinationPort(request.getDestinationPort());
Level.WARNING, "exchange observer has been completed on unregistered exchange [peer: {0}:{1}, origin: {2}]", new Object[]{ originRequest.getDestination(), originRequest.getDestinationPort(), exchange.getOrigin()}); } else {
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 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; } }