diff --git a/src/RemoteSimulationControl.java b/src/RemoteSimulationControl.java index 2148d5543a9bfffb0e9980859af50d74e69a1903..710d13fc632adce42130aa8a33c6c49247e985d7 100755 --- a/src/RemoteSimulationControl.java +++ b/src/RemoteSimulationControl.java @@ -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"); } - public static void printHelp(CommandParser cp, String cmd) { - System.out.println("\nCommand " + cmd + ": "); + public static void printSeparator() { System.out.println("-------------------------------------------------------------"); - System.out.println(cp.getHelp(cmd)); + } + + public static void printEndSeparator() { 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) { String tmp; int i; @@ -173,7 +191,7 @@ public class RemoteSimulationControl extends Thread { try { while(true) { s = rc.readOneLine(); - System.out.print("From server: " + s + "\n>"); + System.out.print("\nFrom server: " + s + "\n>"); System.out.flush(); } } catch (RemoteConnectionException rce) { @@ -191,6 +209,7 @@ public class RemoteSimulationControl extends Thread { // Thread reading from keyboard public void run() { String tmp; + int ret; String input; BufferedReader dataIn; @@ -206,7 +225,8 @@ public class RemoteSimulationControl extends Thread { System.out.print(">"); System.out.flush(); input = dataIn.readLine(); - if (cp.isQuitCommand(input)) { + if (input.trim().length() == 0) { + } else if (cp.isQuitCommand(input)) { mygo = false; try { rc.disconnect(); @@ -217,17 +237,25 @@ public class RemoteSimulationControl extends Thread { } else if (cp.isHelpCommand(input)) { tmp = cp.getHelpWithCommand(input); if ((tmp != null) && (tmp.length() > 0)) { + printHelp(cp, tmp); } else { - printHelp(cp, input); + printHelp(); } } else if (cp.isListCommand(input)) { - System.out.println("Available commands:"); + System.out.println("\nAvailable commands:"); + printSeparator(); System.out.println(cp.getCommandList()); + printEndSeparator(); } else { - if (cp.isAValidCommand(input)) { + ret = cp.isAValidCommand(input); + if (ret > -1) { rc.send(cp.transformCommandFromUserToSimulator(input)); } else { - System.out.println("** Unknown command **"); + if (ret == -1) { + System.out.println("** wrong command **"); + } else { + System.out.println("** wrong number / type of arguments **"); + } } } } diff --git a/src/remotesimulation/CommandParser.java b/src/remotesimulation/CommandParser.java index 1512dc86f22f56407c49f7fbd1c9f0cf07372dfc..1ecbf349b606bc38ac602c28f4e029e4bdb9ce71 100755 --- a/src/remotesimulation/CommandParser.java +++ b/src/remotesimulation/CommandParser.java @@ -96,12 +96,18 @@ public class CommandParser { } public String getHelp(String cmd) { + //System.out.println("calculating help on cmd"); StringBuffer sb = new StringBuffer(""); boolean commandFound = false; for(SimulationCommand sc: commandList) { - if (sc.userCommand.equals(cmd)) { - sb.append(sc.getSynopsis() + "\n" + sc.help + "\n"); + if (sc.userCommand.equals(cmd) || sc.alias.equals(cmd)) { + 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) { @@ -123,32 +129,119 @@ public class CommandParser { return isCommand(cmd, "list"); } - public boolean isAValidCommand(String cmd) { - return true; + public int isAValidCommand(String cmd) { + 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) { - 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() { + int cpt = 0; StringBuffer sb = new StringBuffer(""); 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(); } + // Fill two arrays with information about commands private void fillCommandList() { SimulationCommand sc; int[] params; 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]; 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); } diff --git a/src/remotesimulation/SimulationCommand.java b/src/remotesimulation/SimulationCommand.java index a19ac03943dc5f1a078c1fab241b165a4bcc6a2d..bd657ffe3f350dce2449ee6ca95c5d5fa1bd8833 100755 --- a/src/remotesimulation/SimulationCommand.java +++ b/src/remotesimulation/SimulationCommand.java @@ -53,6 +53,7 @@ import javax.swing.*; public class SimulationCommand { public String userCommand; + public String alias; public String simulatorCommand; public int[] params; public String[] paramNames; @@ -62,12 +63,13 @@ public class SimulationCommand { // 3: optional int // 4: optional String // 5: String to translate to id - + // WARNING: optional parameters must be put at the end of the list 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; + alias = _alias; simulatorCommand = _simulatorCommand; params = _params; paramNames = _paramNames; @@ -90,29 +92,90 @@ public class SimulationCommand { } if (params[i] == 1) { - return "<int " + paramNames[i] + ">"; + return " <int: " + paramNames[i] + ">"; } if (params[i] == 2) { - return "<string " + paramNames[i] + ">"; + return " <string: " + paramNames[i] + ">"; } if (params[i] == 3) { - return "[int " + paramNames[i] + "]"; + return " [int: " + paramNames[i] + "]"; } 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