001    package toolbus.commandline;
002    
003    import java.io.BufferedReader;
004    import java.io.IOException;
005    import java.io.InputStream;
006    import java.io.InputStreamReader;
007    import toolbus.ToolBus;
008    import toolbus.logging.ILogger;
009    import toolbus.logging.IToolBusLoggerConstants;
010    import toolbus.logging.LoggerFactory;
011    
012    public class CommandLine{
013            private final ToolBusInputStreamHandler toolBusInputStreamHandler;
014            
015            private CommandLine(ToolBus toolbus, InputStream is, boolean closeStreamWhenDone){
016                    super();
017                    
018                    toolBusInputStreamHandler = new ToolBusInputStreamHandler(toolbus, is == null ? System.in : is, closeStreamWhenDone);
019            }
020            
021            private void startHandling(){
022                    Thread toolBusInputStreamHandlerThread = new Thread(toolBusInputStreamHandler);
023                    toolBusInputStreamHandlerThread.setName("ToolBus inputstream handler");
024                    toolBusInputStreamHandlerThread.setDaemon(true);
025                    toolBusInputStreamHandlerThread.start();
026            }
027            
028            public static CommandLine createCommandLine(ToolBus toolbus, InputStream is, boolean closeStreamWhenDone){
029                    CommandLine commandLine = new CommandLine(toolbus, is, closeStreamWhenDone);
030                    commandLine.startHandling();
031                    return commandLine;
032            }
033            
034            public static class ToolBusInputStreamHandler implements Runnable{
035                    private final ToolBus toolbus;
036                    private final BufferedReader reader;
037                    private final boolean closeStreamWhenDone;
038                    
039                    public ToolBusInputStreamHandler(ToolBus toolbus, InputStream inputStream, boolean closeStreamWhenDone){
040                            this.toolbus = toolbus;
041                            reader = new BufferedReader(new InputStreamReader(inputStream));
042                            this.closeStreamWhenDone = closeStreamWhenDone;
043                    }
044                    
045                    public void run(){
046                            String line = null;
047                            try{
048                                    while((line = reader.readLine()) != null){
049                                            if(line.equals("dump unhandled messages")){
050                                                    toolbus.dumpUnhandledMessages();
051                                            }else if(line.equals("dump performance stats")){
052                                                    toolbus.dumpPerformanceStats();
053                                            }else if(line.equals("dump tools status")){
054                                                    toolbus.getToolInstanceManager().showStatus();
055                                            }else if(line.equals("dump queued messages")){
056                                                    toolbus.getToolInstanceManager().printQueueTerms();
057                                            }else if(line.equals("dump toolbus state")){
058                                                    toolbus.showStatus();
059                                            }else if(line.equals("dump partnerless communication atoms")){
060                                                    toolbus.getMatchStore().printPartnerlessCommunicationAtoms();
061                                            }else if(line.equals("shutdown")){
062                                                    toolbus.shutdown(toolbus.getTBTermFactory().makeList());
063                                            }else if(line.equals("help")){
064                                                    System.err.println("dump unhandled messages: Prints all (currently) unhandled messages and enqueued notes.");
065                                                    System.err.println("dump performance stats: Prints some performance statistics related to the execution of the ToolBus.");
066                                                    System.err.println("dump tools status: Prints a listing of currently connected tools with their corresponding state.");
067                                                    System.err.println("dump queued messages: Prints all (currently) queued values and events.");
068                                                    System.err.println("dump toolbus state: Dumps all available information about the current state of the process logic to the command line.");
069                                                    System.err.println("dump partnerless communication atoms: Prints a list of all communication atoms of which we can guarantee that they can never be executed.");
070                                                    System.err.println("shutdown: Initiates the forcefull termination of the ToolBus.");
071                                            }else if(line.equals("")){
072                                                    // Ignore this.
073                                            }else{
074                                                    System.err.println("Unknown ToolBus command: '"+line+"'.\nEnter 'help' for a list of available ToolBus commands.");
075                                            }
076                                    }
077                            }catch(IOException ioex){
078                                    LoggerFactory.log("An IOException occured while reading from the ToolBus's inputstream.", ioex, ILogger.ERROR, IToolBusLoggerConstants.EXECUTE);
079                            }catch(RuntimeException rex){
080                                    LoggerFactory.log("An RuntimeException occured while reading from the ToolBus's inputstream.", rex, ILogger.ERROR, IToolBusLoggerConstants.EXECUTE);
081                            }finally{
082                                    if(closeStreamWhenDone){
083                                            try{
084                                                    reader.close();
085                                            }catch(IOException ioex){
086                                                    LoggerFactory.log("Unable to close the ToolBus's input stream.", ILogger.FATAL, IToolBusLoggerConstants.TOOLINSTANCE);
087                                            }
088                                    }
089                            }
090                    }
091            }
092    }