Search in sources :

Example 1 with ObserveRelation

use of org.eclipse.californium.core.observe.ObserveRelation in project thingsboard by thingsboard.

the class CoapTransportResource method checkObserveRelation.

/*
     * Overwritten method from CoapResource to be able to manage our own observe notification counters.
     */
@Override
public void checkObserveRelation(Exchange exchange, Response response) {
    String token = getTokenFromRequest(exchange.getRequest());
    final ObserveRelation relation = exchange.getRelation();
    if (relation == null || relation.isCanceled()) {
        // because request did not try to establish a relation
        return;
    }
    if (response.getCode().isSuccess()) {
        if (!relation.isEstablished()) {
            relation.setEstablished();
            addObserveRelation(relation);
        }
        AtomicInteger state = clients.getNotificationCounterByToken(token);
        if (state != null) {
            response.getOptions().setObserve(state.getAndIncrement());
        } else {
            response.getOptions().removeObserve();
        }
    }
// ObserveLayer takes care of the else case
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ObserveRelation(org.eclipse.californium.core.observe.ObserveRelation)

Example 2 with ObserveRelation

use of org.eclipse.californium.core.observe.ObserveRelation in project thingsboard by thingsboard.

the class LwM2mTransportCoapResource method checkObserveRelation.

@Override
public void checkObserveRelation(Exchange exchange, Response response) {
    String token = getTokenFromRequest(exchange.getRequest());
    final ObserveRelation relation = exchange.getRelation();
    if (relation == null || relation.isCanceled()) {
        // because request did not try to establish a relation
        return;
    }
    if (response.getCode().isSuccess()) {
        if (!relation.isEstablished()) {
            relation.setEstablished();
            addObserveRelation(relation);
        }
        AtomicInteger notificationCounter = tokenToObserveNotificationSeqMap.computeIfAbsent(token, s -> new AtomicInteger(0));
        response.getOptions().setObserve(notificationCounter.getAndIncrement());
    }
// ObserveLayer takes care of the else case
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ObserveRelation(org.eclipse.californium.core.observe.ObserveRelation)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ObserveRelation (org.eclipse.californium.core.observe.ObserveRelation)2