Search in sources :

Example 16 with Registration

use of org.eclipse.leshan.server.registration.Registration 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 17 with Registration

use of org.eclipse.leshan.server.registration.Registration 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 18 with Registration

use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.

the class SecurityTest method register_update_reregister_device_with_psk_to_server_with_psk.

@Test
public void register_update_reregister_device_with_psk_to_server_with_psk() throws NonUniqueSecurityInfoException {
    // Create PSK server & start it
    // default server support PSK
    helper.createServer();
    helper.server.start();
    // Create PSK Client
    helper.createPSKClient();
    // Add client credentials to the server
    helper.getSecurityStore().add(SecurityInfo.newPreSharedKeyInfo(helper.getCurrentEndpoint(), GOOD_PSK_ID, GOOD_PSK_KEY));
    // Check for registration
    helper.assertClientNotRegisterered();
    helper.client.start();
    helper.waitForRegistration(1);
    Registration registration = helper.getCurrentRegistration();
    helper.assertClientRegisterered();
    // Check for update
    helper.waitForUpdate(LIFETIME);
    helper.assertClientRegisterered();
    // Check stop do not de-register
    helper.client.stop(false);
    helper.ensureNoDeregistration(1);
    helper.assertClientRegisterered();
    // Check new registration
    helper.resetLatch();
    helper.client.start();
    helper.waitForRegistration(1);
    helper.assertClientRegisterered();
    Registration newRegistration = helper.getCurrentRegistration();
    assertNotEquals(registration.getId(), newRegistration.getId());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) Test(org.junit.Test)

Example 19 with Registration

use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.

the class RedisRegistrationStore method updateRegistration.

@Override
public UpdatedRegistration updateRegistration(RegistrationUpdate update) {
    try (Jedis j = pool.getResource()) {
        // fetch the client ep by registration ID index
        byte[] ep = j.get(toRegIdKey(update.getRegistrationId()));
        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);
            Registration updatedRegistration = update.update(r);
            // store the new client
            j.set(toEndpointKey(updatedRegistration.getEndpoint()), serializeReg(updatedRegistration));
            return new UpdatedRegistration(r, updatedRegistration);
        } finally {
            RedisLock.release(j, lockKey, lockValue);
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) UpdatedRegistration(org.eclipse.leshan.server.registration.UpdatedRegistration) Registration(org.eclipse.leshan.server.registration.Registration) UpdatedRegistration(org.eclipse.leshan.server.registration.UpdatedRegistration)

Example 20 with Registration

use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.

the class RedisRegistrationStore method remove.

@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);
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) UpdatedRegistration(org.eclipse.leshan.server.registration.UpdatedRegistration) Registration(org.eclipse.leshan.server.registration.Registration)

Aggregations

Registration (org.eclipse.leshan.server.registration.Registration)44 Test (org.junit.Test)19 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)15 Request (org.eclipse.californium.core.coap.Request)14 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)13 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)13 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)13 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)13 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)13 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)13 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)12 Observation (org.eclipse.leshan.core.observation.Observation)10 UpdatedRegistration (org.eclipse.leshan.server.registration.UpdatedRegistration)10 InetSocketAddress (java.net.InetSocketAddress)7 Jedis (redis.clients.jedis.Jedis)6 RegistrationUpdate (org.eclipse.leshan.server.registration.RegistrationUpdate)5 Deregistration (org.eclipse.leshan.server.registration.Deregistration)4 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)3 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)3 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)3