Skip to content

Commit

Permalink
Add a way to use custom encoder/decoder at client side
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jul 19, 2019
1 parent ea62317 commit ae3f4c2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
import org.eclipse.leshan.client.servers.BootstrapHandler;
import org.eclipse.leshan.client.servers.RegistrationEngine;
import org.eclipse.leshan.core.californium.EndpointFactory;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeEncoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
import org.eclipse.leshan.util.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -61,7 +61,8 @@ public class LeshanClient implements LwM2mClient {

public LeshanClient(String endpoint, InetSocketAddress localAddress,
List<? extends LwM2mObjectEnabler> objectEnablers, NetworkConfig coapConfig, Builder dtlsConfigBuilder,
EndpointFactory endpointFactory, Map<String, String> additionalAttributes) {
EndpointFactory endpointFactory, Map<String, String> additionalAttributes, LwM2mNodeEncoder encoder,
LwM2mNodeDecoder decoder) {

Validate.notNull(endpoint);
Validate.notEmpty(objectEnablers);
Expand Down Expand Up @@ -93,8 +94,7 @@ protected Resource createRoot() {

// Create CoAP resources for each lwm2m Objects.
for (LwM2mObjectEnabler enabler : objectEnablers) {
ObjectResource clientObject = new ObjectResource(enabler, bootstrapHandler, new DefaultLwM2mNodeEncoder(),
new DefaultLwM2mNodeDecoder());
ObjectResource clientObject = new ObjectResource(enabler, bootstrapHandler, encoder, decoder);
clientSideServer.add(clientObject);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.core.californium.DefaultEndpointFactory;
import org.eclipse.leshan.core.californium.EndpointFactory;
import org.eclipse.leshan.core.node.LwM2mNode;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeEncoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
import org.eclipse.leshan.core.request.BindingMode;
import org.eclipse.leshan.util.Validate;

Expand All @@ -50,6 +55,9 @@ public class LeshanClientBuilder {
private NetworkConfig coapConfig;
private Builder dtlsConfigBuilder;

private LwM2mNodeEncoder encoder;
private LwM2mNodeDecoder decoder;

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

Expand Down Expand Up @@ -97,6 +105,26 @@ public LeshanClientBuilder setObjects(List<? extends LwM2mObjectEnabler> objectE
return this;
}

/**
* Set the {@link LwM2mNodeEncoder} which will encode {@link LwM2mNode} with supported content format.
* <p>
* By default the {@link DefaultLwM2mNodeEncoder} is used. It supports Text, Opaque, TLV and JSON format.
*/
public LeshanClientBuilder setEncoder(LwM2mNodeEncoder encoder) {
this.encoder = encoder;
return this;
}

/**
* Set the {@link LwM2mNodeDecoder} which will decode data in supported content format to create {@link LwM2mNode}.
* <p>
* By default the {@link DefaultLwM2mNodeDecoder} is used. It supports Text, Opaque, TLV and JSON format.
*/
public LeshanClientBuilder setDecoder(LwM2mNodeDecoder decoder) {
this.decoder = decoder;
return this;
}

/**
* Set the Californium/CoAP {@link NetworkConfig}.
*/
Expand Down Expand Up @@ -162,6 +190,10 @@ public LeshanClient build() {
initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", "model12345", "12345", "U"));
objectEnablers = initializer.createAll();
}
if (encoder == null)
encoder = new DefaultLwM2mNodeEncoder();
if (decoder == null)
decoder = new DefaultLwM2mNodeDecoder();
if (coapConfig == null) {
coapConfig = createDefaultNetworkConfig();
}
Expand Down Expand Up @@ -210,6 +242,6 @@ protected EndpointContextMatcher createSecuredContextMatcher() {
}

return new LeshanClient(endpoint, localAddress, objectEnablers, coapConfig, dtlsConfigBuilder, endpointFactory,
additionalAttributes);
additionalAttributes, encoder, decoder);
}
}

0 comments on commit ae3f4c2

Please sign in to comment.