use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.
the class RedisSecureIntegrationTestHelper 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));
// Create redis store
String redisURI = System.getenv("REDIS_URI");
if (redisURI == null)
redisURI = "";
Pool<Jedis> jedis = new JedisPool(redisURI);
builder.setRegistrationStore(new RedisRegistrationStore(jedis));
builder.setSecurityStore(new RedisSecurityStore(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 ObservationServiceTest method observe_twice_cancels_first.
@Test
public void observe_twice_cancels_first() {
givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 12));
givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 12));
// check the presence of only one observation.
Set<Observation> observations = observationService.getObservations(support.registration);
Assert.assertEquals(1, observations.size());
}
use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.
the class ObservationServiceTest method cancel_by_observation.
@Test
public void cancel_by_observation() throws UnknownHostException {
// create some observations
givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 13));
givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 12));
givenAnObservation("anotherClient", new LwM2mPath(3, 0, 12));
Observation observationToCancel = givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 12));
// check its presence
Set<Observation> observations = observationService.getObservations(support.registration);
Assert.assertEquals(2, observations.size());
// cancel it
observationService.cancelObservation(observationToCancel);
// check its absence
observations = observationService.getObservations(support.registration);
Assert.assertEquals(1, observations.size());
}
use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.
the class ObservationServiceTest method givenAnObservation.
private Observation givenAnObservation(String registrationId, LwM2mPath target) {
Registration registration = store.getRegistration(registrationId);
if (registration == null) {
registration = givenASimpleClient(registrationId);
store.addRegistration(registration);
}
coapRequest = Request.newGet();
coapRequest.setToken(CaliforniumTestSupport.createToken());
coapRequest.getOptions().addUriPath(String.valueOf(target.getObjectId()));
coapRequest.getOptions().addUriPath(String.valueOf(target.getObjectInstanceId()));
coapRequest.getOptions().addUriPath(String.valueOf(target.getResourceId()));
coapRequest.setObserve();
coapRequest.setDestinationContext(EndpointContextUtil.extractContext(support.registration.getIdentity()));
Map<String, String> context = ObserveUtil.createCoapObserveRequestContext(registration.getEndpoint(), registrationId, new ObserveRequest(target.toString()));
coapRequest.setUserContext(context);
store.put(coapRequest.getToken(), new org.eclipse.californium.core.observe.Observation(coapRequest, null));
Observation observation = new Observation(coapRequest.getToken().getBytes(), registrationId, target, null);
observationService.addObservation(registration, observation);
return observation;
}
use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.
the class RedisRequestResponseHandler method sendResponse.
private void sendResponse(String ticket, LwM2mResponse response) {
if (response instanceof ObserveResponse) {
Observation observation = ((ObserveResponse) response).getObservation();
observatioIdToTicket.put(new KeyId(observation.getId()), ticket);
}
try (Jedis j = pool.getResource()) {
JsonObject m = Json.object();
m.add("ticket", ticket);
m.add("rep", ResponseSerDes.jSerialize(response));
j.publish(RESPONSE_CHANNEL, m.toString());
}
}
Aggregations