use of org.eclipse.leshan.core.request.UplinkRequest 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.request.UplinkRequest 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.request.UplinkRequest in project leshan by eclipse.
the class LockStepLwM2mClient method sendLwM2mRequest.
public void sendLwM2mRequest(UplinkRequest<? extends LwM2mResponse> lwm2mReq) {
// create CoAP request
CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(destination);
lwm2mReq.accept(coapRequestBuilder);
Request coapReq = coapRequestBuilder.getRequest();
byte[] token = new byte[8];
r.nextBytes(token);
coapReq.setToken(token);
// serialize request
UdpDataSerializer serializer = new UdpDataSerializer();
RawData raw = serializer.serializeRequest(coapReq);
// send it
super.send(raw);
}
Aggregations