use of org.eclipse.leshan.core.response.LwM2mResponse in project leshan by eclipse.
the class CaliforniumLwM2mRequestSender method send.
@Override
public <T extends LwM2mResponse> T send(InetSocketAddress serverAddress, boolean secure, final UplinkRequest<T> request, long timeout) throws InterruptedException {
// Create the CoAP request from LwM2m request
CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(serverAddress);
request.accept(coapClientRequestBuilder);
Request coapRequest = coapClientRequestBuilder.getRequest();
// Send CoAP request synchronously
SyncRequestObserver<T> syncMessageObserver = new SyncRequestObserver<T>(coapRequest, timeout) {
@Override
public T buildResponse(Response coapResponse) {
// Build LwM2m response
LwM2mClientResponseBuilder<T> lwm2mResponseBuilder = new LwM2mClientResponseBuilder<>(coapResponse);
request.accept(lwm2mResponseBuilder);
return lwm2mResponseBuilder.getResponse();
}
};
coapRequest.addMessageObserver(syncMessageObserver);
// Send CoAP request asynchronously
if (secure)
securedEndpoint.sendRequest(coapRequest);
else
unsecuredEndpoint.sendRequest(coapRequest);
// Wait for response, then return it
return syncMessageObserver.waitForResponse();
}
use of org.eclipse.leshan.core.response.LwM2mResponse in project leshan by eclipse.
the class CaliforniumLwM2mRequestSender method send.
@Override
public <T extends LwM2mResponse> void send(InetSocketAddress serverAddress, boolean secure, final UplinkRequest<T> request, long timeout, ResponseCallback<T> responseCallback, ErrorCallback errorCallback) {
// Create the CoAP request from LwM2m request
CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(serverAddress);
request.accept(coapClientRequestBuilder);
Request coapRequest = coapClientRequestBuilder.getRequest();
// Add CoAP request callback
MessageObserver obs = new AsyncRequestObserver<T>(coapRequest, responseCallback, errorCallback, timeout) {
@Override
public T buildResponse(Response coapResponse) {
// Build LwM2m response
LwM2mClientResponseBuilder<T> lwm2mResponseBuilder = new LwM2mClientResponseBuilder<>(coapResponse);
request.accept(lwm2mResponseBuilder);
return lwm2mResponseBuilder.getResponse();
}
};
coapRequest.addMessageObserver(obs);
// Send CoAP request asynchronously
if (secure)
securedEndpoint.sendRequest(coapRequest);
else
unsecuredEndpoint.sendRequest(coapRequest);
}
use of org.eclipse.leshan.core.response.LwM2mResponse in project leshan by eclipse.
the class CaliforniumLwM2mBootstrapRequestSender method send.
@Override
public <T extends LwM2mResponse> T send(final String endpointName, final Identity destination, final DownlinkRequest<T> request, long timeout) throws InterruptedException {
// Create the CoAP request
// from LwM2m request
CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(destination, model, encoder);
request.accept(coapClientRequestBuilder);
final Request coapRequest = coapClientRequestBuilder.getRequest();
// Send CoAP request synchronously
SyncRequestObserver<T> syncMessageObserver = new SyncRequestObserver<T>(coapRequest, timeout) {
@Override
public T buildResponse(Response coapResponse) {
// TODO we need to fix that by removing the Client dependency from LwM2MResponseBuilder or by creating a
// LwM2mBootstrapResponseBuilder
Registration registration = new Registration.Builder("fakeregistrationid", endpointName, destination, destination.isSecure() ? secureEndpoint.getAddress() : nonSecureEndpoint.getAddress()).build();
// Build LwM2m response
LwM2mResponseBuilder<T> lwm2mResponseBuilder = new LwM2mResponseBuilder<>(coapRequest, coapResponse, registration, model, null, decoder);
request.accept(lwm2mResponseBuilder);
return lwm2mResponseBuilder.getResponse();
}
};
coapRequest.addMessageObserver(syncMessageObserver);
// Send CoAP request asynchronously
if (destination.isSecure())
secureEndpoint.sendRequest(coapRequest);
else
nonSecureEndpoint.sendRequest(coapRequest);
// Wait for response, then return it
return syncMessageObserver.waitForResponse();
}
use of org.eclipse.leshan.core.response.LwM2mResponse in project leshan by eclipse.
the class ObserveTest method can_observe_resource.
@Test
public void can_observe_resource() throws InterruptedException {
TestObservationListener listener = new TestObservationListener();
helper.server.getObservationService().addListener(listener);
// observe device timezone
ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3, 0, 15));
assertEquals(ResponseCode.CONTENT, observeResponse.getCode());
assertNotNull(observeResponse.getCoapResponse());
assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class)));
// an observation response should have been sent
Observation observation = observeResponse.getObservation();
assertEquals("/3/0/15", observation.getPath().toString());
assertEquals(helper.getCurrentRegistration().getId(), observation.getRegistrationId());
Set<Observation> observations = helper.server.getObservationService().getObservations(helper.getCurrentRegistration());
assertTrue("We should have only on observation", observations.size() == 1);
assertTrue("New observation is not there", observations.contains(observation));
// write device timezone
LwM2mResponse writeResponse = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(3, 0, 15, "Europe/Paris"));
// verify result
listener.waitForNotification(2000);
assertEquals(ResponseCode.CHANGED, writeResponse.getCode());
assertTrue(listener.receivedNotify().get());
assertEquals(LwM2mSingleResource.newStringResource(15, "Europe/Paris"), listener.getResponse().getContent());
assertNotNull(listener.getResponse().getCoapResponse());
assertThat(listener.getResponse().getCoapResponse(), is(instanceOf(Response.class)));
}
use of org.eclipse.leshan.core.response.LwM2mResponse in project leshan by eclipse.
the class ObserveTest method can_observe_object.
@Test
public void can_observe_object() throws InterruptedException {
TestObservationListener listener = new TestObservationListener();
helper.server.getObservationService().addListener(listener);
// observe device timezone
ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3));
assertEquals(ResponseCode.CONTENT, observeResponse.getCode());
assertNotNull(observeResponse.getCoapResponse());
assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class)));
// an observation response should have been sent
Observation observation = observeResponse.getObservation();
assertEquals("/3", observation.getPath().toString());
assertEquals(helper.getCurrentRegistration().getId(), observation.getRegistrationId());
Set<Observation> observations = helper.server.getObservationService().getObservations(helper.getCurrentRegistration());
assertTrue("We should have only on observation", observations.size() == 1);
assertTrue("New observation is not there", observations.contains(observation));
// write device timezone
LwM2mResponse writeResponse = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(3, 0, 15, "Europe/Paris"));
// verify result
listener.waitForNotification(2000);
assertEquals(ResponseCode.CHANGED, writeResponse.getCode());
assertTrue(listener.receivedNotify().get());
assertTrue(listener.getResponse().getContent() instanceof LwM2mObject);
assertNotNull(listener.getResponse().getCoapResponse());
assertThat(listener.getResponse().getCoapResponse(), is(instanceOf(Response.class)));
// try to read the object for comparing
ReadResponse readResp = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3));
assertEquals(readResp.getContent(), listener.getResponse().getContent());
}
Aggregations