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

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

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

Request.addMessageObserver介绍

暂无

代码示例

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

  1. private void asynchronous(Request request, CoapHandler handler) {
  2. request.addMessageObserver(new MessageObserverImpl(handler));
  3. send(request);
  4. }

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

  1. @Test
  2. public void testMultipleAddsToMessageObservers() {
  3. Request ping = new Request(null, Type.CON);
  4. List<MessageObserver> observers = new ArrayList<>();
  5. observers.add(new MessageObserverAdapter() {});
  6. observers.add(new MessageObserverAdapter() {});
  7. ping.addMessageObservers(observers);
  8. ping.addMessageObserver(new MessageObserverAdapter() {});
  9. ping.addMessageObserver(new MessageObserverAdapter() {});
  10. observers.add(new MessageObserverAdapter() {});
  11. observers.add(new MessageObserverAdapter() {});
  12. ping.addMessageObservers(observers);
  13. ping.addMessageObserver(new MessageObserverAdapter() {});
  14. assertThat(ping.getMessageObservers().size(), is(9));
  15. }
  16. }

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

  1. private void asynchronous(Request request, CoapHandler handler) {
  2. request.addMessageObserver(new MessageObserverImpl(handler));
  3. send(request);
  4. }

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

  1. /**
  2. * Adds a message observer to the request to be sent which
  3. * completes the exchange if the request gets canceled.
  4. *
  5. * @param exchange The (locally originating) exchange that the request is part of.
  6. * @param request The outbound request.
  7. */
  8. @Override
  9. public void sendRequest(final Exchange exchange, final Request request) {
  10. request.addMessageObserver(new CancelledMessageObserver(exchange));
  11. lower().sendRequest(exchange, request);
  12. }

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

  1. @Test
  2. public void testMultipleAddMessageObserver() {
  3. Request ping = new Request(null, Type.CON);
  4. ping.addMessageObserver(new MessageObserverAdapter() {});
  5. ping.addMessageObserver(new MessageObserverAdapter() {});
  6. ping.addMessageObserver(new MessageObserverAdapter() {});
  7. assertThat(ping.getMessageObservers().size(), is(3));
  8. }

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

  1. private void addPendingRequest(String registrationId, Request coapRequest) {
  2. Validate.notNull(registrationId);
  3. // Theoretically we should add observer only for CONFIRMABLE request but with transparent block-wise mode, an
  4. // UNCONFIRMABLE request could be change in several block-wised requests.
  5. CleanerMessageObserver observer = new CleanerMessageObserver(registrationId, coapRequest);
  6. coapRequest.addMessageObserver(observer);
  7. pendingRequests.put(observer.getRequestKey(), coapRequest);
  8. }

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

  1. private CoapObserveRelation observe(Request request, CoapHandler handler) {
  2. if (request.getOptions().hasObserve()) {
  3. Endpoint outEndpoint = getEffectiveEndpoint(request);
  4. CoapObserveRelation relation = new CoapObserveRelation(request, outEndpoint);
  5. request.addMessageObserver(new ObserveMessageObserverImpl(handler, relation));
  6. send(request, outEndpoint);
  7. return relation;
  8. } else {
  9. throw new IllegalArgumentException("please make sure that the request has observe option set.");
  10. }
  11. }

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

  1. @Test
  2. public void testAddMessageObserver() {
  3. Request ping = new Request(null, Type.CON);
  4. ping.addMessageObserver(new MessageObserverAdapter() {});
  5. assertThat(ping.getMessageObservers().size(), is(1));
  6. }

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

  1. /**
  2. * Sends the specified observe request and invokes the specified handler
  3. * each time a notification arrives.
  4. *
  5. * @param request the request
  6. *
  7. * @param handler the Response handler
  8. *
  9. * @return the CoAP observe relation
  10. * @throws IllegalArgumentException if the observe option is not set in the
  11. * request
  12. */
  13. public CoapObserveRelation observe(Request request, CoapHandler handler) {
  14. if (request.getOptions().hasObserve()) {
  15. Endpoint outEndpoint = getEffectiveEndpoint(request);
  16. CoapObserveRelation relation = new CoapObserveRelation(request, outEndpoint);
  17. request.addMessageObserver(new ObserveMessageObserverImpl(handler, relation));
  18. send(request, outEndpoint);
  19. return relation;
  20. } else {
  21. throw new IllegalArgumentException("please make sure that the request has observe option set.");
  22. }
  23. }

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

  1. @Override
  2. public <T extends LwM2mResponse> void send(InetSocketAddress serverAddress, boolean secure,
  3. final UplinkRequest<T> request, long timeout, ResponseCallback<T> responseCallback,
  4. ErrorCallback errorCallback) {
  5. // Create the CoAP request from LwM2m request
  6. CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(serverAddress);
  7. request.accept(coapClientRequestBuilder);
  8. Request coapRequest = coapClientRequestBuilder.getRequest();
  9. // Add CoAP request callback
  10. MessageObserver obs = new AsyncRequestObserver<T>(coapRequest, responseCallback, errorCallback, timeout) {
  11. @Override
  12. public T buildResponse(Response coapResponse) {
  13. // Build LwM2m response
  14. LwM2mClientResponseBuilder<T> lwm2mResponseBuilder = new LwM2mClientResponseBuilder<>(coapResponse);
  15. request.accept(lwm2mResponseBuilder);
  16. return lwm2mResponseBuilder.getResponse();
  17. }
  18. };
  19. coapRequest.addMessageObserver(obs);
  20. // Send CoAP request asynchronously
  21. endpointsManager.getEndpoint(null).sendRequest(coapRequest);
  22. }
  23. }

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

  1. @Override
  2. public <T extends LwM2mResponse> void send(final Client destination, final DownlinkRequest<T> request,
  3. final ResponseConsumer<T> responseCallback, final ExceptionConsumer errorCallback) {
  4. // Create the CoAP request from LwM2m request
  5. final CoapRequestBuilder CoapRequestBuilder = new CoapRequestBuilder(destination);
  6. request.accept(CoapRequestBuilder);
  7. final Request coapRequest = CoapRequestBuilder.getRequest();
  8. // Add CoAP request callback
  9. coapRequest.addMessageObserver(new AsyncRequestObserver<T>(coapRequest, destination, responseCallback,
  10. errorCallback) {
  11. @Override
  12. public T buildResponse(final Response coapResponse) {
  13. // Build LwM2m response
  14. final LwM2mResponseBuilder<T> lwm2mResponseBuilder = new LwM2mResponseBuilder<T>(coapRequest,
  15. coapResponse, client, observationRegistry);
  16. request.accept(lwm2mResponseBuilder);
  17. return lwm2mResponseBuilder.getResponse();
  18. }
  19. });
  20. // Send CoAP request asynchronously
  21. final Endpoint endpoint = getEndpointForClient(destination);
  22. endpoint.sendRequest(coapRequest);
  23. }

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

  1. private CoapObserveRelation observeAndWait(Request request, CoapHandler handler) {
  2. if (request.getOptions().hasObserve()) {
  3. Endpoint outEndpoint = getEffectiveEndpoint(request);
  4. CoapObserveRelation relation = new CoapObserveRelation(request, outEndpoint);
  5. request.addMessageObserver(new ObserveMessageObserverImpl(handler, relation));
  6. CoapResponse response = synchronous(request, outEndpoint);
  7. if (response == null || !response.advanced().getOptions().hasObserve()) {
  8. relation.setCanceled(true);
  9. }
  10. return relation;
  11. } else {
  12. throw new IllegalArgumentException("please make sure that the request has observe option set.");
  13. }
  14. }

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

  1. @Override
  2. public <T extends LwM2mResponse> T send(InetSocketAddress serverAddress, boolean secure,
  3. final UplinkRequest<T> request, long timeout) throws InterruptedException {
  4. // Create the CoAP request from LwM2m request
  5. CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(serverAddress);
  6. request.accept(coapClientRequestBuilder);
  7. Request coapRequest = coapClientRequestBuilder.getRequest();
  8. // Send CoAP request synchronously
  9. SyncRequestObserver<T> syncMessageObserver = new SyncRequestObserver<T>(coapRequest, timeout) {
  10. @Override
  11. public T buildResponse(Response coapResponse) {
  12. // Build LwM2m response
  13. LwM2mClientResponseBuilder<T> lwm2mResponseBuilder = new LwM2mClientResponseBuilder<>(coapResponse);
  14. request.accept(lwm2mResponseBuilder);
  15. return lwm2mResponseBuilder.getResponse();
  16. }
  17. };
  18. coapRequest.addMessageObserver(syncMessageObserver);
  19. // Send CoAP request asynchronously
  20. endpointsManager.getEndpoint(null).sendRequest(coapRequest);
  21. // Wait for response, then return it
  22. return syncMessageObserver.waitForResponse();
  23. }

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

  1. @Test
  2. public void testRequestForOversizedBodyGetsCanceled() throws InterruptedException {
  3. final CountDownLatch latch = new CountDownLatch(1);
  4. Request req = Request.newGet().setURI(getUri(serverEndpoint, RESOURCE_BIG));
  5. req.addMessageObserver(new MessageObserverAdapter() {
  6. @Override
  7. public void onCancel() {
  8. latch.countDown();
  9. }
  10. });
  11. clientEndpoint.sendRequest(req);
  12. assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
  13. }

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

  1. @Override
  2. public void sendCoapRequest(final Registration destination, final Request coapRequest, long timeout,
  3. CoapResponseCallback responseCallback, ErrorCallback errorCallback) {
  4. // Define destination
  5. EndpointContext context = EndpointContextUtil.extractContext(destination.getIdentity());
  6. coapRequest.setDestinationContext(context);
  7. // Add CoAP request callback
  8. MessageObserver obs = new CoapAsyncRequestObserver(coapRequest, responseCallback, errorCallback, timeout);
  9. coapRequest.addMessageObserver(obs);
  10. // Store pending request to cancel it on de-registration
  11. addPendingRequest(destination.getId(), coapRequest);
  12. // Send CoAP request asynchronously
  13. Endpoint endpoint = getEndpointForClient(destination);
  14. endpoint.sendRequest(coapRequest);
  15. }

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

  1. /**
  2. * Sends the specified observe request and waits for the response whereupon
  3. * the specified handler is invoked when a notification arrives.
  4. *
  5. * @param request the request
  6. *
  7. * @param handler the Response handler
  8. *
  9. * @return the CoAP observe relation
  10. * @throws IllegalArgumentException if the observe option is not set in the
  11. * request
  12. */
  13. public CoapObserveRelation observeAndWait(Request request, CoapHandler handler) {
  14. if (request.getOptions().hasObserve()) {
  15. Endpoint outEndpoint = getEffectiveEndpoint(request);
  16. CoapObserveRelation relation = new CoapObserveRelation(request, outEndpoint);
  17. request.addMessageObserver(new ObserveMessageObserverImpl(handler, relation));
  18. CoapResponse response = synchronous(request, outEndpoint);
  19. if (response == null || !response.advanced().getOptions().hasObserve()) {
  20. relation.setCanceled(true);
  21. }
  22. return relation;
  23. } else {
  24. throw new IllegalArgumentException("please make sure that the request has observe option set.");
  25. }
  26. }

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

  1. @Override
  2. public Response sendCoapRequest(final Registration destination, final Request coapRequest, long timeout)
  3. throws InterruptedException {
  4. // Define destination
  5. EndpointContext context = EndpointContextUtil.extractContext(destination.getIdentity());
  6. coapRequest.setDestinationContext(context);
  7. // Send CoAP request synchronously
  8. CoapSyncRequestObserver syncMessageObserver = new CoapSyncRequestObserver(coapRequest, timeout);
  9. coapRequest.addMessageObserver(syncMessageObserver);
  10. // Store pending request to cancel it on de-registration
  11. addPendingRequest(destination.getId(), coapRequest);
  12. // Send CoAP request asynchronously
  13. Endpoint endpoint = getEndpointForClient(destination);
  14. endpoint.sendRequest(coapRequest);
  15. // Wait for response, then return it
  16. return syncMessageObserver.waitForCoapResponse();
  17. }

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

  1. /**
  2. * Verifies that a request for a resource with a body exceeding the max buffer size is
  3. * cancelled when the first response block is received.
  4. */
  5. @Test
  6. public void testReceiveResponseCancelsRequestForExcessiveResponseBody() {
  7. NetworkConfig config = NetworkConfig.createStandardWithoutFile()
  8. .setInt(Keys.MAX_MESSAGE_SIZE, 128)
  9. .setInt(Keys.MAX_RESOURCE_BODY_SIZE, 200);
  10. MessageObserver requestObserver = mock(MessageObserver.class);
  11. BlockwiseLayer blockwiseLayer = new BlockwiseLayer(config);
  12. Request req = Request.newGet();
  13. req.setURI("coap://127.0.0.1/bigResource");
  14. req.addMessageObserver(requestObserver);
  15. Response response = Response.createResponse(req, ResponseCode.CONTENT);
  16. response.getOptions().setSize2(256).setBlock2(BlockOption.size2Szx(64), true, 0);
  17. Exchange exchange = new Exchange(null, Origin.LOCAL);
  18. exchange.setRequest(req);
  19. blockwiseLayer.receiveResponse(exchange, response);
  20. verify(requestObserver).onCancel();
  21. }

相关文章