Basic resource handling server with Californium/Java

This commit is contained in:
giomba 2019-11-08 23:18:09 +01:00
parent e41959fefa
commit 573bad0fb2
4 changed files with 75 additions and 19 deletions

View File

@ -1,42 +1,50 @@
package netpp;
import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.util.Date;
import javax.json.*;
import org.eclipse.californium.core.*;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import org.eclipse.californium.core.CoapHandler;
import org.eclipse.californium.core.CoapResponse;
public class Handler implements CoapHandler {
private URI uri;
private URI uri;
private Resource resource;
public Handler(URI uri) {
public Handler(URI uri, Resource resource) {
this.uri = uri;
this.resource = resource;
System.err.println("[D] new handler for " + this.uri);
}
@Override
public void onLoad(CoapResponse response) {
String content = response.getResponseText();
// System.out.println("[D] resource: " + uri);
// System.out.println(" new value: " + content);
System.out.println("update" + new Date());
System.out.println("[D] from " + this.uri + " update received at " + new Date() + " > " + content);
JsonArray jsonSenML = null;
try {
InputStream is = new ByteArrayInputStream(content.getBytes());
System.out.println("0");
JsonReader reader = Json.createReader(is);
System.err.println("1: " + content);
jsonSenML = reader.readArray();
System.out.println("2");
reader.close();
System.out.println("Java Json Object");
System.out.println(jsonSenML.getJsonObject(0).getString("n"));
System.out.println(jsonSenML.getJsonObject(0).getJsonNumber("v"));
/* in this application, SenML contains only one resource -- see RFC8428 for further details */
JsonObject res = jsonSenML.getJsonObject(0);
String res_name = res.getString("n"); // name of resource
int res_value = res.getJsonNumber("v").intValue(); // value of resource
resource.setName(res_name);
resource.setValue(res_value);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println("[E] " + ex);
}
}

View File

@ -1,6 +1,8 @@
package netpp;
import java.util.*;
import java.util.ArrayList;
import org.eclipse.californium.core.CoapServer;
public class Main {
public final static String COOJA_MAGIC = "c30c";
@ -31,9 +33,16 @@ public class Main {
for (int i = 2; i <= n; i++) { // node 1 is the gateway, skip. And mind the <=
String uri_string = "coap://[" + prefix + ":" + COOJA_MAGIC + "::" + Integer.toString(i, 16) + "]" + resource;
System.err.println("[I] subscribing to " + uri_string);
nodeList.add(new Node(uri_string));
nodeList.add(new Node(i, uri_string));
}
/* run server */
CoapServer server = new CoapServer();
for (Node node : nodeList) {
server.add(node.getResource());
}
server.start();
System.err.println("[D] server started");
/* thread sleep */
try {

View File

@ -3,19 +3,25 @@ package netpp;
import java.net.URI;
import java.net.URISyntaxException;
import org.eclipse.californium.core.*;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapObserveRelation;
import org.eclipse.californium.core.CoapResource;
public class Node {
private URI uri;
private int quickname;
private CoapClient client;
private CoapObserveRelation relation;
private Handler handler;
Resource resource;
public Node(String uriString) {
public Node(int quickname, String uriString) {
try {
this.uri = new URI(uriString);
this.client = new CoapClient(uri);
this.handler = new Handler(this.uri);
this.quickname = quickname;
this.resource = new Resource(this.quickname); /* this name will be overwritten when an update is received */
this.handler = new Handler(this.uri, resource);
this.relation = client.observe(handler);
} catch (URISyntaxException e) {
System.err.println("[F] Invalid URI: " + e.getMessage());
@ -23,6 +29,10 @@ public class Node {
}
}
CoapResource getResource() {
return this.resource;
}
/* be kind with the poor resource constrained node */
@Override
public void finalize() {

View File

@ -0,0 +1,29 @@
package netpp;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.server.resources.CoapExchange;
public class Resource extends CoapResource {
private int value;
public Resource(int quickname) {
super(Integer.toString(quickname, 16));
// setObservable(true); // TODO -- do we need to make this observable?
System.err.println("[D] resource created: " + super.getName());
}
public void handleGET(CoapExchange exchange) {
// TODO build the proper JSON object
exchange.respond("TODO: " + this.getName() + " : " + this.getValue());
}
public int getValue() {
return this.value;
}
public void setValue(int value) {
this.value = value;
}
}