Search in sources :

Example 36 with Registration

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

the class RegistrationSerDes method deserialize.

public static Registration deserialize(JsonObject jObj) {
    Registration.Builder b = new Registration.Builder(jObj.getString("regId", null), jObj.getString("ep", null), IdentitySerDes.deserialize(jObj.get("identity").asObject()), new InetSocketAddress(jObj.getString("regAddr", null), jObj.getInt("regPort", 0)));
    b.bindingMode(BindingMode.valueOf(jObj.getString("bnd", null)));
    b.lastUpdate(new Date(jObj.getLong("lastUp", 0)));
    b.lifeTimeInSec(jObj.getLong("lt", 0));
    b.lwM2mVersion(jObj.getString("ver", "1.0"));
    b.registrationDate(new Date(jObj.getLong("regDate", 0)));
    if (jObj.get("sms") != null) {
        b.smsNumber(jObj.getString("sms", ""));
    }
    JsonArray links = (JsonArray) jObj.get("objLink");
    Link[] 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;
    }
    b.objectLinks(linkObjs);
    Map<String, String> addAttr = new HashMap<>();
    JsonObject o = (JsonObject) jObj.get("addAttr");
    for (String k : o.names()) {
        addAttr.put(k, o.getString(k, ""));
    }
    b.additionalRegistrationAttributes(addAttr);
    return b.build();
}
Also used : HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) Date(java.util.Date) JsonArray(com.eclipsesource.json.JsonArray) Registration(org.eclipse.leshan.server.registration.Registration) JsonObject(com.eclipsesource.json.JsonObject) Link(org.eclipse.leshan.Link)

Example 37 with Registration

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

the class PresenceServiceTest method testSetOnlineForNonQueueMode.

@Test
public void testSetOnlineForNonQueueMode() throws Exception {
    Registration registration = givenASimpleClient();
    presenceService.addListener(new PresenceListener() {

        @Override
        public void onAwake(Registration registration) {
            fail("No invocation was expected");
        }

        @Override
        public void onSleeping(Registration registration) {
            fail("No invocation was expected");
        }
    });
    presenceService.setAwake(registration);
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) Test(org.junit.Test)

Example 38 with Registration

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

the class PresenceServiceTest method testIsOnline.

@Test
public void testIsOnline() throws Exception {
    Registration queueModeRegistration = givenASimpleClientWithQueueMode();
    assertTrue(presenceService.isClientAwake(queueModeRegistration));
    presenceService.setSleeping(queueModeRegistration);
    assertFalse(presenceService.isClientAwake(queueModeRegistration));
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) Test(org.junit.Test)

Example 39 with Registration

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

the class ClientServlet method doGet.

/**
 * {@inheritDoc}
 */
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // all registered clients
    if (req.getPathInfo() == null) {
        Collection<Registration> registrations = new ArrayList<>();
        for (Iterator<Registration> iterator = server.getRegistrationService().getAllRegistrations(); iterator.hasNext(); ) {
            registrations.add(iterator.next());
        }
        String json = this.gson.toJson(registrations.toArray(new Registration[] {}));
        resp.setContentType("application/json");
        resp.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8));
        resp.setStatus(HttpServletResponse.SC_OK);
        return;
    }
    String[] path = StringUtils.split(req.getPathInfo(), '/');
    if (path.length < 1) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid path");
        return;
    }
    String clientEndpoint = path[0];
    // /endPoint : get client
    if (path.length == 1) {
        Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
        if (registration != null) {
            resp.setContentType("application/json");
            resp.getOutputStream().write(this.gson.toJson(registration).getBytes(StandardCharsets.UTF_8));
            resp.setStatus(HttpServletResponse.SC_OK);
        } else {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.getWriter().format("no registered client with id '%s'", clientEndpoint).flush();
        }
        return;
    }
    // /clients/endPoint/LWRequest/discover : do LightWeight M2M discover request on a given client.
    if (path.length >= 3 && "discover".equals(path[path.length - 1])) {
        String target = StringUtils.substringBetween(req.getPathInfo(), clientEndpoint, "/discover");
        try {
            Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
            if (registration != null) {
                // create & process request
                DiscoverRequest request = new DiscoverRequest(target);
                DiscoverResponse 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 read request on a given client.
    try {
        String target = StringUtils.removeStart(req.getPathInfo(), "/" + clientEndpoint);
        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
            ReadRequest request = new ReadRequest(contentFormat, target);
            ReadResponse 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);
    }
}
Also used : ContentFormat(org.eclipse.leshan.core.request.ContentFormat) ArrayList(java.util.ArrayList) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) Registration(org.eclipse.leshan.server.registration.Registration) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 40 with Registration

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

the class ClientServlet method doPut.

/**
 * {@inheritDoc}
 */
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String[] path = StringUtils.split(req.getPathInfo(), '/');
    String clientEndpoint = path[0];
    // at least /endpoint/objectId/instanceId
    if (path.length < 3) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid path");
        return;
    }
    try {
        String target = StringUtils.removeStart(req.getPathInfo(), "/" + clientEndpoint);
        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);
            WriteRequest request = new WriteRequest(Mode.REPLACE, contentFormat, target, node);
            WriteResponse 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);
    }
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) ContentFormat(org.eclipse.leshan.core.request.ContentFormat) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode)

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