/** 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);

    if (s.contains(SIM_FILES_S)) {
      this.simulateFiles = s.getBoolean(SIM_FILES_S);
    } else {
      this.simulateFiles = false;
    }

    ensurePositiveValue(nrofGroups, NROF_GROUPS_S);
    ensurePositiveValue(endTime, END_TIME_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.queryListeners = new ArrayList<FileEventListener>();
    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();
    if (this.simulateFiles) {
      addFilesToHosts();
    }

    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>();
    int lastGroupWithFiles = -1;
    this.nrofGroupsWithFiles = 0;
    this.groupSizes = new int[nrofGroups];

    for (int i = 1; i <= nrofGroups; i++) {
      List<NetworkInterface> mmNetInterfaces = 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;
      boolean hasFileCapability;
      if (s.contains(FILE_CAPABILITY_S)) {
        hasFileCapability = s.getBoolean(FILE_CAPABILITY_S);
      } else {
        hasFileCapability = false;
      }

      if (hasFileCapability && i != lastGroupWithFiles) {
        lastGroupWithFiles = i;
        this.nrofGroupsWithFiles++;
      }

      // 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)
      ensurePositiveValue(nrofHosts, NROF_HOSTS_S);
      ensurePositiveValue(nrofInterfaces, NROF_INTERF_S);
      this.groupSizes[i - 1] = nrofHosts;

      // setup interfaces
      for (int j = 1; j <= nrofInterfaces; j++) {
        String Intname = s.getSetting(INTERFACENAME_S + j);
        Settings t = new Settings(Intname);
        NetworkInterface mmInterface =
            (NetworkInterface) t.createIntializedObject(INTTYPE_PACKAGE + t.getSetting(INTTYPE_S));
        mmInterface.setClisteners(connectionListeners);
        mmNetInterfaces.add(mmInterface);
      }

      // 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
      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,
                this.queryListeners,
                gid,
                mmNetInterfaces,
                comBus,
                mmProto,
                mRouterProto,
                hasFileCapability);
        hosts.add(host);
      }
    }
  }