本文整理汇总了Java中org.eclipse.californium.core.coap.CoAP类的典型用法代码示例。如果您正苦于以下问题:Java CoAP类的具体用法?Java CoAP怎么用?Java CoAP使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CoAP类属于org.eclipse.californium.core.coap包,在下文中一共展示了CoAP类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
示例16: build_write_request_replace
import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
@Test
public void build_write_request_replace() throws Exception {
Registration reg = newRegistration();
// test
CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(),
reg.getEndpoint(), model, encoder);
WriteRequest request = new WriteRequest(3, 0, 14, "value");
builder.visit(request);
// verify
Request coapRequest = builder.getRequest();
assertEquals(CoAP.Code.PUT, coapRequest.getCode());
}
开发者ID:eclipse,项目名称:leshan,代码行数:15,代码来源:CoapRequestBuilderTest.java
示例17: toCoapResponseCode
import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
public static org.eclipse.californium.core.coap.CoAP.ResponseCode toCoapResponseCode(
ResponseCode Lwm2mResponseCode) {
Validate.notNull(Lwm2mResponseCode);
try {
return org.eclipse.californium.core.coap.CoAP.ResponseCode.valueOf(toCoapCode(Lwm2mResponseCode.getCode()));
} catch (MessageFormatException e) {
throw new IllegalArgumentException("Invalid CoAP code for LWM2M response: " + Lwm2mResponseCode);
}
}
开发者ID:eclipse,项目名称:leshan,代码行数:10,代码来源:ResponseCodeUtil.java
示例18: handlePUT
import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
@Override
public void handlePUT(CoapExchange exchange) {
String data = exchange.getRequestText();
try {
N3Document resp = N3Utils.parseN3Document(data);
semanticContainer.addAnswer(resp);
exchange.respond(CoAP.ResponseCode.CHANGED);
} catch (Exception e) {
exchange.respond(CoAP.ResponseCode.BAD_REQUEST);
}
}
开发者ID:mkovatsc,项目名称:iot-semantics,代码行数:12,代码来源:Answer.java
示例19: testSource
import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
@Test
public void testSource() throws Exception {
CoapServerConfigs coapServerConfigs = new CoapServerConfigs();
coapServerConfigs.resourceName = () -> "sdc";
coapServerConfigs.port = NetworkUtils.getRandomPort();
coapServerConfigs.maxConcurrentRequests = 1;
CoapServerPushSource source =
new CoapServerPushSource(coapServerConfigs, DataFormat.TEXT, new DataParserFormatConfig());
final PushSourceRunner runner =
new PushSourceRunner.Builder(CoapServerDPushSource.class, source).addOutputLane("a").build();
runner.runInit();
try {
final List<Record> records = new ArrayList<>();
runner.runProduce(Collections.<String, String>emptyMap(), 1, new PushSourceRunner.Callback() {
@Override
public void processBatch(StageRunner.Output output) {
records.clear();
records.addAll(output.getRecords().get("a"));
runner.setStop();
}
});
URI coapURI = new URI("coap://localhost:" + coapServerConfigs.port + "/" + coapServerConfigs.resourceName.get());
CoapClient client = new CoapClient(coapURI);
CoapResponse response = client.post("Hello", MediaTypeRegistry.TEXT_PLAIN);
Assert.assertNotNull(response);
Assert.assertEquals(response.getCode(), CoAP.ResponseCode.VALID);
runner.waitOnProduce();
Assert.assertEquals(1, records.size());
Assert.assertEquals("Hello", records.get(0).get("/text").getValue());
} finally {
runner.runDestroy();
}
}
开发者ID:streamsets,项目名称:datacollector,代码行数:37,代码来源:TestCoapServerPushSource.java
示例20: main
import org.eclipse.californium.core.coap.CoAP; //导入依赖的package包/类
public static void main(String[] args) {
/* Create CoAP Server listening on all interfaces (IPv6 & IPv4) */
AllInterfacesCoapServer server = new AllInterfacesCoapServer();
ClientAuthorizationManager cam = createCamWithLocalSam();
/* Add CoAP Resource to process incoming requests */
server.add(new CoapResource("client-authorize"){
{
/* Set title/description for the CoAP Resource */
getAttributes().setTitle("DCAF Client Authorization Manager");
}
@Override
public void handlePOST(CoapExchange exchange) {
byte[] requestPayload = exchange.getRequestPayload();
/* Deserialize incoming requests from CBOR to Java Objects */
Optional<AccessRequest> request = Utils.deserializeCbor(requestPayload, AccessRequest.class);
if (!request.isPresent()) {
logger.error("Error 500 - deserializeCbor failed");
exchange.respond(CoAP.ResponseCode.BAD_REQUEST);
return;
}
/* Do something here (or even before deserialization to check if the client is allowed to
* get what he requested */
/* Get the ticket grant message by calling cam.process.
* The CAM is going to contact the SAM to get the TicketGrantMessage */
TicketGrantMessage grant = cam.process(request.get());
/* Serialize the grant answer and return it to the client. */
Optional<byte[]> answer = Utils.serializeCbor(grant);
if (answer.isPresent()) {
logger.debug("respond: h'" + Hex.encodeHexString(answer.get()) + "'");
exchange.respond(CoAP.ResponseCode.CONTENT, answer.get(), Utils.getDcafMediaType());
} else {
logger.error("Error 500 - serializeCbor failed");
exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR);
}
}
});
}
开发者ID:beduino-project,项目名称:dcaf-java,代码行数:47,代码来源:ExampleServer.java
注:本文中的org.eclipse.californium.core.coap.CoAP类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论