/** * Wait for the response. This function blocks until there is a response or * the request has been canceled. * * @return the response * @throws InterruptedException * the interrupted exception */ public Response waitForResponse() throws InterruptedException { return waitForResponse(0); }
/** * Wait for the response. This function blocks until there is a response or * the request has been canceled. * * @return the response * @throws InterruptedException * the interrupted exception */ public Response waitForResponse() throws InterruptedException { return waitForResponse(0); }
public static void selfTest() { try { Request request = Request.newGet(); request.setURI("localhost:5683/benchmark"); request.send(); Response response = request.waitForResponse(1000); System.out.println("received "+response); } catch (Exception e) { e.printStackTrace(); } } }
private CoapResponse synchronous(Request request, Endpoint outEndpoint) { try { Response response = send(request, outEndpoint).waitForResponse(getTimeout()); if (response == null) return null; else return new CoapResponse(response); } catch (InterruptedException e) { throw new RuntimeException(e); } }
private CoapResponse synchronous(Request request, Endpoint outEndpoint) { try { Response response = send(request, outEndpoint).waitForResponse(getTimeout()); if (response == null) { // Cancel request so appropriate clean up can happen. request.cancel(); return null; } else { return new CoapResponse(response); } } catch (InterruptedException e) { throw new RuntimeException(e); } }
private static boolean ping(String address) { try { Request request = new Request(null); request.setType(Type.CON); request.setToken(new byte[0]); request.setURI(address); System.out.println("++++++ Sending Ping ++++++"); request.send().waitForResponse(5000); return request.isRejected(); } catch (Exception e) { e.printStackTrace(); return false; } }
Response incomingResponse = outgoingRequest.waitForResponse(timeout);
/** * Performs a CoAP ping and gives up after the given number of milliseconds. * * @param timeout the time to wait for a pong in ms * @return success of the ping */ public boolean ping(long timeout) { try { Request request = new Request(null, Type.CON); request.setToken(new byte[0]); request.setURI(uri); send(request).waitForResponse(timeout); return request.isRejected(); } catch (InterruptedException e) { // waiting was interrupted, which is fine } return false; }
/** * Performs a CoAP ping and gives up after the given number of milliseconds. * * @param timeout the time to wait for a pong in ms * @return success of the ping */ public boolean ping(long timeout) { try { Request request = new Request(null, Type.CON); request.setToken(new byte[0]); request.setURI(uri); send(request).waitForResponse(timeout); return request.isRejected(); } catch (InterruptedException e) { // waiting was interrupted, which is fine } return false; }
private void sendRequestAndExpect(String expected) throws Exception { System.out.println(); Thread.sleep(100); Request request = Request.newGet(); request.setURI("localhost:"+serverPort+"/res"); String response = request.send().waitForResponse(1000).getPayloadString(); Assert.assertEquals(expected, response); }
/** * Verifies that the server cleans up all exchanges after serving a NON GET. * * @throws Exception if the test fails. */ @Test public void testSimpleNONGet() throws Exception { String uri = uriOf(URI); LOGGER.log(Level.FINE, "Test simple NON GET to {0}", uri); String currentResponseText = "simple NON GET"; resource.setResponse(currentResponseText, Mode.PIGGY_BACKED_RESPONSE); Request request = Request.newGet(); request.setURI(uri); request.setType(Type.NON); Response response = request.send(clientEndpoint).waitForResponse(ACK_TIMEOUT); assertThat("Client did not receive response to NON request in time", response, is(notNullValue())); LOGGER.log(Level.FINE, "Client received response [{0}] with msg type [{1}]", new Object[]{response.getPayloadString(), response.getType()}); assertThat(response.getPayloadString(), is(currentResponseText)); assertThat(response.getType(), is(Type.NON)); }
@Test public void testNonconfirmable() throws Exception { createSimpleServer(); // send request Request request = new Request(CoAP.Code.POST); request.setConfirmable(false); request.setDestination(InetAddress.getLoopbackAddress()); request.setDestinationPort(serverPort); request.setPayload("client says hi"); request.send(); System.out.println("client sent request"); // receive response and check Response response = request.waitForResponse(1000); assertNotNull("Client received no response", response); System.out.println("client received response"); assertEquals(response.getPayloadString(), SERVER_RESPONSE); }
String resp1 = Request.newGet().setURI(base+NAME_1).send().waitForResponse(1000).getPayloadString(); Assert.assertEquals(PAYLOAD, resp1); String resp2 = Request.newGet().setURI(base+NAME_1+"/"+CHILD).send().waitForResponse(1000).getPayloadString(); Assert.assertEquals(CHILD_PAYLOAD, resp2); String resp3 = Request.newGet().setURI(base+NAME_2).send().waitForResponse(1000).getPayloadString(); Assert.assertEquals(PAYLOAD, resp3); String resp4 = Request.newGet().setURI(base+NAME_2+"/"+CHILD).send().waitForResponse(1000).getPayloadString(); Assert.assertEquals(CHILD_PAYLOAD, resp4); ResponseCode code1 = Request.newGet().setURI(base+NAME_1).send().waitForResponse(1000).getCode(); Assert.assertEquals(ResponseCode.NOT_FOUND, code1); ResponseCode code2 = Request.newGet().setURI(base+NAME_1+"/"+CHILD).send().waitForResponse(1000).getCode(); Assert.assertEquals(ResponseCode.NOT_FOUND, code2);
private void executePOSTRequest(final boolean shortRequest, final boolean respondShort) throws Exception { String payload = "--no payload--"; try { interceptor.clear(); Request request = Request.newPost().setURI(getUri(serverEndpoint, RESOURCE_TEST)); if (shortRequest) { request.setPayload(SHORT_POST_REQUEST); request.getOptions().addUriQuery(PARAM_SHORT_REQ); } else { request.setPayload(LONG_POST_REQUEST); } if (respondShort) { request.getOptions().addUriQuery(PARAM_SHORT_RESP); } clientEndpoint.sendRequest(request); // receive response and check Response response = request.waitForResponse(2000); assertNotNull("Client received no response", response); payload = response.getPayloadString(); if (respondShort) { assertEquals(SHORT_POST_RESPONSE, payload); } else { assertEquals(LONG_POST_RESPONSE, payload); } } finally { Thread.sleep(100); // Quickly wait until last ACKs arrive System.out.println("Client received payload [" + payload + "]" + System.lineSeparator() + interceptor.toString() + System.lineSeparator()); } }
@Test public void testConfirmable() throws Exception { // send request Request req2acc = new Request(Code.POST); req2acc.setConfirmable(true); req2acc.setURI(getUri(ACC_RESOURCE)); req2acc.setPayload("client says hi"); req2acc.send(); // receive response and check Response response = req2acc.waitForResponse(1000); assertPayloadIsOfCorrectType(response, SERVER_RESPONSE, Type.CON); Request req2noacc = new Request(Code.POST); req2noacc.setConfirmable(true); req2noacc.setURI(getUri(NO_ACC_RESOURCE)); req2noacc.setPayload("client says hi"); req2noacc.send(); // receive response and check response = req2noacc.waitForResponse(1000); assertPayloadIsOfCorrectType(response, SERVER_RESPONSE, Type.ACK); }
@Test public void testNonConfirmable() throws Exception { // send request Request req2acc = new Request(Code.POST); req2acc.setConfirmable(false); req2acc.setURI(getUri(ACC_RESOURCE)); req2acc.setPayload("client says hi"); req2acc.send(); // receive response and check Response response = req2acc.waitForResponse(1000); assertPayloadIsOfCorrectType(response, SERVER_RESPONSE, Type.NON); Request req2noacc = new Request(Code.POST); req2noacc.setConfirmable(false); req2noacc.setURI(getUri(NO_ACC_RESOURCE)); req2noacc.setPayload("client says hi"); req2noacc.send(); // receive response and check response = req2noacc.waitForResponse(1000); assertPayloadIsOfCorrectType(response, SERVER_RESPONSE, Type.NON); }
request.getOptions().setBlock2(szx, false, num); Response response = request.send().waitForResponse(1000); Assert.assertNotNull("Client received no response", response); Assert.assertEquals(expectations[i], response.getPayloadString());
server.sendResponse(ACK, CONTENT).loadBoth("C").block2(2, false, 128).payload(respPayload.substring(256, 300)).go(); Response response = request.waitForResponse(1000); assertResponseContainsExpectedPayload(response, respPayload);
server.expectEmpty(ACK, mid).go(); Response response = request.waitForResponse(1000); printServerLog(clientInterceptor); server.expectEmpty(ACK, mid).go(); response = request.waitForResponse(1000); printServerLog(clientInterceptor);
server.sendResponse(ACK, CONTENT).loadBoth("B").payload("possible conflict").go(); Response response = request.waitForResponse(500); assertResponseContainsExpectedPayload(response, CONTENT, payload); response = request.waitForResponse(500); assertNull("Client received duplicate", response);