Search in sources :

Example 1 with RegistrationUpdate

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

the class RegistrationUpdateSerDes method deserialize.

public static RegistrationUpdate deserialize(byte[] data) throws UnknownHostException {
    JsonObject v = (JsonObject) Json.parse(new String(data));
    // mandatory fields
    String regId = v.getString("regId", null);
    Identity identity = IdentitySerDes.deserialize(v.get("identity").asObject());
    // optional fields
    BindingMode b = null;
    if (v.get("bnd") != null) {
        b = BindingMode.valueOf(v.getString("bnd", null));
    }
    Long lifetime = null;
    if (v.get("lt") != null) {
        lifetime = v.getLong("lt", 0);
    }
    String sms = null;
    if (v.get("sms") != null) {
        sms = v.getString("sms", "");
    }
    // parse object link
    JsonArray links = (JsonArray) v.get("objLink");
    Link[] linkObjs = null;
    if (links != null) {
        linkObjs = new Link[links.size()];
        for (int i = 0; i < links.size(); i++) {
            JsonObject ol = (JsonObject) links.get(i);
            Map<String, Object> attMap = new HashMap<>();
            JsonObject att = (JsonObject) ol.get("at");
            for (String k : att.names()) {
                JsonValue jsonValue = att.get(k);
                if (jsonValue.isNull()) {
                    attMap.put(k, null);
                } else if (jsonValue.isNumber()) {
                    attMap.put(k, jsonValue.asInt());
                } else {
                    attMap.put(k, jsonValue.asString());
                }
            }
            Link o = new Link(ol.getString("url", null), attMap);
            linkObjs[i] = o;
        }
    }
    Map<String, String> addAttr = new HashMap<>();
    JsonObject o = (JsonObject) v.get("addAttr");
    for (String k : o.names()) {
        addAttr.put(k, o.getString(k, ""));
    }
    return new RegistrationUpdate(regId, identity, lifetime, sms, b, linkObjs, addAttr);
}
Also used : HashMap(java.util.HashMap) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) BindingMode(org.eclipse.leshan.core.request.BindingMode) JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject) Identity(org.eclipse.leshan.core.request.Identity) RegistrationUpdate(org.eclipse.leshan.server.registration.RegistrationUpdate) Link(org.eclipse.leshan.Link)

Example 2 with RegistrationUpdate

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

the class RegistrationUpdateSerDesTest method ser_and_des_are_equals.

@Test
public void ser_and_des_are_equals() throws Exception {
    Link[] objs = new Link[2];
    Map<String, Object> att = new HashMap<>();
    att.put("ts", 12);
    att.put("rt", "test");
    att.put("hb", null);
    objs[0] = new Link("/0/1024/2", att);
    objs[1] = new Link("/0/2");
    Map<String, String> additionalAtt = new HashMap<>();
    additionalAtt.put("at", "5000");
    RegistrationUpdate ru = new RegistrationUpdate("myId", Identity.unsecure(Inet4Address.getByName("127.0.0.1"), LwM2m.DEFAULT_COAP_PORT), 60000l, null, BindingMode.U, objs, additionalAtt);
    byte[] ser = RegistrationUpdateSerDes.bSerialize(ru);
    RegistrationUpdate ru2 = RegistrationUpdateSerDes.deserialize(ser);
    assertEquals(ru, ru2);
}
Also used : HashMap(java.util.HashMap) RegistrationUpdate(org.eclipse.leshan.server.registration.RegistrationUpdate) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Example 3 with RegistrationUpdate

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

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

the class IntegrationTestHelper method setupRegistrationMonitoring.

protected void setupRegistrationMonitoring() {
    resetLatch();
    server.getRegistrationService().addListener(new RegistrationListener() {

        @Override
        public void updated(RegistrationUpdate update, Registration updatedRegistration, Registration previousRegistration) {
            if (updatedRegistration.getEndpoint().equals(currentEndpointIdentifier.get())) {
                updateLatch.countDown();
            }
        }

        @Override
        public void unregistered(Registration registration, Collection<Observation> observations, boolean expired, Registration newReg) {
            if (registration.getEndpoint().equals(currentEndpointIdentifier.get())) {
                deregisterLatch.countDown();
            }
        }

        @Override
        public void registered(Registration registration, Registration previousReg, Collection<Observation> previousObsersations) {
            if (registration.getEndpoint().equals(currentEndpointIdentifier.get())) {
                last_registration = registration;
                registerLatch.countDown();
            }
        }
    });
}
Also used : RegistrationListener(org.eclipse.leshan.server.registration.RegistrationListener) Registration(org.eclipse.leshan.server.registration.Registration) Observation(org.eclipse.leshan.core.observation.Observation) RegistrationUpdate(org.eclipse.leshan.server.registration.RegistrationUpdate)

Example 5 with RegistrationUpdate

use of org.eclipse.leshan.server.registration.RegistrationUpdate 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)

Aggregations

RegistrationUpdate (org.eclipse.leshan.server.registration.RegistrationUpdate)7 Registration (org.eclipse.leshan.server.registration.Registration)5 Observation (org.eclipse.leshan.core.observation.Observation)3 RegistrationListener (org.eclipse.leshan.server.registration.RegistrationListener)3 Test (org.junit.Test)3 InetSocketAddress (java.net.InetSocketAddress)2 HashMap (java.util.HashMap)2 Link (org.eclipse.leshan.Link)2 DefaultLwM2mNodeDecoder (org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeDecoder)2 LeshanServerBuilder (org.eclipse.leshan.server.californium.LeshanServerBuilder)2 RedisRegistrationStore (org.eclipse.leshan.server.cluster.RedisRegistrationStore)2 StaticModelProvider (org.eclipse.leshan.server.model.StaticModelProvider)2 UpdatedRegistration (org.eclipse.leshan.server.registration.UpdatedRegistration)2 Jedis (redis.clients.jedis.Jedis)2 JedisPool (redis.clients.jedis.JedisPool)2 JsonArray (com.eclipsesource.json.JsonArray)1 JsonObject (com.eclipsesource.json.JsonObject)1 JsonValue (com.eclipsesource.json.JsonValue)1 BindingMode (org.eclipse.leshan.core.request.BindingMode)1 Identity (org.eclipse.leshan.core.request.Identity)1