Ejemplo n.º 1
0
  /** 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 a new instance of HomeActivityMovement
   *
   * @param settings
   */
  public HomeActivityMovement(Settings settings) {
    super(settings);
    distance = 100;
    pathFinder = new DijkstraPathFinder(null);
    mode = WALKING_HOME_MODE;

    String homeLocationsFile = null;
    try {
      homeLocationsFile = settings.getSetting(HOME_LOCATIONS_FILE_SETTING);
    } catch (Throwable t) {
      // Do nothing;
    }

    timeDiffSTD = settings.getInt(STD_FOR_TIME_DIFF_SETTING);

    if (homeLocationsFile == null) {
      MapNode[] mapNodes = (MapNode[]) getMap().getNodes().toArray(new MapNode[0]);
      int homeIndex = rng.nextInt(mapNodes.length - 1);
      homeLocation = mapNodes[homeIndex].getLocation().clone();
    } else {
      try {
        allHomes = new LinkedList<Coord>();
        List<Coord> locationsRead = (new WKTReader()).readPoints(new File(homeLocationsFile));
        for (Coord coord : locationsRead) {
          SimMap map = getMap();
          Coord offset = map.getOffset();
          // mirror points if map data is mirrored
          if (map.isMirrored()) {
            coord.setLocation(coord.getX(), -coord.getY());
          }
          coord.translate(offset.getX(), offset.getY());
          allHomes.add(coord);
        }
        homeLocation = allHomes.get(rng.nextInt(allHomes.size())).clone();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    if (timeDiffSTD == -1) {
      timeDifference = rng.nextInt(DAY_LENGTH) - DAY_LENGTH / 2;
    } else if (timeDiffSTD == 0) {
      timeDifference = 0;
    } else {
      timeDifference =
          (int)
              Math.min(
                  Math.max((rng.nextGaussian() * timeDiffSTD), -DAY_LENGTH / 2), DAY_LENGTH / 2);
    }
  }
Ejemplo n.º 3
0
  /**
   * Fills a String formatted in a special way with values from Settings. String can contain (fully
   * qualified) setting names surrounded by delimiters (see {@link #FILL_DELIMITER}). Values for
   * those settings are retrieved and filled in the place of place holders.
   *
   * @param input The input string that may contain value requests
   * @return A string filled with requested values (or the original string if no requests were
   *     found)
   * @throws SettingsError if such settings were not found
   */
  public String valueFillString(String input) {
    if (!input.contains(FILL_DELIMITER)) {
      return input; // nothing to fill
    }

    Settings s = new Settings(); // don't use any namespace
    String result = "";
    Scanner scan = new Scanner(input);
    scan.useDelimiter(FILL_DELIMITER);

    if (input.startsWith(FILL_DELIMITER)) {
      result += s.getSetting(scan.next());
    }

    while (scan.hasNext()) {
      result += scan.next();
      if (!scan.hasNext()) {
        break;
      }
      result += s.getSetting(scan.next());
    }

    return result;
  }
Ejemplo n.º 4
0
  /**
   * Checks map cache if the requested map file(s) match to the cached sim map
   *
   * @param settings The Settings where map file names are found
   * @return A cached map or null if the cached map didn't match
   */
  private SimMap checkCache(Settings settings) {
    int nrofMapFiles = settings.getInt(NROF_FILES_S);

    if (nrofMapFiles != cachedMapFiles.size() || cachedMap == null) {
      return null; // wrong number of files
    }

    for (int i = 1; i <= nrofMapFiles; i++) {
      String pathFile = settings.getSetting(FILE_S + i);
      if (!pathFile.equals(cachedMapFiles.get(i - 1))) {
        return null; // found wrong file name
      }
    }

    // all files matched -> return cached map
    return cachedMap;
  }
Ejemplo n.º 5
0
  /**
   * Reads a sim map from location set to the settings, mirrors the map and moves its upper left
   * corner to origo.
   *
   * @return A new SimMap based on the settings
   */
  private SimMap readMap() {
    SimMap simMap;
    Settings settings = new Settings(MAP_BASE_MOVEMENT_NS);
    WKTMapReader r = new WKTMapReader(true);

    if (cachedMap == null) {
      cachedMapFiles = new ArrayList<String>(); // no cache present
    } else { // something in cache
      // check out if previously asked map was asked again
      SimMap cached = checkCache(settings);
      if (cached != null) {
        nrofMapFilesRead = cachedMapFiles.size();
        return cached; // we had right map cached -> return it
      } else { // no hit -> reset cache
        cachedMapFiles = new ArrayList<String>();
        cachedMap = null;
      }
    }

    try {
      int nrofMapFiles = settings.getInt(NROF_FILES_S);

      for (int i = 1; i <= nrofMapFiles; i++) {
        String pathFile = settings.getSetting(FILE_S + i);
        cachedMapFiles.add(pathFile);
        r.addPaths(new File(pathFile), i);
      }

      nrofMapFilesRead = nrofMapFiles;
    } catch (IOException e) {
      throw new SimError(e.toString(), e);
    }

    simMap = r.getMap();
    checkMapConnectedness(simMap.getNodes());
    // mirrors the map (y' = -y) and moves its upper left corner to origo
    simMap.mirror();
    Coord offset = simMap.getMinBound().clone();
    simMap.translate(-offset.getX(), -offset.getY());
    checkCoordValidity(simMap.getNodes());

    cachedMap = simMap;
    return simMap;
  }
Ejemplo n.º 6
0
  /** 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);
      }
    }
  }
  /** 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);
      }
    }
  }