use of org.eclipse.californium.core.coap.Request in project leshan by eclipse.
the class CaliforniumLwM2mRequestSender method send.
@Override
public <T extends LwM2mResponse> T send(final Registration destination, final DownlinkRequest<T> request, long timeout) throws InterruptedException {
// Retrieve the objects definition
final LwM2mModel model = modelProvider.getObjectModel(destination);
// Create the CoAP request from LwM2m request
CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(destination.getIdentity(), destination.getRootPath(), destination.getId(), destination.getEndpoint(), model, encoder);
request.accept(coapRequestBuilder);
final Request coapRequest = coapRequestBuilder.getRequest();
// Send CoAP request synchronously
SyncRequestObserver<T> syncMessageObserver = new SyncRequestObserver<T>(coapRequest, timeout) {
@Override
public T buildResponse(Response coapResponse) {
// Build LwM2m response
LwM2mResponseBuilder<T> lwm2mResponseBuilder = new LwM2mResponseBuilder<>(coapRequest, coapResponse, destination, model, observationService, decoder);
request.accept(lwm2mResponseBuilder);
return lwm2mResponseBuilder.getResponse();
}
};
coapRequest.addMessageObserver(syncMessageObserver);
// Store pending request to cancel it on de-registration
addPendingRequest(destination.getId(), coapRequest);
// Send CoAP request asynchronously
Endpoint endpoint = getEndpointForClient(destination);
endpoint.sendRequest(coapRequest);
// Wait for response, then return it
return syncMessageObserver.waitForResponse();
}
use of org.eclipse.californium.core.coap.Request in project leshan by eclipse.
the class RegisterResource method handlePOST.
@Override
public void handlePOST(CoapExchange exchange) {
Request request = exchange.advanced().getRequest();
LOG.debug("POST received : {}", request);
// The LWM2M spec (section 8.2) mandates the usage of confirmable messages
if (!Type.CON.equals(request.getType())) {
exchange.respond(ResponseCode.BAD_REQUEST);
return;
}
List<String> uri = exchange.getRequestOptions().getUriPath();
if (uri == null || uri.size() == 0 || !RESOURCE_NAME.equals(uri.get(0))) {
exchange.respond(ResponseCode.BAD_REQUEST);
return;
}
if (uri.size() == 1) {
handleRegister(exchange, request);
return;
} else if (uri.size() == 2) {
handleUpdate(exchange, request, uri.get(1));
return;
} else {
exchange.respond(ResponseCode.BAD_REQUEST);
return;
}
}
use of org.eclipse.californium.core.coap.Request in project leshan by eclipse.
the class RegisterResource method handlePUT.
// Since the V1_0-20150615-D version of specification, the registration update should be a CoAP POST.
// To keep compatibility with older version, we still accept CoAP PUT.
// TODO remove this backward compatibility when the version 1.0.0 of the spec will be released.
@Override
public void handlePUT(CoapExchange exchange) {
Request request = exchange.advanced().getRequest();
LOG.debug("UPDATE received : {}", request);
if (!Type.CON.equals(request.getType())) {
exchange.respond(ResponseCode.BAD_REQUEST);
return;
}
List<String> uri = exchange.getRequestOptions().getUriPath();
if (uri == null || uri.size() != 2 || !RESOURCE_NAME.equals(uri.get(0))) {
exchange.respond(ResponseCode.BAD_REQUEST);
return;
}
LOG.debug("Warning a client made a registration update using a CoAP PUT, a POST must be used since version V1_0-20150615-D of the specification. Request: {}", request);
handleUpdate(exchange, request, uri.get(1));
}
use of org.eclipse.californium.core.coap.Request in project leshan by eclipse.
the class CoapRequestBuilderTest method build_write_request.
@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());
}
use of org.eclipse.californium.core.coap.Request in project leshan by eclipse.
the class CoapRequestBuilderTest method build_write_attribute_request.
@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());
}
Aggregations