diff --git a/proxy/src/netpp/Handler.java b/proxy/src/netpp/Handler.java index c982c8c..a6f90b5 100644 --- a/proxy/src/netpp/Handler.java +++ b/proxy/src/netpp/Handler.java @@ -2,7 +2,6 @@ package netpp; import java.io.ByteArrayInputStream; import java.io.InputStream; -import java.net.URI; import java.util.Date; import javax.json.Json; @@ -15,19 +14,20 @@ import org.eclipse.californium.core.CoapResponse; public class Handler implements CoapHandler { - private URI uri; - private Resource resource; + private Node node; - public Handler(URI uri, Resource resource) { - this.uri = uri; - this.resource = resource; - System.err.println("[D] new handler for " + this.uri); + public Handler(Node node) { + this.node = node; + node.setStatus(Status.WAIT); + System.err.println("[D] new handler for " + this.node.getURI()); } @Override public void onLoad(CoapResponse response) { String content = response.getResponseText(); - System.out.println("[D] from " + this.uri + " update received at " + new Date() + " > " + content); + System.out.println("[D] from " + this.node.getURI() + " update received at " + new Date() + " > " + content); + + this.node.setStatus(Status.ONLINE); JsonArray jsonSenML = null; try { @@ -41,8 +41,8 @@ public class Handler implements CoapHandler { 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); + this.node.getResource().setName(res_name); + this.node.getResource().setValue(res_value); } catch (Exception ex) { ex.printStackTrace(); System.err.println("[E] " + ex); @@ -51,7 +51,8 @@ public class Handler implements CoapHandler { @Override public void onError() { - System.err.println("[E] failed " + uri); + node.setStatus(Status.OFFLINE); + System.err.println("[E] failed " + this.node.getURI()); } } diff --git a/proxy/src/netpp/Main.java b/proxy/src/netpp/Main.java index b9e2a11..3f7712b 100644 --- a/proxy/src/netpp/Main.java +++ b/proxy/src/netpp/Main.java @@ -25,6 +25,11 @@ public class Main { final String prefix = args[0]; final int n = Integer.parseInt(args[1]); + /* run server */ + CoapServer server = new CoapServer(); + server.start(); + System.err.println("[D] server started"); + /* subscribe to every node specified */ System.err.println("[I] now subscribing to node [2; " + n + "] in " + prefix + "/64"); @@ -33,27 +38,40 @@ 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(i, uri_string)); - } - - /* run server */ - CoapServer server = new CoapServer(); - for (Node node : nodeList) { + + Node node = new Node(i, uri_string); + nodeList.add(node); server.add(node.getResource()); } - server.start(); - System.err.println("[D] server started"); - /* thread sleep */ - try { - Thread.sleep(5 * 60 * 1000); // 5 min - } catch (InterruptedException ex) { - System.err.println(ex); - } - - /* shutting down everything */ - for (Node node : nodeList) { - node.finalize(); + /* periodically check for offline nodes */ + while (true) { + System.err.println("[D] check for offline nodes..."); + Node offline_node = null; + + for (Node node : nodeList) { + if (node.getStatus() == Status.OFFLINE) { + System.err.println("[I] detected offline node"); + offline_node = node; + break; + } + } + + if (offline_node != null) { + System.err.println("[I] tryng again to contact " + offline_node.getURI()); + nodeList.remove(offline_node); + server.remove(offline_node.getResource()); + + Node new_node = new Node(0, offline_node.getURI().toString()); + nodeList.add(new_node); + server.add(new_node.getResource()); + } + /* thread sleep */ + try { + Thread.sleep(5 * 1000); // 5 sec + } catch (InterruptedException ex) { + System.err.println(ex); + } } } diff --git a/proxy/src/netpp/Node.java b/proxy/src/netpp/Node.java index ebe3519..454a6f0 100644 --- a/proxy/src/netpp/Node.java +++ b/proxy/src/netpp/Node.java @@ -5,7 +5,8 @@ import java.net.URISyntaxException; import org.eclipse.californium.core.CoapClient; import org.eclipse.californium.core.CoapObserveRelation; -import org.eclipse.californium.core.CoapResource; + +enum Status { OFFLINE, WAIT, ONLINE }; public class Node { private URI uri; @@ -14,6 +15,7 @@ public class Node { private CoapObserveRelation relation; private Handler handler; Resource resource; + Status status = Status.OFFLINE; public Node(int quickname, String uriString) { try { @@ -21,7 +23,7 @@ public class Node { this.client = new CoapClient(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.handler = new Handler(this); this.relation = client.observe(handler); } catch (URISyntaxException e) { System.err.println("[F] Invalid URI: " + e.getMessage()); @@ -29,10 +31,20 @@ public class Node { } } - CoapResource getResource() { + Resource getResource() { return this.resource; } + URI getURI() { + return this.uri; + } + Status getStatus() { + return this.status; + } + void setStatus(Status status) { + this.status = status; + } + /* 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 index b7668ed..cc08952 100644 --- a/proxy/src/netpp/Resource.java +++ b/proxy/src/netpp/Resource.java @@ -15,12 +15,11 @@ public class Resource extends CoapResource { 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 + /* an RFC8428 (SenML) compliant JSON */ JsonArray senml = Json.createArrayBuilder() .add( Json.createObjectBuilder()