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

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

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

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

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

@Test
  public void testMultipleAddsToMessageObservers() {
    Request ping = new Request(null, Type.CON);
    List<MessageObserver> observers = new ArrayList<>();
    observers.add(new MessageObserverAdapter() {});
    observers.add(new MessageObserverAdapter() {});
    ping.addMessageObservers(observers);
    ping.addMessageObserver(new MessageObserverAdapter() {});
    ping.addMessageObserver(new MessageObserverAdapter() {});
    observers.add(new MessageObserverAdapter() {});
    observers.add(new MessageObserverAdapter() {});
    ping.addMessageObservers(observers);
    ping.addMessageObserver(new MessageObserverAdapter() {});
    assertThat(ping.getMessageObservers().size(), is(9));
  }
}

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

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

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

/**
 * Adds a message observer to the request to be sent which
 * completes the exchange if the request gets canceled.
 * 
 * @param exchange The (locally originating) exchange that the request is part of.
 * @param request The outbound request.
 */
@Override
public void sendRequest(final Exchange exchange, final Request request) {
  request.addMessageObserver(new CancelledMessageObserver(exchange));
  lower().sendRequest(exchange, request);
}

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

@Test
public void testMultipleAddMessageObserver() {
  Request ping = new Request(null, Type.CON);
  ping.addMessageObserver(new MessageObserverAdapter() {});
  ping.addMessageObserver(new MessageObserverAdapter() {});
  ping.addMessageObserver(new MessageObserverAdapter() {});
  assertThat(ping.getMessageObservers().size(), is(3));
}

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

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

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

private CoapObserveRelation observe(Request request, CoapHandler handler) {
  if (request.getOptions().hasObserve()) {
    Endpoint outEndpoint = getEffectiveEndpoint(request);
    CoapObserveRelation relation = new CoapObserveRelation(request, outEndpoint);
    request.addMessageObserver(new ObserveMessageObserverImpl(handler, relation));
    send(request, outEndpoint);
    return relation;
  } else {
    throw new IllegalArgumentException("please make sure that the request has observe option set.");
  }
}

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

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

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

/**
 * Sends the specified observe request and invokes the specified handler
 * each time a notification arrives.
 *
 * @param request the request
 * 
 * @param handler the Response handler
 * 
 * @return the CoAP observe relation
 * @throws IllegalArgumentException if the observe option is not set in the
 *             request
 */
public CoapObserveRelation observe(Request request, CoapHandler handler) {
  if (request.getOptions().hasObserve()) {
    Endpoint outEndpoint = getEffectiveEndpoint(request);
    CoapObserveRelation relation = new CoapObserveRelation(request, outEndpoint);
    request.addMessageObserver(new ObserveMessageObserverImpl(handler, relation));
    send(request, outEndpoint);
    return relation;
  } else {
    throw new IllegalArgumentException("please make sure that the request has observe option set.");
  }
}

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

@Override
  public <T extends LwM2mResponse> void send(InetSocketAddress serverAddress, boolean secure,
      final UplinkRequest<T> request, long timeout, ResponseCallback<T> responseCallback,
      ErrorCallback errorCallback) {
    // Create the CoAP request from LwM2m request
    CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(serverAddress);
    request.accept(coapClientRequestBuilder);
    Request coapRequest = coapClientRequestBuilder.getRequest();

    // Add CoAP request callback
    MessageObserver obs = new AsyncRequestObserver<T>(coapRequest, responseCallback, errorCallback, timeout) {
      @Override
      public T buildResponse(Response coapResponse) {
        // Build LwM2m response
        LwM2mClientResponseBuilder<T> lwm2mResponseBuilder = new LwM2mClientResponseBuilder<>(coapResponse);
        request.accept(lwm2mResponseBuilder);
        return lwm2mResponseBuilder.getResponse();
      }
    };
    coapRequest.addMessageObserver(obs);

    // Send CoAP request asynchronously
    endpointsManager.getEndpoint(null).sendRequest(coapRequest);
  }
}

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

@Override
public <T extends LwM2mResponse> void send(final Client destination, final DownlinkRequest<T> request,
    final ResponseConsumer<T> responseCallback, final ExceptionConsumer errorCallback) {
  // Create the CoAP request from LwM2m request
  final CoapRequestBuilder CoapRequestBuilder = new CoapRequestBuilder(destination);
  request.accept(CoapRequestBuilder);
  final Request coapRequest = CoapRequestBuilder.getRequest();
  // Add CoAP request callback
  coapRequest.addMessageObserver(new AsyncRequestObserver<T>(coapRequest, destination, responseCallback,
      errorCallback) {
    @Override
    public T buildResponse(final Response coapResponse) {
      // Build LwM2m response
      final LwM2mResponseBuilder<T> lwm2mResponseBuilder = new LwM2mResponseBuilder<T>(coapRequest,
          coapResponse, client, observationRegistry);
      request.accept(lwm2mResponseBuilder);
      return lwm2mResponseBuilder.getResponse();
    }
  });
  // Send CoAP request asynchronously
  final Endpoint endpoint = getEndpointForClient(destination);
  endpoint.sendRequest(coapRequest);
}

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

private CoapObserveRelation observeAndWait(Request request, CoapHandler handler) {
  if (request.getOptions().hasObserve()) {
    Endpoint outEndpoint = getEffectiveEndpoint(request);
    CoapObserveRelation relation = new CoapObserveRelation(request, outEndpoint);
    request.addMessageObserver(new ObserveMessageObserverImpl(handler, relation));
    CoapResponse response = synchronous(request, outEndpoint);
    if (response == null || !response.advanced().getOptions().hasObserve()) {
      relation.setCanceled(true);
    }
    return relation;
  } else {
    throw new IllegalArgumentException("please make sure that the request has observe option set.");
  }
}

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

@Override
public <T extends LwM2mResponse> T send(InetSocketAddress serverAddress, boolean secure,
    final UplinkRequest<T> request, long timeout) throws InterruptedException {
  // Create the CoAP request from LwM2m request
  CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(serverAddress);
  request.accept(coapClientRequestBuilder);
  Request coapRequest = coapClientRequestBuilder.getRequest();
  // Send CoAP request synchronously
  SyncRequestObserver<T> syncMessageObserver = new SyncRequestObserver<T>(coapRequest, timeout) {
    @Override
    public T buildResponse(Response coapResponse) {
      // Build LwM2m response
      LwM2mClientResponseBuilder<T> lwm2mResponseBuilder = new LwM2mClientResponseBuilder<>(coapResponse);
      request.accept(lwm2mResponseBuilder);
      return lwm2mResponseBuilder.getResponse();
    }
  };
  coapRequest.addMessageObserver(syncMessageObserver);
  // Send CoAP request asynchronously
  endpointsManager.getEndpoint(null).sendRequest(coapRequest);
  // Wait for response, then return it
  return syncMessageObserver.waitForResponse();
}

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

@Test
public void testRequestForOversizedBodyGetsCanceled() throws InterruptedException {
  final CountDownLatch latch = new CountDownLatch(1);
  Request req = Request.newGet().setURI(getUri(serverEndpoint, RESOURCE_BIG));
  req.addMessageObserver(new MessageObserverAdapter() {
    @Override
    public void onCancel() {
      latch.countDown();
    }
  });
  clientEndpoint.sendRequest(req);
  assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
}

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

@Override
public void sendCoapRequest(final Registration destination, final Request coapRequest, long timeout,
    CoapResponseCallback responseCallback, ErrorCallback errorCallback) {
  // Define destination
  EndpointContext context = EndpointContextUtil.extractContext(destination.getIdentity());
  coapRequest.setDestinationContext(context);
  // Add CoAP request callback
  MessageObserver obs = new CoapAsyncRequestObserver(coapRequest, responseCallback, errorCallback, timeout);
  coapRequest.addMessageObserver(obs);
  // Store pending request to cancel it on de-registration
  addPendingRequest(destination.getId(), coapRequest);
  // Send CoAP request asynchronously
  Endpoint endpoint = getEndpointForClient(destination);
  endpoint.sendRequest(coapRequest);
}

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

/**
 * Sends the specified observe request and waits for the response whereupon
 * the specified handler is invoked when a notification arrives.
 *
 * @param request the request
 * 
 * @param handler the Response handler
 * 
 * @return the CoAP observe relation
 * @throws IllegalArgumentException if the observe option is not set in the
 *             request
 */
public CoapObserveRelation observeAndWait(Request request, CoapHandler handler) {
  if (request.getOptions().hasObserve()) {
    Endpoint outEndpoint = getEffectiveEndpoint(request);
    CoapObserveRelation relation = new CoapObserveRelation(request, outEndpoint);
    request.addMessageObserver(new ObserveMessageObserverImpl(handler, relation));
    CoapResponse response = synchronous(request, outEndpoint);
    if (response == null || !response.advanced().getOptions().hasObserve()) {
      relation.setCanceled(true);
    }
    return relation;
  } else {
    throw new IllegalArgumentException("please make sure that the request has observe option set.");
  }
}

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

@Override
public Response sendCoapRequest(final Registration destination, final Request coapRequest, long timeout)
    throws InterruptedException {
  // Define destination
  EndpointContext context = EndpointContextUtil.extractContext(destination.getIdentity());
  coapRequest.setDestinationContext(context);
  // Send CoAP request synchronously
  CoapSyncRequestObserver syncMessageObserver = new CoapSyncRequestObserver(coapRequest, timeout);
  coapRequest.addMessageObserver(syncMessageObserver);
  // Store pending request to cancel it on de-registration
  addPendingRequest(destination.getId(), coapRequest);
  // Send CoAP request asynchronously
  Endpoint endpoint = getEndpointForClient(destination);
  endpoint.sendRequest(coapRequest);
  // Wait for response, then return it
  return syncMessageObserver.waitForCoapResponse();
}

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

/**
 * Verifies that a request for a resource with a body exceeding the max buffer size is
 * cancelled when the first response block is received.
 */
@Test
public void testReceiveResponseCancelsRequestForExcessiveResponseBody() {
  NetworkConfig config = NetworkConfig.createStandardWithoutFile()
      .setInt(Keys.MAX_MESSAGE_SIZE, 128)
      .setInt(Keys.MAX_RESOURCE_BODY_SIZE, 200);
  MessageObserver requestObserver = mock(MessageObserver.class);
  BlockwiseLayer blockwiseLayer = new BlockwiseLayer(config);
  Request req = Request.newGet();
  req.setURI("coap://127.0.0.1/bigResource");
  req.addMessageObserver(requestObserver);
  Response response = Response.createResponse(req, ResponseCode.CONTENT);
  response.getOptions().setSize2(256).setBlock2(BlockOption.size2Szx(64), true, 0);
  Exchange exchange = new Exchange(null, Origin.LOCAL);
  exchange.setRequest(req);
  blockwiseLayer.receiveResponse(exchange, response);
  verify(requestObserver).onCancel();
}

相关文章