use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.
the class ObserveTest method can_observe_object.
@Test
public void can_observe_object() throws InterruptedException {
TestObservationListener listener = new TestObservationListener();
helper.server.getObservationService().addListener(listener);
// observe device timezone
ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3));
assertEquals(ResponseCode.CONTENT, observeResponse.getCode());
assertNotNull(observeResponse.getCoapResponse());
assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class)));
// an observation response should have been sent
Observation observation = observeResponse.getObservation();
assertEquals("/3", observation.getPath().toString());
assertEquals(helper.getCurrentRegistration().getId(), observation.getRegistrationId());
Set<Observation> observations = helper.server.getObservationService().getObservations(helper.getCurrentRegistration());
assertTrue("We should have only on observation", observations.size() == 1);
assertTrue("New observation is not there", observations.contains(observation));
// write device timezone
LwM2mResponse writeResponse = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(3, 0, 15, "Europe/Paris"));
// verify result
listener.waitForNotification(2000);
assertEquals(ResponseCode.CHANGED, writeResponse.getCode());
assertTrue(listener.receivedNotify().get());
assertTrue(listener.getResponse().getContent() instanceof LwM2mObject);
assertNotNull(listener.getResponse().getCoapResponse());
assertThat(listener.getResponse().getCoapResponse(), is(instanceOf(Response.class)));
// try to read the object for comparing
ReadResponse readResp = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3));
assertEquals(readResp.getContent(), listener.getResponse().getContent());
}
use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.
the class RedisIntegrationTestHelper method createServer.
@Override
public void createServer() {
LeshanServerBuilder builder = new LeshanServerBuilder();
StaticModelProvider modelProvider = new StaticModelProvider(createObjectModels());
builder.setObjectModelProvider(modelProvider);
DefaultLwM2mNodeDecoder decoder = new DefaultLwM2mNodeDecoder();
builder.setDecoder(decoder);
builder.setLocalAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
builder.setLocalSecureAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
builder.setSecurityStore(new InMemorySecurityStore());
// Create redis store
String redisURI = System.getenv("REDIS_URI");
if (redisURI == null)
redisURI = "";
Pool<Jedis> jedis = new JedisPool(redisURI);
builder.setRegistrationStore(new RedisRegistrationStore(jedis));
// Build server !
server = builder.build();
// monitor client registration
resetLatch();
server.getRegistrationService().addListener(new RegistrationListener() {
@Override
public void updated(RegistrationUpdate update, Registration updatedRegistration, Registration previousRegistration) {
if (updatedRegistration.getEndpoint().equals(getCurrentEndpoint())) {
updateLatch.countDown();
}
}
@Override
public void unregistered(Registration registration, Collection<Observation> observations, boolean expired, Registration newReg) {
if (registration.getEndpoint().equals(getCurrentEndpoint())) {
deregisterLatch.countDown();
}
}
@Override
public void registered(Registration registration, Registration previousReg, Collection<Observation> previousObsersations) {
if (registration.getEndpoint().equals(getCurrentEndpoint())) {
last_registration = registration;
registerLatch.countDown();
}
}
});
}
use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.
the class RegistrationTest method register_observe_deregister_observe.
@Test
public void register_observe_deregister_observe() throws NonUniqueSecurityInfoException, InterruptedException {
// Check client is not registered
helper.assertClientNotRegisterered();
// Start it and wait for registration
helper.client.start();
helper.waitForRegistration(1);
// Check client is well registered
helper.assertClientRegisterered();
// observe device timezone
ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3, 0));
assertEquals(ResponseCode.CONTENT, observeResponse.getCode());
assertNotNull(observeResponse.getCoapResponse());
assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class)));
// check observation registry is not null
Registration currentRegistration = helper.getCurrentRegistration();
Set<Observation> observations = helper.server.getObservationService().getObservations(currentRegistration);
assertEquals(1, observations.size());
Observation obs = observations.iterator().next();
assertEquals(currentRegistration.getId(), obs.getRegistrationId());
assertEquals(new LwM2mPath(3, 0), obs.getPath());
// Check de-registration
helper.client.stop(true);
helper.waitForDeregistration(1);
helper.assertClientNotRegisterered();
observations = helper.server.getObservationService().getObservations(currentRegistration);
assertTrue(observations.isEmpty());
// try to send a new observation
observeResponse = helper.server.send(currentRegistration, new ObserveRequest(3, 0), 50);
assertNull(observeResponse);
// check observationStore is empty
observations = helper.server.getObservationService().getObservations(currentRegistration);
assertTrue(observations.isEmpty());
}
use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.
the class RedisRegistrationStore 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 (Jedis j = pool.getResource()) {
// 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);
// cancel existing observations for the same path and registration id.
for (Observation obs : getObservations(j, registrationId)) {
if (observation.getPath().equals(obs.getPath()) && !Arrays.equals(observation.getId(), obs.getId())) {
removed.add(obs);
unsafeRemoveObservation(j, registrationId, obs.getId());
}
}
} finally {
RedisLock.release(j, lockKey, lockValue);
}
}
return removed;
}
use of org.eclipse.leshan.core.observation.Observation 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);
}
}
}
Aggregations