Search in sources :

Example 1 with Registration

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

the class PresenceServiceTest method givenASimpleClient.

private Registration givenASimpleClient() throws UnknownHostException {
    InetSocketAddress address = InetSocketAddress.createUnresolved("localhost", 5683);
    Registration.Builder builder = new Registration.Builder("ID", "urn:client", Identity.unsecure(Inet4Address.getLoopbackAddress(), 12354), address);
    Registration reg = builder.build();
    presenceService.setAwake(reg);
    return reg;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Registration(org.eclipse.leshan.server.registration.Registration)

Example 2 with Registration

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

the class PresenceServiceTest method givenASimpleClientWithQueueMode.

private Registration givenASimpleClientWithQueueMode() throws UnknownHostException {
    InetSocketAddress address = InetSocketAddress.createUnresolved("localhost", 5683);
    Registration.Builder builder = new Registration.Builder("ID", "urn:client", Identity.unsecure(Inet4Address.getLoopbackAddress(), 12354), address);
    Registration reg = builder.bindingMode(BindingMode.UQ).build();
    presenceService.setAwake(reg);
    return reg;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Registration(org.eclipse.leshan.server.registration.Registration)

Example 3 with Registration

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

the class ClientServlet method doDelete.

@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String[] path = StringUtils.split(req.getPathInfo(), '/');
    String clientEndpoint = path[0];
    // /clients/endPoint/LWRequest/observe : cancel observation for the given resource.
    if (path.length >= 3 && "observe".equals(path[path.length - 1])) {
        try {
            String target = StringUtils.substringsBetween(req.getPathInfo(), clientEndpoint, "/observe")[0];
            Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
            if (registration != null) {
                server.getObservationService().cancelObservations(registration, target);
                resp.setStatus(HttpServletResponse.SC_OK);
            } else {
                resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                resp.getWriter().format("no registered client with id '%s'", clientEndpoint).flush();
            }
        } catch (RuntimeException e) {
            handleException(e, resp);
        }
        return;
    }
    // /clients/endPoint/LWRequest/ : delete instance
    try {
        String target = StringUtils.removeStart(req.getPathInfo(), "/" + clientEndpoint);
        Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
        if (registration != null) {
            DeleteRequest request = new DeleteRequest(target);
            DeleteResponse 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 : DeleteResponse(org.eclipse.leshan.core.response.DeleteResponse) Registration(org.eclipse.leshan.server.registration.Registration) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest)

Example 4 with Registration

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

the class CaliforniumLwM2mBootstrapRequestSender method send.

@Override
public <T extends LwM2mResponse> T send(final String endpointName, final Identity destination, final DownlinkRequest<T> request, long timeout) throws InterruptedException {
    // Create the CoAP request
    // from LwM2m request
    CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(destination, model, encoder);
    request.accept(coapClientRequestBuilder);
    final Request coapRequest = coapClientRequestBuilder.getRequest();
    // Send CoAP request synchronously
    SyncRequestObserver<T> syncMessageObserver = new SyncRequestObserver<T>(coapRequest, timeout) {

        @Override
        public T buildResponse(Response coapResponse) {
            // TODO we need to fix that by removing the Client dependency from LwM2MResponseBuilder or by creating a
            // LwM2mBootstrapResponseBuilder
            Registration registration = new Registration.Builder("fakeregistrationid", endpointName, destination, destination.isSecure() ? secureEndpoint.getAddress() : nonSecureEndpoint.getAddress()).build();
            // Build LwM2m response
            LwM2mResponseBuilder<T> lwm2mResponseBuilder = new LwM2mResponseBuilder<>(coapRequest, coapResponse, registration, model, null, decoder);
            request.accept(lwm2mResponseBuilder);
            return lwm2mResponseBuilder.getResponse();
        }
    };
    coapRequest.addMessageObserver(syncMessageObserver);
    // Send CoAP request asynchronously
    if (destination.isSecure())
        secureEndpoint.sendRequest(coapRequest);
    else
        nonSecureEndpoint.sendRequest(coapRequest);
    // Wait for response, then return it
    return syncMessageObserver.waitForResponse();
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) Registration(org.eclipse.leshan.server.registration.Registration) Request(org.eclipse.californium.core.coap.Request) DownlinkRequest(org.eclipse.leshan.core.request.DownlinkRequest) SyncRequestObserver(org.eclipse.leshan.core.californium.SyncRequestObserver)

Example 5 with Registration

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

the class InMemoryRegistrationStore method removeRegistration.

@Override
public Deregistration removeRegistration(String registrationId) {
    try {
        lock.writeLock().lock();
        Registration registration = getRegistration(registrationId);
        if (registration != null) {
            Collection<Observation> observationsRemoved = unsafeRemoveAllObservations(registration.getId());
            regsByEp.remove(registration.getEndpoint());
            return new Deregistration(registration, observationsRemoved);
        }
        return null;
    } finally {
        lock.writeLock().unlock();
    }
}
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)

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