@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(); } }
@Override public org.eclipse.californium.core.observe.Observation get(Token token) { try (Jedis j = pool.getResource()) { byte[] obs = j.get(toKey(OBS_TKN, token.getBytes())); if (obs == null) { return null; } else { return deserializeObs(obs); } } }
@Override public Observation getObservation(String registrationId, byte[] observationId) { return build(get(new Token(observationId))); }
@Override public org.eclipse.californium.core.observe.Observation get(Token token) { try (Jedis j = pool.getResource()) { byte[] obs = j.get(toKey(OBS_TKN, token.getBytes())); if (obs == null) { return null; } else { return deserializeObs(obs); } } }
@Override public Observation getObservation(String registrationId, byte[] observationId) { return build(get(new Token(observationId))); }
/** * Create a LWM2M observation from a CoAP request. */ public static Observation createLwM2mObservation(Request request) { String regId = null; String lwm2mPath = null; Map<String, String> context = null; for (Entry<String, String> ctx : request.getUserContext().entrySet()) { switch (ctx.getKey()) { case CTX_REGID: regId = ctx.getValue(); break; case CTX_LWM2M_PATH: lwm2mPath = ctx.getValue(); break; case CTX_ENDPOINT: break; default: if (context == null) { context = new HashMap<>(); } context.put(ctx.getKey(), ctx.getValue()); } } return new Observation(request.getToken().getBytes(), regId, new LwM2mPath(lwm2mPath), context); }
@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(); } }
Observation observation = registrationStore.getObservation(regid, coapResponse.getToken().getBytes()); if (observation == null) { LOG.error("Unexpected error: Unable to find observation with token {} for registration {}",
@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; }
@Override public void remove(Token token) { try (Jedis j = pool.getResource()) { byte[] tokenKey = toKey(OBS_TKN, token.getBytes()); // fetch the observation by token byte[] serializedObs = j.get(tokenKey); if (serializedObs == null) return; org.eclipse.californium.core.observe.Observation obs = deserializeObs(serializedObs); String registrationId = ObserveUtil.extractRegistrationId(obs); Registration registration = getRegistration(j, registrationId); if (registration == null) { LOG.warn("Unable to remove observation {}, registration {} does not exist anymore", obs.getRequest(), registrationId); return; } String endpoint = registration.getEndpoint(); byte[] lockValue = null; byte[] lockKey = toKey(LOCK_EP, endpoint); try { lockValue = RedisLock.acquire(j, lockKey); unsafeRemoveObservation(j, registrationId, token.getBytes()); } finally { RedisLock.release(j, lockKey, lockValue); } } }
private void cancel(Observation observation) { Token token = new Token(observation.getId()); if (secureEndpoint != null) secureEndpoint.cancelObservation(token); if (nonSecureEndpoint != null) nonSecureEndpoint.cancelObservation(token); for (ObservationListener listener : listeners) { listener.cancelled(observation); } }
@Override public void remove(Token token) { try (Jedis j = pool.getResource()) { byte[] tokenKey = toKey(OBS_TKN, token.getBytes()); // fetch the observation by token byte[] serializedObs = j.get(tokenKey); if (serializedObs == null) return; org.eclipse.californium.core.observe.Observation obs = deserializeObs(serializedObs); String registrationId = ObserveUtil.extractRegistrationId(obs); Registration registration = getRegistration(j, registrationId); if (registration == null) { LOG.warn("Unable to remove observation {}, registration {} does not exist anymore", obs.getRequest(), registrationId); return; } String endpoint = registration.getEndpoint(); byte[] lockValue = null; byte[] lockKey = toKey(LOCK_EP, endpoint); try { lockValue = RedisLock.acquire(j, lockKey); unsafeRemoveObservation(j, registrationId, token.getBytes()); } finally { RedisLock.release(j, lockKey, lockValue); } } }
@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); } } }
if (!j.exists(toRegIdKey(registrationId))) throw new IllegalStateException("no registration for this Id"); byte[] key = toKey(OBS_TKN, obs.getRequest().getToken().getBytes()); byte[] serializeObs = serializeObs(obs); byte[] previousValue = null; j.lpush(toKey(OBS_TKNS_REGID_IDX, registrationId), obs.getRequest().getToken().getBytes());
@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); } } }
if (!j.exists(toRegIdKey(registrationId))) throw new IllegalStateException("no registration for this Id"); byte[] key = toKey(OBS_TKN, obs.getRequest().getToken().getBytes()); byte[] serializeObs = serializeObs(obs); byte[] previousValue = null; j.lpush(toKey(OBS_TKNS_REGID_IDX, registrationId), obs.getRequest().getToken().getBytes());