Exemplo n.º 1
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);
      }
    }
  }