Skip to content

Commit

Permalink
Showing 3 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -79,6 +79,8 @@ public class DefaultRegistrationEngine implements RegistrationEngine {
private final int bootstrapSessionTimeoutInSec;
// time between bootstrap retry should be configurable and incremental
private final int retryWaitingTimeInMs;
// time between 2 update requests (used only if it is smaller than the lifetime)
private Integer communicationPeriodInMs;

private static enum Status {
SUCCESS, FAILURE, TIMEOUT
@@ -108,7 +110,8 @@ private static enum Status {
public DefaultRegistrationEngine(String endpoint, LwM2mObjectTree objectTree, EndpointsManager endpointsManager,
LwM2mRequestSender requestSender, BootstrapHandler bootstrapState, LwM2mClientObserver observer,
Map<String, String> additionalAttributes, ScheduledExecutorService executor, long requestTimeoutInMs,
long deregistrationTimeoutInMs, int bootstrapSessionTimeoutInSec, int retryWaitingTimeInMs) {
long deregistrationTimeoutInMs, int bootstrapSessionTimeoutInSec, int retryWaitingTimeInMs,
Integer communicationPeriodInMs) {
this.endpoint = endpoint;
this.objectEnablers = objectTree.getObjectEnablers();
this.bootstrapHandler = bootstrapState;
@@ -120,6 +123,7 @@ public DefaultRegistrationEngine(String endpoint, LwM2mObjectTree objectTree, En
this.deregistrationTimeoutInMs = deregistrationTimeoutInMs;
this.bootstrapSessionTimeoutInSec = bootstrapSessionTimeoutInSec;
this.retryWaitingTimeInMs = retryWaitingTimeInMs;
this.communicationPeriodInMs = communicationPeriodInMs;

if (executor == null) {
schedExecutor = createScheduledExecutor();
@@ -404,7 +408,11 @@ 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
return lifetimeInSeconds * 900l;
if (communicationPeriodInMs != null) {
return Math.min(communicationPeriodInMs, lifetimeInSeconds * 900l);
} else {
return lifetimeInSeconds * 900l;
}
}

private synchronized boolean scheduleClientInitiatedBootstrap(long timeInMs) {
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ public class DefaultRegistrationEngineFactory implements RegistrationEngineFacto
private long deregistrationTimeoutInMs = 1000; // 1s in ms
private int bootstrapSessionTimeoutInSec = 93;
private int retryWaitingTimeInMs = 10 * 60 * 1000; // 10min in ms
private Integer communicationPeriodInMs = null;

public DefaultRegistrationEngineFactory() {
}
@@ -41,7 +42,19 @@ public RegistrationEngine createRegistratioEngine(String endpoint, LwM2mObjectTr
ScheduledExecutorService sharedExecutor) {
return new DefaultRegistrationEngine(endpoint, objectTree, endpointsManager, requestSender, bootstrapState,
observer, additionalAttributes, sharedExecutor, requestTimeoutInMs, deregistrationTimeoutInMs,
bootstrapSessionTimeoutInSec, retryWaitingTimeInMs);
bootstrapSessionTimeoutInSec, retryWaitingTimeInMs, communicationPeriodInMs);
}

/**
* Set the period between 2 communications (update request).
* <p>
* Client will communicate periodically to refresh its lifetime but if you want to communication periodically more
* often you can set a smaller communication period.
*
* @param communicationPeriodInMs the communication period in ms
*/
public void setCommunicationPeriod(Integer communicationPeriodInMs) {
this.communicationPeriodInMs = communicationPeriodInMs;
}

/**
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@
import org.eclipse.leshan.LwM2m;
import org.eclipse.leshan.client.californium.LeshanClient;
import org.eclipse.leshan.client.californium.LeshanClientBuilder;
import org.eclipse.leshan.client.engine.DefaultRegistrationEngineFactory;
import org.eclipse.leshan.client.object.Server;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
@@ -143,6 +144,8 @@ public static void main(final String[] args) {
options.addOption("b", false, "If present use bootstrap.");
options.addOption("l", true, String.format(
"The lifetime in seconds used to register, ignored if -b is used.\n Default : %ds", DEFAULT_LIFETIME));
options.addOption("cp", true,
"The communication period in seconds which should be smaller than the lifeime, will be used even if -b is used.");

This comment has been minimized.

Copy link
@kevincali

kevincali Apr 30, 2020

There's a small typo:
lifeime -> lifetime

This comment has been minimized.

Copy link
@sbernard31

sbernard31 Apr 30, 2020

Author Contributor

I will fix this !
Thx to reporting it 🙏

This comment has been minimized.

Copy link
@sbernard31

sbernard31 Apr 30, 2020

Author Contributor

Done : 5e9753a

options.addOption("lh", true, "Set the local CoAP address of the Client.\n Default: any local address.");
options.addOption("lp", true,
"Set the local CoAP port of the Client.\n Default: A valid port value is between 0 and 65535.");
@@ -261,6 +264,12 @@ public static void main(final String[] args) {
lifetime = DEFAULT_LIFETIME;
}

// Get lifetime
Integer communicationPeriod = null;
if (cl.hasOption("cp")) {
communicationPeriod = Integer.valueOf(cl.getOptionValue("cp")) * 1000;
}

// Get additional attributes
Map<String, String> additionalAttributes = null;
if (cl.hasOption("aa")) {
@@ -385,9 +394,9 @@ public static void main(final String[] args) {
}
try {
createAndStartClient(endpoint, localAddress, localPort, cl.hasOption("b"), additionalAttributes, lifetime,
serverURI, pskIdentity, pskKey, clientPrivateKey, clientPublicKey, serverPublicKey,
clientCertificate, serverCertificate, latitude, longitude, scaleFactor, cl.hasOption("ocf"),
cl.hasOption("oc"));
communicationPeriod, serverURI, pskIdentity, pskKey, clientPrivateKey, clientPublicKey,
serverPublicKey, clientCertificate, serverCertificate, latitude, longitude, scaleFactor,
cl.hasOption("ocf"), cl.hasOption("oc"));
} catch (Exception e) {
System.err.println("Unable to create and start client ...");
e.printStackTrace();
@@ -396,11 +405,11 @@ public static void main(final String[] args) {
}

public static void createAndStartClient(String endpoint, String localAddress, int localPort, boolean needBootstrap,
Map<String, String> additionalAttributes, int lifetime, String serverURI, byte[] pskIdentity, byte[] pskKey,
PrivateKey clientPrivateKey, PublicKey clientPublicKey, PublicKey serverPublicKey,
X509Certificate clientCertificate, X509Certificate serverCertificate, Float latitude, Float longitude,
float scaleFactor, boolean supportOldFormat, boolean supportDeprecatedCiphers)
throws CertificateEncodingException {
Map<String, String> additionalAttributes, int lifetime, Integer communicationPeriod, String serverURI,
byte[] pskIdentity, byte[] pskKey, PrivateKey clientPrivateKey, PublicKey clientPublicKey,
PublicKey serverPublicKey, X509Certificate clientCertificate, X509Certificate serverCertificate,
Float latitude, Float longitude, float scaleFactor, boolean supportOldFormat,
boolean supportDeprecatedCiphers) throws CertificateEncodingException {

locationInstance = new MyLocation(latitude, longitude, scaleFactor);

@@ -464,12 +473,17 @@ public static void createAndStartClient(String endpoint, String localAddress, in
DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder();
dtlsConfig.setRecommendedCipherSuitesOnly(!supportDeprecatedCiphers);

// Configure Registration Engine
DefaultRegistrationEngineFactory engineFactory = new DefaultRegistrationEngineFactory();
engineFactory.setCommunicationPeriod(communicationPeriod);

// Create client
LeshanClientBuilder builder = new LeshanClientBuilder(endpoint);
builder.setLocalAddress(localAddress, localPort);
builder.setObjects(enablers);
builder.setCoapConfig(coapConfig);
builder.setDtlsConfig(dtlsConfig);
builder.setRegistrationEngineFactory(engineFactory);
if (supportOldFormat) {
builder.setDecoder(new DefaultLwM2mNodeDecoder(true));
builder.setEncoder(new DefaultLwM2mNodeEncoder(true));

0 comments on commit 2d63090

Please sign in to comment.