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

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

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

  1. Request request = new Request(Code.GET);
  2. request.setURI("coap://example.com:5683/sensors/temperature");
  3. request.send();
  4. 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:

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

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

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

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

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

代码示例

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

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

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

  1. public CC04(String serverURI) {
  2. super(CC04.class.getSimpleName());
  3. // create the request
  4. Request request = Request.newPost();
  5. // add payload
  6. request.setPayload("TD_COAP_CORE_04");
  7. request.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
  8. // set the parameters and execute the request
  9. executeRequest(request, serverURI, RESOURCE_URI);
  10. }

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

  1. private boolean requiresBlockwise(final Request request) {
  2. if (request.getCode() == Code.PUT || request.getCode() == Code.POST) {
  3. return request.getPayloadSize() > max_message_size;
  4. } else {
  5. return false;
  6. }
  7. }

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

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

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

  1. @Override
  2. public void sendNotification(String url, String payload) {
  3. Request request = Request.newPost();
  4. request.setURI(url);
  5. request.setPayload(payload);
  6. request.send();
  7. LOG.debug("CoAP: Send notification uri: {}, payload: {}:", url, payload);
  8. }

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

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

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

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

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

  1. /**
  2. * Sends a PUT request with payload and required Content-Format and blocks
  3. * until the response is available.
  4. *
  5. * @param payload the payload
  6. * @param format the Content-Format
  7. * @return the CoAP response
  8. */
  9. public CoapResponse put(String payload, int format) {
  10. return synchronous(format(Request.newPut().setURI(uri).setPayload(payload), format));
  11. }

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

  1. @Test
  2. public void testSetURISetsDestination() {
  3. InetSocketAddress dest = InetSocketAddress.createUnresolved("192.168.0.1", 12000);
  4. Request req = Request.newGet().setURI("coap://192.168.0.1:12000");
  5. assertThat(req.getDestination().getHostAddress(), is(dest.getHostString()));
  6. assertThat(req.getDestinationPort(), is(dest.getPort()));
  7. }

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

  1. protected final void appendRequestDetails(final Request request) {
  2. if (request.isCanceled()) {
  3. buffer.append("CANCELED ");
  4. }
  5. buffer.append(request.getType()).append(" [MID=").append(request.getMID())
  6. .append(", T=").append(request.getTokenString()).append("], ")
  7. .append(request.getCode()).append(", /").append(request.getOptions().getUriPathString());
  8. appendBlockOption(1, request.getOptions().getBlock1());
  9. appendBlockOption(2, request.getOptions().getBlock2());
  10. appendObserveOption(request.getOptions());
  11. appendSize1(request.getOptions());
  12. appendEtags(request.getOptions());
  13. }

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

  1. @Override
  2. public void handleGET(CoapExchange exchange) {
  3. // get request to read out details
  4. Request request = exchange.advanced().getRequest();
  5. StringBuilder payload = new StringBuilder();
  6. payload.append(String.format("Type: %d (%s)\nCode: %d (%s)\nMID: %d\n",
  7. request.getType().value,
  8. request.getType(),
  9. request.getCode().value,
  10. request.getCode(),
  11. request.getMID()
  12. ));
  13. payload.append("?").append(request.getOptions().getUriQueryString());
  14. if (payload.length()>64) {
  15. payload.delete(63, payload.length());
  16. payload.append('»');
  17. }
  18. // complete the request
  19. exchange.respond(CONTENT, payload.toString(), TEXT_PLAIN);
  20. }
  21. }

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

  1. @Override
  2. public void visit(BootstrapWriteRequest request) {
  3. coapRequest = Request.newPut();
  4. coapRequest.setConfirmable(true);
  5. ContentFormat format = request.getContentFormat();
  6. coapRequest.getOptions().setContentFormat(format.getCode());
  7. coapRequest.setPayload(encoder.encode(request.getNode(), format, request.getPath(), model));
  8. setTarget(coapRequest, request.getPath());
  9. }

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

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

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

  1. public CC08(String serverURI) {
  2. super(CC08.class.getSimpleName());
  3. // create the request
  4. Request request = new Request(Code.POST);
  5. request.setConfirmable(false);
  6. // add payload
  7. request.setPayload("TD_COAP_CORE_08");
  8. request.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
  9. // set the parameters and execute the request
  10. executeRequest(request, serverURI, RESOURCE_URI);
  11. }

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

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

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

  1. private static Response responseFor(final Request request) {
  2. Response response = new Response(ResponseCode.CONTENT);
  3. response.setMID(request.getMID());
  4. response.setToken(request.getToken());
  5. response.setBytes(new byte[]{});
  6. response.setSource(request.getDestination());
  7. response.setSourcePort(request.getDestinationPort());
  8. response.setDestination(request.getSource());
  9. response.setDestinationPort(request.getSourcePort());
  10. return response;
  11. }
  12. }

代码示例来源: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. request.setURI(uri);
  2. long time = 5000;
  3. request.send();
  4. System.out.println("**** BEGIN CHECK ****");
  5. response = request.waitForResponse(time);
  6. if (response != null) {
  7. success &= checkType(Type.ACK, response.getType());
  8. success &= checkInt(EXPECTED_RESPONSE_CODE.value, response.getCode().value, "code");
  9. success &= checkToken(request.getToken(), response.getToken());
  10. success &= hasContentType(response);
  11. success &= hasNonEmptyPalyoad(response);
  12. response = request.waitForResponse(time + 1000);
  13. request.cancel(); // stack should send RST

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

  1. @Override
  2. public void visit(final BootstrapRequest request) {
  3. coapRequest = Request.newPost();
  4. buildRequestSettings(request);
  5. coapRequest.getOptions().addUriPath("bs");
  6. coapRequest.getOptions().addUriQuery("ep=" + request.getClientEndpointIdentifier());
  7. parametersValid = true;
  8. }

相关文章