org.eclipse.californium.core.coap.Request类的使用及代码示例

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

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

Request介绍

[英]Request represents a CoAP request and has either the Type CON or NON and one of the CoAP.Codes GET, POST, PUT or DELETE. A request must be sent over an Endpoint to its destination. By default, a request uses the default endpoint defined by EndpointManager. The server responds with a Response.

A client can send a request and wait for for a response using a synchronous (blocking) call like this:

Request request = new Request(Code.GET); 
request.setURI("coap://example.com:5683/sensors/temperature"); 
request.send(); 
Response response = request.waitForResponse();

A client may also send requests asynchronously (non-blocking) and define a handler to be invoked when a response arrives. This is in particular useful when a client wants to observe the target resource and react to notifications. For instance:

Request request = new Request(Code.GET); 
request.setURI("coap://example.com:5683/sensors/temperature"); 
request.setObserve(); 
request.addMessageObserver(new MessageObserverAdapter() {

[中]请求表示CoAP请求,类型为CON或NON,其中一个为CoAP。代码获取、发布、放置或删除。请求必须通过端点发送到目的地。默认情况下,请求使用EndpointManager定义的默认端点。服务器会做出响应。
客户端可以使用如下同步(阻塞)调用发送请求并等待响应:

Request request = new Request(Code.GET); 
request.setURI("coap://example.com:5683/sensors/temperature"); 
request.send(); 
Response response = request.waitForResponse();

客户端还可以异步(非阻塞)发送请求,并定义响应到达时要调用的处理程序。当客户端希望观察目标资源并对通知做出反应时,这一点尤其有用。例如:

Request request = new Request(Code.GET); 
request.setURI("coap://example.com:5683/sensors/temperature"); 
request.setObserve(); 
request.addMessageObserver(new MessageObserverAdapter() {

代码示例

代码示例来源: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: eclipse/leshan

public CoapMessage(Request request, boolean incoming) {
  this(incoming, request.getType(), request.getMID(), request.getTokenString(), request.getOptions(), request
      .getPayload());
  this.code = request.getCode().toString();
}

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

public CC04(String serverURI) {
  super(CC04.class.getSimpleName());
  // create the request
  Request request = Request.newPost();
  // add payload
  request.setPayload("TD_COAP_CORE_04");
  request.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
  // set the parameters and execute the request
  executeRequest(request, serverURI, RESOURCE_URI);
}

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

private boolean requiresBlockwise(final Request request) {
  if (request.getCode() == Code.PUT || request.getCode() == Code.POST) {
    return request.getPayloadSize() > max_message_size;
  } else {
    return false;
  }
}

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

@Override
public String toString() {
  String payload = getPayloadTracingString();
  return String.format("%s-%-6s MID=%5d, Token=%s, OptionSet=%s, %s", getType(), getCode(), getMID(), getTokenString(), getOptions(), payload);
}

代码示例来源:origin: org.opendaylight.iotdm/onem2m-protocol-coap

@Override
public void sendNotification(String url, String payload) {
  Request request = Request.newPost();
  request.setURI(url);
  request.setPayload(payload);
  request.send();
  LOG.debug("CoAP: Send notification uri: {}, payload: {}:", url, payload);
}

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

@Override
public void visit(WriteRequest request) {
  coapRequest = request.isReplaceRequest() ? Request.newPut() : Request.newPost();
  ContentFormat format = request.getContentFormat();
  coapRequest.getOptions().setContentFormat(format.getCode());
  coapRequest.setPayload(encoder.encode(request.getNode(), format, request.getPath(), model));
  setTarget(coapRequest, request.getPath());
}

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

public CB03(String serverURI) {
  super(CB03.class.getSimpleName());
  Request request = Request.newPut();
  request.setPayload(data);
  request.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
  // set the parameters and execute the request
  executeRequest(request, serverURI, "/large-update");
}

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

/**
 * Sends a PUT request with payload and required Content-Format and blocks
 * until the response is available.
 *
 * @param payload the payload
 * @param format the Content-Format
 * @return the CoAP response
 */
public CoapResponse put(String payload, int format) {
  return synchronous(format(Request.newPut().setURI(uri).setPayload(payload), format));
}

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

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

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

protected final void appendRequestDetails(final Request request) {
  if (request.isCanceled()) {
    buffer.append("CANCELED ");
  }
  buffer.append(request.getType()).append(" [MID=").append(request.getMID())
    .append(", T=").append(request.getTokenString()).append("], ")
    .append(request.getCode()).append(", /").append(request.getOptions().getUriPathString());
  appendBlockOption(1, request.getOptions().getBlock1());
  appendBlockOption(2, request.getOptions().getBlock2());
  appendObserveOption(request.getOptions());
  appendSize1(request.getOptions());
  appendEtags(request.getOptions());
}

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

@Override
  public void handleGET(CoapExchange exchange) {

    // get request to read out details
    Request request = exchange.advanced().getRequest();
    
    StringBuilder payload = new StringBuilder();
    payload.append(String.format("Type: %d (%s)\nCode: %d (%s)\nMID: %d\n",
                   request.getType().value,
                   request.getType(),
                   request.getCode().value,
                   request.getCode(),
                   request.getMID()
                  ));
    payload.append("?").append(request.getOptions().getUriQueryString());
    if (payload.length()>64) {
      payload.delete(63, payload.length());
      payload.append('»');
    }
    
    // complete the request
    exchange.respond(CONTENT, payload.toString(), TEXT_PLAIN);
  }
}

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

@Override
public void visit(BootstrapWriteRequest request) {
  coapRequest = Request.newPut();
  coapRequest.setConfirmable(true);
  ContentFormat format = request.getContentFormat();
  coapRequest.getOptions().setContentFormat(format.getCode());
  coapRequest.setPayload(encoder.encode(request.getNode(), format, request.getPath(), model));
  setTarget(coapRequest, request.getPath());
}

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

@Test
public void testSetURIDoesNotSetUriHostOptionForIp6Address() {
  // use www.google.com's IPv6 address
  Request req = Request.newGet().setURI("coap://[2a00:1450:4001:817::2003]");
  assertNull(req.getOptions().getUriHost());
}

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

public CC08(String serverURI) {
  super(CC08.class.getSimpleName());
  // create the request
  Request request = new Request(Code.POST);
  request.setConfirmable(false);
  // add payload
  request.setPayload("TD_COAP_CORE_08");
  request.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
  // set the parameters and execute the request
  executeRequest(request, serverURI, RESOURCE_URI);
}

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

public CC19(String serverURI) {
  super(CC19.class.getSimpleName());
  // create the request
  Request request = new Request(Code.POST, Type.CON);
  // add payload
  request.setPayload("TD_COAP_CORE_19");
  request.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
  // set the parameters and execute the request
  executeRequest(request, serverURI, RESOURCE_URI);
}

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

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

代码示例来源: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

request.setURI(uri);
  long time = 5000;
  request.send();
  System.out.println("**** BEGIN CHECK ****");
  response = request.waitForResponse(time);
  if (response != null) {
    success &= checkType(Type.ACK, response.getType());
    success &= checkInt(EXPECTED_RESPONSE_CODE.value, response.getCode().value, "code");
    success &= checkToken(request.getToken(), response.getToken());
    success &= hasContentType(response);
    success &= hasNonEmptyPalyoad(response);
        response = request.waitForResponse(time + 1000);
        request.cancel(); // stack should send RST

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

@Override
public void visit(final BootstrapRequest request) {
  coapRequest = Request.newPost();
  buildRequestSettings(request);
  coapRequest.getOptions().addUriPath("bs");
  coapRequest.getOptions().addUriQuery("ep=" + request.getClientEndpointIdentifier());
  parametersValid = true;
}

相关文章