Search in sources :

Example 6 with Registration

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

the class InMemoryRegistrationStore method addRegistration.

/* *************** Leshan Registration API **************** */
@Override
public Deregistration addRegistration(Registration registration) {
    try {
        lock.writeLock().lock();
        Registration registrationRemoved = regsByEp.put(registration.getEndpoint(), registration);
        if (registrationRemoved != null) {
            Collection<Observation> observationsRemoved = unsafeRemoveAllObservations(registrationRemoved.getId());
            return new Deregistration(registrationRemoved, observationsRemoved);
        }
    } finally {
        lock.writeLock().unlock();
    }
    return null;
}
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 7 with Registration

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

the class InMemoryRegistrationStore method updateRegistration.

@Override
public UpdatedRegistration updateRegistration(RegistrationUpdate update) {
    try {
        lock.writeLock().lock();
        Registration registration = getRegistration(update.getRegistrationId());
        if (registration == null) {
            return null;
        } else {
            Registration updatedRegistration = update.update(registration);
            regsByEp.put(updatedRegistration.getEndpoint(), updatedRegistration);
            return new UpdatedRegistration(registration, updatedRegistration);
        }
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : UpdatedRegistration(org.eclipse.leshan.server.registration.UpdatedRegistration) Registration(org.eclipse.leshan.server.registration.Registration) UpdatedRegistration(org.eclipse.leshan.server.registration.UpdatedRegistration)

Example 8 with Registration

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

the class ObservationServiceImpl method onNotification.

// ********** NotificationListener interface **********//
@Override
public void onNotification(Request coapRequest, Response coapResponse) {
    LOG.trace("notification received for request {}: {}", coapRequest, coapResponse);
    if (listeners.isEmpty())
        return;
    // get registration Id
    String regid = coapRequest.getUserContext().get(ObserveUtil.CTX_REGID);
    // get observation for this request
    Observation observation = registrationStore.getObservation(regid, coapResponse.getToken().getBytes());
    if (observation == null) {
        LOG.error("Unexpected error: Unable to find observation with token {} for registration {}", coapResponse.getToken(), regid);
        return;
    }
    // get registration
    Registration registration = registrationStore.getRegistration(observation.getRegistrationId());
    if (registration == null) {
        LOG.error("Unexpected error: There is no registration with id {} for this observation {}", observation.getRegistrationId(), observation);
        return;
    }
    try {
        // get model for this registration
        LwM2mModel model = modelProvider.getObjectModel(registration);
        // create response
        ObserveResponse response = createObserveResponse(observation, model, coapResponse);
        // notify all listeners
        for (ObservationListener listener : listeners) {
            listener.onResponse(observation, registration, response);
        }
    } catch (InvalidResponseException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Invalid notification for observation [%s]", observation), e);
        }
        for (ObservationListener listener : listeners) {
            listener.onError(observation, registration, e);
        }
    } catch (RuntimeException e) {
        if (LOG.isErrorEnabled()) {
            LOG.error(String.format("Unable to handle notification for observation [%s]", observation), e);
        }
        for (ObservationListener listener : listeners) {
            listener.onError(observation, registration, e);
        }
    }
}
Also used : ObservationListener(org.eclipse.leshan.server.observation.ObservationListener) Registration(org.eclipse.leshan.server.registration.Registration) Observation(org.eclipse.leshan.core.observation.Observation) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) InvalidResponseException(org.eclipse.leshan.core.request.exception.InvalidResponseException) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse)

Example 9 with Registration

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

the class CoapRequestBuilderTest method build_read_request.

@Test
public void build_read_request() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    ReadRequest request = new ReadRequest(3, 0);
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.GET, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals("coap://127.0.0.1:12354/3/0", coapRequest.getURI());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) Request(org.eclipse.californium.core.coap.Request) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 10 with Registration

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

the class CoapRequestBuilderTest method build_discover_request.

@Test
public void build_discover_request() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    DiscoverRequest request = new DiscoverRequest(3, 0);
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.GET, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals(MediaTypeRegistry.APPLICATION_LINK_FORMAT, coapRequest.getOptions().getAccept());
    assertEquals("coap://127.0.0.1:12354/3/0", coapRequest.getURI());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) Request(org.eclipse.californium.core.coap.Request) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) 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