private void asynchronous(Request request, CoapHandler handler) { request.addMessageObserver(new MessageObserverImpl(handler)); send(request); }
@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)); } }
private void asynchronous(Request request, CoapHandler handler) { request.addMessageObserver(new MessageObserverImpl(handler)); send(request); }
/** * 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); }
@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)); }
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); }
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."); } }
@Test public void testAddMessageObserver() { Request ping = new Request(null, Type.CON); ping.addMessageObserver(new MessageObserverAdapter() {}); assertThat(ping.getMessageObservers().size(), is(1)); }
/** * 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."); } }
@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); } }
@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); }
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."); } }
@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(); }
@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)); }
@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); }
/** * 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."); } }
@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(); }
/** * 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); }
/** * 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); }
/** * 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(); }