use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.
the class CoapRequestBuilderTest method build_read_request_with_non_default_object_path.
@Test
public void build_read_request_with_non_default_object_path() throws Exception {
Registration reg = newRegistration("/lwm2m");
// test
CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
ReadRequest request = new ReadRequest(3, 0, 1);
builder.visit(request);
// verify
Request coapRequest = builder.getRequest();
assertEquals("coap://127.0.0.1:12354/lwm2m/3/0/1", coapRequest.getURI());
}
use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.
the class CoapRequestBuilderTest method build_execute_request.
@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());
}
use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.
the class CoapRequestBuilderTest method build_write_request_replace.
@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());
}
use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.
the class ObservationServiceTest method givenAnObservation.
private Observation givenAnObservation(String registrationId, LwM2mPath target) {
Registration registration = store.getRegistration(registrationId);
if (registration == null) {
registration = givenASimpleClient(registrationId);
store.addRegistration(registration);
}
coapRequest = Request.newGet();
coapRequest.setToken(CaliforniumTestSupport.createToken());
coapRequest.getOptions().addUriPath(String.valueOf(target.getObjectId()));
coapRequest.getOptions().addUriPath(String.valueOf(target.getObjectInstanceId()));
coapRequest.getOptions().addUriPath(String.valueOf(target.getResourceId()));
coapRequest.setObserve();
coapRequest.setDestinationContext(EndpointContextUtil.extractContext(support.registration.getIdentity()));
Map<String, String> context = ObserveUtil.createCoapObserveRequestContext(registration.getEndpoint(), registrationId, new ObserveRequest(target.toString()));
coapRequest.setUserContext(context);
store.put(coapRequest.getToken(), new org.eclipse.californium.core.observe.Observation(coapRequest, null));
Observation observation = new Observation(coapRequest.getToken().getBytes(), registrationId, target, null);
observationService.addObservation(registration, observation);
return observation;
}
use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.
the class RedisRequestResponseHandler method sendRequest.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void sendRequest(final String message) {
// Parse JSON and extract ticket
final String ticket;
JsonObject jMessage;
try {
jMessage = (JsonObject) Json.parse(message);
ticket = jMessage.getString("ticket", null);
} catch (RuntimeException t) {
LOG.error(String.format("Unexpected exception during request message handling. (%s)", message), t);
return;
}
// Now if an error occurred we can prevent message sender
try {
// Check if we must handle this request
String endpoint = jMessage.getString("ep", null);
if (!isResponsibleFor(endpoint))
return;
// Get the registration for this endpoint
final Registration destination = registrationService.getByEndpoint(endpoint);
if (destination == null) {
sendError(ticket, String.format("No registration for this endpoint %s.", endpoint));
}
// Deserialize Request
DownlinkRequest<?> request = DownlinkRequestSerDes.deserialize((JsonObject) jMessage.get("req"));
// Ack we will handle this request
sendAck(ticket);
// Send it
server.send(destination, request, new ResponseCallback() {
@Override
public void onResponse(LwM2mResponse response) {
handleResponse(destination.getEndpoint(), ticket, response);
}
}, new ErrorCallback() {
@Override
public void onError(Exception e) {
handlerError(destination.getEndpoint(), ticket, e);
}
});
} catch (RuntimeException t) {
String errorMessage = String.format("Unexpected exception during request message handling.(%s:%s)", t.toString(), t.getMessage());
LOG.error(errorMessage, t);
sendError(ticket, errorMessage);
}
}
Aggregations