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

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

本文整理了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

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

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

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

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

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

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

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

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

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

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

@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"));
}

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

@Override
  public void go() {
    Request request = new Request(code);
    if (destination != null) {
      request.setDestination(destination.getAddress());
      request.setDestinationPort(destination.getPort());
    }
    setProperties(request);
    RawData raw = serializer.serializeRequest(request);
    send(raw);
  }
}

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

/**
 * 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"));
  }
}

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

private static Exchange sendObserveRequest(final UdpMatcher matcher) {
  Request request = Request.newGet();
  request.setDestination(dest.getAddress());
  request.setDestinationPort(dest.getPort());
  request.setObserve();
  Exchange exchange = new Exchange(request, Origin.LOCAL);
  matcher.sendRequest(exchange, request);
  return exchange;
}

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

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

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

private Exchange sendRequest(final TcpMatcher matcher, final CorrelationContext ctx) {
  Request request = Request.newGet();
  request.setDestination(dest.getAddress());
  request.setDestinationPort(dest.getPort());
  Exchange exchange = new Exchange(request, Origin.LOCAL);
  exchange.setRequest(request);
  matcher.sendRequest(exchange, request);
  exchange.setCorrelationContext(ctx);
  return exchange;
}

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

/**
   * Verifies that the serializeRequest() method sets the Message's <em>bytes</em> property.
   */
  @Test
  public void testSerializeRequestStoresBytesInMessage() {

    // GIVEN a CoAP request
    Request req = Request.newGet();
    req.setToken(new byte[]{0x00});
    req.getOptions().setObserve(0);
    req.setDestination(InetAddress.getLoopbackAddress());

    // WHEN serializing the request to a RawData object
    RawData raw = serializer.serializeRequest(req);

    // THEN the serialized byte array is stored in the request's bytes property
    assertNotNull(req.getBytes());
    assertThat(raw.getBytes(), is(req.getBytes()));
  }
}

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

private static Exchange sendRequest(final UdpMatcher matcher, final CorrelationContext ctx) {
  Request request = Request.newGet();
  request.setDestination(dest.getAddress());
  request.setDestinationPort(dest.getPort());
  Exchange exchange = new Exchange(request, Origin.LOCAL);
  exchange.setRequest(request);
  matcher.sendRequest(exchange, request);
  exchange.setCorrelationContext(ctx);
  return exchange;
}

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

/**
 * 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);
}

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

/**
 * 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);
}

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

private final void setTarget(Request coapRequest, Client client, LwM2mPath path) {
  coapRequest.setDestination(client.getAddress());
  coapRequest.setDestinationPort(client.getPort());
  // root path
  if (client.getRootPath() != null) {
    for (String rootPath : client.getRootPath().split("/")) {
      if (!StringUtils.isEmpty(rootPath)) {
        coapRequest.getOptions().addUriPath(rootPath);
      }
    }
  }
  // objectId
  coapRequest.getOptions().addUriPath(Integer.toString(path.getObjectId()));
  // objectInstanceId
  if (path.getObjectInstanceId() == null) {
    if (path.getResourceId() != null) {
      coapRequest.getOptions().addUriPath("0"); // default instanceId
    }
  } else {
    coapRequest.getOptions().addUriPath(Integer.toString(path.getObjectInstanceId()));
  }
  // resourceId
  if (path.getResourceId() != null) {
    coapRequest.getOptions().addUriPath(Integer.toString(path.getResourceId()));
  }
}

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

@Test
public void testNonconfirmable() throws Exception {
  createSimpleServer();
  // send request
  Request request = new Request(CoAP.Code.POST);
  request.setConfirmable(false);
  request.setDestination(InetAddress.getLoopbackAddress());
  request.setDestinationPort(serverPort);
  request.setPayload("client says hi");
  request.send();
  System.out.println("client sent request");
  // receive response and check
  Response response = request.waitForResponse(1000);
  assertNotNull("Client received no response", response);
  System.out.println("client received response");
  assertEquals(response.getPayloadString(), SERVER_RESPONSE);
}

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

/**
 * Verifies that canceling a message that has no MID set does not throw an exception.
 * 
 */
@Test
public void testExchangeObserverToleratesUnsentMessages() {
  // GIVEN a request that has no MID and has not been sent yet
  UdpMatcher matcher = newMatcher(false);
  Request request = Request.newGet();
  request.setDestination(dest.getAddress());
  request.setDestinationPort(dest.getPort());
  Exchange exchange = new Exchange(request, Origin.LOCAL);
  exchange.setRequest(request);
  assertFalse(request.hasMID());
  matcher.observe(exchange);
  // WHEN the request is canceled and thus the message exchange completes
  exchange.setComplete();
  // THEN the matcher's exchange observer does not throw an exception
}

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

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;
}

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

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;
}

相关文章

微信公众号

最新文章

更多