Search in sources :

Example 1 with RegisterResponse

use of org.eclipse.leshan.core.response.RegisterResponse in project leshan by eclipse.

the class RegistrationEngine method register.

private boolean register() throws InterruptedException {
    ServersInfo serversInfo = ServersInfoExtractor.getInfo(objectEnablers);
    DmServerInfo dmInfo = serversInfo.deviceMangements.values().iterator().next();
    if (dmInfo == null) {
        LOG.error("Trying to register device but there is no LWM2M server config.");
        return false;
    }
    // send register request
    LOG.info("Trying to register to {} ...", dmInfo.getFullUri());
    RegisterRequest regRequest = new RegisterRequest(endpoint, dmInfo.lifetime, LwM2m.VERSION, dmInfo.binding, null, LinkFormatHelper.getClientDescription(objectEnablers.values(), null), additionalAttributes);
    RegisterResponse response = sender.send(dmInfo.getAddress(), dmInfo.isSecure(), regRequest, DEFAULT_TIMEOUT);
    if (response == null) {
        registrationID = null;
        LOG.error("Registration failed: Timeout.");
        if (observer != null) {
            observer.onRegistrationTimeout(dmInfo);
        }
    } else if (response.isSuccess()) {
        registrationID = response.getRegistrationID();
        LOG.info("Registered with location '{}'.", response.getRegistrationID());
        // update every lifetime period
        scheduleUpdate(dmInfo);
        if (observer != null) {
            observer.onRegistrationSuccess(dmInfo, response.getRegistrationID());
        }
    } else {
        registrationID = null;
        LOG.error("Registration failed: {} {}.", response.getCode(), response.getErrorMessage());
        if (observer != null) {
            observer.onRegistrationFailure(dmInfo, response.getCode(), response.getErrorMessage());
        }
    }
    return registrationID != null;
}
Also used : RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) RegisterResponse(org.eclipse.leshan.core.response.RegisterResponse)

Example 2 with RegisterResponse

use of org.eclipse.leshan.core.response.RegisterResponse 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 RegisterResponse

use of org.eclipse.leshan.core.response.RegisterResponse in project leshan by eclipse.

the class RegisterResource method handleRegister.

private void handleRegister(CoapExchange exchange, Request request) {
    // Get identity
    // --------------------------------
    Identity sender = extractIdentity(request.getSourceContext());
    // Create LwM2m request from CoAP request
    // --------------------------------
    // We don't check content media type is APPLICATION LINK FORMAT for now as this is the only format we can expect
    String endpoint = null;
    Long lifetime = null;
    String smsNumber = null;
    String lwVersion = null;
    BindingMode binding = null;
    // Get object Links
    Link[] objectLinks = Link.parse(request.getPayload());
    Map<String, String> additionalParams = new HashMap<>();
    // Get parameters
    for (String param : request.getOptions().getUriQuery()) {
        if (param.startsWith(QUERY_PARAM_ENDPOINT)) {
            endpoint = param.substring(3);
        } else if (param.startsWith(QUERY_PARAM_LIFETIME)) {
            lifetime = Long.valueOf(param.substring(3));
        } else if (param.startsWith(QUERY_PARAM_SMS)) {
            smsNumber = param.substring(4);
        } else if (param.startsWith(QUERY_PARAM_LWM2M_VERSION)) {
            lwVersion = param.substring(6);
        } else if (param.startsWith(QUERY_PARAM_BINDING_MODE)) {
            binding = BindingMode.valueOf(param.substring(2));
        } else {
            String[] tokens = param.split("\\=");
            if (tokens != null && tokens.length == 2) {
                additionalParams.put(tokens[0], tokens[1]);
            }
        }
    }
    // Create request
    RegisterRequest registerRequest = new RegisterRequest(endpoint, lifetime, lwVersion, binding, smsNumber, objectLinks, additionalParams);
    // Handle request
    // -------------------------------
    InetSocketAddress serverEndpoint = exchange.advanced().getEndpoint().getAddress();
    final SendableResponse<RegisterResponse> sendableResponse = registrationHandler.register(sender, registerRequest, serverEndpoint);
    RegisterResponse response = sendableResponse.getResponse();
    // -------------------------------
    if (response.getCode() == org.eclipse.leshan.ResponseCode.CREATED) {
        exchange.setLocationPath(RESOURCE_NAME + "/" + response.getRegistrationID());
        exchange.respond(ResponseCode.CREATED);
    } else {
        exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
    }
    sendableResponse.sent();
}
Also used : RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) BindingMode(org.eclipse.leshan.core.request.BindingMode) RegisterResponse(org.eclipse.leshan.core.response.RegisterResponse) Identity(org.eclipse.leshan.core.request.Identity) EndpointContextUtil.extractIdentity(org.eclipse.leshan.core.californium.EndpointContextUtil.extractIdentity) Link(org.eclipse.leshan.Link)

Aggregations

RegisterRequest (org.eclipse.leshan.core.request.RegisterRequest)3 RegisterResponse (org.eclipse.leshan.core.response.RegisterResponse)3 HashMap (java.util.HashMap)2 Link (org.eclipse.leshan.Link)2 InetSocketAddress (java.net.InetSocketAddress)1 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)1 Endpoint (org.eclipse.californium.core.network.Endpoint)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 BindingMode (org.eclipse.leshan.core.request.BindingMode)1 DeregisterRequest (org.eclipse.leshan.core.request.DeregisterRequest)1 Identity (org.eclipse.leshan.core.request.Identity)1 Test (org.junit.Test)1