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);
}
}
}
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);
}
}
Aggregations