Search in sources :

Example 1 with LwM2MClientStateException

use of org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientStateException in project thingsboard by thingsboard.

the class DefaultLwM2mUplinkMsgHandler method onRegistered.

/**
 * Start registration device
 * Create session: Map<String <registrationId >, LwM2MClient>
 * 1. replaceNewRegistration -> (solving the problem of incorrect termination of the previous session with this endpoint)
 * 1.1 When we initialize the registration, we register the session by endpoint.
 * 1.2 If the server has incomplete requests (canceling the registration of the previous session),
 * delete the previous session only by the previous registration.getId
 * 1.2 Add Model (Entity) for client (from registration & observe) by registration.getId
 * 1.2 Remove from sessions Model by enpPoint
 * Next ->  Create new LwM2MClient for current session -> setModelClient...
 *
 * @param registration         - Registration LwM2M Client
 * @param previousObservations - may be null
 */
public void onRegistered(Registration registration, Collection<Observation> previousObservations) {
    executor.submit(() -> {
        LwM2mClient lwM2MClient = this.clientContext.getClientByEndpoint(registration.getEndpoint());
        try {
            log.debug("[{}] [{{}] Client: create after Registration", registration.getEndpoint(), registration.getId());
            Optional<SessionInfoProto> oldSessionInfo = this.clientContext.register(lwM2MClient, registration);
            if (oldSessionInfo.isPresent()) {
                log.info("[{}] Closing old session: {}", registration.getEndpoint(), new UUID(oldSessionInfo.get().getSessionIdMSB(), oldSessionInfo.get().getSessionIdLSB()));
                sessionManager.deregister(oldSessionInfo.get());
            }
            logService.log(lwM2MClient, LOG_LWM2M_INFO + ": Client registered with registration id: " + registration.getId() + " version: " + registration.getLwM2mVersion() + " and modes: " + registration.getQueueMode() + ", " + registration.getBindingMode());
            sessionManager.register(lwM2MClient.getSession());
            this.initClientTelemetry(lwM2MClient);
            this.initAttributes(lwM2MClient, true);
            otaService.init(lwM2MClient);
            lwM2MClient.getRetryAttempts().set(0);
        } catch (LwM2MClientStateException stateException) {
            if (LwM2MClientState.UNREGISTERED.equals(stateException.getState())) {
                log.info("[{}] retry registration due to race condition: [{}].", registration.getEndpoint(), stateException.getState());
                // Race condition detected and the client was in progress of unregistration while new registration arrived. Let's try again.
                if (lwM2MClient.getRetryAttempts().incrementAndGet() <= 5) {
                    context.getScheduler().schedule(() -> onRegistered(registration, previousObservations), 1, TimeUnit.SECONDS);
                } else {
                    logService.log(lwM2MClient, LOG_LWM2M_WARN + ": Client registration failed due to retry attempts: " + lwM2MClient.getRetryAttempts().get());
                }
            } else {
                logService.log(lwM2MClient, LOG_LWM2M_WARN + ": Client registration failed due to invalid state: " + stateException.getState());
            }
        } catch (Throwable t) {
            log.error("[{}] endpoint [{}] error Unable registration.", registration.getEndpoint(), t);
            logService.log(lwM2MClient, LOG_LWM2M_WARN + ": Client registration failed due to: " + t.getMessage());
        }
    });
}
Also used : SessionInfoProto(org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto) LwM2MClientStateException(org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientStateException) UUID(java.util.UUID) LwM2mClient(org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient)

Example 2 with LwM2MClientStateException

use of org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientStateException in project thingsboard by thingsboard.

the class DefaultLwM2mUplinkMsgHandler method doUnReg.

private void doUnReg(Registration registration, LwM2mClient client) {
    try {
        logService.log(client, LOG_LWM2M_INFO + ": Client unRegistration");
        clientContext.unregister(client, registration);
        SessionInfoProto sessionInfo = client.getSession();
        if (sessionInfo != null) {
            sessionManager.deregister(sessionInfo);
            sessionStore.remove(registration.getEndpoint());
            log.info("Client close session: [{}] unReg [{}] name  [{}] profile ", registration.getId(), registration.getEndpoint(), sessionInfo.getDeviceType());
        } else {
            log.error("Client close session: [{}] unReg [{}] name  [{}] sessionInfo ", registration.getId(), registration.getEndpoint(), null);
        }
    } catch (LwM2MClientStateException stateException) {
        log.info("[{}] delete registration: [{}] {}.", registration.getEndpoint(), stateException.getState(), stateException.getMessage());
    } catch (Throwable t) {
        log.error("[{}] endpoint [{}] error Unable un registration.", registration.getEndpoint(), t);
        logService.log(client, LOG_LWM2M_ERROR + String.format(": Client Unable un Registration, %s", t.getMessage()));
    }
}
Also used : SessionInfoProto(org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto) LwM2MClientStateException(org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientStateException)

Example 3 with LwM2MClientStateException

use of org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientStateException in project thingsboard by thingsboard.

the class DefaultLwM2mUplinkMsgHandler method updatedReg.

/**
 * if sessionInfo removed from sessions, then new registerAsyncSession
 *
 * @param registration - Registration LwM2M Client
 */
public void updatedReg(Registration registration) {
    executor.submit(() -> {
        LwM2mClient lwM2MClient = clientContext.getClientByEndpoint(registration.getEndpoint());
        try {
            log.info("[{}] [{{}] Client: update after Registration", registration.getEndpoint(), registration.getId());
            logService.log(lwM2MClient, String.format("[%s][%s] Updated registration.", registration.getId(), registration.getSocketAddress()));
            clientContext.updateRegistration(lwM2MClient, registration);
            this.reportActivityAndRegister(lwM2MClient.getSession());
        } catch (LwM2MClientStateException stateException) {
            if (LwM2MClientState.REGISTERED.equals(stateException.getState())) {
                log.info("[{}] update registration failed because client has different registration id: [{}] {}.", registration.getEndpoint(), stateException.getState(), stateException.getMessage());
            } else {
                onRegistered(registration, Collections.emptyList());
            }
        } catch (Throwable t) {
            log.error("[{}] endpoint [{}] error Unable update registration.", registration.getEndpoint(), t);
            logService.log(lwM2MClient, LOG_LWM2M_ERROR + String.format(": Client update Registration, %s", t.getMessage()));
        }
    });
}
Also used : LwM2MClientStateException(org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientStateException) LwM2mClient(org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient)

Aggregations

LwM2MClientStateException (org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientStateException)3 SessionInfoProto (org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto)2 LwM2mClient (org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient)2 UUID (java.util.UUID)1