예제 #1
0
  private void exerciseMonitors() throws IOException, HubTestException, InterruptedException {
    PlasticMonitor mon1 = new PlasticMonitor("mon1", true, null, null);
    PlasticMonitor mon2 = new PlasticMonitor("mon2", false, null, null);
    ApplicationItem[] regApps = PlasticUtils.getRegisteredApplications(hub_);
    ApplicationListModel appList1 = new ApplicationListModel(regApps);
    ApplicationListModel appList2 = new ApplicationListModel(regApps);
    mon1.setListModel(appList1);
    mon2.setListModel(appList2);
    mon1.setHub(hub_);
    mon2.setHub(hub_);
    PlasticConnection conn1 = PlasticUtils.registerRMI(mon1);
    URI id1 = conn1.getId();
    assertTrue(hub_.getRegisteredIds().contains(id1));
    PlasticConnection conn2 = PlasticUtils.registerXMLRPC(mon2);
    URI id2 = conn2.getId();
    assertTrue(hub_.getRegisteredIds().contains(id2));

    Thread.sleep(2000);

    assertTrue(hub_.getRegisteredIds().contains(id1));
    assertTrue(hub_.getRegisteredIds().contains(id2));
    conn1.unregister();
    assertTrue(!hub_.getRegisteredIds().contains(id1));
    conn2.unregister();
    assertTrue(!hub_.getRegisteredIds().contains(id2));
  }
예제 #2
0
  /**
   * Starts a monitor of the PLASTIC message bus which logs message descriptions to standard output.
   * A choice of Java-RMI and XML-RPC communication is offered.
   *
   * <h2>Flags</h2>
   *
   * <dl>
   *   <dt>-rmi
   *   <dd>Use Java-RMI for communications (default)
   *   <dt>-xmlrpc
   *   <dd>Use XML-RPC for communications
   *   <dt>-gui
   *   <dd>Pops up a window monitoring currently registered applications
   *   <dt>-verbose
   *   <dd>Writes a log to standard output of all PLASTIC traffic
   *   <dt>-warn
   *   <dd>Writes a log to standard output of illegal or questionable conditions
   *   <dt>-multi
   *   <dd>Attempt to implement as many messages as possible (a dummy implementation is provided for
   *       every message know by the {@link MessageDefinition} class)
   *   <dt>-name name
   *   <dd>Supply application name which monitor will register under
   * </dl>
   */
  public static void main(String[] args) throws IOException {
    String usage =
        "\nUsage:"
            + "\n       "
            + PlasticMonitor.class.getName()
            + "\n           "
            + " [-xmlrpc|-rmi]"
            + " [-gui]"
            + " [-verbose]"
            + " [-warn]"
            + " [-multi]"
            + " [-name name]"
            + "\n";

    /* Process flags. */
    List argv = new ArrayList(Arrays.asList(args));
    String mode = "rmi";
    boolean gui = false;
    boolean verbose = false;
    boolean validate = false;
    boolean multiclient = false;
    String name = "monitor";
    for (Iterator it = argv.iterator(); it.hasNext(); ) {
      String arg = (String) it.next();
      if ("-xmlrpc".equals(arg)) {
        it.remove();
        mode = "xmlrpc";
      } else if ("-rmi".equals(arg)) {
        it.remove();
        mode = "rmi";
      } else if ("-gui".equals(arg)) {
        it.remove();
        gui = true;
      } else if ("-verbose".equals(arg)) {
        it.remove();
        verbose = true;
      } else if ("-warn".equals(arg)) {
        it.remove();
        validate = true;
      } else if ("-multi".equals(arg)) {
        it.remove();
        multiclient = true;
      } else if ("-name".equals(arg) && it.hasNext()) {
        it.remove();
        name = (String) it.next();
        it.remove();
      } else if (arg.startsWith("-h")) {
        System.out.println(usage);
        return;
      }
    }
    if (!argv.isEmpty()) {
      System.err.println(usage);
      System.exit(1);
    }

    PrintStream logOut = verbose ? System.out : null;
    PrintStream warnOut = validate ? System.out : null;
    PlasticMonitor mon = new PlasticMonitor(name, multiclient, logOut, warnOut);
    if (gui) {
      PlasticHubListener hub = PlasticUtils.getLocalHub();
      ApplicationItem[] regApps = PlasticUtils.getRegisteredApplications(hub);
      ApplicationListModel appsList = new ApplicationListModel(regApps);
      mon.setListModel(appsList);
      mon.setHub(hub);
      JFrame window = new PlasticListWindow(appsList);
      window.setTitle("PlasticMonitor");
      window.pack();
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.setVisible(true);
    }

    if (logOut != null) {
      logOut.println("Connnecting in " + mode + " mode...");
    }
    if ("rmi".equals(mode)) {
      PlasticUtils.registerRMI(mon);
    } else if ("xmlrpc".equals(mode)) {
      PlasticUtils.registerXMLRPC(mon);
    } else {
      assert false;
    }
    if (logOut != null) {
      logOut.println("...connected.");
    }

    /* Wait on the monitor.  If it receives a HUB_STOPPING message
     * it will be notified and execution of this method can complete. */
    try {
      synchronized (mon) {
        while (!mon.stopped_) {
          mon.wait();
        }
      }
      if (logOut != null) {
        logOut.println("Hub stopped.");
      }
      System.exit(0);
    } catch (InterruptedException e) {
      System.out.println("Interrupted.");
    }
  }