From f5f85762faaaf0665e3463c753c3919754d137d0 Mon Sep 17 00:00:00 2001 From: hengwen Date: Wed, 22 May 2019 15:04:01 +0200 Subject: [PATCH] Fix hostname issue on windows --- .../restful/registry/RemoteService.java | 6 +++ .../restful/service/SimulationService.java | 15 +------- .../main/java/server/restful/util/Util.java | 38 ++++++++++++++++--- restful/src/main/resources/config.properties | 3 ++ restful/src/test/resources/config.properties | 1 + 5 files changed, 44 insertions(+), 19 deletions(-) diff --git a/restful/src/main/java/server/restful/registry/RemoteService.java b/restful/src/main/java/server/restful/registry/RemoteService.java index b4399c2..e8680bc 100644 --- a/restful/src/main/java/server/restful/registry/RemoteService.java +++ b/restful/src/main/java/server/restful/registry/RemoteService.java @@ -5,6 +5,7 @@ import org.I0Itec.zkclient.exception.ZkTimeoutException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import server.restful.registry.util.ZookeeperFactory; +import server.restful.util.Util; /** * RemoteService is a zookeeper ephemeral node under the hood. When a service goes online, it creates an ephemeral @@ -52,6 +53,11 @@ public abstract class RemoteService { } public String getHost() { + // windows platform has problem connecting to local service with hostname other than localhost and "127.0.0.1" + // in this case we hardcode the host name to "localhost" + if (Util.isWindows()){ + return "localhost"; + } return host; } diff --git a/restful/src/main/java/server/restful/service/SimulationService.java b/restful/src/main/java/server/restful/service/SimulationService.java index 2b2228c..fe45003 100644 --- a/restful/src/main/java/server/restful/service/SimulationService.java +++ b/restful/src/main/java/server/restful/service/SimulationService.java @@ -15,9 +15,9 @@ import server.restful.model.SimulationModel; import server.restful.model.VehicleModel; import server.restful.registry.RemoteService; import server.restful.registry.ServiceRegistry; +import server.restful.util.Util; import java.io.File; -import java.io.IOException; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; @@ -44,13 +44,6 @@ public class SimulationService implements Runnable { uuid = UUID.randomUUID().toString(); simulation = SimulationDAO.getByID(simulationID); - - try { - prop = new Properties(); - prop.load(getClass().getResourceAsStream("/config.properties")); - } catch (IOException e) { - logger.warn(e.getMessage(), e); - } } public SimulationService(int simulationID, ServiceRegistry registry) { @@ -124,7 +117,7 @@ public class SimulationService implements Runnable { } // run simulation in each simulators until finish - int stepDurationMs = Integer.parseInt(prop.getProperty("simulation_step_ms", "500")); + int stepDurationMs = Integer.parseInt(Util.getProperties().getProperty("simulation_step_ms", "500")); ExecutorService pool = Executors.newCachedThreadPool(); while (!isSimulationFinished()) { // start all simulators @@ -225,9 +218,5 @@ public class SimulationService implements Runnable { public List getResult() { return results.get(simulation.getId()); } - - public Properties getProp() { - return prop; - } } diff --git a/restful/src/main/java/server/restful/util/Util.java b/restful/src/main/java/server/restful/util/Util.java index ac6d69c..0b94230 100644 --- a/restful/src/main/java/server/restful/util/Util.java +++ b/restful/src/main/java/server/restful/util/Util.java @@ -1,12 +1,18 @@ package server.restful.util; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import server.restful.service.MapService; import java.io.*; import java.nio.file.Paths; +import java.util.Properties; public class Util { - public static void writeToFile(InputStream inputStream, String path){ + static Properties properties = null; + private static Logger logger = LoggerFactory.getLogger(Util.class); + + public static void writeToFile(InputStream inputStream, String path) { try { OutputStream out; int read = 0; @@ -23,23 +29,43 @@ public class Util { } } - public static String getWorkDir(){ + public static String getWorkDir() { // get paths like: // file:/app/app.jar!/BOOT-INF/classes!/server/restful/service/ // as: /app String jarPath = MapService.class.getResource("/").getPath() .replaceAll("^.*file:", "") .replaceAll("jar!.*", "jar"); - if (jarPath.indexOf("/")==0){ + if (isWindows() && jarPath.indexOf("/") == 0) { jarPath = jarPath.substring(1); } - if(jarPath.contains("test-classes")){ + if (jarPath.contains("test-classes")) { return Paths.get(Paths.get(jarPath).getParent().toString(), "test-classes").toString(); } - return Paths.get(Paths.get(jarPath).getParent().toString(), "classes").toString(); + return Paths.get(Paths.get(jarPath).getParent().toString(), "classes").toString(); } - public static String getDatabasePath(){ + public static String getDatabasePath() { return "jdbc:sqlite:" + Paths.get(getWorkDir(), "app.db").toString(); } + + public static Properties getProperties() { + if (properties != null) { + return properties; + } + properties = new Properties(); + + try { + properties.load(Util.class.getResourceAsStream("/config.properties")); + return properties; + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + + return properties; + } + + public static boolean isWindows(){ + return Boolean.valueOf(Util.getProperties().getProperty("windows_dev_mode", "false")); + } } diff --git a/restful/src/main/resources/config.properties b/restful/src/main/resources/config.properties index e833de6..40fa7b0 100644 --- a/restful/src/main/resources/config.properties +++ b/restful/src/main/resources/config.properties @@ -9,3 +9,6 @@ v_model=AutopilotAdapter # How often should this server synchronize all sub-simulators. The minimum is 33ms. simulation_step_ms=500 + +# Set to true if you are developing on windows +windows_dev_mode=false diff --git a/restful/src/test/resources/config.properties b/restful/src/test/resources/config.properties index 9d407fc..a5abf61 100644 --- a/restful/src/test/resources/config.properties +++ b/restful/src/test/resources/config.properties @@ -5,3 +5,4 @@ use_modelica=false v_model=AutopilotAdapter weather_rain=0 simulation_step_ms=500 +windows_dev_mode=false -- GitLab