Skip to content

Commit

Permalink
Issue #421: Support additionalAttributes on LeshanClientBuilder
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Hernandez <alexis22229@gmail.com>
  • Loading branch information
AlexITC authored and sbernard31 committed Nov 9, 2017
1 parent 8a6ac2b commit a2493fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.californium.core.CoapServer;
Expand Down Expand Up @@ -65,7 +66,8 @@ public class LeshanClient implements LwM2mClient {
private CoapEndpoint unsecuredEndpoint;

public LeshanClient(String endpoint, CoapEndpoint unsecuredEndpoint, CoapEndpoint securedEndpoint,
List<? extends LwM2mObjectEnabler> objectEnablers, NetworkConfig coapConfig) {
List<? extends LwM2mObjectEnabler> objectEnablers, NetworkConfig coapConfig,
Map<String, String> additionalAttributes) {

Validate.notNull(endpoint);
Validate.notEmpty(objectEnablers);
Expand Down Expand Up @@ -121,7 +123,8 @@ public void onUpdateTimeout(DmServerInfo server) {

// Create registration engine
bootstrapHandler = new BootstrapHandler(this.objectEnablers);
engine = new RegistrationEngine(endpoint, this.objectEnablers, requestSender, bootstrapHandler, observers);
engine = new RegistrationEngine(endpoint, this.objectEnablers, requestSender, bootstrapHandler, observers,
additionalAttributes);

// Create CoAP Server
clientSideServer = new CoapServer(coapConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;

import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.network.config.NetworkConfig;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class LeshanClientBuilder {
private boolean noUnsecuredEndpoint;

private EndpointFactory endpointFactory;
private Map<String, String> additionalAttributes;

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

/**
* Set the additionalAttributes for {@link org.eclipse.leshan.core.request.RegisterRequest}.
*/
public LeshanClientBuilder setAdditionalAttributes(Map<String, String> additionalAttributes) {
this.additionalAttributes = additionalAttributes;
return this;
}

public static NetworkConfig createDefaultNetworkConfig() {
NetworkConfig networkConfig = new NetworkConfig();
networkConfig.set(Keys.MID_TRACKER, "NULL");
Expand Down Expand Up @@ -254,6 +264,6 @@ public LeshanClient build() {
"All CoAP enpoints are deactivated, at least one endpoint should be activated");
}

return new LeshanClient(endpoint, unsecuredEndpoint, securedEndpoint, objectEnablers, coapConfig);
return new LeshanClient(endpoint, unsecuredEndpoint, securedEndpoint, objectEnablers, coapConfig, additionalAttributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,20 @@ public class RegistrationEngine {

// registration update
private String registrationID;
private Map<String, String> additionalAttributes;
private Future<?> registerFuture;
private ScheduledFuture<?> updateFuture;
private final ScheduledExecutorService schedExecutor = Executors
.newSingleThreadScheduledExecutor(new NamedThreadFactory("RegistrationEngine#%d"));

public RegistrationEngine(String endpoint, Map<Integer, LwM2mObjectEnabler> objectEnablers,
LwM2mRequestSender requestSender, BootstrapHandler bootstrapState, LwM2mClientObserver observer) {
LwM2mRequestSender requestSender, BootstrapHandler bootstrapState, LwM2mClientObserver observer,
Map<String, String> additionalAttributes) {
this.endpoint = endpoint;
this.objectEnablers = objectEnablers;
this.bootstrapHandler = bootstrapState;
this.observer = observer;
this.additionalAttributes = additionalAttributes;

sender = requestSender;
}
Expand Down Expand Up @@ -160,7 +163,7 @@ private boolean register() throws InterruptedException {
LOG.info("Trying to register to {} ...", dmInfo.getFullUri());
RegisterResponse response = sender.send(dmInfo.getAddress(), dmInfo.isSecure(),
new RegisterRequest(endpoint, dmInfo.lifetime, LwM2m.VERSION, dmInfo.binding, null,
LinkFormatHelper.getClientDescription(objectEnablers.values(), null), null),
LinkFormatHelper.getClientDescription(objectEnablers.values(), null), additionalAttributes),
null);
if (response == null) {
registrationID = null;
Expand Down

0 comments on commit a2493fa

Please sign in to comment.