Search in sources :

Example 1 with InvalidResponseException

use of org.eclipse.leshan.core.request.exception.InvalidResponseException 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)

Aggregations

LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)1 Observation (org.eclipse.leshan.core.observation.Observation)1 InvalidResponseException (org.eclipse.leshan.core.request.exception.InvalidResponseException)1 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)1 ObservationListener (org.eclipse.leshan.server.observation.ObservationListener)1 Registration (org.eclipse.leshan.server.registration.Registration)1