⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 startitinfo.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
/**
 * The contents of this file are subject to the OAA  Community Research
 * License Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License
 * at http://www.ai.sri.com/~oaa/.  Software distributed under the License
 * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * rights and limitations under the License.  Portions of the software are
 * Copyright (c) SRI International, 1999-2003.  All rights reserved.
 * "OAA" is a registered trademark, and "Open Agent Architecture" is a
 * trademark, of SRI International, a California nonprofit public benefit
 * corporation.
*/

package com.sri.oaa2.agt.startit;

import java.io.*;
import java.util.*;

public class StartitInfo {

  public Vector/*appInfo*/ apps;
  private OaaConnection oaa;
  protected AppInfo facilitator = null;
  private String[] cmdLineArgs; // used only for OAA args
  protected boolean showDevOptions = false;
  private boolean waitingToExit = false; // user asked to quit
  private static Outputter outputter = new Outputter() {
    // Output to stdout by default, if no one overrides with
    // setOutputter();
    public void outputLine(AppInfo app, String line, int type) {
      String appid = (app == null)? "startit" : app.appid;
      System.out.println(appid + "> " + line);
    }
  };

  public final static String PROJECTLIST = "projectlist";

  public Vector/*String*/ projectlist;
  private String activeProject = null; // null is global

  public StartitInfo(String filename, String args[]) {
    try {
      ConfigReader cr = new ConfigReader(filename);
      initialize(cr, args);
    }
    catch (FileNotFoundException e) {
      System.err.println(e);
    }
  }

  public StartitInfo(ConfigReader reader, String args[]) {
    initialize(reader, args);
  }

  private void initialize(ConfigReader reader, String args[]) {
    String token;
    AppInfo app;
    apps = new Vector/*AppInfo*/();

    cmdLineArgs = args;

    while (reader.readLine()) {
      token = reader.getFirstToken();
      if (token.equals(PROJECTLIST)) {
	projectlist = reader.getRemainingTokens();
	activeProject = (String) projectlist.get(0);
      }
      else if (token.equals(AppInfo.APPNAME)) {
	app = new AppInfo(this, reader, args);
	if (app.facilitator) {
	  if (facilitator != null) {
	    System.err.println("WARNING: multiple facilitators not allowed in config file");
	  }
	  facilitator = app;
	}
	apps.add(app);
      }
      else {
	reader.printError("unknown startit keyword: " + token);
      }
    }

    // Config file has been read in completely, now do things we needed
    // all information to do

    // (resolve waitfor's, check vars can resolve, ...?
    for (Iterator i = apps.iterator(); i.hasNext(); ) {
      app = (AppInfo) i.next();

      // if we're managing a facilitator, make all agents wait for it
      if (facilitator != null && app.isAgent() && app != facilitator) {
	app.agentsToWaitFor.add(facilitator);
	facilitator.appsWaitingForMe.add(app);
      }

      // do all post-creation initialization; this is stuff that had
      // to wait for all apps to have been created (stuff that requires
      // name resolution)
      app.initialize();
    }

    // if we're not managing our own facilitator, we try to connect to
    // some external facilitator immediately, and just stay connected
    if (facilitator == null) {
      oaa = new OaaConnection(this, cmdLineArgs);
      oaa.connect();
    }
    else {
      oaa = facilitator.getProcess().getOaaConnection();
    }
    for (Iterator i = apps.iterator(); i.hasNext(); ) {
      app = (AppInfo) i.next();
      if (app.isAgent() && !app.isFacilitator()) {
	app.getProcess().setOaaConnection(oaa);
      }
    }

    // set up the startDelayTimer
    // StartitProcess.setStartTimerDelay(100);
  }
  
  public boolean managedFacilitator() {
    return (facilitator != null);
  }

  public String getActiveProject() {
    return activeProject;
  }

  public boolean setActiveProject(String proj) {
    // check for an invalid project name
    if (proj != null && // (null is ok - it's the global project)
	!projectlist.contains(proj)) return false;

    activeProject = proj;
    return true;
  }

  public OaaConnection getOaaConnection() {
    return oaa;
  }

  public String resolveVariable(String v) {
    AppInfo app;
    String s;

    try {
      // try ${var:app} syntax
      if (v.indexOf(':') >= 0) {
	String[] arr = v.split(":", 2);
	
	String var = arr[1];
	app = findAppByName(arr[0]);
	s = app.resolveVariable(var, false);
	if (s != null) return s;
      }
      // Not a good idea, so commenting it out:
      //      // otherwise, try resolving it in all apps
      //      for (Iterator i = apps.iterator(); i.hasNext(); ) {
      //	app = (AppInfo) i.next();
      //	s = app.resolveVariable(v, false);
      //	if (s != null) return s;
      //      }
    }
    catch (NullPointerException e) {} // return null, below

    // if that didn't work, give up
    return null;
  }

  public AppInfo findAppByName(String name) {
    if (name == null) return null;

    for (Iterator i = apps.iterator(); i.hasNext(); ) {
      AppInfo app = (AppInfo) i.next();
      if (name.equals(app.appid)) return app;
    }
    return null;
  }

  public AppInfo findAppByOaaName(String name) {
    if (name == null) return null;

    for (Iterator i = apps.iterator(); i.hasNext(); ) {
      AppInfo app = (AppInfo) i.next();
      if (name.equals(app.oaaname)) return app;
    }
    return null;
  }

  public void startShownApps() {
    for (Iterator i = apps.iterator(); i.hasNext(); ) {
      AppInfo app = (AppInfo) i.next();
      if ((!app.hidden && app.isInProject(activeProject)) ||
	  app == facilitator) { // ALWAYS start the main facilitator, even if hidden!
	app.start();
      }
    }
  }

  public void startWaitingApps() {
    for (Iterator i = apps.iterator(); i.hasNext(); ) {
      AppInfo app = (AppInfo) i.next();
      if (app.getProcess().isWaiting()) {
	// try to start it (it won't start if there's still something to wait for)
	app.start();
      }
    }
  }

  public void killWaitingApps() {
    for (Iterator i = apps.iterator(); i.hasNext(); ) {
      AppInfo app = (AppInfo) i.next();
      if (app.getProcess().isWaitingToBeKilled()) {
	app.getProcess().kill();
      }
    }
  }

  public void killAllApps() {
    for (Iterator i = apps.iterator(); i.hasNext(); ) {
      AppInfo app = (AppInfo) i.next();
      app.getProcess().kill();
    }
  }

  public boolean areAnyAppsRunning() {
    for (Iterator i = apps.iterator(); i.hasNext(); ) {
      AppInfo app = (AppInfo) i.next();
      if (app.getProcess().isStarted()) return true;
    }
    return false;
  }

  protected void appKilled() {
    // when an app is successfully killed off:
    // 1: check if all apps are dead and if we were waiting to exit
    if (waitingToExit && !areAnyAppsRunning()) {
      System.exit(0);
    }
    // 2: check if any apps were waiting for that one to die, to be killed
    killWaitingApps();
  }

  public void killAndExit() {
    killAllApps();
    waitingToExit = true;
  }

  public void setOutputter(Outputter p) {
    outputter = p;
  }

  static public Outputter getOutputter() {
    return outputter;
  }

  /** calls debugPrint(String), where String is composed the name and
   * line number of the function that called debugPrint() */
  public static void debugPrint() {
    debugPrint((AppInfo) null);
  }
  /** calls debugPrint(AppInfo, String), where String is composed the name and
   * line number of the function that called debugPrint() */
  public static void debugPrint(AppInfo app) {
    try{ throw(new Throwable()); }
    catch(Throwable t){ 
      debugPrint(app, t.getStackTrace()[1].toString());
    }
  }
  public static void debugPrint(String s) {
    debugPrint(null, s);
  }
  public static void debugPrint(AppInfo app, String s) {
    debugPrint(app, s, Outputter.DEBUG);
  }
  public static void warningPrint(String s) {
    warningPrint(null, s);
  }
  public static void warningPrint(AppInfo app, String s) {
    debugPrint(app, "WARNING: " + s, Outputter.WARNING);
  }
  public static void errorPrint(String s) {
    errorPrint(null, s);
  }
  public static void errorPrint(AppInfo app, String s) {
    debugPrint(app, "ERROR: " + s, Outputter.WARNING);
  }
  public static void debugPrint(AppInfo app, String s, int type) {
    StartitInfo.getOutputter().outputLine(app, s, type);
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -