Search in sources :

Example 1 with Deregistration

use of org.eclipse.leshan.server.registration.Deregistration 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 Deregistration

use of org.eclipse.leshan.server.registration.Deregistration 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 3 with Deregistration

use of org.eclipse.leshan.server.registration.Deregistration 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 4 with Deregistration

use of org.eclipse.leshan.server.registration.Deregistration 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)4 Deregistration (org.eclipse.leshan.server.registration.Deregistration)4 Registration (org.eclipse.leshan.server.registration.Registration)4 UpdatedRegistration (org.eclipse.leshan.server.registration.UpdatedRegistration)4 Jedis (redis.clients.jedis.Jedis)1