@Override public <T extends LwM2mResponse> T send(final UplinkRequest<T> request, Long timeout) { // Create the CoAP request from LwM2m request final CoapClientRequestBuilder coapClientRequestBuilder = new CoapClientRequestBuilder(serverAddress, client); request.accept(coapClientRequestBuilder); // TODO manage invalid parameters // if (!coapClientRequestBuilder.areParametersValid()) { // return OperationResponse.failure(ResponseCode.INTERNAL_SERVER_ERROR, // "Request has invalid parameters. Not sending."); // } final Request coapRequest = coapClientRequestBuilder.getRequest(); // Send CoAP request synchronously final SyncRequestObserver<T> syncMessageObserver = new SyncRequestObserver<T>(coapRequest, timeout) { @Override public T buildResponse(final Response coapResponse) { // Build LwM2m response final LwM2mClientResponseBuilder<T> lwm2mResponseBuilder = new LwM2mClientResponseBuilder<T>(coapRequest, coapResponse); request.accept(lwm2mResponseBuilder); return lwm2mResponseBuilder.getResponse(); } }; coapRequest.addMessageObserver(syncMessageObserver); // Send CoAP request asynchronously clientEndpoint.sendRequest(coapRequest); // Wait for response, then return it return syncMessageObserver.waitForResponse(); }
@Override public <T extends LwM2mResponse> void send( final UplinkRequest<T> request, final ResponseCallback<T> responseCallback, final ErrorCallback errorCallback) { // Create the CoAP request from LwM2m request final CoapClientRequestBuilder coapClientRequestBuilder = new CoapClientRequestBuilder(serverAddress, client); request.accept(coapClientRequestBuilder); // TODO manage invalid parameters // if (!coapClientRequestBuilder.areParametersValid()) { // responseCallback.onFailure(OperationResponse.failure(ResponseCode.INTERNAL_SERVER_ERROR, // "Request has invalid parameters. Not sending.")); // return; // } final Request coapRequest = coapClientRequestBuilder.getRequest(); // Add CoAP request callback coapRequest.addMessageObserver( new AsyncRequestObserver<T>(coapRequest, responseCallback, errorCallback) { @Override public T buildResponse(final Response coapResponse) { // Build LwM2m response final LwM2mClientResponseBuilder<T> lwm2mResponseBuilder = new LwM2mClientResponseBuilder<T>(coapRequest, coapResponse); request.accept(lwm2mResponseBuilder); return lwm2mResponseBuilder.getResponse(); } }); // Send CoAP request asynchronously clientEndpoint.sendRequest(coapRequest); }
public void updateStatistics(Request request, boolean cachedResponse) { URI proxyUri = null; try { proxyUri = new URI(request.getOptions().getProxyURI()); } catch (URISyntaxException e) { LOGGER.warning(String.format("Proxy-uri malformed: %s", request.getOptions().getProxyURI())); } if (proxyUri == null) { // throw new IllegalArgumentException("proxyUri == null"); return; } // manage the address requester String addressString = proxyUri.getHost(); if (addressString != null) { // manage the resource requested String resourceString = proxyUri.getPath(); if (resourceString != null) { // check if there is already an entry for the row/column // association StatHelper statHelper = statsTable.get(addressString, resourceString); if (statHelper == null) { // create a new stat if it not present statHelper = new StatHelper(); // add the new element to the table statsTable.put(addressString, resourceString, statHelper); } // increment the count of the requests statHelper.increment(cachedResponse); } } }
public CO06(String serverURI) { super(CO06.class.getSimpleName()); // create the request Request request = new Request(Code.GET, Type.CON); // set Observe option request.setObserve(); // set the parameters and execute the request executeRequest(request, serverURI, RESOURCE_URI); }
/** * When we receive a duplicate of a request, we stop it here and do not forward it to the upper * layer. If the server has already sent a response, we send it again. If the request has only * been acknowledged (but the ACK has gone lost or not reached the client yet), we resent the ACK. * If the request has neither been responded, acknowledged or rejected yet, the server has not yet * decided what to do with the request and we cannot do anything. */ @Override public void receiveRequest(Exchange exchange, Request request) { if (request.isDuplicate()) { // Request is a duplicate, so resend ACK, RST or response if (exchange.getCurrentResponse() != null) { LOGGER.fine("Respond with the current response to the duplicate request"); // Do not restart retransmission cycle super.sendResponse(exchange, exchange.getCurrentResponse()); } else if (exchange.getCurrentRequest().isAcknowledged()) { LOGGER.fine( "The duplicate request was acknowledged but no response computed yet. Retransmit ACK"); EmptyMessage ack = EmptyMessage.newACK(request); sendEmptyMessage(exchange, ack); } else if (exchange.getCurrentRequest().isRejected()) { LOGGER.fine("The duplicate request was rejected. Reject again"); EmptyMessage rst = EmptyMessage.newRST(request); sendEmptyMessage(exchange, rst); } else { LOGGER.fine( "The server has not yet decided what to do with the request. We ignore the duplicate."); // The server has not yet decided, whether to acknowledge or // reject the request. We know for sure that the server has // received the request though and can drop this duplicate here. } } else { // Request is not a duplicate exchange.setCurrentRequest(request); super.receiveRequest(exchange, request); } }
/** Schedules a retransmission for confirmable messages. */ @Override public void sendRequest(final Exchange exchange, final Request request) { LOGGER.finer("Send request, failed transmissions: " + exchange.getFailedTransmissionCount()); if (request.getType() == null) request.setType(Type.CON); if (request.getType() == Type.CON) { prepareRetransmission( exchange, new RetransmissionTask(exchange, request) { public void retransmit() { sendRequest(exchange, request); } }); } super.sendRequest(exchange, request); }
@Override public void sendRequest(Exchange exchange, Request request) { if (request.getDestination() == null) throw new NullPointerException("Request has no destination address"); if (request.getDestinationPort() == 0) throw new NullPointerException("Request has no destination port"); matcher.sendRequest(exchange, request); /* * Logging here causes significant performance loss. * If necessary, add an interceptor that logs the messages, * e.g., the MessageTracer. */ for (MessageInterceptor interceptor : interceptors) interceptor.sendRequest(request); // MessageInterceptor might have canceled if (!request.isCanceled()) connector.send(serializer.serialize(request)); }
protected boolean checkResponse(Request request, Response response) { boolean success = true; success &= checkType(Type.CON, response.getType()); success &= checkInt(EXPECTED_RESPONSE_CODE.value, response.getCode().value, "code"); success &= checkToken(request.getToken(), response.getToken()); success &= hasContentType(response); success &= hasNonEmptyPalyoad(response); success &= hasObserve(response); return success; }
/* * The endpoint's executor executes this method to convert the raw bytes * into a message, look for an associated exchange and forward it to * the stack of layers. */ private void receiveMessage(RawData raw) { DataParser parser = new DataParser(raw.getBytes()); if (parser.isRequest()) { // This is a request Request request; try { request = parser.parseRequest(); } catch (IllegalStateException e) { StringBuffer log = new StringBuffer("message format error caused by ") .append(raw.getInetSocketAddress()); if (!parser.isReply()) { // manually build RST from raw information EmptyMessage rst = new EmptyMessage(Type.RST); rst.setDestination(raw.getAddress()); rst.setDestinationPort(raw.getPort()); rst.setMID(parser.getMID()); for (MessageInterceptor interceptor : interceptors) interceptor.sendEmptyMessage(rst); connector.send(serializer.serialize(rst)); log.append(" and reset"); } if (LOGGER.isLoggable(Level.INFO)) { LOGGER.info(log.toString()); } return; } request.setSource(raw.getAddress()); request.setSourcePort(raw.getPort()); request.setSenderIdentity(raw.getSenderIdentity()); /* * Logging here causes significant performance loss. * If necessary, add an interceptor that logs the messages, * e.g., the MessageTracer. */ for (MessageInterceptor interceptor : interceptors) interceptor.receiveRequest(request); // MessageInterceptor might have canceled if (!request.isCanceled()) { Exchange exchange = matcher.receiveRequest(request); if (exchange != null) { exchange.setEndpoint(CoAPEndpoint.this); coapstack.receiveRequest(exchange, request); } } } else if (parser.isResponse()) { // This is a response Response response = parser.parseResponse(); response.setSource(raw.getAddress()); response.setSourcePort(raw.getPort()); /* * Logging here causes significant performance loss. * If necessary, add an interceptor that logs the messages, * e.g., the MessageTracer. */ for (MessageInterceptor interceptor : interceptors) interceptor.receiveResponse(response); // MessageInterceptor might have canceled if (!response.isCanceled()) { Exchange exchange = matcher.receiveResponse(response); if (exchange != null) { exchange.setEndpoint(CoAPEndpoint.this); response.setRTT(System.currentTimeMillis() - exchange.getTimestamp()); coapstack.receiveResponse(exchange, response); } else if (response.getType() != Type.ACK) { LOGGER.fine("Rejecting unmatchable response from " + raw.getInetSocketAddress()); reject(response); } } } else if (parser.isEmpty()) { // This is an empty message EmptyMessage message = parser.parseEmptyMessage(); message.setSource(raw.getAddress()); message.setSourcePort(raw.getPort()); /* * Logging here causes significant performance loss. * If necessary, add an interceptor that logs the messages, * e.g., the MessageTracer. */ for (MessageInterceptor interceptor : interceptors) interceptor.receiveEmptyMessage(message); // MessageInterceptor might have canceled if (!message.isCanceled()) { // CoAP Ping if (message.getType() == Type.CON || message.getType() == Type.NON) { LOGGER.info("Responding to ping by " + raw.getInetSocketAddress()); reject(message); } else { Exchange exchange = matcher.receiveEmptyMessage(message); if (exchange != null) { exchange.setEndpoint(CoAPEndpoint.this); coapstack.receiveEmptyMessage(exchange, message); } } } } else { LOGGER.finest("Silently ignoring non-CoAP message from " + raw.getInetSocketAddress()); } }
@Override protected synchronized void executeRequest( Request request, String serverURI, String resourceUri) { // defensive check for slash if (!serverURI.endsWith("/") && !resourceUri.startsWith("/")) { resourceUri = "/" + resourceUri; } URI uri = null; try { uri = new URI(serverURI + resourceUri); } catch (URISyntaxException use) { throw new IllegalArgumentException("Invalid URI: " + use.getMessage()); } request.setURI(uri); // for observing int observeLoop = 2; // print request info if (verbose) { System.out.println("Request for test " + this.testName + " sent"); Utils.prettyPrint(request); } // execute the request try { Response response = null; boolean success = true; long time = 5000; request.send(); System.out.println(); System.out.println("**** TEST: " + testName + " ****"); 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); success &= hasObserve(response); if (success) { time = response.getOptions().getMaxAge() * 1000; System.out.println("+++++ Max-Age: " + time + " +++++"); if (time == 0) time = 5000; for (int l = 0; success && (l < observeLoop); ++l) { response = request.waitForResponse(time + 1000); // checking the response if (response != null) { System.out.println("Received notification " + l); // print response info if (verbose) { System.out.println("Response received"); System.out.println("Time elapsed (ms): " + response.getRTT()); Utils.prettyPrint(response); } success &= checkResponse(request, response); } else { System.out.println("FAIL: Notifications stopped"); success = false; break; } // response != null } // observeLoop if (response != null) { System.out.println("+++++++ Canceling +++++++"); request.cancel(); // stack should send RST Thread.sleep(time + time / 2); } else { System.out.println("FAIL: Notifications stopped"); success = false; } } } else { System.out.println("FAIL: No notification after registration"); success = false; } if (success) { System.out.println("**** TEST PASSED ****"); addSummaryEntry(testName + ": PASSED (conditionally)"); } else { System.out.println("**** TEST FAILED ****"); addSummaryEntry(testName + ": --FAILED--"); } tickOffTest(); } catch (InterruptedException e) { System.err.println("Interupted during receive: " + e.getMessage()); System.exit(-1); } }
@Test public void testNonConfirmable() throws Exception { // send request Request req2acc = new Request(Code.POST); req2acc.setConfirmable(false); req2acc.setURI("localhost:" + serverPort + "/" + ACC_RESOURCE); req2acc.setPayload("client says hi"); req2acc.send(); // receive response and check Response response = req2acc.waitForResponse(100); assertNotNull(response); assertEquals(response.getPayloadString(), SERVER_RESPONSE); assertEquals(response.getType(), Type.NON); Request req2noacc = new Request(Code.POST); req2noacc.setConfirmable(false); req2noacc.setURI("coap://localhost:" + serverPort + "/" + NO_ACC_RESOURCE); req2noacc.setPayload("client says hi"); req2noacc.send(); // receive response and check response = req2noacc.waitForResponse(100); assertNotNull(response); assertEquals(response.getPayloadString(), SERVER_RESPONSE); assertEquals(response.getType(), Type.NON); }