Search in sources :

Example 41 with Registration

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

the class ClientServlet method doPost.

/**
 * {@inheritDoc}
 */
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String[] path = StringUtils.split(req.getPathInfo(), '/');
    String clientEndpoint = path[0];
    // /clients/endPoint/LWRequest/observe : do LightWeight M2M observe request on a given client.
    if (path.length >= 3 && "observe".equals(path[path.length - 1])) {
        try {
            String target = StringUtils.substringBetween(req.getPathInfo(), clientEndpoint, "/observe");
            Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
            if (registration != null) {
                // get content format
                String contentFormatParam = req.getParameter(FORMAT_PARAM);
                ContentFormat contentFormat = contentFormatParam != null ? ContentFormat.fromName(contentFormatParam.toUpperCase()) : null;
                // create & process request
                ObserveRequest request = new ObserveRequest(contentFormat, target);
                ObserveResponse cResponse = server.send(registration, request, TIMEOUT);
                processDeviceResponse(req, resp, cResponse);
            } else {
                resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                resp.getWriter().format("no registered client with id '%s'", clientEndpoint).flush();
            }
        } catch (RuntimeException | InterruptedException e) {
            handleException(e, resp);
        }
        return;
    }
    String target = StringUtils.removeStart(req.getPathInfo(), "/" + clientEndpoint);
    // /clients/endPoint/LWRequest : do LightWeight M2M execute request on a given client.
    if (path.length == 4) {
        try {
            Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
            if (registration != null) {
                ExecuteRequest request = new ExecuteRequest(target, IOUtils.toString(req.getInputStream()));
                ExecuteResponse cResponse = server.send(registration, request, TIMEOUT);
                processDeviceResponse(req, resp, cResponse);
            } else {
                resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                resp.getWriter().format("no registered client with id '%s'", clientEndpoint).flush();
            }
        } catch (RuntimeException | InterruptedException e) {
            handleException(e, resp);
        }
        return;
    }
    // /clients/endPoint/LWRequest : do LightWeight M2M create request on a given client.
    if (2 <= path.length && path.length <= 3) {
        try {
            Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
            if (registration != null) {
                // get content format
                String contentFormatParam = req.getParameter(FORMAT_PARAM);
                ContentFormat contentFormat = contentFormatParam != null ? ContentFormat.fromName(contentFormatParam.toUpperCase()) : null;
                // create & process request
                LwM2mNode node = extractLwM2mNode(target, req);
                if (node instanceof LwM2mObjectInstance) {
                    CreateRequest request = new CreateRequest(contentFormat, target, (LwM2mObjectInstance) node);
                    CreateResponse cResponse = server.send(registration, request, TIMEOUT);
                    processDeviceResponse(req, resp, cResponse);
                } else {
                    throw new IllegalArgumentException("payload must contain an object instance");
                }
            } else {
                resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                resp.getWriter().format("no registered client with id '%s'", clientEndpoint).flush();
            }
        } catch (RuntimeException | InterruptedException e) {
            handleException(e, resp);
        }
        return;
    }
}
Also used : ContentFormat(org.eclipse.leshan.core.request.ContentFormat) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) ExecuteResponse(org.eclipse.leshan.core.response.ExecuteResponse) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) Registration(org.eclipse.leshan.server.registration.Registration)

Example 42 with Registration

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

the class RedisRegistrationStore method addRegistration.

/* *************** Leshan Registration API **************** */
@Override
public Deregistration addRegistration(Registration registration) {
    try (Jedis j = pool.getResource()) {
        byte[] lockValue = null;
        byte[] lockKey = toLockKey(registration.getEndpoint());
        try {
            lockValue = RedisLock.acquire(j, lockKey);
            // add registration
            byte[] k = toEndpointKey(registration.getEndpoint());
            byte[] old = j.getSet(k, serializeReg(registration));
            // add registration: secondary index
            byte[] idx = toRegIdKey(registration.getId());
            j.set(idx, registration.getEndpoint().getBytes(UTF_8));
            if (old != null) {
                Registration oldRegistration = deserializeReg(old);
                // remove old secondary index
                if (registration.getId() != oldRegistration.getId())
                    j.del(toRegIdKey(oldRegistration.getId()));
                // remove old observation
                Collection<Observation> obsRemoved = unsafeRemoveAllObservations(j, oldRegistration.getId());
                return new Deregistration(oldRegistration, obsRemoved);
            }
            return null;
        } finally {
            RedisLock.release(j, lockKey, lockValue);
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Deregistration(org.eclipse.leshan.server.registration.Deregistration) UpdatedRegistration(org.eclipse.leshan.server.registration.UpdatedRegistration) Registration(org.eclipse.leshan.server.registration.Registration) Observation(org.eclipse.leshan.core.observation.Observation)

Example 43 with Registration

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

the class RedisRegistrationStore method removeRegistration.

private Deregistration removeRegistration(Jedis j, String registrationId, boolean removeOnlyIfNotAlive) {
    // fetch the client ep by registration ID index
    byte[] ep = j.get(toRegIdKey(registrationId));
    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);
        if (!removeOnlyIfNotAlive || !r.isAlive(gracePeriod)) {
            long nbRemoved = j.del(toRegIdKey(r.getId()));
            if (nbRemoved > 0) {
                j.del(toEndpointKey(r.getEndpoint()));
                Collection<Observation> obsRemoved = unsafeRemoveAllObservations(j, r.getId());
                return new Deregistration(r, obsRemoved);
            }
        }
        return null;
    } finally {
        RedisLock.release(j, lockKey, lockValue);
    }
}
Also used : Deregistration(org.eclipse.leshan.server.registration.Deregistration) UpdatedRegistration(org.eclipse.leshan.server.registration.UpdatedRegistration) Registration(org.eclipse.leshan.server.registration.Registration) Observation(org.eclipse.leshan.core.observation.Observation)

Example 44 with Registration

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

the class RegistrationSerDesTest 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");
    Registration.Builder builder = new Registration.Builder("registrationId", "endpoint", Identity.unsecure(Inet4Address.getLoopbackAddress(), 1), new InetSocketAddress(212)).objectLinks(objs);
    builder.registrationDate(new Date(100L));
    builder.lastUpdate(new Date(101L));
    Registration r = builder.build();
    byte[] ser = RegistrationSerDes.bSerialize(r);
    Registration r2 = RegistrationSerDes.deserialize(ser);
    assertEquals(r, r2);
}
Also used : HashMap(java.util.HashMap) Registration(org.eclipse.leshan.server.registration.Registration) InetSocketAddress(java.net.InetSocketAddress) Link(org.eclipse.leshan.Link) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Registration (org.eclipse.leshan.server.registration.Registration)44 Test (org.junit.Test)19 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)15 Request (org.eclipse.californium.core.coap.Request)14 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)13 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)13 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)13 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)13 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)13 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)13 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)12 Observation (org.eclipse.leshan.core.observation.Observation)10 UpdatedRegistration (org.eclipse.leshan.server.registration.UpdatedRegistration)10 InetSocketAddress (java.net.InetSocketAddress)7 Jedis (redis.clients.jedis.Jedis)6 RegistrationUpdate (org.eclipse.leshan.server.registration.RegistrationUpdate)5 Deregistration (org.eclipse.leshan.server.registration.Deregistration)4 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)3 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)3 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)3