当前位置: 首页>>代码示例>>Java>>正文


Java CoAP类代码示例

本文整理汇总了Java中org.eclipse.californium.core.coap.CoAP的典型用法代码示例。如果您正苦于以下问题:Java CoAP类的具体用法?Java CoAP怎么用?Java CoAP使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


CoAP类属于org.eclipse.californium.core.coap包,在下文中一共展示了CoAP类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getEffectiveEndpoint

import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
/**
 * Returns the effective endpoint that the specified request is supposed to
 * be sent over. If an endpoint has explicitly been set to this CoapClient,
 * this endpoint will be used. If no endpoint has been set, the client will
 * effectively use a default endpoint of the {@link EndpointManager}.
 * 
 * @param request the request to be sent
 * @return the effective endpoint that the request is going o be sent over.
 */
protected Endpoint getEffectiveEndpoint(Request request) {
	Endpoint myEndpoint = getEndpoint();
	
	// custom endpoint
	if (myEndpoint != null) return myEndpoint;
	
	// default endpoints
	if (CoAP.COAP_SECURE_URI_SCHEME.equals(request.getScheme())) {
		// this is the case when secure coap is supposed to be used
		return EndpointManager.getEndpointManager().getDefaultSecureEndpoint();
	} else {
		// this is the normal case
		return EndpointManager.getEndpointManager().getDefaultEndpoint();
	}
}
 
开发者ID:iotoasis,项目名称:SI,代码行数:25,代码来源:CoapClient.java

示例2: build_write_attribute_request

import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
@Test
public void build_write_attribute_request() throws Exception {
    Registration reg = newRegistration();

    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(),
            reg.getEndpoint(), model, encoder);
    WriteAttributesRequest request = new WriteAttributesRequest(3, 0, 14,
            new ObserveSpec.Builder().minPeriod(10).maxPeriod(100).build());
    builder.visit(request);

    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.PUT, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals("coap://127.0.0.1:12354/3/0/14?pmin=10&pmax=100", coapRequest.getURI());
}
 
开发者ID:eclipse,项目名称:leshan,代码行数:19,代码来源:CoapRequestBuilderTest.java

示例3: checkObserveRelation

import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
/**
 * This method is used to apply resource-specific knowledge on the exchange.
 * If the request was successful, it sets the Observe option for the
 * response. It is important to use the notificationOrderer of the resource
 * here. Further down the layer, race conditions could cause local
 * reordering of notifications. If the response has an error code, no
 * observe relation can be established and if there was one previously it is
 * canceled. When this resource allows to be observed by clients and the
 * request is a GET request with an observe option, the
 * {@link ServerMessageDeliverer} already created the relation, as it
 * manages the observing endpoints globally.
 * 
 * @param exchange the exchange
 * @param response the response
 */
public void checkObserveRelation(Exchange exchange, Response response) {
	/*
	 * If the request for the specified exchange tries to establish an observer
	 * relation, then the ServerMessageDeliverer must have created such a relation
	 * and added to the exchange. Otherwise, there is no such relation.
	 * Remember that different paths might lead to this resource.
	 */
	
	ObserveRelation relation = exchange.getRelation();
	if (relation == null) return; // because request did not try to establish a relation
	
	if (CoAP.ResponseCode.isSuccess(response.getCode())) {
		response.getOptions().setObserve(notificationOrderer.getCurrent());
		
		if (!relation.isEstablished()) {
			relation.setEstablished(true);
			addObserveRelation(relation);
		} else if (observeType != null) {
			// The resource can control the message type of the notification
			response.setType(observeType);
		}
	} // ObserveLayer takes care of the else case
}
 
开发者ID:iotoasis,项目名称:SI,代码行数:39,代码来源:CoapResource.java

示例4: build_read_request

import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
@Test
public void build_read_request() throws Exception {
    Registration reg = newRegistration();

    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(),
            reg.getEndpoint(), model, encoder);
    ReadRequest request = new ReadRequest(3, 0);
    builder.visit(request);

    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.GET, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals("coap://127.0.0.1:12354/3/0", coapRequest.getURI());
}
 
开发者ID:eclipse,项目名称:leshan,代码行数:18,代码来源:CoapRequestBuilderTest.java

示例5: build_discover_request

import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
@Test
public void build_discover_request() throws Exception {
    Registration reg = newRegistration();

    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(),
            reg.getEndpoint(), model, encoder);
    DiscoverRequest request = new DiscoverRequest(3, 0);
    builder.visit(request);

    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.GET, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals(MediaTypeRegistry.APPLICATION_LINK_FORMAT, coapRequest.getOptions().getAccept());
    assertEquals("coap://127.0.0.1:12354/3/0", coapRequest.getURI());
}
 
开发者ID:eclipse,项目名称:leshan,代码行数:19,代码来源:CoapRequestBuilderTest.java

示例6: build_write_request

import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
@Test
public void build_write_request() throws Exception {
    Registration reg = newRegistration();

    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(),
            reg.getEndpoint(), model, encoder);
    WriteRequest request = new WriteRequest(Mode.UPDATE, 3, 0, LwM2mSingleResource.newStringResource(4, "value"));
    builder.visit(request);

    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.POST, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals(ContentFormat.TLV.getCode(), coapRequest.getOptions().getContentFormat());
    assertNotNull(coapRequest.getPayload());
    // assert it is encoded as array of resources TLV
    Tlv[] tlvs = TlvDecoder.decode(ByteBuffer.wrap(coapRequest.getPayload()));
    assertEquals(TlvType.RESOURCE_VALUE, tlvs[0].getType());
    assertEquals("value", TlvDecoder.decodeString(tlvs[0].getValue()));
    assertEquals("coap://127.0.0.1:12354/3/0", coapRequest.getURI());
}
 
开发者ID:eclipse,项目名称:leshan,代码行数:24,代码来源:CoapRequestBuilderTest.java

示例7: build_execute_request

import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
@Test
public void build_execute_request() throws Exception {
    Registration reg = newRegistration();

    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(),
            reg.getEndpoint(), model, encoder);
    ExecuteRequest request = new ExecuteRequest(3, 0, 12, "params");
    builder.visit(request);

    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.POST, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals("coap://127.0.0.1:12354/3/0/12", coapRequest.getURI());
    assertEquals("params", coapRequest.getPayloadString());
}
 
开发者ID:eclipse,项目名称:leshan,代码行数:19,代码来源:CoapRequestBuilderTest.java

示例8: build_create_request__without_instance_id

import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
@Test
public void build_create_request__without_instance_id() throws Exception {
    Registration reg = newRegistration();

    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(),
            reg.getEndpoint(), model, encoder);
    CreateRequest request = new CreateRequest(12, LwM2mSingleResource.newStringResource(0, "value"));
    builder.visit(request);

    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.POST, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals("coap://127.0.0.1:12354/12", coapRequest.getURI());
    assertEquals(ContentFormat.TLV.getCode(), coapRequest.getOptions().getContentFormat());
    assertNotNull(coapRequest.getPayload());
    // assert it is encoded as array of resources TLV
    Tlv[] tlvs = TlvDecoder.decode(ByteBuffer.wrap(coapRequest.getPayload()));
    assertEquals(TlvType.RESOURCE_VALUE, tlvs[0].getType());
}
 
开发者ID:eclipse,项目名称:leshan,代码行数:23,代码来源:CoapRequestBuilderTest.java

示例9: build_create_request__with_instance_id

import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
@Test
public void build_create_request__with_instance_id() throws Exception {
    Registration reg = newRegistration();

    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(),
            reg.getEndpoint(), model, encoder);
    CreateRequest request = new CreateRequest(12,
            new LwM2mObjectInstance(26, LwM2mSingleResource.newStringResource(0, "value")));
    builder.visit(request);

    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.POST, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals("coap://127.0.0.1:12354/12", coapRequest.getURI());
    assertEquals(ContentFormat.TLV.getCode(), coapRequest.getOptions().getContentFormat());
    assertNotNull(coapRequest.getPayload());
    // assert it is encoded as array of instance TLV
    Tlv[] tlvs = TlvDecoder.decode(ByteBuffer.wrap(coapRequest.getPayload()));
    assertEquals(TlvType.OBJECT_INSTANCE, tlvs[0].getType());
    assertEquals(26, tlvs[0].getIdentifier());
}
 
开发者ID:eclipse,项目名称:leshan,代码行数:25,代码来源:CoapRequestBuilderTest.java

示例10: build_delete_request

import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
@Test
public void build_delete_request() throws Exception {
    Registration reg = newRegistration();

    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(),
            reg.getEndpoint(), model, encoder);
    DeleteRequest request = new DeleteRequest(12, 0);
    builder.visit(request);

    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.DELETE, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals("coap://127.0.0.1:12354/12/0", coapRequest.getURI());
}
 
开发者ID:eclipse,项目名称:leshan,代码行数:18,代码来源:CoapRequestBuilderTest.java

示例11: build_observe_request

import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
@Test
public void build_observe_request() throws Exception {
    Registration reg = newRegistration();

    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(),
            reg.getEndpoint(), model, encoder);
    ObserveRequest request = new ObserveRequest(12, 0);
    builder.visit(request);

    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.GET, coapRequest.getCode());
    assertEquals(0, coapRequest.getOptions().getObserve().intValue());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals("coap://127.0.0.1:12354/12/0", coapRequest.getURI());
}
 
开发者ID:eclipse,项目名称:leshan,代码行数:19,代码来源:CoapRequestBuilderTest.java

示例12: handle

import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
public void handle(CoapExchange exchange) {
  if (shuttingDown) {
    LOG.debug("Shutting down, discarding incoming request from '{}'", exchange.getSourceAddress());
    exchange.respond(CoAP.ResponseCode.SERVICE_UNAVAILABLE);
  } else {
    long start = System.currentTimeMillis();
    LOG.debug("Request accepted from '{}'", exchange.getSourceAddress());
    try {
      if (receiver.process(exchange.getRequestPayload())) {
        exchange.respond(CoAP.ResponseCode.VALID);
        exchange.accept();
        requestMeter.mark();
      } else {
        exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR);
        exchange.accept();
        errorRequestMeter.mark();
      }
    } catch (IOException ex) {
      exchange.reject();
      errorQueue.offer(ex);
      errorRequestMeter.mark();
    } finally {
      requestTimer.update(System.currentTimeMillis() - start, TimeUnit.MILLISECONDS);
    }
  }
}
 
开发者ID:streamsets,项目名称:datacollector,代码行数:27,代码来源:CoapReceiverResource.java

示例13: setUp

import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
  int port =  TestHttpClientTarget.getFreePort();
  coapServer = new CoapServer(NetworkConfig.createStandardWithoutFile(), port);
  coapServer.add(new CoapResource("test") {
    @Override
    public void handlePOST(CoapExchange exchange) {
      serverRequested = true;
      if (returnErrorResponse) {
        exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR);
        return;
      }
      requestPayload = new String(exchange.getRequestPayload());
      exchange.respond(CoAP.ResponseCode.VALID);
    }
  });
  resourceURl = "coap://localhost:" + port + "/test";
  coapServer.start();
}
 
开发者ID:streamsets,项目名称:datacollector,代码行数:20,代码来源:TestCoapClientTarget.java

示例14: newRequest

import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
/**
 * New request.
 *
 * @param method the method
 * @return the request
 */
private static Request newRequest(String method) {
    if (method.equals("GET")) {
        return new Request(CoAP.Code.GET);
    } else if (method.equals("POST")) {
        return Request.newPost();
        
    } else if (method.equals("PUT")) {
        return Request.newPut();
    }  else if (method.equals("OBSERVE")) {
        return Request.newGet();
    }
    else {
        return null;
    }
}
 
开发者ID:sieben,项目名称:gateway-calipso,代码行数:22,代码来源:CoAPClient.java

示例15: createObserveResponse

import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
private ObserveResponse createObserveResponse(Observation observation, LwM2mModel model, Response coapResponse) {
    // CHANGED response is supported for backward compatibility with old spec.
    if (coapResponse.getCode() != CoAP.ResponseCode.CHANGED
            && coapResponse.getCode() != CoAP.ResponseCode.CONTENT) {
        throw new InvalidResponseException("Unexpected response code [%s] for %s", coapResponse.getCode(),
                observation);
    }

    // get content format
    ContentFormat contentFormat = null;
    if (coapResponse.getOptions().hasContentFormat()) {
        contentFormat = ContentFormat.fromCode(coapResponse.getOptions().getContentFormat());
    }

    // decode response
    try {
        List<TimestampedLwM2mNode> timestampedNodes = decoder.decodeTimestampedData(coapResponse.getPayload(),
                contentFormat, observation.getPath(), model);

        // create lwm2m response
        if (timestampedNodes.size() == 1 && !timestampedNodes.get(0).isTimestamped()) {
            return new ObserveResponse(toLwM2mResponseCode(coapResponse.getCode()),
                    timestampedNodes.get(0).getNode(), null, observation, null, coapResponse);
        } else {
            return new ObserveResponse(toLwM2mResponseCode(coapResponse.getCode()), null, timestampedNodes,
                    observation, null, coapResponse);
        }
    } catch (CodecException e) {
        if (LOG.isDebugEnabled()) {
            byte[] payload = coapResponse.getPayload() == null ? new byte[0] : coapResponse.getPayload();
            LOG.debug(String.format("Unable to decode notification payload [%s] of observation [%s] ",
                    Hex.encodeHexString(payload), observation), e);
        }
        throw new InvalidResponseException(e, "Unable to decode notification payload  of observation [%s] ",
                observation);
    }
}
 
开发者ID:eclipse,项目名称:leshan,代码行数:38,代码来源:ObservationServiceImpl.java


注:本文中的org.eclipse.californium.core.coap.CoAP类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。