Esempio n. 1
0
  /** Does the work for the main method. */
  public static int runMain(String[] args) {
    String usage =
        new StringBuffer()
            .append("\n   Usage:")
            .append("\n      ")
            .append(HubMonitor.class.getName())
            .append("\n           ")
            .append(" [-help]")
            .append(" [+/-verbose]")
            .append("\n           ")
            .append(" [-auto <secs>]")
            .append(" [-nomsg]")
            .append(" [-nogui]")
            .append("\n           ")
            .append(" [-mtype <pattern>]")
            .append("\n")
            .toString();
    List argList = new ArrayList(Arrays.asList(args));
    int verbAdjust = 0;
    boolean gui = true;
    boolean trackMsgs = true;
    int autoSec = 3;
    Subscriptions subs = new Subscriptions();
    for (Iterator it = argList.iterator(); it.hasNext(); ) {
      String arg = (String) it.next();
      if (arg.startsWith("-auto") && it.hasNext()) {
        it.remove();
        String sauto = (String) it.next();
        it.remove();
        autoSec = Integer.parseInt(sauto);
      } else if (arg.equals("-gui")) {
        it.remove();
        gui = true;
      } else if (arg.equals("-nogui")) {
        it.remove();
        gui = false;
      } else if (arg.equals("-msg")) {
        it.remove();
        trackMsgs = true;
      } else if (arg.equals("-nomsg")) {
        it.remove();
        trackMsgs = false;
      } else if (arg.startsWith("-mtype") && it.hasNext()) {
        it.remove();
        String mpat = (String) it.next();
        it.remove();
        subs.addMType(mpat);
      } else if (arg.startsWith("-v")) {
        it.remove();
        verbAdjust--;
      } else if (arg.startsWith("+v")) {
        it.remove();
        verbAdjust++;
      } else if (arg.startsWith("-h")) {
        it.remove();
        System.out.println(usage);
        return 0;
      } else {
        it.remove();
        System.err.println(usage);
        return 1;
      }
    }
    assert argList.isEmpty();

    // Adjust logging in accordance with verboseness flags.
    int logLevel = Level.WARNING.intValue() + 100 * verbAdjust;
    Logger.getLogger("org.astrogrid.samp").setLevel(Level.parse(Integer.toString(logLevel)));

    // Get profile.
    final ClientProfile profile = DefaultClientProfile.getProfile();

    // Create the HubMonitor.
    final HubMonitor monitor = new HubMonitor(profile, trackMsgs, autoSec);

    // Add a handler for extra MTypes if so requested.
    if (!subs.isEmpty()) {
      final Subscriptions extraSubs = subs;
      HubConnector connector = monitor.getHubConnector();
      final Response dummyResponse = new Response();
      dummyResponse.setStatus(Response.WARNING_STATUS);
      dummyResponse.setResult(new HashMap());
      dummyResponse.setErrInfo(new ErrInfo("Message logged, " + "no other action taken"));
      connector.addMessageHandler(
          new MessageHandler() {
            public Map getSubscriptions() {
              return extraSubs;
            }

            public void receiveNotification(
                HubConnection connection, String senderId, Message msg) {}

            public void receiveCall(
                HubConnection connection, String senderId, String msgId, Message msg)
                throws SampException {
              connection.reply(msgId, dummyResponse);
            }
          });
      connector.declareSubscriptions(connector.computeSubscriptions());
    }

    // Start the gui in a new window.
    final boolean isVisible = gui;
    SwingUtilities.invokeLater(
        new Runnable() {
          public void run() {
            JFrame frame = new JFrame("SAMP HubMonitor");
            frame.getContentPane().add(monitor);
            frame.setIconImage(
                new ImageIcon(Metadata.class.getResource("images/eye.gif")).getImage());
            frame.pack();
            frame.setVisible(isVisible);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
        });
    return 0;
  }