/** Creates a scenario based on Settings object. */
  protected SimScenario() {
    Settings s = new Settings(SCENARIO_NS);
    nrofGroups = s.getInt(NROF_GROUPS_S);

    this.name = s.valueFillString(s.getSetting(NAME_S));
    this.endTime = s.getDouble(END_TIME_S);
    this.updateInterval = s.getDouble(UP_INT_S);
    this.simulateConnections = s.getBoolean(SIM_CON_S);

    s.ensurePositiveValue(nrofGroups, NROF_GROUPS_S);
    s.ensurePositiveValue(endTime, END_TIME_S);
    s.ensurePositiveValue(updateInterval, UP_INT_S);

    this.simMap = null;
    this.maxHostRange = 1;

    this.connectionListeners = new ArrayList<ConnectionListener>();
    this.messageListeners = new ArrayList<MessageListener>();
    this.movementListeners = new ArrayList<MovementListener>();
    this.updateListeners = new ArrayList<UpdateListener>();
    this.appListeners = new ArrayList<ApplicationListener>();
    this.eqHandler = new EventQueueHandler();

    /* TODO: check size from movement models */
    s.setNameSpace(MovementModel.MOVEMENT_MODEL_NS);
    int[] worldSize = s.getCsvInts(MovementModel.WORLD_SIZE, 2);
    this.worldSizeX = worldSize[0];
    this.worldSizeY = worldSize[1];

    createHosts();

    this.world =
        new World(
            hosts,
            worldSizeX,
            worldSizeY,
            updateInterval,
            updateListeners,
            simulateConnections,
            eqHandler.getEventQueues());
  }
  /** Creates hosts for the scenario */
  protected void createHosts() {
    this.hosts = new ArrayList<DTNHost>();

    for (int i = 1; i <= nrofGroups; i++) {
      List<NetworkInterface> interfaces = new ArrayList<NetworkInterface>();
      Settings s = new Settings(GROUP_NS + i);
      s.setSecondaryNamespace(GROUP_NS);
      String gid = s.getSetting(GROUP_ID_S);
      int nrofHosts = s.getInt(NROF_HOSTS_S);
      int nrofInterfaces = s.getInt(NROF_INTERF_S);
      int appCount;

      // creates prototypes of MessageRouter and MovementModel
      MovementModel mmProto =
          (MovementModel) s.createIntializedObject(MM_PACKAGE + s.getSetting(MOVEMENT_MODEL_S));
      MessageRouter mRouterProto =
          (MessageRouter) s.createIntializedObject(ROUTING_PACKAGE + s.getSetting(ROUTER_S));

      /* checks that these values are positive (throws Error if not) */
      s.ensurePositiveValue(nrofHosts, NROF_HOSTS_S);
      s.ensurePositiveValue(nrofInterfaces, NROF_INTERF_S);

      // setup interfaces
      for (int j = 1; j <= nrofInterfaces; j++) {
        String intName = s.getSetting(INTERFACENAME_S + j);
        Settings intSettings = new Settings(intName);
        NetworkInterface iface =
            (NetworkInterface)
                intSettings.createIntializedObject(
                    INTTYPE_PACKAGE + intSettings.getSetting(INTTYPE_S));
        iface.setClisteners(connectionListeners);
        iface.setGroupSettings(s);
        interfaces.add(iface);
      }

      // setup applications
      if (s.contains(APPCOUNT_S)) {
        appCount = s.getInt(APPCOUNT_S);
      } else {
        appCount = 0;
      }
      for (int j = 1; j <= appCount; j++) {
        String appname = null;
        Application protoApp = null;
        try {
          // Get name of the application for this group
          appname = s.getSetting(GAPPNAME_S + j);
          // Get settings for the given application
          Settings t = new Settings(appname);
          // Load an instance of the application
          protoApp = (Application) t.createIntializedObject(APP_PACKAGE + t.getSetting(APPTYPE_S));

          // Set application listeners
          protoApp.setAppListeners(this.appListeners);
          // Set the proto application in proto router
          // mRouterProto.setApplication(protoApp);
          mRouterProto.addApplication(protoApp);
        } catch (SettingsError se) {
          // Failed to create an application for this group
          System.err.println("Failed to setup an application: " + se);
          System.err.println("Caught at " + se.getStackTrace()[0]);
          System.exit(-1);
        }
      }

      if (mmProto instanceof MapBasedMovement) {
        this.simMap = ((MapBasedMovement) mmProto).getMap();
      }

      // creates hosts of ith group
      // smcho added
      // database =
      // end added

      for (int j = 0; j < nrofHosts; j++) {
        ModuleCommunicationBus comBus = new ModuleCommunicationBus();

        // prototypes are given to new DTNHost which replicates
        // new instances of movement model and message router
        DTNHost host =
            new DTNHost(
                this.messageListeners,
                this.movementListeners,
                gid,
                interfaces,
                comBus,
                mmProto,
                mRouterProto);

        // smcho added
        // todo:: no error checking when there are no files or directories exist
        //                String initialContext = s.getSetting("host" + Integer.toString(j));
        //                List<String> contexts = Arrays.asList(initialContext);
        //                ContextMessage c =
        // ContextMessage.load(JavaConversions.asScalaBuffer(Arrays.asList(initialContext)));
        //                host.setContextMessage(c);
        // end added

        hosts.add(host);
      }
    }
  }