Commit a855744f authored by ham's avatar ham
Browse files

Allow RMI Server to start with different port and add better support for mac users

parent 7783aad2
Pipeline #89725 failed with stages
in 1 minute and 28 seconds
......@@ -20,4 +20,16 @@
#
#run rmi server and registry
java -cp ../target/rmi-model-server-1.0.0.jar -Djava.rmi.server.codebase=file:../target/rmi-model-server-1.0.1.jar -Djava.rmi.server.hostname=127.0.0.1 rwth.rmi.model.server.RMIServer
if [ -z "$1" ]; then
PORT=10101
else
PORT="$1"
fi
# Please replace this with the actual ABSOLUTE PATH to AutopilotAdapter.dll on your machine
DLL=/ABSOLUTE/PATH/TO/AutopilotAdapter.dll
java -cp ../target/rmi-model-server-1.0.1-SNAPSHOT.jar\
-Djava.rmi.server.codebase=file:../target/rmi-model-server-1.0.1-SNAPSHOT.jar\
-Djava.rmi.server.hostname=127.0.0.1 rwth.rmi.model.server.RMIServer\
$PORT $DLL
......@@ -36,15 +36,16 @@ public class ModelManager implements RMIManager {
private int lastModelId = 0;
private Map<Integer, VehicleModel> models;
//Served models
private VehicleModel[] supportedModels = new VehicleModel[] {
new AutopilotAdapter()
};
public ModelManager() {
private VehicleModel[] supportedModels;
public ModelManager(String libraryPath) {
super();
this.models = new HashMap<Integer, VehicleModel>();
this.supportedModels = new VehicleModel[] {
new AutopilotAdapter(libraryPath)
};
}
@Override
......
......@@ -20,6 +20,10 @@
*/
package rwth.rmi.model.server;
import java.io.File;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
......@@ -27,22 +31,70 @@ import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class RMIServer {
public final static String MODEL_MANAGER = "ModelManager";
private final static int INIT_PORT = 10101;
public final static String MODEL_MANAGER = "ModelManager";
public static void main(String[] args) {
ModelManager manager = new ModelManager();
try {
Registry registry = LocateRegistry.createRegistry(INIT_PORT);
System.out.println("RMIRegistry serving on port: " + INIT_PORT);
//bind the ModelManager to a stub, representing the remote object
registry.bind(MODEL_MANAGER, UnicastRemoteObject.exportObject(manager, 0));
System.out.println("RMIManager listening");
} catch (RemoteException | AlreadyBoundException e) {
e.printStackTrace();
}
if (args.length < 2){
System.out.println("Please assign a port that the RMI server need to bind, also the path to AutopilotAdapter.dll");
return;
}
if (!isPortAvailable(args[0])){
System.out.printf("Port %s is currently unavailable, please use another port.\n", args[0]);
return;
}
File autopilotAdapterDLL = new File(args[1]);
if (!autopilotAdapterDLL.exists()){
System.out.printf("AutopilotAdapter: %s not exist\n", autopilotAdapterDLL);
return;
}
ModelManager manager = new ModelManager(args[1]);
try {
int port = Integer.valueOf(args[0]);
Registry registry = LocateRegistry.createRegistry(port);
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, 0));
System.out.println("RMIManager listening");
} catch (RemoteException | AlreadyBoundException e) {
e.printStackTrace();
}
}
public static boolean isPortAvailable(String portString) {
int port;
try {
port = Integer.valueOf(portString);
} catch (NumberFormatException e) {
System.out.printf("Port %s is illegel, please check your input.", portString);
throw e;
}
ServerSocket ss = null;
DatagramSocket ds = null;
try {
ss = new ServerSocket(port);
ss.setReuseAddress(true);
ds = new DatagramSocket(port);
ds.setReuseAddress(true);
return true;
} catch (IOException e) {
} finally {
if (ds != null) {
ds.close();
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
}
}
}
return false;
}
}
......@@ -32,14 +32,10 @@ import rwth.rmi.model.server.interfaces.VehicleModel;
*/
public class AutopilotAdapter implements VehicleModel {
static {
// make sure the corresponding DLL is on the classpath
// when the java app with simulation is started
System.loadLibrary("AutopilotAdapter");
}
public AutopilotAdapter() {
public AutopilotAdapter(String libraryPath) {
super();
// Load AutopilotAdapter.dll from libraryPath
System.load(libraryPath);
initialize();
}
......
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