Search in sources :

Example 11 with Observation

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());
}
Also used : ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) Observation(org.eclipse.leshan.core.observation.Observation) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 12 with Observation

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();
            }
        }
    });
}
Also used : StaticModelProvider(org.eclipse.leshan.server.model.StaticModelProvider) RegistrationListener(org.eclipse.leshan.server.registration.RegistrationListener) InetSocketAddress(java.net.InetSocketAddress) Jedis(redis.clients.jedis.Jedis) LeshanServerBuilder(org.eclipse.leshan.server.californium.LeshanServerBuilder) InMemorySecurityStore(org.eclipse.leshan.server.impl.InMemorySecurityStore) DefaultLwM2mNodeDecoder(org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeDecoder) Registration(org.eclipse.leshan.server.registration.Registration) RedisRegistrationStore(org.eclipse.leshan.server.cluster.RedisRegistrationStore) Observation(org.eclipse.leshan.core.observation.Observation) JedisPool(redis.clients.jedis.JedisPool) RegistrationUpdate(org.eclipse.leshan.server.registration.RegistrationUpdate)

Example 13 with Observation

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());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) Test(org.junit.Test)

Example 14 with Observation

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;
}
Also used : Jedis(redis.clients.jedis.Jedis) Observation(org.eclipse.leshan.core.observation.Observation) ArrayList(java.util.ArrayList)

Example 15 with Observation

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);
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Observation(org.eclipse.leshan.core.observation.Observation) Token(org.eclipse.californium.core.coap.Token)

Aggregations

Observation (org.eclipse.leshan.core.observation.Observation)32 Test (org.junit.Test)12 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)11 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)10 Registration (org.eclipse.leshan.server.registration.Registration)10 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)9 ArrayList (java.util.ArrayList)7 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)7 Token (org.eclipse.californium.core.coap.Token)6 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)6 Jedis (redis.clients.jedis.Jedis)6 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)5 Response (org.eclipse.californium.core.coap.Response)4 DefaultLwM2mValueConverter (org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter)4 Deregistration (org.eclipse.leshan.server.registration.Deregistration)4 UpdatedRegistration (org.eclipse.leshan.server.registration.UpdatedRegistration)4 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)3 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)3 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)3 RegistrationListener (org.eclipse.leshan.server.registration.RegistrationListener)3