[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [leshan-dev] Registration Listener
|
In that case you need to embedded leshan in your application. But if leshan is running in other machine. How can you use the API?
-------- Original message --------
From: Simon Bernard <contact@xxxxxxxxxxxxxxx>
Date: 3/8/2016 18:02 (GMT+01:00)
To: leshan developer discussions <leshan-dev@xxxxxxxxxxx>
Subject: Re: [leshan-dev] Registration Listener
...
Please consider using the Leshan API instead of using this kind of hack.
// create a server
LeshanServer server = new LeshanServerBuilder().build();
server.start();
// get current registered clients
Collection<Client> allClients = server.getClientRegistry().allClients();
// be notified of new registration
server.getClientRegistry().addListener(new ClientRegistryListener() {
public void updated(Client client) {
}
public void unregistered(Client client) {
}
public void registered(Client newClient) {
System.out.println("New registration:" + newClient);
}
});
If you're limited by the current API please open an issue[1].
[1]
https://github.com/eclipse/leshan/issues/new
Le 08/03/2016 16:59, Pablo Punal a écrit :
Hi,
You can add a Simple CoAP resource on Leshan and request periodically (also you can implement it with observe).
Modify "leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/impl/RegisterResource.java” and add this:
@Override
public void handleGET(CoapExchange exchange) {
Request request = exchange.advanced().getRequest();
Collection<Client> clients = registrationHandler.clientRegistry.allClients();
ArrayList<JSONObject> clientsArray = new ArrayList<>();
Client client;
JSONObject json;
for(int i=0; i<clients.size(); i++) {
client = (Client)clients.toArray()[i];
json = new JSONObject();
json.put("endPoint", client.getEndpoint());
json.put("registrationId", client.getRegistrationId());
json.put("address", client.getAddress().toString());
json.put("port", client.getPort());
json.put("objectLinks", Arrays.toString(client.getObjectLinks()));
clientsArray.add(json);
}
String resp = clientsArray.toString();
exchange.respond(ResponseCode.CONTENT, resp, MediaTypeRegistry.APPLICATION_LINK_FORMAT);
}
Now you can request to Leshan server for connected devices. In this example you will receive this:
[
- {
- "endPoint":"mulle-216",
- "address":"\/fdfd:0:0:0:0:0:0:1",
- "objectLinks":"[<\/1\/1>, <\/1\/2>, <\/2\/0>, <\/3\/0>, <\/4\/0>, <\/3311\/0>, <\/3311\/1>]",
- "port":5683,
- "registrationId":"3KnmBsPJ8T"
}, - {
- "endPoint":"mulle-248",
- "address":"\/fdfd:0:0:0:f95e:73b7:a8e7:eeb0",
- "objectLinks":"[<\/1\/1>, <\/1\/2>, <\/2\/0>, <\/3\/0>, <\/4\/0>, <\/3311\/1>, <\/3311\/2>, <\/3311\/3>, <\/3311\/4>]",
- "port":5683,
- "registrationId":"YmDbZUBAph"
}
]
<Attached Message Part.txt>
_______________________________________________
leshan-dev mailing list
leshan-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/leshan-dev
Attachment:
raw
Description: raw