-
Notifications
You must be signed in to change notification settings - Fork 407
Getting Started : Server
Here you will see how to code your own LWM2M server with Leshan.
To develop your server you need to use the leshan-server-cf
module.
To know the last 1.x version.
<dependencies>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-server-cf</artifactId>
<version><!-- use the last version --></version>
</dependency>
<!-- add any slf4j backend, here we use one of the smallest one: slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
<scope>runtime</scope>
</dependency>
</dependencies>
You need to use Leshan builder to create server.
LeshanServerBuilder builder = new LeshanServerBuilder();
LeshanServer server = builder.build();
server.start();
If you execute this code, you should see something like this in your log :
[...] INFO ... - LWM2M server started at coap://0.0.0.0/0.0.0.0:5683 coaps://0.0.0.0/0.0.0.0:5684
You can try to connect a client, for example using leshan-client-demo
:
wget https://hudson.eclipse.org/leshan/job/leshan/lastSuccessfulBuild/artifact/leshan-client-demo.jar
java -jar ./leshan-client-demo.jar
You should see client successfully registered to your server:
... INFO ... - Trying to register to coap://localhost:5683 ...
... INFO ... - Registered with location '/rd/SfDZcGn8p7'.
Now let's print when devices connect or leave this server.
server.getRegistrationService().addListener(new RegistrationListener() {
public void registered(Registration registration, Registration previousReg,
Collection<Observation> previousObsersations) {
System.out.println("new device: " + registration.getEndpoint());
}
public void updated(RegistrationUpdate update, Registration updatedReg, Registration previousReg) {
System.out.println("device is still here: " + updatedReg.getEndpoint());
}
public void unregistered(Registration registration, Collection<Observation> observations, boolean expired,
Registration newReg) {
System.out.println("device left: " + registration.getEndpoint());
}
});
Now let's try to send request on registration :
public void registered(Registration registration, Registration previousReg,
Collection<Observation> previousObsersations) {
System.out.println("new device: " + registration.getEndpoint());
try {
ReadResponse response = server.send(registration, new ReadRequest(3,0,13));
if (response.isSuccess()) {
System.out.println("Device time:" + ((LwM2mResource)response.getContent()).getValue());
}else {
System.out.println("Failed to read:" + response.getCode() + " " + response.getErrorMessage());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
You can have a look to the code of our server demo: LeshanServerDemo
in leshan-server-demo
.
All contributions you make to our web site (including this wiki) are governed by our Terms of Use, so please take the time to actually read it. Your interactions with the Eclipse Foundation web properties and any information you may provide us about yourself are governed by our Privacy Policy.