Commit a29b76d7 authored by Jean Meurice's avatar Jean Meurice Committed by Evgeny Kusmenko

Upgraded for use in BasicSimulator

parent e48254ce
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
/target/
/.idea
.project
*.iml
.classpath
/.settings
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>RMIModelServer</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
#!/usr/bin/env bash
#
#
# ******************************************************************************
# MontiCAR Modeling Family, www.se-rwth.de
# Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
# All rights reserved.
#
# This project is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3.0 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this project. If not, see <http://www.gnu.org/licenses/>.
# *******************************************************************************
#
if [ -z "$1" ]; then
PORT=10101
......
......@@ -26,7 +26,7 @@
<groupId>montisim</groupId>
<artifactId>rmi-model-server</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
<packaging>jar</packaging>
<name>rmi-model-server</name>
......@@ -71,6 +71,11 @@
<artifactId>zkclient</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>montisim</groupId>
<artifactId>commons</artifactId>
<version>1.0.7</version>
</dependency>
</dependencies>
<!-- == PROJECT BUILD SETTINGS =========================================== -->
......
......@@ -35,50 +35,87 @@ import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Arrays;
import java.util.HashMap;
public class RMIServer {
public final static String MODEL_MANAGER = "ModelManager";
public final static String ZK_NODE = "/rmi-servers/all";
public static void parse_args(String[] args, HashMap<String, String> options){
String default_config = "";
boolean add_to_default_config = false;
for (String arg : args){
if (add_to_default_config){
default_config += arg + "\n";
} else {
String sub_args[] = arg.split("=");
if (arg.equals("no-zookeeper")){
options.put("no-zookeeper", "true");
} else if (arg.equals("default_config")){
add_to_default_config = true;
} else if (sub_args.length > 1) {
if (sub_args[0].equals("port")){
options.put("port", sub_args[1]);
} else if (sub_args[0].equals("autopilots_folder")){
options.put("autopilots_folder", sub_args[1]);
}
}
}
}
options.put("default_config", default_config);
}
public static void main(String[] args) throws UnknownHostException {
if (args.length < 2){
System.out.println("Please assign a port that the RMI server need to bind, also the path to the autopilot(s)");
HashMap<String, String> options = new HashMap<String, String>();
parse_args(args, options);
if (!options.containsKey("port")){
System.out.println("Please assign a port that the RMI server need to bind (port=...)");
return;
}
if (!isPortAvailable(args[0])){
System.out.printf("Port %s is currently unavailable, please use another port.\n", args[0]);
String port = options.get("port");
if (!isPortAvailable(port)){
System.out.printf("Port %s is currently unavailable, please use another port.\n", port);
return;
}
File autopilot_folder = new File(args[1]);
File autopilot_folder = new File(options.containsKey("autopilots_folder") ? options.get("autopilots_folder") : "autopilots");
if (!autopilot_folder.exists()){
System.out.printf("The autopilot folder %s does not exist\n", autopilot_folder);
return;
}
boolean use_zookeeper = true;
int configIndex = 2;
if (args.length >= 3){
if (args[2].equals("--no-zookeeper")){
use_zookeeper = false;
configIndex++;
boolean use_zookeeper = !options.containsKey("no-zookeeper");
if (!use_zookeeper)
System.out.println("Not using Zookeeper.");
}
}
String default_config = options.get("default_config");
String emu_config = "";
try {// Load AutopilotAdapter.dll from libraryPath
emu_config += "autopilots_folder=" + autopilot_folder.getCanonicalPath() + "\n";
} catch (IOException e) {
e.printStackTrace();
System.out.println("Could not resolve the Hardware Emulator autopilots path.");
}
HardwareEmulatorInterface manager = new HardwareEmulatorInterface(args, configIndex);
HardwareEmulatorInterface manager = null;
try {
manager = new HardwareEmulatorInterface(emu_config, default_config);
} catch (Exception e) {
e.printStackTrace();
System.exit(-2);
}
String host = InetAddress.getLocalHost().getHostName();
int port = Integer.valueOf(args[0]);
int port_id = Integer.valueOf(port);
try {
Registry registry = LocateRegistry.createRegistry(port);
Registry registry = LocateRegistry.createRegistry(port_id);
System.out.println("RMIRegistry serving on port: " + port);
//bind the ModelManager to a stub, representing the remote object
registry.bind(MODEL_MANAGER, UnicastRemoteObject.exportObject(manager, port));
registry.bind(MODEL_MANAGER, UnicastRemoteObject.exportObject(manager, port_id));
if (use_zookeeper){
String zoo_servers;
......@@ -87,11 +124,11 @@ public class RMIServer {
zoo_servers = "localhost:2181";
}
ZkClient zk = new ZkClient(zoo_servers, 1000, 5000);
register(zk, host, port);
register(zk, host, port_id);
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run(){
unregister(zk, host, port);
unregister(zk, host, port_id);
}
});
}
......
......@@ -25,37 +25,32 @@ import java.io.IOException;
import java.util.HashMap;
import java.io.Serializable;
import rwth.rmi.model.server.interfaces.RMIManager;
import commons.utils.LibraryService;
public class HardwareEmulatorInterface implements RMIManager {
public HardwareEmulatorInterface(String[] args, int configIndex) {
String lib_path = "";
public HardwareEmulatorInterface(String emu_config, String default_config) throws LibraryService.LibraryException, Exception {
super();
String default_config = "";
for (int i = configIndex; i < args.length; i++){
default_config += args[i] + "\n";
load_library();
if (!init(emu_config, default_config)){
String error_msg = query("get_error_msg");
throw new Exception("Error initializing the HardwareEmulator: " + error_msg);
}
String libraryPath = args[1];
String emu = System.getProperty("user.dir") + "/HardwareEmulator";
String OS = System.getProperty("os.name").toLowerCase();
if (OS.contains("win")){
System.load(emu+".dll");
} else if (OS.contains("nix")|| OS.contains("nux")|| OS.contains("aix")){
System.load(emu+".so");
} else {
System.out.println("Could not deduce Operating System for loading the HardwareEmulator.");
}
try {// Load AutopilotAdapter.dll from libraryPath
File b = new File(libraryPath);
String absolute = b.getCanonicalPath();
init("autopilots_folder=" + absolute, default_config);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Could not initiate the Hardware Emulator");
init("", default_config);
public void load_library() throws LibraryService.LibraryException {
//Use LibraryService to get the library from resources to the working directory (if not already there)
String working_dir = LibraryService.getWorkingDirectory();
String system_library_name = LibraryService.getSystemLibraryName("HardwareEmulator");
LibraryService.prepareLibrary(working_dir, system_library_name);
lib_path = working_dir + system_library_name;
//Load the library
System.load(lib_path);
//System.out.println("LibPath: " + lib_path);
}
public File get_library(){
return new File(lib_path);
}
private native boolean init(String config, String default_config);
......
......@@ -23,14 +23,15 @@ package simulator.integration;
import org.junit.Test;
import org.junit.Assert;
import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
public class HardwareEmulatorTest {
@Test
public void basic_test(){
public void basic_test() throws Exception {
System.out.println("Current test directory: " + System.getProperty("user.dir"));
HardwareEmulatorInterface manager = new HardwareEmulatorInterface(new String[]{"10101", "autopilots"}, 2);
HardwareEmulatorInterface manager = new HardwareEmulatorInterface("autopilots_folder=autopilots", "");
String querry = "get_available_autopilots\nget_available_threads";
String res = manager.query(querry);
......@@ -78,5 +79,11 @@ public class HardwareEmulatorTest {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
manager.free_autopilot(id);
//Try to clear up the library from the test directory
File lib = manager.get_library();
manager = null;
System.gc();
lib.delete();
}
}
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