Skip to content

Commit

Permalink
Adjust update period based on lifetime and COAP_EXCHANGE_LIFETIME.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Feb 28, 2020
1 parent 2d63090 commit b611660
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,28 @@ public synchronized Collection<Server> createEndpoints(Collection<? extends Serv
}
}

@Override
public long getMaxCommunicationPeriodFor(Server server, long lifetimeInMs) {
// See https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/283 to better understand.
// TODO For DTLS, worst Handshake scenario should be taking into account too.

int floor = 30000; // value from which we stop to adjust communication period using COAP EXCHANGE LIFETIME.

// To be sure registration doesn't expired, update request should be send considering all CoAP retransmissions
// and registration lifetime.
// See https://tools.ietf.org/html/rfc7252#section-4.8.2
long exchange_lifetime = coapConfig.getLong(NetworkConfig.Keys.EXCHANGE_LIFETIME, 247);
if (lifetimeInMs - exchange_lifetime >= floor) {
return lifetimeInMs - exchange_lifetime;
} else {
LOG.warn("Too small lifetime : we advice to not use a lifetime < (COAP EXCHANGE LIFETIME + 30s)");
// lifetime value is too short, so we do a compromise and we don't remove COAP EXCHANGE LIFETIME completely
// We distribute the remaining lifetime range [0, exchange_lifetime + floor] on the remaining range
// [1,floor]s.
return lifetimeInMs * (floor - 1000) / (exchange_lifetime + floor) + 1000;
}
}

@Override
public synchronized void forceReconnection(Server server) {
// TODO support multi server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public interface EndpointsManager {

Collection<Server> createEndpoints(Collection<? extends ServerInfo> serverInfo);

long getMaxCommunicationPeriodFor(Server server, long lifetimeInSeconds);

void forceReconnection(Server server);

void start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,15 @@ public class DefaultRegistrationEngine implements RegistrationEngine {

private static final long NOW = 0;

// We choose a default timeout a bit higher to the MAX_TRANSMIT_WAIT(62-93s) which is the time from starting to
// send a Confirmable message to the time when an acknowledgement is no longer expected.
// Timeout for bootstrap/register/update request
private final long requestTimeoutInMs;
// de-registration is only used on stop/destroy for now.
private final long deregistrationTimeoutInMs;
// bootstrap timeout
// Bootstrap session timeout
private final int bootstrapSessionTimeoutInSec;
// time between bootstrap retry should be configurable and incremental
// Time between bootstrap retry should incremental
private final int retryWaitingTimeInMs;
// time between 2 update requests (used only if it is smaller than the lifetime)
// Time between 2 update requests (used only if it is smaller than the lifetime)
private Integer communicationPeriodInMs;

private static enum Status {
Expand Down Expand Up @@ -278,7 +277,7 @@ private Status register(Server server) throws InterruptedException {
LOG.info("Registered with location '{}'.", registrationID);

// Update every lifetime period
long delay = calculateNextUpdate(dmInfo.lifetime);
long delay = calculateNextUpdate(server, dmInfo.lifetime);
scheduleUpdate(server, registrationID, new RegistrationUpdate(), delay);

if (observer != null) {
Expand Down Expand Up @@ -385,7 +384,7 @@ private Status update(Server server, String registrationID, RegistrationUpdate r
} else if (response.getCode() == ResponseCode.CHANGED) {
// Update successful, so we reschedule new update
LOG.info("Registration update succeed.");
long delay = calculateNextUpdate(dmInfo.lifetime);
long delay = calculateNextUpdate(server, dmInfo.lifetime);
scheduleUpdate(server, registrationID, new RegistrationUpdate(), delay);
if (observer != null) {
observer.onUpdateSuccess(server, registrationID);
Expand All @@ -405,13 +404,12 @@ private Status update(Server server, String registrationID, RegistrationUpdate r
}
}

private long calculateNextUpdate(long lifetimeInSeconds) {
// lifetime - 10%
// life time is in seconds and we return the delay in milliseconds
private long calculateNextUpdate(Server server, long lifetimeInSeconds) {
long maxComminucationPeriod = endpointsManager.getMaxCommunicationPeriodFor(server, lifetimeInSeconds * 1000);
if (communicationPeriodInMs != null) {
return Math.min(communicationPeriodInMs, lifetimeInSeconds * 900l);
return Math.min(communicationPeriodInMs, maxComminucationPeriod);
} else {
return lifetimeInSeconds * 900l;
return maxComminucationPeriod;
}
}

Expand Down

0 comments on commit b611660

Please sign in to comment.