Search in sources :

Example 26 with Observation

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();
            }
        }
    });
}
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) RedisSecurityStore(org.eclipse.leshan.server.cluster.RedisSecurityStore) LeshanServerBuilder(org.eclipse.leshan.server.californium.LeshanServerBuilder) 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 27 with Observation

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());
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) Test(org.junit.Test)

Example 28 with Observation

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());
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) Test(org.junit.Test)

Example 29 with Observation

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

Example 30 with 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());
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Observation(org.eclipse.leshan.core.observation.Observation) JsonObject(com.eclipsesource.json.JsonObject) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse)

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