[proxy] try to contact again nodes when they go offline
This commit is contained in:
parent
70fdffda98
commit
18bc5a48a5
@ -2,7 +2,6 @@ package netpp;
|
|||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URI;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import javax.json.Json;
|
import javax.json.Json;
|
||||||
@ -15,19 +14,20 @@ import org.eclipse.californium.core.CoapResponse;
|
|||||||
|
|
||||||
public class Handler implements CoapHandler {
|
public class Handler implements CoapHandler {
|
||||||
|
|
||||||
private URI uri;
|
private Node node;
|
||||||
private Resource resource;
|
|
||||||
|
|
||||||
public Handler(URI uri, Resource resource) {
|
public Handler(Node node) {
|
||||||
this.uri = uri;
|
this.node = node;
|
||||||
this.resource = resource;
|
node.setStatus(Status.WAIT);
|
||||||
System.err.println("[D] new handler for " + this.uri);
|
System.err.println("[D] new handler for " + this.node.getURI());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoad(CoapResponse response) {
|
public void onLoad(CoapResponse response) {
|
||||||
String content = response.getResponseText();
|
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;
|
JsonArray jsonSenML = null;
|
||||||
try {
|
try {
|
||||||
@ -41,8 +41,8 @@ public class Handler implements CoapHandler {
|
|||||||
String res_name = res.getString("n"); // name of resource
|
String res_name = res.getString("n"); // name of resource
|
||||||
int res_value = res.getJsonNumber("v").intValue(); // value of resource
|
int res_value = res.getJsonNumber("v").intValue(); // value of resource
|
||||||
|
|
||||||
resource.setName(res_name);
|
this.node.getResource().setName(res_name);
|
||||||
resource.setValue(res_value);
|
this.node.getResource().setValue(res_value);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
System.err.println("[E] " + ex);
|
System.err.println("[E] " + ex);
|
||||||
@ -51,7 +51,8 @@ public class Handler implements CoapHandler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError() {
|
public void onError() {
|
||||||
System.err.println("[E] failed " + uri);
|
node.setStatus(Status.OFFLINE);
|
||||||
|
System.err.println("[E] failed " + this.node.getURI());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,11 @@ public class Main {
|
|||||||
final String prefix = args[0];
|
final String prefix = args[0];
|
||||||
final int n = Integer.parseInt(args[1]);
|
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 */
|
/* subscribe to every node specified */
|
||||||
System.err.println("[I] now subscribing to node [2; " + n + "] in " + prefix + "/64");
|
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 <=
|
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;
|
String uri_string = "coap://[" + prefix + ":" + COOJA_MAGIC + "::" + Integer.toString(i, 16) + "]" + resource;
|
||||||
System.err.println("[I] subscribing to " + uri_string);
|
System.err.println("[I] subscribing to " + uri_string);
|
||||||
nodeList.add(new Node(i, uri_string));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* run server */
|
Node node = new Node(i, uri_string);
|
||||||
CoapServer server = new CoapServer();
|
nodeList.add(node);
|
||||||
for (Node node : nodeList) {
|
|
||||||
server.add(node.getResource());
|
server.add(node.getResource());
|
||||||
}
|
}
|
||||||
server.start();
|
|
||||||
System.err.println("[D] server started");
|
|
||||||
|
|
||||||
|
/* 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 */
|
/* thread sleep */
|
||||||
try {
|
try {
|
||||||
Thread.sleep(5 * 60 * 1000); // 5 min
|
Thread.sleep(5 * 1000); // 5 sec
|
||||||
} catch (InterruptedException ex) {
|
} catch (InterruptedException ex) {
|
||||||
System.err.println(ex);
|
System.err.println(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* shutting down everything */
|
|
||||||
for (Node node : nodeList) {
|
|
||||||
node.finalize();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,8 @@ import java.net.URISyntaxException;
|
|||||||
|
|
||||||
import org.eclipse.californium.core.CoapClient;
|
import org.eclipse.californium.core.CoapClient;
|
||||||
import org.eclipse.californium.core.CoapObserveRelation;
|
import org.eclipse.californium.core.CoapObserveRelation;
|
||||||
import org.eclipse.californium.core.CoapResource;
|
|
||||||
|
enum Status { OFFLINE, WAIT, ONLINE };
|
||||||
|
|
||||||
public class Node {
|
public class Node {
|
||||||
private URI uri;
|
private URI uri;
|
||||||
@ -14,6 +15,7 @@ public class Node {
|
|||||||
private CoapObserveRelation relation;
|
private CoapObserveRelation relation;
|
||||||
private Handler handler;
|
private Handler handler;
|
||||||
Resource resource;
|
Resource resource;
|
||||||
|
Status status = Status.OFFLINE;
|
||||||
|
|
||||||
public Node(int quickname, String uriString) {
|
public Node(int quickname, String uriString) {
|
||||||
try {
|
try {
|
||||||
@ -21,7 +23,7 @@ public class Node {
|
|||||||
this.client = new CoapClient(uri);
|
this.client = new CoapClient(uri);
|
||||||
this.quickname = quickname;
|
this.quickname = quickname;
|
||||||
this.resource = new Resource(this.quickname); /* this name will be overwritten when an update is received */
|
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);
|
this.relation = client.observe(handler);
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
System.err.println("[F] Invalid URI: " + e.getMessage());
|
System.err.println("[F] Invalid URI: " + e.getMessage());
|
||||||
@ -29,10 +31,20 @@ public class Node {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CoapResource getResource() {
|
Resource getResource() {
|
||||||
return this.resource;
|
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 */
|
/* be kind with the poor resource constrained node */
|
||||||
@Override
|
@Override
|
||||||
public void finalize() {
|
public void finalize() {
|
||||||
|
@ -15,12 +15,11 @@ public class Resource extends CoapResource {
|
|||||||
|
|
||||||
public Resource(int quickname) {
|
public Resource(int quickname) {
|
||||||
super(Integer.toString(quickname, 16));
|
super(Integer.toString(quickname, 16));
|
||||||
// setObservable(true); // TODO -- do we need to make this observable?
|
|
||||||
System.err.println("[D] resource created: " + super.getName());
|
System.err.println("[D] resource created: " + super.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleGET(CoapExchange exchange) {
|
public void handleGET(CoapExchange exchange) {
|
||||||
// TODO build the proper JSON object
|
/* an RFC8428 (SenML) compliant JSON */
|
||||||
JsonArray senml = Json.createArrayBuilder()
|
JsonArray senml = Json.createArrayBuilder()
|
||||||
.add(
|
.add(
|
||||||
Json.createObjectBuilder()
|
Json.createObjectBuilder()
|
||||||
|
Loading…
Reference in New Issue
Block a user