org.eclipse.californium.core.coap.Request.setDestination()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(13.1k)|赞(0)|评价(0)|浏览(257)

本文整理了Java中org.eclipse.californium.core.coap.Request.setDestination方法的一些代码示例,展示了Request.setDestination的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Request.setDestination方法的具体详情如下:
包路径:org.eclipse.californium.core.coap.Request
类名称:Request
方法名:setDestination

Request.setDestination介绍

暂无

代码示例

代码示例来源:origin: org.github.leshan/leshan-client

  1. private void buildRequestSettings(final AbstractLwM2mClientRequest request) {
  2. timeout = request.getTimeout();
  3. coapRequest.setDestination(serverAddress.getAddress());
  4. coapRequest.setDestinationPort(serverAddress.getPort());
  5. }

代码示例来源:origin: eclipse/californium

  1. /**
  2. * Sets the destination address and port and options from a given URI.
  3. * <p>
  4. * This method sets the <em>destination</em> to the IP address that the host part of the URI
  5. * has been resolved to and then delegates to the {@link #setOptions(URI)} method in order
  6. * to populate the request's options.
  7. *
  8. * @param uri The target URI.
  9. * @return This request for command chaining.
  10. * @throws NullPointerException if the URI is {@code null}.
  11. * @throws IllegalArgumentException if the URI contains a non-resolvable host name, an
  12. * unsupported scheme or a fragment.
  13. */
  14. public Request setURI(final URI uri) {
  15. if (uri == null) {
  16. throw new NullPointerException("URI must not be null");
  17. }
  18. final String host = uri.getHost() == null ? "localhost" : uri.getHost();
  19. try {
  20. InetAddress destAddress = InetAddress.getByName(host);
  21. setDestination(destAddress);
  22. return setOptions(new URI(uri.getScheme(), null, host, uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()));
  23. } catch (UnknownHostException e) {
  24. throw new IllegalArgumentException("cannot resolve host name: " + host);
  25. } catch (URISyntaxException e) {
  26. // should not happen because we are creating the URI from an existing URI object
  27. LOGGER.log(Level.WARNING, "cannot set URI on request", e);
  28. throw new IllegalArgumentException(e);
  29. }
  30. }

代码示例来源:origin: org.eclipse.leshan/leshan-client

  1. private void buildRequestSettings(final AbstractLwM2mClientRequest request) {
  2. timeout = request.getTimeout();
  3. coapRequest.setDestination(serverAddress.getAddress());
  4. coapRequest.setDestinationPort(serverAddress.getPort());
  5. }

代码示例来源:origin: org.eclipse.californium/californium-core

  1. /**
  2. * Sets the destination address and port and options from a given URI.
  3. * <p>
  4. * This method sets the <em>destination</em> to the IP address that the host part of the URI
  5. * has been resolved to and then delegates to the {@link #setOptions(URI)} method in order
  6. * to populate the request's options.
  7. *
  8. * @param uri The target URI.
  9. * @return This request for command chaining.
  10. * @throws NullPointerException if the URI is {@code null}.
  11. * @throws IllegalArgumentException if the URI contains a non-resolvable host name, an
  12. * unsupported scheme or a fragment.
  13. */
  14. public Request setURI(final URI uri) {
  15. if (uri == null) {
  16. throw new NullPointerException("URI must not be null");
  17. }
  18. final String host = uri.getHost() == null ? "localhost" : uri.getHost();
  19. try {
  20. InetAddress destAddress = InetAddress.getByName(host);
  21. setDestination(destAddress);
  22. return setOptions(new URI(uri.getScheme(), null, host, uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()));
  23. } catch (UnknownHostException e) {
  24. throw new IllegalArgumentException("cannot resolve host name: " + host);
  25. } catch (URISyntaxException e) {
  26. // should not happen because we are creating the URI from an existing URI object
  27. LOGGER.log(Level.WARNING, "cannot set URI on request", e);
  28. throw new IllegalArgumentException(e);
  29. }
  30. }

代码示例来源:origin: eclipse/californium

  1. @Test
  2. public void testSendRequestAddsMessageCallbackToOutboundMessage() throws Exception {
  3. // GIVEN an outbound request
  4. latch = new CountDownLatch(1);
  5. Request request = Request.newGet();
  6. request.setDestination(InetAddress.getLoopbackAddress());
  7. request.setDestinationPort(CoAP.DEFAULT_COAP_PORT);
  8. // WHEN sending the request to the peer
  9. endpoint.sendRequest(request);
  10. // THEN assert that the message delivered to the Connector contains a
  11. // MessageCallback
  12. assertTrue(latch.await(1, TimeUnit.SECONDS));
  13. }

代码示例来源:origin: eclipse/californium

  1. @Test
  2. public void testSetOptionsSetsUriHostOption() {
  3. Request req = Request.newGet();
  4. req.setDestination(InetAddress.getLoopbackAddress());
  5. req.setOptions(URI.create("coap://iot.eclipse.org"));
  6. assertThat(req.getDestinationPort(), is(CoAP.DEFAULT_COAP_PORT));
  7. assertThat(req.getOptions().getUriHost(), is("iot.eclipse.org"));
  8. }

代码示例来源:origin: eclipse/californium

  1. @Override
  2. public void go() {
  3. Request request = new Request(code);
  4. if (destination != null) {
  5. request.setDestination(destination.getAddress());
  6. request.setDestinationPort(destination.getPort());
  7. }
  8. setProperties(request);
  9. RawData raw = serializer.serializeRequest(request);
  10. send(raw);
  11. }
  12. }

代码示例来源:origin: eclipse/californium

  1. /**
  2. * Verifies that the URI examples from <a href="https://tools.ietf.org/html/rfc7252#section-6.3">
  3. * RFC 7252, Section 6.3</a> result in the same option values.
  4. * @throws URISyntaxException
  5. */
  6. @Test
  7. public void testSetOptionsCompliesWithRfcExample() throws URISyntaxException {
  8. String[] exampleUris = new String[]{
  9. "coap://example.com:5683/~sensors/temp.xml",
  10. "coap://EXAMPLE.com/%7Esensors/temp.xml",
  11. "coap://EXAMPLE.com:/%7esensors/temp.xml"
  12. };
  13. for (String uriString : exampleUris) {
  14. URI uri = new URI(uriString);
  15. Request req = Request.newGet();
  16. // explicitly set destination address so that we do not rely on working DNS
  17. req.setDestination(InetAddress.getLoopbackAddress());
  18. req.setOptions(uri);
  19. assertThat(req.getOptions().getUriHost(), is("example.com"));
  20. assertThat(req.getDestinationPort(), is(5683));
  21. assertThat(req.getOptions().getUriPort(), is(nullValue()));
  22. assertThat(req.getOptions().getUriPathString(), is("~sensors/temp.xml"));
  23. }
  24. }

代码示例来源:origin: eclipse/californium

  1. private static Exchange sendObserveRequest(final UdpMatcher matcher) {
  2. Request request = Request.newGet();
  3. request.setDestination(dest.getAddress());
  4. request.setDestinationPort(dest.getPort());
  5. request.setObserve();
  6. Exchange exchange = new Exchange(request, Origin.LOCAL);
  7. matcher.sendRequest(exchange, request);
  8. return exchange;
  9. }

代码示例来源:origin: eclipse/californium

  1. /**
  2. * Verifies that the getByteArray() method does not set the Message's <em>bytes</em> property.
  3. */
  4. @Test
  5. public void testGetByteArrayDoesNotAlterMessage() {
  6. // GIVEN a CoAP request
  7. Request req = Request.newGet();
  8. req.setToken(new byte[]{0x00});
  9. req.getOptions().setObserve(0);
  10. req.setDestination(InetAddress.getLoopbackAddress());
  11. // WHEN serializing the request to a byte array
  12. serializer.getByteArray(req);
  13. // THEN the serialized byte array is not written to the request's bytes property
  14. assertNull(req.getBytes());
  15. }

代码示例来源:origin: eclipse/californium

  1. private Exchange sendRequest(final TcpMatcher matcher, final CorrelationContext ctx) {
  2. Request request = Request.newGet();
  3. request.setDestination(dest.getAddress());
  4. request.setDestinationPort(dest.getPort());
  5. Exchange exchange = new Exchange(request, Origin.LOCAL);
  6. exchange.setRequest(request);
  7. matcher.sendRequest(exchange, request);
  8. exchange.setCorrelationContext(ctx);
  9. return exchange;
  10. }

代码示例来源:origin: eclipse/californium

  1. /**
  2. * Verifies that the serializeRequest() method sets the Message's <em>bytes</em> property.
  3. */
  4. @Test
  5. public void testSerializeRequestStoresBytesInMessage() {
  6. // GIVEN a CoAP request
  7. Request req = Request.newGet();
  8. req.setToken(new byte[]{0x00});
  9. req.getOptions().setObserve(0);
  10. req.setDestination(InetAddress.getLoopbackAddress());
  11. // WHEN serializing the request to a RawData object
  12. RawData raw = serializer.serializeRequest(req);
  13. // THEN the serialized byte array is stored in the request's bytes property
  14. assertNotNull(req.getBytes());
  15. assertThat(raw.getBytes(), is(req.getBytes()));
  16. }
  17. }

代码示例来源:origin: eclipse/californium

  1. private static Exchange sendRequest(final UdpMatcher matcher, final CorrelationContext ctx) {
  2. Request request = Request.newGet();
  3. request.setDestination(dest.getAddress());
  4. request.setDestinationPort(dest.getPort());
  5. Exchange exchange = new Exchange(request, Origin.LOCAL);
  6. exchange.setRequest(request);
  7. matcher.sendRequest(exchange, request);
  8. exchange.setCorrelationContext(ctx);
  9. return exchange;
  10. }

代码示例来源:origin: eclipse/californium

  1. /**
  2. * Send request with option "cancel observe" (GET with Observe=1).
  3. */
  4. private void sendCancelObserve() {
  5. Request request = this.request;
  6. Request cancel = Request.newGet();
  7. cancel.setDestination(request.getDestination());
  8. cancel.setDestinationPort(request.getDestinationPort());
  9. // use same Token
  10. cancel.setToken(request.getToken());
  11. // copy options, but set Observe to cancel
  12. cancel.setOptions(request.getOptions());
  13. cancel.setObserveCancel();
  14. // dispatch final response to the same message observers
  15. for (MessageObserver mo : request.getMessageObservers()) {
  16. cancel.addMessageObserver(mo);
  17. }
  18. endpoint.sendRequest(cancel);
  19. }

代码示例来源:origin: org.eclipse.californium/californium-core

  1. /**
  2. * Send request with option "cancel observe" (GET with Observe=1).
  3. */
  4. private void sendCancelObserve() {
  5. Request request = this.request;
  6. Request cancel = Request.newGet();
  7. cancel.setDestination(request.getDestination());
  8. cancel.setDestinationPort(request.getDestinationPort());
  9. // use same Token
  10. cancel.setToken(request.getToken());
  11. // copy options, but set Observe to cancel
  12. cancel.setOptions(request.getOptions());
  13. cancel.setObserveCancel();
  14. // dispatch final response to the same message observers
  15. for (MessageObserver mo: request.getMessageObservers()) {
  16. cancel.addMessageObserver(mo);
  17. }
  18. endpoint.sendRequest(cancel);
  19. }

代码示例来源:origin: org.eclipse.leshan/leshan-server-cf

  1. private final void setTarget(Request coapRequest, Client client, LwM2mPath path) {
  2. coapRequest.setDestination(client.getAddress());
  3. coapRequest.setDestinationPort(client.getPort());
  4. // root path
  5. if (client.getRootPath() != null) {
  6. for (String rootPath : client.getRootPath().split("/")) {
  7. if (!StringUtils.isEmpty(rootPath)) {
  8. coapRequest.getOptions().addUriPath(rootPath);
  9. }
  10. }
  11. }
  12. // objectId
  13. coapRequest.getOptions().addUriPath(Integer.toString(path.getObjectId()));
  14. // objectInstanceId
  15. if (path.getObjectInstanceId() == null) {
  16. if (path.getResourceId() != null) {
  17. coapRequest.getOptions().addUriPath("0"); // default instanceId
  18. }
  19. } else {
  20. coapRequest.getOptions().addUriPath(Integer.toString(path.getObjectInstanceId()));
  21. }
  22. // resourceId
  23. if (path.getResourceId() != null) {
  24. coapRequest.getOptions().addUriPath(Integer.toString(path.getResourceId()));
  25. }
  26. }

代码示例来源:origin: eclipse/californium

  1. @Test
  2. public void testNonconfirmable() throws Exception {
  3. createSimpleServer();
  4. // send request
  5. Request request = new Request(CoAP.Code.POST);
  6. request.setConfirmable(false);
  7. request.setDestination(InetAddress.getLoopbackAddress());
  8. request.setDestinationPort(serverPort);
  9. request.setPayload("client says hi");
  10. request.send();
  11. System.out.println("client sent request");
  12. // receive response and check
  13. Response response = request.waitForResponse(1000);
  14. assertNotNull("Client received no response", response);
  15. System.out.println("client received response");
  16. assertEquals(response.getPayloadString(), SERVER_RESPONSE);
  17. }

代码示例来源:origin: eclipse/californium

  1. /**
  2. * Verifies that canceling a message that has no MID set does not throw an exception.
  3. *
  4. */
  5. @Test
  6. public void testExchangeObserverToleratesUnsentMessages() {
  7. // GIVEN a request that has no MID and has not been sent yet
  8. UdpMatcher matcher = newMatcher(false);
  9. Request request = Request.newGet();
  10. request.setDestination(dest.getAddress());
  11. request.setDestinationPort(dest.getPort());
  12. Exchange exchange = new Exchange(request, Origin.LOCAL);
  13. exchange.setRequest(request);
  14. assertFalse(request.hasMID());
  15. matcher.observe(exchange);
  16. // WHEN the request is canceled and thus the message exchange completes
  17. exchange.setComplete();
  18. // THEN the matcher's exchange observer does not throw an exception
  19. }

代码示例来源:origin: eclipse/californium

  1. private static Request getNextRequestBlock(final Request request, final BlockwiseStatus status) {
  2. int num = status.getCurrentNum();
  3. int szx = status.getCurrentSzx();
  4. Request block = new Request(request.getCode());
  5. // do not enforce CON, since NON could make sense over SMS or similar transports
  6. block.setType(request.getType());
  7. block.setDestination(request.getDestination());
  8. block.setDestinationPort(request.getDestinationPort());
  9. // copy options
  10. block.setOptions(new OptionSet(request.getOptions()));
  11. // copy message observers so that a failing blockwise request also notifies observers registered with
  12. // the original request
  13. block.addMessageObservers(request.getMessageObservers());
  14. int currentSize = 1 << (4 + szx);
  15. int from = num * currentSize;
  16. int to = Math.min((num + 1) * currentSize, request.getPayloadSize());
  17. int length = to - from;
  18. byte[] blockPayload = new byte[length];
  19. System.arraycopy(request.getPayload(), from, blockPayload, 0, length);
  20. block.setPayload(blockPayload);
  21. boolean m = (to < request.getPayloadSize());
  22. block.getOptions().setBlock1(szx, m, num);
  23. status.setComplete(!m);
  24. return block;
  25. }

代码示例来源:origin: org.eclipse.californium/californium-core

  1. private static Request getNextRequestBlock(final Request request, final BlockwiseStatus status) {
  2. int num = status.getCurrentNum();
  3. int szx = status.getCurrentSzx();
  4. Request block = new Request(request.getCode());
  5. // do not enforce CON, since NON could make sense over SMS or similar transports
  6. block.setType(request.getType());
  7. block.setDestination(request.getDestination());
  8. block.setDestinationPort(request.getDestinationPort());
  9. // copy options
  10. block.setOptions(new OptionSet(request.getOptions()));
  11. // copy message observers so that a failing blockwise request also notifies observers registered with
  12. // the original request
  13. block.addMessageObservers(request.getMessageObservers());
  14. int currentSize = 1 << (4 + szx);
  15. int from = num * currentSize;
  16. int to = Math.min((num + 1) * currentSize, request.getPayloadSize());
  17. int length = to - from;
  18. byte[] blockPayload = new byte[length];
  19. System.arraycopy(request.getPayload(), from, blockPayload, 0, length);
  20. block.setPayload(blockPayload);
  21. boolean m = (to < request.getPayloadSize());
  22. block.getOptions().setBlock1(szx, m, num);
  23. status.setComplete(!m);
  24. return block;
  25. }

相关文章