View Javadoc

1   package org.lsst.ccs.plugin.jas3.console;
2   
3   import java.io.BufferedReader;
4   import java.io.IOException;
5   import java.io.InputStreamReader;
6   
7   import java.io.PrintWriter;
8   import org.freehep.jas.plugin.console.Console;
9   import org.freehep.jas.plugin.console.ConsoleOutputStream;
10  import org.lsst.ccs.HardwareException;
11  import org.lsst.ccs.subsystems.console.BaseConsole;
12  
13  /**
14   * This class extends BaseConsole from CCS core, and adds JAS specific 
15   * functionality. Much confusion ensues from the fact the fact that this
16   * is a CCS console, which displays its output in a JAS console (same name
17   * different meaning).
18   * @author tonyj
19   */
20  
21  class CommandConsole extends BaseConsole {
22  
23      private BufferedReader rdr;
24      private PrintWriter pw;
25      private final Console console;
26      
27      /**
28       * Create a CommandConsole. 
29       * @param console The JAS console (GUI) where this CCS console will be displayed.
30       */
31      CommandConsole(Console console) {
32          this.rdr = new BufferedReader(new InputStreamReader(console.getInputStream("console>")));
33          ConsoleOutputStream out = console.getOutputStream(null);
34          this.pw = new PrintWriter(out, true);
35          this.console = console;
36      }
37  
38     
39      void runConsole() throws HardwareException {
40          pw.println("Console ready, locking key = " + getMessagingFactory().getToken());
41          while (true) {
42              try {
43                  String line = rdr.readLine();
44                  if (line == null) {
45                      // Getting a null here indicates that the input stream has been closed, 
46                      // meaning that the JAS console has been closed
47                      break;
48                  }
49                  line = line.trim();
50                  if ("quit".equals(line) || "exit".equals(line)) {
51                      // Closing the console should result in the associated input stream being closed
52                      // which should cause readline above to return null next time it is called.
53                      console.close();
54                  } else if (!"".equals(line)) {
55                      execute(line);
56                  }
57              } catch (IOException e) {
58                  // FIXME: Should we just log this instead of printing it on the console?
59                  // Hopefully it will rarely happen anyway.
60                  pw.println(e);
61              }
62          }
63          shutdown();
64      }
65  
66      @Override
67      protected void message(String message, boolean isError) {
68          // FIXME: We could use a stream with different attributes to write
69          // errors (for example in red)
70          pw.println(message);
71      }
72  
73      @Override
74      protected void messageFromBus(String message) {
75          pw.println(message);
76      }
77  }