Search in sources :

Example 1 with UpdatedRegistration

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

the class InMemoryRegistrationStore method updateRegistration.

@Override
public UpdatedRegistration updateRegistration(RegistrationUpdate update) {
    try {
        lock.writeLock().lock();
        Registration registration = getRegistration(update.getRegistrationId());
        if (registration == null) {
            return null;
        } else {
            Registration updatedRegistration = update.update(registration);
            regsByEp.put(updatedRegistration.getEndpoint(), updatedRegistration);
            return new UpdatedRegistration(registration, updatedRegistration);
        }
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : UpdatedRegistration(org.eclipse.leshan.server.registration.UpdatedRegistration) Registration(org.eclipse.leshan.server.registration.Registration) UpdatedRegistration(org.eclipse.leshan.server.registration.UpdatedRegistration)

Example 2 with UpdatedRegistration

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

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

the class InMemoryRegistrationStoreTest method update_registration_keeps_properties_unchanged.

@Test
public void update_registration_keeps_properties_unchanged() {
    givenASimpleRegistration(lifetime);
    store.addRegistration(registration);
    RegistrationUpdate update = new RegistrationUpdate(registrationId, Identity.unsecure(address, port), null, null, null, null, null);
    UpdatedRegistration updatedRegistration = store.updateRegistration(update);
    Assert.assertEquals(lifetime, updatedRegistration.getUpdatedRegistration().getLifeTimeInSec());
    Assert.assertSame(binding, updatedRegistration.getUpdatedRegistration().getBindingMode());
    Assert.assertEquals(sms, updatedRegistration.getUpdatedRegistration().getSmsNumber());
    Assert.assertEquals(registration, updatedRegistration.getPreviousRegistration());
    Registration reg = store.getRegistrationByEndpoint(ep);
    Assert.assertEquals(lifetime, reg.getLifeTimeInSec());
    Assert.assertSame(binding, reg.getBindingMode());
    Assert.assertEquals(sms, reg.getSmsNumber());
}
Also used : UpdatedRegistration(org.eclipse.leshan.server.registration.UpdatedRegistration) Registration(org.eclipse.leshan.server.registration.Registration) UpdatedRegistration(org.eclipse.leshan.server.registration.UpdatedRegistration) RegistrationUpdate(org.eclipse.leshan.server.registration.RegistrationUpdate) Test(org.junit.Test)

Example 4 with UpdatedRegistration

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

the class InMemoryRegistrationStoreTest method update_registration_to_extend_time_to_live.

@Test
public void update_registration_to_extend_time_to_live() {
    givenASimpleRegistration(0L);
    store.addRegistration(registration);
    Assert.assertFalse(registration.isAlive());
    RegistrationUpdate update = new RegistrationUpdate(registrationId, Identity.unsecure(address, port), lifetime, null, null, null, null);
    UpdatedRegistration updatedRegistration = store.updateRegistration(update);
    Assert.assertTrue(updatedRegistration.getUpdatedRegistration().isAlive());
    Registration reg = store.getRegistrationByEndpoint(ep);
    Assert.assertTrue(reg.isAlive());
}
Also used : UpdatedRegistration(org.eclipse.leshan.server.registration.UpdatedRegistration) Registration(org.eclipse.leshan.server.registration.Registration) UpdatedRegistration(org.eclipse.leshan.server.registration.UpdatedRegistration) RegistrationUpdate(org.eclipse.leshan.server.registration.RegistrationUpdate) Test(org.junit.Test)

Aggregations

Registration (org.eclipse.leshan.server.registration.Registration)4 UpdatedRegistration (org.eclipse.leshan.server.registration.UpdatedRegistration)4 RegistrationUpdate (org.eclipse.leshan.server.registration.RegistrationUpdate)2 Test (org.junit.Test)2 Jedis (redis.clients.jedis.Jedis)1