use of org.eclipse.californium.core.coap.Token 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();
}
}
use of org.eclipse.californium.core.coap.Token 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;
}
use of org.eclipse.californium.core.coap.Token in project leshan by eclipse.
the class RedisRegistrationStore method removeObservation.
@Override
public Observation removeObservation(String registrationId, byte[] observationId) {
try (Jedis j = pool.getResource()) {
// fetch the client ep by registration ID index
byte[] ep = j.get(toRegIdKey(registrationId));
if (ep == null) {
return null;
}
// remove observation
byte[] lockValue = null;
byte[] lockKey = toLockKey(ep);
try {
lockValue = RedisLock.acquire(j, lockKey);
Observation observation = build(get(new Token(observationId)));
if (observation != null && registrationId.equals(observation.getRegistrationId())) {
unsafeRemoveObservation(j, registrationId, observationId);
return observation;
}
return null;
} finally {
RedisLock.release(j, lockKey, lockValue);
}
}
}
use of org.eclipse.californium.core.coap.Token in project leshan by eclipse.
the class InMemoryRegistrationStore method removeObservation.
@Override
public Observation removeObservation(String registrationId, byte[] observationId) {
try {
lock.writeLock().lock();
Token token = new Token(observationId);
Observation observation = build(unsafeGetObservation(token));
if (observation != null && registrationId.equals(observation.getRegistrationId())) {
unsafeRemoveObservation(token);
return observation;
}
return null;
} finally {
lock.writeLock().unlock();
}
}
use of org.eclipse.californium.core.coap.Token in project leshan by eclipse.
the class InMemoryRegistrationStore method addObservation.
/* *************** Leshan Observation API **************** */
/*
* The observation is not persisted here, it is done by the Californium layer (in the implementation of the
* org.eclipse.californium.core.observe.ObservationStore#add method)
*/
@Override
public Collection<Observation> addObservation(String registrationId, Observation observation) {
List<Observation> removed = new ArrayList<>();
try {
lock.writeLock().lock();
// cancel existing observations for the same path and registration id.
for (Observation obs : unsafeGetObservations(registrationId)) {
if (observation.getPath().equals(obs.getPath()) && !Arrays.equals(observation.getId(), obs.getId())) {
unsafeRemoveObservation(new Token(obs.getId()));
removed.add(obs);
}
}
} finally {
lock.writeLock().unlock();
}
return removed;
}
Aggregations