Search in sources :

Example 1 with Observation

use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.

the class InMemoryRegistrationStore method removeRegistration.

@Override
public Deregistration removeRegistration(String registrationId) {
    try {
        lock.writeLock().lock();
        Registration registration = getRegistration(registrationId);
        if (registration != null) {
            Collection<Observation> observationsRemoved = unsafeRemoveAllObservations(registration.getId());
            regsByEp.remove(registration.getEndpoint());
            return new Deregistration(registration, observationsRemoved);
        }
        return null;
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : Deregistration(org.eclipse.leshan.server.registration.Deregistration) UpdatedRegistration(org.eclipse.leshan.server.registration.UpdatedRegistration) Registration(org.eclipse.leshan.server.registration.Registration) Observation(org.eclipse.leshan.core.observation.Observation)

Example 2 with Observation

use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.

the class InMemoryRegistrationStore method getObservation.

@Override
public Observation getObservation(String registrationId, byte[] observationId) {
    try {
        lock.readLock().lock();
        Observation observation = build(unsafeGetObservation(new Token(observationId)));
        if (observation != null && registrationId.equals(observation.getRegistrationId())) {
            return observation;
        }
        return null;
    } finally {
        lock.readLock().unlock();
    }
}
Also used : Observation(org.eclipse.leshan.core.observation.Observation) Token(org.eclipse.californium.core.coap.Token)

Example 3 with Observation

use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.

the class InMemoryRegistrationStore method addRegistration.

/* *************** Leshan Registration API **************** */
@Override
public Deregistration addRegistration(Registration registration) {
    try {
        lock.writeLock().lock();
        Registration registrationRemoved = regsByEp.put(registration.getEndpoint(), registration);
        if (registrationRemoved != null) {
            Collection<Observation> observationsRemoved = unsafeRemoveAllObservations(registrationRemoved.getId());
            return new Deregistration(registrationRemoved, observationsRemoved);
        }
    } finally {
        lock.writeLock().unlock();
    }
    return null;
}
Also used : Deregistration(org.eclipse.leshan.server.registration.Deregistration) UpdatedRegistration(org.eclipse.leshan.server.registration.UpdatedRegistration) Registration(org.eclipse.leshan.server.registration.Registration) Observation(org.eclipse.leshan.core.observation.Observation)

Example 4 with Observation

use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.

the class InMemoryRegistrationStore method unsafeGetObservations.

private Collection<Observation> unsafeGetObservations(String registrationId) {
    Collection<Observation> result = new ArrayList<>();
    Set<Token> tokens = tokensByRegId.get(registrationId);
    if (tokens != null) {
        for (Token token : tokens) {
            Observation obs = build(unsafeGetObservation(token));
            if (obs != null) {
                result.add(obs);
            }
        }
    }
    return result;
}
Also used : Observation(org.eclipse.leshan.core.observation.Observation) ArrayList(java.util.ArrayList) Token(org.eclipse.californium.core.coap.Token)

Example 5 with Observation

use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.

the class ObservationServiceImpl method onNotification.

// ********** NotificationListener interface **********//
@Override
public void onNotification(Request coapRequest, Response coapResponse) {
    LOG.trace("notification received for request {}: {}", coapRequest, coapResponse);
    if (listeners.isEmpty())
        return;
    // get registration Id
    String regid = coapRequest.getUserContext().get(ObserveUtil.CTX_REGID);
    // get observation for this request
    Observation observation = registrationStore.getObservation(regid, coapResponse.getToken().getBytes());
    if (observation == null) {
        LOG.error("Unexpected error: Unable to find observation with token {} for registration {}", coapResponse.getToken(), regid);
        return;
    }
    // get registration
    Registration registration = registrationStore.getRegistration(observation.getRegistrationId());
    if (registration == null) {
        LOG.error("Unexpected error: There is no registration with id {} for this observation {}", observation.getRegistrationId(), observation);
        return;
    }
    try {
        // get model for this registration
        LwM2mModel model = modelProvider.getObjectModel(registration);
        // create response
        ObserveResponse response = createObserveResponse(observation, model, coapResponse);
        // notify all listeners
        for (ObservationListener listener : listeners) {
            listener.onResponse(observation, registration, response);
        }
    } catch (InvalidResponseException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Invalid notification for observation [%s]", observation), e);
        }
        for (ObservationListener listener : listeners) {
            listener.onError(observation, registration, e);
        }
    } catch (RuntimeException e) {
        if (LOG.isErrorEnabled()) {
            LOG.error(String.format("Unable to handle notification for observation [%s]", observation), e);
        }
        for (ObservationListener listener : listeners) {
            listener.onError(observation, registration, e);
        }
    }
}
Also used : ObservationListener(org.eclipse.leshan.server.observation.ObservationListener) Registration(org.eclipse.leshan.server.registration.Registration) Observation(org.eclipse.leshan.core.observation.Observation) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) InvalidResponseException(org.eclipse.leshan.core.request.exception.InvalidResponseException) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse)

Aggregations

Observation (org.eclipse.leshan.core.observation.Observation)32 Test (org.junit.Test)12 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)11 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)10 Registration (org.eclipse.leshan.server.registration.Registration)10 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)9 ArrayList (java.util.ArrayList)7 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)7 Token (org.eclipse.californium.core.coap.Token)6 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)6 Jedis (redis.clients.jedis.Jedis)6 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)5 Response (org.eclipse.californium.core.coap.Response)4 DefaultLwM2mValueConverter (org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter)4 Deregistration (org.eclipse.leshan.server.registration.Deregistration)4 UpdatedRegistration (org.eclipse.leshan.server.registration.UpdatedRegistration)4 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)3 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)3 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)3 RegistrationListener (org.eclipse.leshan.server.registration.RegistrationListener)3