Commit f5f85762 authored by hengwen's avatar hengwen

Fix hostname issue on windows

parent 93a133c4
...@@ -5,6 +5,7 @@ import org.I0Itec.zkclient.exception.ZkTimeoutException; ...@@ -5,6 +5,7 @@ import org.I0Itec.zkclient.exception.ZkTimeoutException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import server.restful.registry.util.ZookeeperFactory; 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 * 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 { ...@@ -52,6 +53,11 @@ public abstract class RemoteService {
} }
public String getHost() { 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; return host;
} }
......
...@@ -15,9 +15,9 @@ import server.restful.model.SimulationModel; ...@@ -15,9 +15,9 @@ import server.restful.model.SimulationModel;
import server.restful.model.VehicleModel; import server.restful.model.VehicleModel;
import server.restful.registry.RemoteService; import server.restful.registry.RemoteService;
import server.restful.registry.ServiceRegistry; import server.restful.registry.ServiceRegistry;
import server.restful.util.Util;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
...@@ -44,13 +44,6 @@ public class SimulationService implements Runnable { ...@@ -44,13 +44,6 @@ public class SimulationService implements Runnable {
uuid = UUID.randomUUID().toString(); uuid = UUID.randomUUID().toString();
simulation = SimulationDAO.getByID(simulationID); 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) { public SimulationService(int simulationID, ServiceRegistry registry) {
...@@ -124,7 +117,7 @@ public class SimulationService implements Runnable { ...@@ -124,7 +117,7 @@ public class SimulationService implements Runnable {
} }
// run simulation in each simulators until finish // 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(); ExecutorService pool = Executors.newCachedThreadPool();
while (!isSimulationFinished()) { while (!isSimulationFinished()) {
// start all simulators // start all simulators
...@@ -225,9 +218,5 @@ public class SimulationService implements Runnable { ...@@ -225,9 +218,5 @@ public class SimulationService implements Runnable {
public List<VehicleModel> getResult() { public List<VehicleModel> getResult() {
return results.get(simulation.getId()); return results.get(simulation.getId());
} }
public Properties getProp() {
return prop;
}
} }
package server.restful.util; package server.restful.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import server.restful.service.MapService; import server.restful.service.MapService;
import java.io.*; import java.io.*;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Properties;
public class Util { 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 { try {
OutputStream out; OutputStream out;
int read = 0; int read = 0;
...@@ -23,23 +29,43 @@ public class Util { ...@@ -23,23 +29,43 @@ public class Util {
} }
} }
public static String getWorkDir(){ public static String getWorkDir() {
// get paths like: // get paths like:
// file:/app/app.jar!/BOOT-INF/classes!/server/restful/service/ // file:/app/app.jar!/BOOT-INF/classes!/server/restful/service/
// as: /app // as: /app
String jarPath = MapService.class.getResource("/").getPath() String jarPath = MapService.class.getResource("/").getPath()
.replaceAll("^.*file:", "") .replaceAll("^.*file:", "")
.replaceAll("jar!.*", "jar"); .replaceAll("jar!.*", "jar");
if (jarPath.indexOf("/")==0){ if (isWindows() && jarPath.indexOf("/") == 0) {
jarPath = jarPath.substring(1); 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(), "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(); 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"));
}
} }
...@@ -9,3 +9,6 @@ v_model=AutopilotAdapter ...@@ -9,3 +9,6 @@ v_model=AutopilotAdapter
# How often should this server synchronize all sub-simulators. The minimum is 33ms. # How often should this server synchronize all sub-simulators. The minimum is 33ms.
simulation_step_ms=500 simulation_step_ms=500
# Set to true if you are developing on windows
windows_dev_mode=false
...@@ -5,3 +5,4 @@ use_modelica=false ...@@ -5,3 +5,4 @@ use_modelica=false
v_model=AutopilotAdapter v_model=AutopilotAdapter
weather_rain=0 weather_rain=0
simulation_step_ms=500 simulation_step_ms=500
windows_dev_mode=false
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment