Skip to content

Commit

Permalink
Add Support of 'ct' option in registration at client side.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jun 3, 2021
1 parent 266445f commit 55d318c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.security.cert.Certificate;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ScheduledExecutorService;

import org.eclipse.californium.core.CoapResource;
Expand Down Expand Up @@ -125,8 +127,10 @@ public LeshanClient(String endpoint, InetSocketAddress localAddress,
endpointsManager = createEndpointsManager(localAddress, coapConfig, dtlsConfigBuilder, trustStore,
endpointFactory);
requestSender = createRequestSender(endpointsManager, sharedExecutor, encoder, objectTree.getModel());

engine = engineFactory.createRegistratioEngine(endpoint, objectTree, endpointsManager, requestSender,
bootstrapHandler, observers, additionalAttributes, bsAdditionalAttributes, sharedExecutor);
bootstrapHandler, observers, additionalAttributes, bsAdditionalAttributes,
getSupportedContentFormat(decoder, encoder), sharedExecutor);

coapServer = createCoapServer(coapConfig, sharedExecutor);
coapServer.add(createBootstrapResource(engine, endpointsManager, bootstrapHandler));
Expand Down Expand Up @@ -245,6 +249,13 @@ protected RegistrationUpdateHandler createRegistrationUpdateHandler(Registration
return registrationUpdateHandler;
}

protected Set<ContentFormat> getSupportedContentFormat(LwM2mNodeDecoder decoder, LwM2mNodeEncoder encoder) {
Set<ContentFormat> supportedContentFormat = new TreeSet<>();
supportedContentFormat.addAll(decoder.getSupportedContentFormat());
supportedContentFormat.addAll(encoder.getSupportedContentFormat());
return supportedContentFormat;
}

@Override
public void start() {
LOG.info("Starting Leshan client ...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,28 @@ public void listen(final LwM2mObjectTree objecTree) {
public void objectInstancesRemoved(LwM2mObjectEnabler object, int... instanceIds) {
if (!bsHandler.isBootstrapping())
engine.triggerRegistrationUpdate(new RegistrationUpdate(
LinkFormatHelper.getClientDescription(objecTree.getObjectEnablers().values(), null)));
LinkFormatHelper.getClientDescription(objecTree.getObjectEnablers().values(), null, null)));
}

@Override
public void objectInstancesAdded(LwM2mObjectEnabler object, int... instanceIds) {
if (!bsHandler.isBootstrapping())
engine.triggerRegistrationUpdate(new RegistrationUpdate(
LinkFormatHelper.getClientDescription(objecTree.getObjectEnablers().values(), null)));
LinkFormatHelper.getClientDescription(objecTree.getObjectEnablers().values(), null, null)));
}

@Override
public void objectRemoved(LwM2mObjectEnabler object) {
if (!bsHandler.isBootstrapping())
engine.triggerRegistrationUpdate(new RegistrationUpdate(
LinkFormatHelper.getClientDescription(objecTree.getObjectEnablers().values(), null)));
LinkFormatHelper.getClientDescription(objecTree.getObjectEnablers().values(), null, null)));
}

@Override
public void objectAdded(LwM2mObjectEnabler object) {
if (!bsHandler.isBootstrapping())
engine.triggerRegistrationUpdate(new RegistrationUpdate(
LinkFormatHelper.getClientDescription(objecTree.getObjectEnablers().values(), null)));
LinkFormatHelper.getClientDescription(objecTree.getObjectEnablers().values(), null, null)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
Expand All @@ -41,6 +42,7 @@
import org.eclipse.leshan.client.servers.ServerInfo;
import org.eclipse.leshan.client.servers.ServersInfoExtractor;
import org.eclipse.leshan.client.util.LinkFormatHelper;
import org.eclipse.leshan.core.Link;
import org.eclipse.leshan.core.LwM2m.Version;
import org.eclipse.leshan.core.LwM2mId;
import org.eclipse.leshan.core.ResponseCode;
Expand Down Expand Up @@ -101,6 +103,7 @@ private static enum Status {
// device state
private final String endpoint;
private final ContentFormat preferredContentFormat; // used for bootstrap
private final Set<ContentFormat> supportedContentFormats;
private final Map<String, String> additionalAttributes;
private final Map<String, String> bsAdditionalAttributes; // @since 1.1
private final Map<Integer /* objectId */, LwM2mObjectEnabler> objectEnablers;
Expand All @@ -123,24 +126,13 @@ private static enum Status {
private final ScheduledExecutorService schedExecutor;
private final boolean attachedExecutor;

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,
Integer communicationPeriodInMs, boolean reconnectOnUpdate, boolean resumeOnConnect) {
this(endpoint, objectTree, endpointsManager, requestSender, bootstrapState, observer, additionalAttributes,
null, executor, requestTimeoutInMs, deregistrationTimeoutInMs, bootstrapSessionTimeoutInSec,
retryWaitingTimeInMs, communicationPeriodInMs, reconnectOnUpdate, resumeOnConnect, false, null);
}

/** @since 1.1 */
public DefaultRegistrationEngine(String endpoint, LwM2mObjectTree objectTree, EndpointsManager endpointsManager,
LwM2mRequestSender requestSender, BootstrapHandler bootstrapState, LwM2mClientObserver observer,
Map<String, String> additionalAttributes, Map<String, String> bsAdditionalAttributes,
ScheduledExecutorService executor, long requestTimeoutInMs, long deregistrationTimeoutInMs,
int bootstrapSessionTimeoutInSec, int retryWaitingTimeInMs, Integer communicationPeriodInMs,
boolean reconnectOnUpdate, boolean resumeOnConnect, boolean useQueueMode,
ContentFormat preferredContentFormat) {
ContentFormat preferredContentFormat, Set<ContentFormat> supportedContentFormats) {
this.endpoint = endpoint;
this.objectEnablers = objectTree.getObjectEnablers();
this.bootstrapHandler = bootstrapState;
Expand All @@ -160,6 +152,7 @@ public DefaultRegistrationEngine(String endpoint, LwM2mObjectTree objectTree, En
this.resumeOnConnect = resumeOnConnect;
this.queueMode = useQueueMode;
this.preferredContentFormat = preferredContentFormat;
this.supportedContentFormats = supportedContentFormats;

if (executor == null) {
schedExecutor = createScheduledExecutor();
Expand Down Expand Up @@ -307,11 +300,14 @@ private Status register(ServerIdentity server) throws InterruptedException {
LOG.info("Trying to register to {} ...", server.getUri());
RegisterRequest request = null;
try {
Version lwM2mVersion = Version.lastSupported();
EnumSet<BindingMode> supportedBindingMode = ServersInfoExtractor
.getDeviceSupportedBindingMode(objectEnablers.get(LwM2mId.DEVICE), 0);
request = new RegisterRequest(endpoint, dmInfo.lifetime, Version.lastSupported().toString(),
supportedBindingMode, queueMode, null,
LinkFormatHelper.getClientDescription(objectEnablers.values(), null), additionalAttributes);
Link[] links = LinkFormatHelper.getClientDescription(objectEnablers.values(), null,
ContentFormat.getOptionalContentFormatForClient(supportedContentFormats, lwM2mVersion));

request = new RegisterRequest(endpoint, dmInfo.lifetime, lwM2mVersion.toString(), supportedBindingMode,
queueMode, null, links, additionalAttributes);
if (observer != null) {
observer.onRegistrationStarted(server, request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.eclipse.leshan.client.engine;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;

import org.eclipse.leshan.client.EndpointsManager;
Expand Down Expand Up @@ -49,11 +50,12 @@ public DefaultRegistrationEngineFactory() {
public RegistrationEngine createRegistratioEngine(String endpoint, LwM2mObjectTree objectTree,
EndpointsManager endpointsManager, LwM2mRequestSender requestSender, BootstrapHandler bootstrapState,
LwM2mClientObserver observer, Map<String, String> additionalAttributes,
Map<String, String> bsAdditionalAttributes, ScheduledExecutorService sharedExecutor) {
Map<String, String> bsAdditionalAttributes, Set<ContentFormat> supportedContentFormat,
ScheduledExecutorService sharedExecutor) {
return new DefaultRegistrationEngine(endpoint, objectTree, endpointsManager, requestSender, bootstrapState,
observer, additionalAttributes, bsAdditionalAttributes, sharedExecutor, requestTimeoutInMs,
deregistrationTimeoutInMs, bootstrapSessionTimeoutInSec, retryWaitingTimeInMs, communicationPeriodInMs,
reconnectOnUpdate, resumeOnConnect, queueMode, preferredContentFormat);
reconnectOnUpdate, resumeOnConnect, queueMode, preferredContentFormat, supportedContentFormat);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
package org.eclipse.leshan.client.engine;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;

import org.eclipse.leshan.client.EndpointsManager;
import org.eclipse.leshan.client.bootstrap.BootstrapHandler;
import org.eclipse.leshan.client.observer.LwM2mClientObserver;
import org.eclipse.leshan.client.request.LwM2mRequestSender;
import org.eclipse.leshan.client.resource.LwM2mObjectTree;
import org.eclipse.leshan.core.request.ContentFormat;

/**
* A factory for {@link RegistrationEngine}
Expand All @@ -32,5 +34,6 @@ public interface RegistrationEngineFactory {
RegistrationEngine createRegistratioEngine(String endpoint, LwM2mObjectTree objectTree,
EndpointsManager endpointsManager, LwM2mRequestSender requestSender, BootstrapHandler bootstrapState,
LwM2mClientObserver observer, Map<String, String> additionalAttributes,
Map<String, String> bsAdditionalAttributes, ScheduledExecutorService sharedExecutor);
Map<String, String> bsAdditionalAttributes, Set<ContentFormat> supportedContentFormat,
ScheduledExecutorService sharedExecutor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand All @@ -30,6 +31,7 @@
import org.eclipse.leshan.core.LwM2mId;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.core.util.StringUtils;

/**
Expand All @@ -41,7 +43,8 @@ public final class LinkFormatHelper {
private LinkFormatHelper() {
}

public static Link[] getClientDescription(Collection<LwM2mObjectEnabler> objectEnablers, String rootPath) {
public static Link[] getClientDescription(Collection<LwM2mObjectEnabler> objectEnablers, String rootPath,
List<ContentFormat> supportedContentFormats) {
List<Link> links = new ArrayList<>();

// clean root path
Expand All @@ -51,6 +54,18 @@ public static Link[] getClientDescription(Collection<LwM2mObjectEnabler> objectE
String rootURL = getPath("/", root);
Map<String, String> attributes = new HashMap<>();
attributes.put("rt", "\"oma.lwm2m\"");
// serialize contentFormat;
if (supportedContentFormats != null && !supportedContentFormats.isEmpty()) {
StringBuilder b = new StringBuilder();
Iterator<ContentFormat> iterator = supportedContentFormats.iterator();
b.append(iterator.next().getCode());
while (iterator.hasNext()) {
b.append(" ");
b.append(iterator.next().getCode());
}
attributes.put("ct", b.toString());
}

links.add(new Link(rootURL, attributes));

// sort resources
Expand Down

0 comments on commit 55d318c

Please sign in to comment.