Search in sources :

Example 1 with Endpoint

use of org.eclipse.californium.core.network.Endpoint in project leshan by eclipse.

the class CaliforniumLwM2mRequestSender method send.

@Override
public <T extends LwM2mResponse> void send(final Registration destination, final DownlinkRequest<T> request, long timeout, ResponseCallback<T> responseCallback, ErrorCallback errorCallback) {
    // 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();
    // Add CoAP request callback
    MessageObserver obs = new AsyncRequestObserver<T>(coapRequest, responseCallback, errorCallback, 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(obs);
    // Store pending request to cancel it on de-registration
    addPendingRequest(destination.getId(), coapRequest);
    // Send CoAP request asynchronously
    Endpoint endpoint = getEndpointForClient(destination);
    endpoint.sendRequest(coapRequest);
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) MessageObserver(org.eclipse.californium.core.coap.MessageObserver) Endpoint(org.eclipse.californium.core.network.Endpoint) Request(org.eclipse.californium.core.coap.Request) DownlinkRequest(org.eclipse.leshan.core.request.DownlinkRequest) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) AsyncRequestObserver(org.eclipse.leshan.core.californium.AsyncRequestObserver)

Example 2 with Endpoint

use of org.eclipse.californium.core.network.Endpoint 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();
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) Endpoint(org.eclipse.californium.core.network.Endpoint) Request(org.eclipse.californium.core.coap.Request) DownlinkRequest(org.eclipse.leshan.core.request.DownlinkRequest) SyncRequestObserver(org.eclipse.leshan.core.californium.SyncRequestObserver) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel)

Example 3 with Endpoint

use of org.eclipse.californium.core.network.Endpoint in project leshan by eclipse.

the class RegistrationTest method register_with_additional_attributes.

@Test
public void register_with_additional_attributes() throws InterruptedException {
    // Check registration
    helper.assertClientNotRegisterered();
    // HACK to be able to send a Registration request with additional attributes
    LeshanClient lclient = helper.client;
    lclient.getCoapServer().start();
    Endpoint secureEndpoint = lclient.getCoapServer().getEndpoint(lclient.getSecuredAddress());
    Endpoint nonSecureEndpoint = lclient.getCoapServer().getEndpoint(lclient.getUnsecuredAddress());
    CaliforniumLwM2mRequestSender sender = new CaliforniumLwM2mRequestSender(secureEndpoint, nonSecureEndpoint);
    // Create Request with additional attributes
    Map<String, String> additionalAttributes = new HashMap<>();
    additionalAttributes.put("key1", "value1");
    additionalAttributes.put("imei", "2136872368");
    Link[] objectLinks = Link.parse("</>;rt=\"oma.lwm2m\",</1/0>,</2>,</3/0>".getBytes());
    RegisterRequest registerRequest = new RegisterRequest(helper.getCurrentEndpoint(), null, null, null, null, objectLinks, additionalAttributes);
    // Send request
    RegisterResponse resp = sender.send(helper.server.getUnsecuredAddress(), false, registerRequest, 5000l);
    helper.waitForRegistration(1);
    // Check we are registered with the expected attributes
    helper.assertClientRegisterered();
    assertNotNull(helper.last_registration);
    assertEquals(additionalAttributes, helper.last_registration.getAdditionalRegistrationAttributes());
    assertArrayEquals(Link.parse("</>;rt=\"oma.lwm2m\",</1/0>,</2>,</3/0>".getBytes()), helper.getCurrentRegistration().getObjectLinks());
    sender.send(helper.server.getUnsecuredAddress(), false, new DeregisterRequest(resp.getRegistrationID()), 5000l);
    lclient.getCoapServer().stop();
}
Also used : CaliforniumLwM2mRequestSender(org.eclipse.leshan.client.californium.impl.CaliforniumLwM2mRequestSender) RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) RegisterResponse(org.eclipse.leshan.core.response.RegisterResponse) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Endpoint(org.eclipse.californium.core.network.Endpoint) HashMap(java.util.HashMap) DeregisterRequest(org.eclipse.leshan.core.request.DeregisterRequest) Link(org.eclipse.leshan.Link) LeshanClient(org.eclipse.leshan.client.californium.LeshanClient) Test(org.junit.Test)

Example 4 with Endpoint

use of org.eclipse.californium.core.network.Endpoint in project leshan by eclipse.

the class SecurityTest method dont_sent_request_if_identity_change.

@Test
public void dont_sent_request_if_identity_change() throws NonUniqueSecurityInfoException, InterruptedException, IOException {
    // Create PSK server & start it
    // default server support PSK
    helper.createServer();
    helper.server.start();
    // Create PSK Client
    helper.createPSKClient();
    // Add client credentials to the server
    helper.getSecurityStore().add(SecurityInfo.newPreSharedKeyInfo(helper.getCurrentEndpoint(), GOOD_PSK_ID, GOOD_PSK_KEY));
    // Check client is not registered
    helper.assertClientNotRegisterered();
    // Start it and wait for registration
    helper.client.start();
    helper.waitForRegistration(1);
    // Check client is well registered
    helper.assertClientRegisterered();
    // Ensure we can send a read request
    helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3, 0, 1));
    // Pause the client
    // helper.client.stop(false);
    // Add new credential to the server
    helper.getSecurityStore().add(SecurityInfo.newPreSharedKeyInfo(GOOD_ENDPOINT, "anotherPSK", GOOD_PSK_KEY));
    // Get connector
    Endpoint endpoint = helper.client.getCoapServer().getEndpoint(helper.client.getSecuredAddress());
    DTLSConnector connector = (DTLSConnector) ((CoapEndpoint) endpoint).getConnector();
    // Clear DTLS session to force new handshake
    connector.clearConnectionState();
    // Change PSK idea
    helper.setNewPsk(helper.client, "anotherPSK");
    // restart connector
    connector.start();
    // send and empty message to force a new handshake with new credentials
    SimpleMessageCallback callback = new SimpleMessageCallback();
    connector.send(RawData.outbound(new byte[0], new AddressEndpointContext(helper.server.getSecuredAddress()), callback, false));
    // Wait until new handshake DTLS is done
    EndpointContext endpointContext = callback.getEndpointContext(1000);
    assertEquals(endpointContext.getPeerIdentity().getName(), "anotherPSK");
    // Try to send a read request this should failed with an SendFailedException.
    try {
        helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3, 0, 1), 1000);
        fail("send must failed");
    } catch (SendFailedException e) {
        assertTrue("must be caused by an EndpointMismatchException", e.getCause() instanceof EndpointMismatchException);
    } finally {
        connector.stop();
        helper.client.destroy(false);
        helper.client = null;
    }
}
Also used : SendFailedException(org.eclipse.leshan.core.request.exception.SendFailedException) EndpointMismatchException(org.eclipse.californium.elements.EndpointMismatchException) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Endpoint(org.eclipse.californium.core.network.Endpoint) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) EndpointContext(org.eclipse.californium.elements.EndpointContext) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) SimpleMessageCallback(org.eclipse.californium.elements.util.SimpleMessageCallback) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Aggregations

Endpoint (org.eclipse.californium.core.network.Endpoint)4 Request (org.eclipse.californium.core.coap.Request)2 Response (org.eclipse.californium.core.coap.Response)2 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)2 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)2 DownlinkRequest (org.eclipse.leshan.core.request.DownlinkRequest)2 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 MessageObserver (org.eclipse.californium.core.coap.MessageObserver)1 AddressEndpointContext (org.eclipse.californium.elements.AddressEndpointContext)1 EndpointContext (org.eclipse.californium.elements.EndpointContext)1 EndpointMismatchException (org.eclipse.californium.elements.EndpointMismatchException)1 SimpleMessageCallback (org.eclipse.californium.elements.util.SimpleMessageCallback)1 DTLSConnector (org.eclipse.californium.scandium.DTLSConnector)1 Link (org.eclipse.leshan.Link)1 LeshanClient (org.eclipse.leshan.client.californium.LeshanClient)1 CaliforniumLwM2mRequestSender (org.eclipse.leshan.client.californium.impl.CaliforniumLwM2mRequestSender)1 AsyncRequestObserver (org.eclipse.leshan.core.californium.AsyncRequestObserver)1 SyncRequestObserver (org.eclipse.leshan.core.californium.SyncRequestObserver)1