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();
}
}
});
}
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());
}
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());
}
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);
}
}
}
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);
}
}
}
Aggregations