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

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

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

Request.waitForResponse介绍

[英]Wait for the response. This function blocks until there is a response or the request has been canceled.
[中]等待回应。此功能会一直阻止,直到有响应或请求被取消。

代码示例

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

/**
 * 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);
}

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

/**
 * 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);
}

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

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();
    }
  }
}

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

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);
  }
}

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

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);
  }
}

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

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;
  }
}

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

Response incomingResponse = outgoingRequest.waitForResponse(timeout);

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

/**
 * 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;
}

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

/**
 * 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;
}

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

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);
}

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

/**
 * 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));
}

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

@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);
}

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

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);

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

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());
  }
}

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

@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);
}

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

@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);
}

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

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());

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

server.sendResponse(ACK, CONTENT).loadBoth("C").block2(2, false, 128).payload(respPayload.substring(256, 300)).go();
Response response = request.waitForResponse(1000);
assertResponseContainsExpectedPayload(response, respPayload);

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

server.expectEmpty(ACK, mid).go();
Response response = request.waitForResponse(1000);
printServerLog(clientInterceptor);
server.expectEmpty(ACK, mid).go();
response = request.waitForResponse(1000);
printServerLog(clientInterceptor);

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

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);

相关文章

微信公众号

最新文章

更多