Skip to content

Commit

Permalink
Make LinkFormatHelper Dynamic.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jun 14, 2023
1 parent d3a6f46 commit 3786149
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 77 deletions.
Expand Up @@ -47,6 +47,7 @@
import org.eclipse.leshan.client.send.DataSenderManager;
import org.eclipse.leshan.client.send.SendService;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.client.util.LinkFormatHelper;
import org.eclipse.leshan.core.link.LinkSerializer;
import org.eclipse.leshan.core.link.lwm2m.attributes.LwM2mAttributeParser;
import org.eclipse.leshan.core.node.LwM2mNode;
Expand Down Expand Up @@ -84,14 +85,14 @@ public LeshanClient(String endpoint, List<? extends LwM2mObjectEnabler> objectEn
List<DataSender> dataSenders, List<Certificate> trustStore, RegistrationEngineFactory engineFactory,
BootstrapConsistencyChecker checker, Map<String, String> additionalAttributes,
Map<String, String> bsAdditionalAttributes, LwM2mEncoder encoder, LwM2mDecoder decoder,
ScheduledExecutorService sharedExecutor, LinkSerializer linkSerializer,
ScheduledExecutorService sharedExecutor, LinkSerializer linkSerializer, LinkFormatHelper linkFormatHelper,
LwM2mAttributeParser attributeParser, LwM2mClientEndpointsProvider endpointsProvider) {

Validate.notNull(endpoint);
Validate.notEmpty(objectEnablers);
Validate.notNull(checker);

objectTree = createObjectTree(objectEnablers);
objectTree = createObjectTree(objectEnablers, linkFormatHelper);
List<String> errors = checker.checkconfig(objectTree.getObjectEnablers());
if (errors != null) {
throw new IllegalArgumentException(
Expand All @@ -101,7 +102,7 @@ public LeshanClient(String endpoint, List<? extends LwM2mObjectEnabler> objectEn
this.endpointsProvider = endpointsProvider;
rootEnabler = createRootEnabler(objectTree);
observers = createClientObserverDispatcher();
bootstrapHandler = createBoostrapHandler(objectTree, checker);
bootstrapHandler = createBoostrapHandler(objectTree, checker, linkFormatHelper);

ClientEndpointToolbox toolbox = new ClientEndpointToolbox(decoder, encoder, linkSerializer,
objectTree.getModel(), attributeParser);
Expand All @@ -111,11 +112,11 @@ public LeshanClient(String endpoint, List<? extends LwM2mObjectEnabler> objectEn

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

DownlinkRequestReceiver requestReceiver = createRequestReceiver(bootstrapHandler, rootEnabler, objectTree,
engine);
createRegistrationUpdateHandler(engine, endpointsManager, bootstrapHandler, objectTree);
createRegistrationUpdateHandler(engine, endpointsManager, bootstrapHandler, objectTree, linkFormatHelper);

endpointsProvider.init(objectTree, requestReceiver, toolbox);
}
Expand All @@ -124,8 +125,9 @@ protected LwM2mRootEnabler createRootEnabler(LwM2mObjectTree tree) {
return new RootEnabler(tree);
}

protected LwM2mObjectTree createObjectTree(List<? extends LwM2mObjectEnabler> objectEnablers) {
return new LwM2mObjectTree(this, objectEnablers);
protected LwM2mObjectTree createObjectTree(List<? extends LwM2mObjectEnabler> objectEnablers,
LinkFormatHelper linkFormatHelper) {
return new LwM2mObjectTree(this, linkFormatHelper, objectEnablers);
}

protected DataSenderManager createDataSenderManager(List<DataSender> dataSenders, LwM2mRootEnabler rootEnabler,
Expand All @@ -148,8 +150,9 @@ public void onUnexpectedError(Throwable unexpectedError) {
return observer;
}

protected BootstrapHandler createBoostrapHandler(LwM2mObjectTree objectTree, BootstrapConsistencyChecker checker) {
return new BootstrapHandler(objectTree.getObjectEnablers(), checker);
protected BootstrapHandler createBoostrapHandler(LwM2mObjectTree objectTree, BootstrapConsistencyChecker checker,
LinkFormatHelper linkFormatHelper) {
return new BootstrapHandler(objectTree.getObjectEnablers(), checker, linkFormatHelper);
}

protected DownlinkRequestReceiver createRequestReceiver(BootstrapHandler bootstrapHandler,
Expand All @@ -167,8 +170,10 @@ protected UplinkRequestSender createRequestSender(LwM2mClientEndpointsProvider e
}

protected RegistrationUpdateHandler createRegistrationUpdateHandler(RegistrationEngine engine,
EndpointsManager endpointsManager, BootstrapHandler bootstrapHandler, LwM2mObjectTree objectTree) {
RegistrationUpdateHandler registrationUpdateHandler = new RegistrationUpdateHandler(engine, bootstrapHandler);
EndpointsManager endpointsManager, BootstrapHandler bootstrapHandler, LwM2mObjectTree objectTree,
LinkFormatHelper linkFormatHelper) {
RegistrationUpdateHandler registrationUpdateHandler = new RegistrationUpdateHandler(engine, bootstrapHandler,
linkFormatHelper);
registrationUpdateHandler.listen(objectTree);
return registrationUpdateHandler;
}
Expand Down
Expand Up @@ -36,6 +36,8 @@
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.client.send.DataSender;
import org.eclipse.leshan.client.util.LinkFormatHelper;
import org.eclipse.leshan.core.LwM2m.LwM2mVersion;
import org.eclipse.leshan.core.LwM2mId;
import org.eclipse.leshan.core.link.DefaultLinkSerializer;
import org.eclipse.leshan.core.link.LinkSerializer;
Expand Down Expand Up @@ -79,6 +81,8 @@ public class LeshanClientBuilder {

private LwM2mClientEndpointsProvider endpointsProvider;

private LinkFormatHelper linkFormatHelper;

/**
* Creates a new instance for setting the configuration options for a {@link LeshanClient} instance.
*
Expand Down Expand Up @@ -168,6 +172,14 @@ public LeshanClientBuilder setLinkSerializer(LinkSerializer linkSerializer) {
return this;
}

/**
* Set the Link Format Helper.
*/
public LeshanClientBuilder setLinkFormatHelper(LinkFormatHelper linkFormatHelper) {
this.linkFormatHelper = linkFormatHelper;
return this;
}

/**
* Set the {@link LwM2mAttributeParser} used to parse {@link LwM2mAttribute} from {@link WriteAttributesRequest}.
* <p>
Expand Down Expand Up @@ -282,6 +294,8 @@ public LeshanClient build() {
decoder = new DefaultLwM2mDecoder();
if (linkSerializer == null)
linkSerializer = new DefaultLinkSerializer();
if (linkFormatHelper == null)
linkFormatHelper = new LinkFormatHelper(LwM2mVersion.V1_1);
if (attributeParser == null)
attributeParser = new DefaultLwM2mAttributeParser();
if (engineFactory == null) {
Expand All @@ -293,7 +307,7 @@ public LeshanClient build() {

return createLeshanClient(endpoint, objectEnablers, dataSenders, this.trustStore, engineFactory,
bootstrapConsistencyChecker, additionalAttributes, bsAdditionalAttributes, encoder, decoder, executor,
linkSerializer, attributeParser, endpointsProvider);
linkSerializer, linkFormatHelper, attributeParser, endpointsProvider);
}

/**
Expand All @@ -315,6 +329,7 @@ public LeshanClient build() {
* @param decoder used to decode response payload.
* @param sharedExecutor an optional shared executor.
* @param linkSerializer a serializer {@link LinkSerializer} used to serialize a CoRE Link.
* @param linkFormatHelper a helper used to create Link Object from ObjectTree.
* @param attributeParser a {@link LwM2mAttributeParser} used to parse {@link LwM2mAttribute} from
* {@link WriteAttributesRequest}.
*
Expand All @@ -324,10 +339,10 @@ protected LeshanClient createLeshanClient(String endpoint, List<? extends LwM2mO
List<DataSender> dataSenders, List<Certificate> trustStore, RegistrationEngineFactory engineFactory,
BootstrapConsistencyChecker checker, Map<String, String> additionalAttributes,
Map<String, String> bsAdditionalAttributes, LwM2mEncoder encoder, LwM2mDecoder decoder,
ScheduledExecutorService sharedExecutor, LinkSerializer linkSerializer,
ScheduledExecutorService sharedExecutor, LinkSerializer linkSerializer, LinkFormatHelper linkFormatHelper,
LwM2mAttributeParser attributeParser, LwM2mClientEndpointsProvider endpointsProvider) {
return new LeshanClient(endpoint, objectEnablers, dataSenders, trustStore, engineFactory, checker,
additionalAttributes, bsAdditionalAttributes, encoder, decoder, sharedExecutor, linkSerializer,
attributeParser, endpointsProvider);
linkFormatHelper, attributeParser, endpointsProvider);
}
}
Expand Up @@ -37,10 +37,13 @@ public class RegistrationUpdateHandler {

private final RegistrationEngine engine;
private final BootstrapHandler bsHandler;
private final LinkFormatHelper linkFormatHelper;

public RegistrationUpdateHandler(RegistrationEngine engine, BootstrapHandler bsHandler) {
public RegistrationUpdateHandler(RegistrationEngine engine, BootstrapHandler bsHandler,
LinkFormatHelper linkFormatHelper) {
this.engine = engine;
this.bsHandler = bsHandler;
this.linkFormatHelper = linkFormatHelper;
}

public void listen(final LwM2mObjectTree objecTree) {
Expand All @@ -49,28 +52,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, 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, 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, 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, null)));
linkFormatHelper.getClientDescription(objecTree.getObjectEnablers().values(), null, null)));
}

@Override
Expand Down
Expand Up @@ -46,11 +46,14 @@ public class BootstrapHandler {
private volatile List<String> lastConsistencyError = null;

private final Map<Integer, LwM2mObjectEnabler> objects;
private BootstrapConsistencyChecker checker;
private final BootstrapConsistencyChecker checker;
private final LinkFormatHelper linkFormatHelper;

public BootstrapHandler(Map<Integer, LwM2mObjectEnabler> objectEnablers, BootstrapConsistencyChecker checker) {
public BootstrapHandler(Map<Integer, LwM2mObjectEnabler> objectEnablers, BootstrapConsistencyChecker checker,
LinkFormatHelper linkFormatHelper) {
objects = objectEnablers;
this.checker = checker;
this.linkFormatHelper = linkFormatHelper;
}

public synchronized SendableResponse<BootstrapFinishResponse> finished(ServerIdentity server,
Expand Down Expand Up @@ -143,7 +146,7 @@ public BootstrapDiscoverResponse discover(ServerIdentity identity, BootstrapDisc
LwM2mPath path = request.getPath();
if (path.isRoot()) {
// Manage discover on object
LwM2mLink[] ObjectLinks = LinkFormatHelper.getBootstrapClientDescription(objects.values());
LwM2mLink[] ObjectLinks = linkFormatHelper.getBootstrapClientDescription(objects.values());
return BootstrapDiscoverResponse.success(ObjectLinks);
}
return BootstrapDiscoverResponse.badRequest("invalid path");
Expand Down
Expand Up @@ -118,6 +118,7 @@ private static enum Status {
private final BootstrapHandler bootstrapHandler;
private final EndpointsManager endpointsManager;
private final LwM2mClientObserver observer;
private final LinkFormatHelper linkFormatHelper;

// tasks stuff
private boolean started = false;
Expand All @@ -134,11 +135,13 @@ public DefaultRegistrationEngine(String endpoint, LwM2mObjectTree objectTree, En
ScheduledExecutorService executor, long requestTimeoutInMs, long deregistrationTimeoutInMs,
int bootstrapSessionTimeoutInSec, int retryWaitingTimeInMs, Integer communicationPeriodInMs,
boolean reconnectOnUpdate, boolean resumeOnConnect, boolean useQueueMode,
ContentFormat preferredContentFormat, Set<ContentFormat> supportedContentFormats) {
ContentFormat preferredContentFormat, Set<ContentFormat> supportedContentFormats,
LinkFormatHelper linkFormatHelper) {
this.endpoint = endpoint;
this.objectEnablers = objectTree.getObjectEnablers();
this.bootstrapHandler = bootstrapState;
this.endpointsManager = endpointsManager;
this.linkFormatHelper = linkFormatHelper;
this.observer = observer;
this.additionalAttributes = additionalAttributes;
this.bsAdditionalAttributes = bsAdditionalAttributes;
Expand Down Expand Up @@ -317,7 +320,7 @@ private Status register(ServerIdentity server) throws InterruptedException {
LwM2mVersion lwM2mVersion = LwM2mVersion.lastSupported();
EnumSet<BindingMode> supportedBindingMode = ServersInfoExtractor
.getDeviceSupportedBindingMode(objectEnablers.get(LwM2mId.DEVICE), 0);
Link[] links = LinkFormatHelper.getClientDescription(objectEnablers.values(), null,
Link[] links = linkFormatHelper.getClientDescription(objectEnablers.values(), null,
ContentFormat.getOptionalContentFormatForClient(supportedContentFormats, lwM2mVersion));

request = new RegisterRequest(endpoint, dmInfo.lifetime, lwM2mVersion.toString(), supportedBindingMode,
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.leshan.client.observer.LwM2mClientObserver;
import org.eclipse.leshan.client.request.UplinkRequestSender;
import org.eclipse.leshan.client.resource.LwM2mObjectTree;
import org.eclipse.leshan.client.util.LinkFormatHelper;
import org.eclipse.leshan.core.request.ContentFormat;

/**
Expand Down Expand Up @@ -51,11 +52,12 @@ public RegistrationEngine createRegistratioEngine(String endpoint, LwM2mObjectTr
EndpointsManager endpointsManager, UplinkRequestSender requestSender, BootstrapHandler bootstrapState,
LwM2mClientObserver observer, Map<String, String> additionalAttributes,
Map<String, String> bsAdditionalAttributes, Set<ContentFormat> supportedContentFormat,
ScheduledExecutorService sharedExecutor) {
ScheduledExecutorService sharedExecutor, LinkFormatHelper linkFormatHelper) {
return new DefaultRegistrationEngine(endpoint, objectTree, endpointsManager, requestSender, bootstrapState,
observer, additionalAttributes, bsAdditionalAttributes, sharedExecutor, requestTimeoutInMs,
deregistrationTimeoutInMs, bootstrapSessionTimeoutInSec, retryWaitingTimeInMs, communicationPeriodInMs,
reconnectOnUpdate, resumeOnConnect, queueMode, preferredContentFormat, supportedContentFormat);
reconnectOnUpdate, resumeOnConnect, queueMode, preferredContentFormat, supportedContentFormat,
linkFormatHelper);
}

/**
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.leshan.client.observer.LwM2mClientObserver;
import org.eclipse.leshan.client.request.UplinkRequestSender;
import org.eclipse.leshan.client.resource.LwM2mObjectTree;
import org.eclipse.leshan.client.util.LinkFormatHelper;
import org.eclipse.leshan.core.request.ContentFormat;

/**
Expand All @@ -35,5 +36,5 @@ RegistrationEngine createRegistratioEngine(String endpoint, LwM2mObjectTree obje
EndpointsManager endpointsManager, UplinkRequestSender requestSender, BootstrapHandler bootstrapState,
LwM2mClientObserver observer, Map<String, String> additionalAttributes,
Map<String, String> bsAdditionalAttributes, Set<ContentFormat> supportedContentFormat,
ScheduledExecutorService sharedExecutor);
ScheduledExecutorService sharedExecutor, LinkFormatHelper linkFormatHelper);
}

0 comments on commit 3786149

Please sign in to comment.