Skip to content
Snippets Groups Projects
Commit c1969e1d authored by Ludovic Apvrille's avatar Ludovic Apvrille
Browse files

New version of the remote simulation controller

parent 8ef5580a
No related branches found
No related tags found
No related merge requests found
...@@ -81,13 +81,31 @@ public class RemoteSimulationControl extends Thread { ...@@ -81,13 +81,31 @@ public class RemoteSimulationControl extends Thread {
System.out.println("port: port on which the simulator accepts commands (default: 3490). Must be a positive integer value"); System.out.println("port: port on which the simulator accepts commands (default: 3490). Must be a positive integer value");
} }
public static void printHelp(CommandParser cp, String cmd) { public static void printSeparator() {
System.out.println("\nCommand " + cmd + ": ");
System.out.println("-------------------------------------------------------------"); System.out.println("-------------------------------------------------------------");
System.out.println(cp.getHelp(cmd)); }
public static void printEndSeparator() {
System.out.println("-------------------------------------------------------------\n"); System.out.println("-------------------------------------------------------------\n");
} }
public static void printHelp() {
System.out.println("\nHelp: ");
printSeparator();
System.out.println("help: prints that help");
System.out.println("help <string cmd>: prints the help on a given command");
System.out.println("list: lists all commands");
System.out.println("quit: quits this simulation remote controller");
printEndSeparator();
}
public static void printHelp(CommandParser cp, String cmd) {
System.out.println("\nHelp on command " + cmd + ": ");
printSeparator();
System.out.println(cp.getHelp(cmd));
printEndSeparator();
}
public static boolean analyseArgs(String [] args) { public static boolean analyseArgs(String [] args) {
String tmp; String tmp;
int i; int i;
...@@ -173,7 +191,7 @@ public class RemoteSimulationControl extends Thread { ...@@ -173,7 +191,7 @@ public class RemoteSimulationControl extends Thread {
try { try {
while(true) { while(true) {
s = rc.readOneLine(); s = rc.readOneLine();
System.out.print("From server: " + s + "\n>"); System.out.print("\nFrom server: " + s + "\n>");
System.out.flush(); System.out.flush();
} }
} catch (RemoteConnectionException rce) { } catch (RemoteConnectionException rce) {
...@@ -191,6 +209,7 @@ public class RemoteSimulationControl extends Thread { ...@@ -191,6 +209,7 @@ public class RemoteSimulationControl extends Thread {
// Thread reading from keyboard // Thread reading from keyboard
public void run() { public void run() {
String tmp; String tmp;
int ret;
String input; String input;
BufferedReader dataIn; BufferedReader dataIn;
...@@ -206,7 +225,8 @@ public class RemoteSimulationControl extends Thread { ...@@ -206,7 +225,8 @@ public class RemoteSimulationControl extends Thread {
System.out.print(">"); System.out.print(">");
System.out.flush(); System.out.flush();
input = dataIn.readLine(); input = dataIn.readLine();
if (cp.isQuitCommand(input)) { if (input.trim().length() == 0) {
} else if (cp.isQuitCommand(input)) {
mygo = false; mygo = false;
try { try {
rc.disconnect(); rc.disconnect();
...@@ -217,17 +237,25 @@ public class RemoteSimulationControl extends Thread { ...@@ -217,17 +237,25 @@ public class RemoteSimulationControl extends Thread {
} else if (cp.isHelpCommand(input)) { } else if (cp.isHelpCommand(input)) {
tmp = cp.getHelpWithCommand(input); tmp = cp.getHelpWithCommand(input);
if ((tmp != null) && (tmp.length() > 0)) { if ((tmp != null) && (tmp.length() > 0)) {
printHelp(cp, tmp);
} else { } else {
printHelp(cp, input); printHelp();
} }
} else if (cp.isListCommand(input)) { } else if (cp.isListCommand(input)) {
System.out.println("Available commands:"); System.out.println("\nAvailable commands:");
printSeparator();
System.out.println(cp.getCommandList()); System.out.println(cp.getCommandList());
printEndSeparator();
} else { } else {
if (cp.isAValidCommand(input)) { ret = cp.isAValidCommand(input);
if (ret > -1) {
rc.send(cp.transformCommandFromUserToSimulator(input)); rc.send(cp.transformCommandFromUserToSimulator(input));
} else { } else {
System.out.println("** Unknown command **"); if (ret == -1) {
System.out.println("** wrong command **");
} else {
System.out.println("** wrong number / type of arguments **");
}
} }
} }
} }
......
...@@ -96,12 +96,18 @@ public class CommandParser { ...@@ -96,12 +96,18 @@ public class CommandParser {
} }
public String getHelp(String cmd) { public String getHelp(String cmd) {
//System.out.println("calculating help on cmd");
StringBuffer sb = new StringBuffer(""); StringBuffer sb = new StringBuffer("");
boolean commandFound = false; boolean commandFound = false;
for(SimulationCommand sc: commandList) { for(SimulationCommand sc: commandList) {
if (sc.userCommand.equals(cmd)) { if (sc.userCommand.equals(cmd) || sc.alias.equals(cmd)) {
sb.append(sc.getSynopsis() + "\n" + sc.help + "\n"); sb.append(sc.getSynopsis() + "\n" + sc.help);
if (sc.hasAlias()) {
sb.append("\nalias: " + sc.alias);
}
//System.out.println("Command found" + sc.help);
commandFound = true;
} }
} }
if (commandFound) { if (commandFound) {
...@@ -123,32 +129,119 @@ public class CommandParser { ...@@ -123,32 +129,119 @@ public class CommandParser {
return isCommand(cmd, "list"); return isCommand(cmd, "list");
} }
public boolean isAValidCommand(String cmd) { public int isAValidCommand(String cmd) {
return true; int index = -1;
int cpt = 0;
String cmds[] = cmd.split(" ");
//System.out.println("cmd " + cmd + " has " + cmds.length + " elements");
for(SimulationCommand sc: commandList) {
// Same command name?
if (sc.userCommand.equals(cmds[0]) || sc.alias.equals(cmds[0])) {
// Compatible arguments?
if (sc.areParametersCompatible(cmds)) {
index = cpt;
break;
} else {
index = -2;
}
}
cpt ++;
}
if (index < 0) {
return index;
}
return index;
} }
public String transformCommandFromUserToSimulator(String cmd) { public String transformCommandFromUserToSimulator(String cmd) {
return cmd; int index = isAValidCommand(cmd);
if (index < 0) {
return "";
}
SimulationCommand sc = commandList.get(index);
return sc.translateCommand(cmd.split(" "));
} }
// Returns the list of all commands
public String getCommandList() { public String getCommandList() {
int cpt = 0;
StringBuffer sb = new StringBuffer(""); StringBuffer sb = new StringBuffer("");
for(SimulationCommand sc: commandList) { for(SimulationCommand sc: commandList) {
sb.append(sc.userCommand); if (cpt == 1) {
cpt = 0;
sb.append("\n");
}
if (sc.userCommand.equals(sc.alias)) {
sb.append(sc.userCommand + " ");
} else {
sb.append(sc.userCommand + "/" + sc.alias + " ");
}
cpt ++;
} }
return sb.toString(); return sb.toString();
} }
// Fill two arrays with information about commands
private void fillCommandList() { private void fillCommandList() {
SimulationCommand sc; SimulationCommand sc;
int[] params; int[] params;
String[] paramNames; String[] paramNames;
int i;
// get-command-and-task
params = new int[0];
paramNames = new String[0];
sc = new SimulationCommand("get-command-and-task", "gcat", "14", params, paramNames, "Returns the current command and task");
commandList.add(sc);
// get-simulation-time
params = new int[0];
paramNames = new String[0];
sc = new SimulationCommand("get-simulation-time", "time", "13", params, paramNames, "Returns the current absolute time unit of the simulation");
commandList.add(sc);
// kill
params = new int[0];
paramNames = new String[0];
sc = new SimulationCommand("kill", "kill", "0", params, paramNames, "Terminates the remote simulator");
commandList.add(sc);
// rawcmd
params = new int[5];
paramNames = new String[5];
for(i=0; i<5; i++) {
params[i] = 4;
paramNames[i] = "param #" + i;
}
sc = new SimulationCommand("raw-command", "rc", "", params, paramNames, "Sends a raw command to the remote simulator");
commandList.add(sc);
// run-to-next-breakpoint
params = new int[0];
paramNames = new String[0];
sc = new SimulationCommand("run-to-next-breakpoint", "rtnb", "1 0", params, paramNames, "Runs the simulation until a breakpoint is met");
commandList.add(sc);
// run-x-time-units
params = new int[1];
paramNames = new String[1];
params[0] = 1;
paramNames[0] = "nb of time units";
sc = new SimulationCommand("run-x-time-units", "rxtu", "1 6", params, paramNames, "Runs the simulation for x units of time");
commandList.add(sc);
// kill-simulator // stop
params = new int[0]; params = new int[0];
paramNames = new String[0]; paramNames = new String[0];
sc = new SimulationCommand("kill-simulator", "0", params, paramNames, "Terminates the remote simulator"); sc = new SimulationCommand("stop", "stop", "15", params, paramNames, "Stops the currently running simulation");
commandList.add(sc); commandList.add(sc);
} }
......
...@@ -53,6 +53,7 @@ import javax.swing.*; ...@@ -53,6 +53,7 @@ import javax.swing.*;
public class SimulationCommand { public class SimulationCommand {
public String userCommand; public String userCommand;
public String alias;
public String simulatorCommand; public String simulatorCommand;
public int[] params; public int[] params;
public String[] paramNames; public String[] paramNames;
...@@ -62,12 +63,13 @@ public class SimulationCommand { ...@@ -62,12 +63,13 @@ public class SimulationCommand {
// 3: optional int // 3: optional int
// 4: optional String // 4: optional String
// 5: String to translate to id // 5: String to translate to id
// WARNING: optional parameters must be put at the end of the list
public String help; public String help;
public SimulationCommand(String _userCommand, String _simulatorCommand, int _params[], String _paramNames[], String _help) { public SimulationCommand(String _userCommand, String _alias, String _simulatorCommand, int _params[], String _paramNames[], String _help) {
userCommand = _userCommand; userCommand = _userCommand;
alias = _alias;
simulatorCommand = _simulatorCommand; simulatorCommand = _simulatorCommand;
params = _params; params = _params;
paramNames = _paramNames; paramNames = _paramNames;
...@@ -90,29 +92,90 @@ public class SimulationCommand { ...@@ -90,29 +92,90 @@ public class SimulationCommand {
} }
if (params[i] == 1) { if (params[i] == 1) {
return "<int " + paramNames[i] + ">"; return " <int: " + paramNames[i] + ">";
} }
if (params[i] == 2) { if (params[i] == 2) {
return "<string " + paramNames[i] + ">"; return " <string: " + paramNames[i] + ">";
} }
if (params[i] == 3) { if (params[i] == 3) {
return "[int " + paramNames[i] + "]"; return " [int: " + paramNames[i] + "]";
} }
if (params[i] == 4) { if (params[i] == 4) {
return "[string " + paramNames[i] + "]"; return " [string: " + paramNames[i] + "]";
} }
return "<unknow param>"; return " <unknow param>";
} }
public int getMaxNumberOfParameters() {
return params.length;
}
public int getMinNumberOfParameters() {
int min = 0;
for(int i=0; i<params.length; i++) {
if ((params[i] >0) && (params[i] <3)) {
min ++;
}
}
return min;
}
private boolean checkForInteger(String s) {
try {
int i = Integer.decode(s).intValue();
return true;
} catch (Exception e) {
return false;
}
}
public boolean areParametersCompatible(String[] splitCmd) {
if ((splitCmd.length - 1) < getMinNumberOfParameters()) {
return false;
}
if ((splitCmd.length - 1) > getMaxNumberOfParameters()) {
return false;
}
for(int i=0; i<params.length; i++) {
if (params[i] == 1) {
if (!checkForInteger(splitCmd[i+1])) {
return false;
}
}
if (params[i] == 3) {
if (!checkForInteger(splitCmd[i+1])) {
return false;
}
}
}
return true;
}
public boolean hasAlias() {
if ((alias == null) || (alias.length() == 0)) {
return false;
}
if (alias.equals(userCommand)) {
return false;
}
return true;
}
public String translateCommand(String cmds[]) {
String ret = simulatorCommand;
for(int i=1; i<cmds.length; i++) {
ret += " " + cmds[i];
}
return ret;
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment