Search in sources :

Example 1 with DeregisterRequest

use of org.eclipse.leshan.core.request.DeregisterRequest in project leshan by eclipse.

the class RegistrationEngine method deregister.

private boolean deregister() throws InterruptedException {
    if (registrationID == null)
        return true;
    ServersInfo serversInfo = ServersInfoExtractor.getInfo(objectEnablers);
    DmServerInfo dmInfo = serversInfo.deviceMangements.values().iterator().next();
    if (dmInfo == null) {
        LOG.error("Trying to deregister device but there is no LWM2M server config.");
        return false;
    }
    // Send deregister request
    LOG.info("Trying to deregister to {} ...", dmInfo.getFullUri());
    DeregisterResponse response = sender.send(dmInfo.getAddress(), dmInfo.isSecure(), new DeregisterRequest(registrationID), DEREGISTRATION_TIMEOUT);
    if (response == null) {
        registrationID = null;
        LOG.error("Deregistration failed: Timeout.");
        if (observer != null) {
            observer.onDeregistrationTimeout(dmInfo);
        }
        return false;
    } else if (response.isSuccess() || response.getCode() == ResponseCode.NOT_FOUND) {
        registrationID = null;
        cancelUpdateTask(true);
        LOG.info("De-register response {} {}.", response.getCode(), response.getErrorMessage());
        if (observer != null) {
            if (response.isSuccess()) {
                observer.onDeregistrationSuccess(dmInfo, registrationID);
            } else {
                observer.onDeregistrationFailure(dmInfo, response.getCode(), response.getErrorMessage());
            }
        }
        return true;
    } else {
        LOG.error("Deregistration failed: {} {}.", response.getCode(), response.getErrorMessage());
        if (observer != null) {
            observer.onDeregistrationFailure(dmInfo, response.getCode(), response.getErrorMessage());
        }
        return false;
    }
}
Also used : DeregisterResponse(org.eclipse.leshan.core.response.DeregisterResponse) DeregisterRequest(org.eclipse.leshan.core.request.DeregisterRequest)

Example 2 with DeregisterRequest

use of org.eclipse.leshan.core.request.DeregisterRequest 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 3 with DeregisterRequest

use of org.eclipse.leshan.core.request.DeregisterRequest in project leshan by eclipse.

the class RegisterResource method handleDeregister.

private void handleDeregister(CoapExchange exchange, String registrationId) {
    // Get identity
    Identity sender = extractIdentity(exchange.advanced().getRequest().getSourceContext());
    // Create request
    DeregisterRequest deregisterRequest = new DeregisterRequest(registrationId);
    // Handle request
    final SendableResponse<DeregisterResponse> sendableResponse = registrationHandler.deregister(sender, deregisterRequest);
    DeregisterResponse deregisterResponse = sendableResponse.getResponse();
    // Create CoAP Response from LwM2m request
    if (deregisterResponse.getCode().isError()) {
        exchange.respond(toCoapResponseCode(deregisterResponse.getCode()), deregisterResponse.getErrorMessage());
    } else {
        exchange.respond(toCoapResponseCode(deregisterResponse.getCode()));
    }
    sendableResponse.sent();
}
Also used : DeregisterResponse(org.eclipse.leshan.core.response.DeregisterResponse) Identity(org.eclipse.leshan.core.request.Identity) EndpointContextUtil.extractIdentity(org.eclipse.leshan.core.californium.EndpointContextUtil.extractIdentity) DeregisterRequest(org.eclipse.leshan.core.request.DeregisterRequest)

Aggregations

DeregisterRequest (org.eclipse.leshan.core.request.DeregisterRequest)3 DeregisterResponse (org.eclipse.leshan.core.response.DeregisterResponse)2 HashMap (java.util.HashMap)1 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)1 Endpoint (org.eclipse.californium.core.network.Endpoint)1 Link (org.eclipse.leshan.Link)1 LeshanClient (org.eclipse.leshan.client.californium.LeshanClient)1 CaliforniumLwM2mRequestSender (org.eclipse.leshan.client.californium.impl.CaliforniumLwM2mRequestSender)1 EndpointContextUtil.extractIdentity (org.eclipse.leshan.core.californium.EndpointContextUtil.extractIdentity)1 Identity (org.eclipse.leshan.core.request.Identity)1 RegisterRequest (org.eclipse.leshan.core.request.RegisterRequest)1 RegisterResponse (org.eclipse.leshan.core.response.RegisterResponse)1 Test (org.junit.Test)1