Search in sources :

Example 31 with Observation

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

the class RedisRegistrationStore method addRegistration.

/* *************** Leshan Registration API **************** */
@Override
public Deregistration addRegistration(Registration registration) {
    try (Jedis j = pool.getResource()) {
        byte[] lockValue = null;
        byte[] lockKey = toLockKey(registration.getEndpoint());
        try {
            lockValue = RedisLock.acquire(j, lockKey);
            // add registration
            byte[] k = toEndpointKey(registration.getEndpoint());
            byte[] old = j.getSet(k, serializeReg(registration));
            // add registration: secondary index
            byte[] idx = toRegIdKey(registration.getId());
            j.set(idx, registration.getEndpoint().getBytes(UTF_8));
            if (old != null) {
                Registration oldRegistration = deserializeReg(old);
                // remove old secondary index
                if (registration.getId() != oldRegistration.getId())
                    j.del(toRegIdKey(oldRegistration.getId()));
                // remove old observation
                Collection<Observation> obsRemoved = unsafeRemoveAllObservations(j, oldRegistration.getId());
                return new Deregistration(oldRegistration, obsRemoved);
            }
            return null;
        } finally {
            RedisLock.release(j, lockKey, lockValue);
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) 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 32 with Observation

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

the class RedisRegistrationStore method removeRegistration.

private Deregistration removeRegistration(Jedis j, String registrationId, boolean removeOnlyIfNotAlive) {
    // fetch the client ep by registration ID index
    byte[] ep = j.get(toRegIdKey(registrationId));
    if (ep == null) {
        return null;
    }
    byte[] lockValue = null;
    byte[] lockKey = toLockKey(ep);
    try {
        lockValue = RedisLock.acquire(j, lockKey);
        // fetch the client
        byte[] data = j.get(toEndpointKey(ep));
        if (data == null) {
            return null;
        }
        Registration r = deserializeReg(data);
        if (!removeOnlyIfNotAlive || !r.isAlive(gracePeriod)) {
            long nbRemoved = j.del(toRegIdKey(r.getId()));
            if (nbRemoved > 0) {
                j.del(toEndpointKey(r.getEndpoint()));
                Collection<Observation> obsRemoved = unsafeRemoveAllObservations(j, r.getId());
                return new Deregistration(r, obsRemoved);
            }
        }
        return null;
    } finally {
        RedisLock.release(j, lockKey, lockValue);
    }
}
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)

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