mainWindow.setBackground(mainWindow.getBackground());//this get the system color for window background and apply it to the log area
mainWindow.setVisible(true);
ACommandLineUtilities.dispArgs(args);
ACommandLineUtilities clUtil = null;
try {
List<GenericTerminal> availableTerminals = new LinkedList<GenericTerminal>();
ArgSpec argSpecs[] = {
new ArgSpec(ARG_PARALLEL_MODE, ARG_PARALLEL_MODE_ID),
new ArgSpec(ARG_CHECK_CARD_CONNECTION, ARG_CHECK_CARD_CONNECTION_ID)
};
//params = processStdArgs(args, argSpecs, Main.class, "NimpSmartCardScriptPlayer");
ArgSpec internalArgSpecs[] = addStdArgSpecs(argSpecs);
//internalArgSpecs = ACommandLineUtilities.addArgFileArgSpecs(internalArgSpecs);
Set specs = ACommandLineUtilities.addArgFileArgSpecs(internalArgSpecs);
internalArgSpecs = new ArgSpec[specs.size()];
specs.toArray(internalArgSpecs);
ConfigArgProcessor configArgProcessor = new ConfigArgProcessor(Main.class, "NimpSmartCardScriptPlayer");
clUtil = new ACommandLineUtilities(new File(AFileUtilities.getCurrentDirectory()), args, internalArgSpecs, configArgProcessor, true);
clUtil.processArgs();
clUtil.setAllowUnprocessedArgs(false);
clUtil.throwIfError();
params = configArgProcessor.getStdParams();
//params.args = clUtil.getArgsAsArray();
params.args=null;
getAvailableTerminals(params.terminalManagers, availableTerminals);
if (0 == availableTerminals.size()) {
printNoTerminalMessage(params.terminalManagers);
//throw new RuntimeException();
error = new RuntimeException();
return;
}
try {
long startOfTerminalChooser = System.nanoTime();
selectedTerminals = new LinkedList<GenericTerminal>();
players = new LinkedList<ScriptPlayer>();
errors = new LinkedList<Boolean>();
logFiles = new LinkedList<File>();
if (clUtil.hasArg(ARG_PARALLEL_MODE)) {
StringBuilder sb = new StringBuilder("Cards detected in following readers:\n");
StringBuilder terminalNames = new StringBuilder();
for (GenericTerminal terminal : availableTerminals) {
if (terminal.isCardPresent()) {
selectedTerminals.add(terminal);
players.add(new ScriptPlayer());
errors.add(Boolean.FALSE);
logFiles.add(null);
terminalNames.append("\t- ");
terminalNames.append(terminal.getName());
terminalNames.append("\n");
}
}
if (0 == selectedTerminals.size()) {
System.out.println("Could not find any card.");
mainWindow.setBackground(Color.red);
return;
}
if (clUtil.hasArg(ARG_FIXED_PARALLEL_MODE)) {
String nTerminalsStr = clUtil.getArgValue(ARG_FIXED_PARALLEL_MODE, null);
int nTerminals = Integer.decode(nTerminalsStr);
if (selectedTerminals.size() != nTerminals) {
String msg = "Command line request " + nTerminals + " terminals with card but " + selectedTerminals.size() + " have been detected:";
/*System.out.println(msg);
System.out.println(terminalNames);
mainWindow.setBackground(Color.red);
return;*/
msg += "\n" + terminalNames;
throw new RuntimeException(msg);
}
} else {
clUtil.getArgValue(ARG_PARALLEL_MODE, null);
sb.append(terminalNames);
sb.append("\nContinue ?\n");
int choice = JOptionPane.showConfirmDialog(null, sb.toString(), "Parallel mode", JOptionPane.OK_CANCEL_OPTION);
if (JOptionPane.OK_OPTION == choice) {
//nothing to do
}
if (JOptionPane.CANCEL_OPTION == choice) {
throw new StopRequestFromUserException("Operation cancelled by user.");
}
}
System.out.println("Launching parallel mode on " + selectedTerminals.size() + " terminals:");
System.out.println(terminalNames.toString());
} else {
selectedTerminals.add(Utilities.terminalChooser(availableTerminals));
players.add(new ScriptPlayer());
errors.add(Boolean.FALSE);
logFiles.add(null);
}
deadTime += System.nanoTime() - startOfTerminalChooser;
if (clUtil.hasArg(ARG_CHECK_CARD_CONNECTION, null)) {
List<GenericTerminal> unconnectedCardTerminals = new LinkedList<GenericTerminal>();
StringBuilder sb = new StringBuilder();
StringBuilder terminalNames = new StringBuilder();
for (GenericTerminal terminal : selectedTerminals) {
boolean connected = false;
try {
terminal.coldConnect();
connected = terminal.isConnected();
} catch (Throwable ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
if (false == connected) {
unconnectedCardTerminals.add(terminal);
terminalNames.append("\t- ");
terminalNames.append(terminal.getName());
terminalNames.append("\n");
}
}
if (unconnectedCardTerminals.size() > 0) {
sb.append("Card connection failed on following ");
if (unconnectedCardTerminals.size() > 1) {
sb.append(unconnectedCardTerminals.size());
sb.append(" terminals:\n");
} else {
sb.append("terminal:\n");
}
sb.append(terminalNames);
//JOptionPane.showMessageDialog(null, sb.toString(), "Card connection check", JOptionPane.ERROR_MESSAGE);
//System.out.println(sb.toString());
//mainWindow.setBackground(Color.red);
//return;
throw new RuntimeException(sb.toString());
}
}
} catch (StopRequestFromUserException ex) {
/*System.out.println("Operation stopped by user.");
mainWindow.setBackground(Color.red);
return;*/
throw ex;
}
if (1 == selectedTerminals.size()) {
selectedTerminals.get(0).addLogHandler(params.getPrintStreamLogHandler());
System.out.println("Connected to " + selectedTerminals.get(0).getName());
}
//ACommandLineUtilities clUtil = new ACommandLineUtilities(new File(AFileUtilities.getCurrentDirectory()), args, argSpecs, this);
clUtil.setArgProcessor(this);
clUtil.reset();
//ACommandLineUtilities.dispArgs(clUtil.getArgsAsArray());
//try {
while (clUtil.processArg()) {
if (null != errorHappened) {
throw errorHappened;
}
}
clUtil.throwIfError();
System.out.println("No more arguments to process, exit.");
} catch (StopRequestFromUserException ex) {
if (ex != errorHappened) {
error = ex;
}
} catch (Throwable ex) {
if (ex != errorHappened) {
error = ex;
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
} finally {
//TODO: parallel mode handling
//--> log stopped by user in each terminal log
//--> if a terminal crash (throw something else than StopRequestFromUserException), only this one should be stopped
long end = System.nanoTime();
if (null != error) {
errorHappened = new RuntimeException();
//Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, error);
if (error instanceof StopRequestFromUserException) {
System.out.println("Operations stopped by user.");
} else if (null != clUtil) {
System.out.println("Current argument index: " + clUtil.getCurArgIndex());
System.out.println("Current argument: " + clUtil.getCurArg());
ACommandLineUtilities.dispArgs(clUtil.getArgsAsArray());
}
}
long minTime = Long.MAX_VALUE;
long maxTime = Long.MIN_VALUE;
GenericTerminal minTimeTerminal = null;