From 573bad0fb2cb60f828908cdab801fa83204d2311 Mon Sep 17 00:00:00 2001 From: giomba Date: Fri, 8 Nov 2019 23:18:09 +0100 Subject: [PATCH] Basic resource handling server with Californium/Java --- proxy/src/netpp/Handler.java | 36 +++++++++++++++++++++-------------- proxy/src/netpp/Main.java | 13 +++++++++++-- proxy/src/netpp/Node.java | 16 +++++++++++++--- proxy/src/netpp/Resource.java | 29 ++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 19 deletions(-) create mode 100644 proxy/src/netpp/Resource.java diff --git a/proxy/src/netpp/Handler.java b/proxy/src/netpp/Handler.java index bc5f719..c982c8c 100644 --- a/proxy/src/netpp/Handler.java +++ b/proxy/src/netpp/Handler.java @@ -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); } } diff --git a/proxy/src/netpp/Main.java b/proxy/src/netpp/Main.java index 70c00b3..b9e2a11 100644 --- a/proxy/src/netpp/Main.java +++ b/proxy/src/netpp/Main.java @@ -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 { diff --git a/proxy/src/netpp/Node.java b/proxy/src/netpp/Node.java index 9ca34da..ebe3519 100644 --- a/proxy/src/netpp/Node.java +++ b/proxy/src/netpp/Node.java @@ -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() { diff --git a/proxy/src/netpp/Resource.java b/proxy/src/netpp/Resource.java new file mode 100644 index 0000000..c670df8 --- /dev/null +++ b/proxy/src/netpp/Resource.java @@ -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; + } + +}