org.eclipse.californium.core.coap.Response类的使用及代码示例

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

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

Response介绍

[英]Response represents a CoAP response to a CoAP request.

A response is either a piggy-backed response of type ACK or a separate response of type CON or NON. Each response carries a ( CoAP.ResponseCode) indicating the outcome of the request it is the response for.
[中]Response表示CoAP对CoAP请求的响应。
响应可以是ACK类型的背驮响应,也可以是CON或NON类型的单独响应。每个响应都带有一个(CoAP.ResponseCode),指示它作为响应的请求的结果。

代码示例

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

if (response.getOptions().hasObserve()) {
  block = new Response(response.getCode());
  block.setDestination(response.getDestination());
  block.setDestinationPort(response.getDestinationPort());
  block.setOptions(new OptionSet(response.getOptions()));
  block.addMessageObserver(new TimeoutForwarder(response));
int payloadsize = response.getPayloadSize();
int currentSize = 1 << (4 + szx);
int from = num * currentSize;
  int to = Math.min((num + 1) * currentSize, response.getPayloadSize());
  int length = to - from;
  byte[] blockPayload = new byte[length];
  boolean m = (to < response.getPayloadSize());
  block.getOptions().setBlock2(szx, m, num);
  System.arraycopy(response.getPayload(), from, blockPayload, 0, length);
  block.setPayload(blockPayload);
  block.setLast(!m && !response.getOptions().hasObserve());
  block.getOptions().setBlock2(szx, false, num);
  block.setLast(true);
  status.setComplete(true);

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

protected boolean checkResponse(Request request, Response response) {
    boolean success = true;

    success &= checkType(Type.ACK, response.getType());
    success &= checkInt(EXPECTED_RESPONSE_CODE.value, response.getCode().value, "code");
    success &= checkOption(MediaTypeRegistry.APPLICATION_LINK_FORMAT, response.getOptions().getContentFormat(), "Content-Format");
    success &= checkDiscoveryAttributes(EXPECTED_IF, response.getPayloadString());
    
    return success;
  }
}

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

public CoapMessage(Response request, boolean incoming) {
  this(incoming, request.getType(), request.getMID(), request.getTokenString(), request.getOptions(), request
      .getPayload());
  this.code = request.getCode().toString();
}

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

@Override
public String toString() {
  String payload = getPayloadTracingString();
  return String.format("%s-%-6s MID=%5d, Token=%s, OptionSet=%s, %s", getType(), getCode(), getMID(), getTokenString(), getOptions(), payload);
}

代码示例来源:origin: sitewhere/sitewhere

/**
 * Send an error response on the given exchange.
 * 
 * @param code
 * @param message
 * @param exchange
 */
protected void createAndSendResponse(ResponseCode code, String message, Exchange exchange) {
Response response = new Response(code);
response.setPayload(message);
exchange.sendResponse(response);
}

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

@Override
public void handleGET(CoapExchange exchange) {
  String payload = "Available commands:\n - GET: show statistics\n - POST write stats to file\n - DELETE: reset statistics\n\n";
  payload += getStatString();
  Response response = new Response(ResponseCode.CONTENT);
  response.setPayload(payload);
  response.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
  exchange.respond(response);
}

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

ResponseCode status = incomingResponse.getCode();
Response outgoingResponse = new Response(status);
byte[] payload = incomingResponse.getPayload();
outgoingResponse.setPayload(payload);
long timestamp = incomingResponse.getTimestamp();
outgoingResponse.setTimestamp(timestamp);
outgoingResponse.setOptions(new OptionSet(
    incomingResponse.getOptions()));

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

if (response.getType()!=Type.ACK) {
    LOGGER.finer("Rejecting blockwise transfer for canceled Exchange");
    EmptyMessage rst = EmptyMessage.newRST(response);
if (!response.getOptions().hasBlock1() && !response.getOptions().hasBlock2()) {
if (response.getOptions().hasBlock1()) {
  BlockOption block1 = response.getOptions().getBlock1();
  LOGGER.log(Level.FINER, "Response acknowledges block {0}", block1);
    nextBlock.setToken(response.getToken());
  } else if (!response.getOptions().hasBlock2()) {
if (response.getOptions().hasBlock2()) {
  BlockOption block2 = response.getOptions().getBlock2();
  BlockwiseStatus status = findResponseBlockStatus(exchange, response);
  if (response.getOptions().hasObserve() && block2.getNum()==0 && status.getCurrentNum()!=0) {
    if (response.getOptions().getObserve()>status.getObserve()) {
  if (block2.getNum() == status.getCurrentNum() && (block2.getNum()==0 || Arrays.equals(response.getToken(), exchange.getCurrentRequest().getToken()))) {
    status.addBlock(response.getPayload());
    if (response.getOptions().hasObserve()) {

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

@Test public void testUTF8Encoding() {
    Response response = new Response(ResponseCode.CONTENT);
    response.setType(Type.NON);
    response.setMID(expectedMid);
    response.setToken(new byte[] {});
    response.getOptions().addLocationPath("ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ").addLocationPath("γλώσσα")
        .addLocationPath("пустынных").addLocationQuery("ვეპხის=யாமறிந்த").addLocationQuery("⠊⠀⠉⠁⠝=⠑⠁⠞⠀⠛⠇⠁⠎⠎");
    response.setPayload("⠊⠀⠉⠁⠝⠀⠑⠁⠞⠀⠛⠇⠁⠎⠎⠀⠁⠝⠙⠀⠊⠞⠀⠙⠕⠑⠎⠝⠞⠀⠓⠥⠗⠞⠀⠍⠑");

    RawData rawData = serializer.serializeResponse(response);

    Response result = (Response) parser.parseMessage(rawData);
    assertEquals("ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ/γλώσσα/пустынных", response.getOptions().getLocationPathString());
    assertEquals("ვეპხის=யாமறிந்த&⠊⠀⠉⠁⠝=⠑⠁⠞⠀⠛⠇⠁⠎⠎", response.getOptions().getLocationQueryString());
    assertEquals("⠊⠀⠉⠁⠝⠀⠑⠁⠞⠀⠛⠇⠁⠎⠎⠀⠁⠝⠙⠀⠊⠞⠀⠙⠕⠑⠎⠝⠞⠀⠓⠥⠗⠞⠀⠍⠑", result.getPayloadString());
    assertEquals(response.getMID(), result.getMID());
  }
}

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

/**
 * Formats a {@link Response} into a readable String representation. 
 * 
 * @param r the Response
 * @return the pretty print
 */
public static String prettyPrint(Response r) {
  StringBuilder sb = new StringBuilder();
  sb.append("==[ CoAP Response ]============================================").append(System.lineSeparator());
  sb.append(String.format("MID    : %d", r.getMID())).append(System.lineSeparator());
  sb.append(String.format("Token  : %s", r.getTokenString())).append(System.lineSeparator());
  sb.append(String.format("Type   : %s", r.getType().toString())).append(System.lineSeparator());
  sb.append(String.format("Status : %s", r.getCode().toString())).append(System.lineSeparator());
  sb.append(String.format("Options: %s", r.getOptions().toString())).append(System.lineSeparator());
  sb.append(String.format("Payload: %d Bytes", r.getPayloadSize())).append(System.lineSeparator());
  if (r.getPayloadSize() > 0 && MediaTypeRegistry.isPrintable(r.getOptions().getContentFormat())) {
    sb.append("---------------------------------------------------------------").append(System.lineSeparator());
    sb.append(r.getPayloadString());
    sb.append(System.lineSeparator());
  }
  sb.append("===============================================================");
  return sb.toString();
}

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

@Override
public void visit(RegisterRequest request) {
  if (coapResponse.isError()) {
    // handle error response:
    lwM2mresponse = new RegisterResponse(toLwM2mResponseCode(coapResponse.getCode()), null,
        coapResponse.getPayloadString());
  } else if (coapResponse.getCode() == org.eclipse.californium.core.coap.CoAP.ResponseCode.CREATED) {
    // handle success response:
    lwM2mresponse = RegisterResponse.success(coapResponse.getOptions().getLocationString());
  } else {
    // handle unexpected response:
    handleUnexpectedResponseCode(request, coapResponse);
  }
}

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

@Override
public void onResponse(Response coapResponse) {
  if (coapResponse.getCode() == CoAP.ResponseCode.CHANGED || coapResponse.getCode() == CoAP.ResponseCode.CONTENT) {
    try {
      LwM2mNode content = LwM2mNodeDecoder.decode(coapResponse.getPayload(),
          ContentFormat.fromCode(coapResponse.getOptions().getContentFormat()), path);
      ValueResponse response = new ValueResponse(ResponseCode.CHANGED, content);
      for (ObservationListener listener : listeners) {
        listener.newValue(this, response.getContent());
      }
    } catch (InvalidValueException e) {
      String msg = String.format("[%s] ([%s])", e.getMessage(), e.getPath().toString());
      LOG.debug(msg);
    }
  }
}

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

@Test public void testResponseParsing() {
  Response response = new Response(ResponseCode.CONTENT);
  response.setType(Type.NON);
  response.setMID(expectedMid);
  response.setToken(new byte[] { 22, -1, 0, 78, 100, 22 });
  response.getOptions().addETag(new byte[] { 1, 0, 0, 0, 0, 1 })
      .addLocationPath("/one/two/three/four/five/six/seven/eight/nine/ten")
      .addOption(new Option(57453, "Arbitrary".hashCode())).addOption(new Option(19205, "Arbitrary1"))
      .addOption(new Option(19205, "Arbitrary2")).addOption(new Option(19205, "Arbitrary3"));
  RawData rawData = serializer.serializeResponse(response);
  Response result = (Response) parser.parseMessage(rawData);
  assertEquals(response.getMID(), result.getMID());
  assertArrayEquals(response.getToken(), result.getToken());
  assertEquals(response.getOptions().asSortedList(), result.getOptions().asSortedList());
}

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

private Response responseFor(final Request request) {
    Response response = new Response(ResponseCode.CONTENT);
    response.setMID(request.getMID());
    response.setToken(request.getToken());
    response.setBytes(new byte[]{});
    response.setSource(request.getDestination());
    response.setSourcePort(request.getDestinationPort());
    response.setDestination(request.getSource());
    response.setDestinationPort(request.getSourcePort());
    return response;
  }
}

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

public void handleGET(CoapExchange exchange) {
  Response response = new Response(CONTENT);
  response.setType(respType); // respType is altered throughout the test cases
  response.setPayload(respPayload); // payload is altered throughout the test cases
  exchange.respond(response);
}

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

@Override
public void visit(BootstrapRequest request) {
  if (coapResponse.isError()) {
    // handle error response:
    lwM2mresponse = new BootstrapResponse(toLwM2mResponseCode(coapResponse.getCode()),
        coapResponse.getPayloadString());
  } else if (coapResponse.getCode() == org.eclipse.californium.core.coap.CoAP.ResponseCode.CHANGED) {
    // handle success response:
    lwM2mresponse = BootstrapResponse.success();
  } else {
    // handle unexpected response:
    handleUnexpectedResponseCode(request, coapResponse);
  }
}

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

protected final void appendResponseDetails(final Response response) {
  if (response.isCanceled()) {
    buffer.append("CANCELED ");
  }
  buffer.append(response.getType()).append(" [MID=").append(response.getMID())
    .append(", T=").append(response.getTokenString()).append("], ")
    .append(response.getCode());
  appendBlockOption(1, response.getOptions().getBlock1());
  appendBlockOption(2, response.getOptions().getBlock2());
  appendObserveOption(response.getOptions());
  appendSize1(response.getOptions());
  appendSize2(response.getOptions());
  appendEtags(response.getOptions());
}

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

@Override
public void handleRequest(Exchange exchange) {
  try {
    super.handleRequest(exchange);
  } catch (Exception e) {
    LOG.error("Exception while handling a request on the /rd resource", e);
    // unexpected error, we should sent something like a INTERNAL_SERVER_ERROR.
    // but it would not be LWM2M compliant. so BAD_REQUEST for now...
    exchange.sendResponse(new Response(ResponseCode.BAD_REQUEST));
  }
}

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

protected boolean checkResponse(Request request, Response response) {
    boolean success = true;

    success &= checkType(Type.ACK, response.getType());
    success &= checkInt(EXPECTED_RESPONSE_CODE.value,
        response.getCode().value, "code");
    success &= checkInt(request.getMID(), response.getMID(), "MID");

    return success;
  }
}

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

protected boolean checkResponse(Request request, Response response) {
    boolean success = true;

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

    return success;
  }
}

相关文章