Skip to content

Commit

Permalink
Unify Valhalla server check
Browse files Browse the repository at this point in the history
  • Loading branch information
inigo-cobian committed Feb 12, 2025
1 parent 23f6ecc commit 5c00b77
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import org.integratedmodelling.klab.api.observations.scale.space.IShape;
import org.integratedmodelling.klab.components.geospace.routing.ValhallaConfiguration.GeometryCollapser;
import org.integratedmodelling.klab.exceptions.KlabException;
import org.integratedmodelling.klab.exceptions.KlabRemoteException;
import org.locationtech.jts.geom.Geometry;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.util.EdgeType;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import kong.unirest.json.JSONObject;

/**
Expand Down Expand Up @@ -163,4 +166,16 @@ public static double[] getCoordinates(IDirectObservation observation, GeometryCo
}
}

public static boolean isServerOnline(String server) {
HttpResponse<JsonNode> response;
try {
response = Unirest.get(server + "/status").asJson();
} catch (Exception e) {
throw new KlabRemoteException("Cannot access Valhalla server. Reason: " + e.getMessage());
}
if (response.getStatus() != 200) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
import org.integratedmodelling.klab.utils.Parameters;
import org.integratedmodelling.klab.utils.Utils;
import org.locationtech.jts.geom.Geometry;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;

public class IsochroneInstantiator extends AbstractContextualizer implements IExpression, IInstantiator {
private String source = null;
Expand All @@ -42,24 +39,10 @@ public class IsochroneInstantiator extends AbstractContextualizer implements IEx
public IsochroneInstantiator() {
}

// TODO merge with the one in RoutingRelationshipInstantiator
private boolean isValhallaServerOnline(String server) {
HttpResponse<JsonNode> response;
try {
response = Unirest.get(server + "/status").asJson();
} catch (Exception e) {
throw new KlabRemoteException("Cannot access Valhalla server. Reason: " + e.getMessage());
}
if (response.getStatus() != 200) {
return false;
}
return true;
}

public IsochroneInstantiator(Parameters<Object> parameters, IContextualizationScope scope) {
this.server = parameters.get("server", String.class);
this.source = parameters.get("source", String.class);
if (isValhallaServerOnline(server)) {
if (Valhalla.isServerOnline(server)) {
this.valhalla = new Valhalla(server);
} else {
throw new KlabRemoteException("The server " + server + " is offline or not a valid Valhalla instance.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,6 @@ static enum CostingOptions {

public RoutingRelationshipInstantiator() {
/* to instantiate as expression - do not remove (or use) */}

private boolean isValhallaServerOnline(String server) {
HttpResponse<JsonNode> response;
try {
response = Unirest.get(server + "/status").asJson();
} catch (Exception e) {
throw new KlabRemoteException("Cannot access Valhalla server. Reason: " + e.getMessage());
}
if (response.getStatus() != 200) {
return false;
}
return true;
}

public RoutingRelationshipInstantiator(IParameters<String> parameters, IContextualizationScope scope) {
this.scope = scope;
Expand All @@ -105,7 +92,7 @@ public RoutingRelationshipInstantiator(IParameters<String> parameters, IContextu
throw new KlabIllegalArgumentException("The server for Valhalla has not been defined.");
}
this.server = parameters.get("server", String.class);
if (isValhallaServerOnline(server)) {
if (Valhalla.isServerOnline(server)) {
this.valhalla = new Valhalla(server);
} else {
throw new KlabRemoteException("The server " + server + " is offline or not a valid Valhalla instance.");
Expand Down

0 comments on commit 5c00b77

Please sign in to comment.