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

📄 gui.java

📁 伟大的Contiki工程, 短小精悍 的操作系统, 学习编程不可不看
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
      moteTypeID = "mtype1";    }    File contikiBaseDir = new File(getExternalToolsSetting("PATH_CONTIKI"));    File contikiCoreDir = new File(contikiBaseDir,        getExternalToolsSetting("PATH_COOJA_CORE_RELATIVE"));    File libFile = new File(ContikiMoteType.tempOutputDirectory, moteTypeID        + ContikiMoteType.librarySuffix);    File mapFile = new File(ContikiMoteType.tempOutputDirectory, moteTypeID        + ContikiMoteType.mapSuffix);    File depFile = new File(ContikiMoteType.tempOutputDirectory, moteTypeID        + ContikiMoteType.dependSuffix);    if (libFile.exists()) {      libFile.delete();    }    if (depFile.exists()) {      depFile.delete();    }    if (mapFile.exists()) {      mapFile.delete();    }    if (libFile.exists()) {      logger.fatal(">> Can't delete output file, aborting: " + libFile);      return false;    }    if (depFile.exists()) {      logger.fatal(">> Can't delete output file, aborting: " + depFile);      return false;    }    if (mapFile.exists()) {      logger.fatal(">> Can't delete output file, aborting: " + mapFile);      return false;    }    // Search for main file in current directory (or arg)    File mainProcessFile = new File(filename);    logger.info(">> Searching main process file: "        + mainProcessFile.getAbsolutePath());    if (!mainProcessFile.exists()) {      logger.info(">> Searching main process file: "          + mainProcessFile.getAbsolutePath());      boolean foundFile = false;      for (String projectDir : projectDirs) {        mainProcessFile = new File(projectDir, filename);        logger.info(">> Searching main process file: "            + mainProcessFile.getAbsolutePath());        if (mainProcessFile.exists()) {          foundFile = true;          break;        }      }      if (!foundFile) {        logger.fatal(">> Could not locate main process file, aborting");        return false;      }    }    // Setup compilation arguments    logger.info("> Setting up compilation arguments");    Vector<File> filesToCompile = new Vector<File>();    filesToCompile.add(mainProcessFile); // main process file    for (String projectDir : projectDirs) {      // project directories      filesToCompile.add(new File(projectDir));    }    String[] projectSources = // project config sources    gui.getProjectConfig().getStringArrayValue(ContikiMoteType.class,        "C_SOURCES");    for (String projectSource : projectSources) {      if (!projectSource.equals("")) {        File file = new File(projectSource);        if (file.getParent() != null) {          // Find which project directory added this file          File projectDir = gui.getProjectConfig().getUserProjectDefining(              ContikiMoteType.class, "C_SOURCES", projectSource);          if (projectDir != null) {            // We found a project directory - Add it            filesToCompile.add(new File(projectDir.getPath(), file                .getParent()));          }        }        filesToCompile.add(new File(file.getName()));      }    }    // Scan for sensors    if (sensors == null) {      logger.info("> Scanning for sensors");      sensors = new Vector<String>();      Vector<String[]> scannedSensorInfo = ContikiMoteTypeDialog          .scanForSensors(contikiCoreDir);      for (String projectDir : projectDirs) {        // project directories        scannedSensorInfo.addAll(ContikiMoteTypeDialog.scanForSensors(new File(            projectDir)));      }      for (String[] sensorInfo : scannedSensorInfo) {        // logger.info(">> Found and added: " + sensorInfo[1] + " (" +        // sensorInfo[0] + ")");        sensors.add(sensorInfo[1]);      }    }    // Scan for core interfaces    if (coreInterfaces == null) {      logger.info("> Scanning for core interfaces");      coreInterfaces = new Vector<String>();      Vector<String[]> scannedCoreInterfaceInfo = ContikiMoteTypeDialog          .scanForInterfaces(contikiCoreDir);      for (String projectDir : projectDirs) {        // project directories        scannedCoreInterfaceInfo.addAll(ContikiMoteTypeDialog            .scanForInterfaces(new File(projectDir)));      }      for (String[] coreInterfaceInfo : scannedCoreInterfaceInfo) {        // logger.info(">> Found and added: " + coreInterfaceInfo[1] + " (" +        // coreInterfaceInfo[0] + ")");        coreInterfaces.add(coreInterfaceInfo[1]);      }    }    // Scan for mote interfaces    logger.info("> Loading mote interfaces");    String[] moteInterfaces = gui.getProjectConfig().getStringArrayValue(        ContikiMoteType.class, "MOTE_INTERFACES");    Vector<Class<? extends MoteInterface>> moteIntfClasses = new Vector<Class<? extends MoteInterface>>();    for (String moteInterface : moteInterfaces) {      try {        Class<? extends MoteInterface> newMoteInterfaceClass = gui            .tryLoadClass(gui, MoteInterface.class, moteInterface);        moteIntfClasses.add(newMoteInterfaceClass);        // logger.info(">> Loaded mote interface: " + newMoteInterfaceClass);      } catch (Exception e) {        logger.fatal(">> Failed to load mote interface, aborting: "            + moteInterface + ", " + e.getMessage());        return false;      }    }    // Scan for processes    if (userProcesses == null) {      logger.info("> Scanning for user processes");      userProcesses = new Vector<String>();      Vector<String> autostartProcesses = new Vector<String>();      Vector<String[]> scannedProcessInfo = ContikiMoteTypeDialog          .scanForProcesses(contikiCoreDir);      for (String projectDir : projectDirs) {        // project directories        scannedProcessInfo.addAll(ContikiMoteTypeDialog            .scanForProcesses(new File(projectDir)));      }      for (String[] processInfo : scannedProcessInfo) {        if (processInfo[0].equals(mainProcessFile.getName())) {          logger.info(">> Found and added: " + processInfo[1] + " ("              + processInfo[0] + ")");          userProcesses.add(processInfo[1]);          if (addAutostartProcesses) {            // Parse any autostart processes            try {              // logger.info(">>> Parsing " + processInfo[0] + " for autostart              // processes");              Vector<String> autostarters = ContikiMoteTypeDialog                  .parseAutostartProcesses(mainProcessFile);              if (autostarters != null) {                autostartProcesses.addAll(autostarters);              }            } catch (Exception e) {              logger                  .fatal(">>> Error when parsing autostart processes, aborting: "                      + e);              return false;            }          }        } else {          // logger.info(">> Found and ignored: " + processInfo[1] + " (" +          // processInfo[0] + ")");        }      }      if (addAutostartProcesses) {        // Add autostart process sources if found        logger.info("> Adding autostart processes");        for (String autostartProcess : autostartProcesses) {          boolean alreadyExists = false;          for (String existingProcess : userProcesses) {            if (existingProcess.equals(autostartProcess)) {              alreadyExists = true;              break;            }          }          if (!alreadyExists) {            userProcesses.add(autostartProcess);            logger.info(">> Added autostart process: " + autostartProcess);          }        }      }    }    // Generate Contiki main source file    logger.info("> Generating Contiki main source file");    if (!ContikiMoteType.tempOutputDirectory.exists()) {      ContikiMoteType.tempOutputDirectory.mkdir();    }    if (!ContikiMoteType.tempOutputDirectory.exists()) {      logger.fatal(">> Could not create output directory: "          + ContikiMoteType.tempOutputDirectory);      return false;    }    try {      String generatedFilename = ContikiMoteTypeDialog.generateSourceFile(          moteTypeID, sensors, coreInterfaces, userProcesses);      // logger.info(">> Generated source file: " + generatedFilename);    } catch (Exception e) {      logger.fatal(">> Error during file generation, aborting: "          + e.getMessage());      return false;    }    // Compile library    logger.info("> Compiling library (Rime comm stack)");    // TODO Warning, assuming Rime communication stack    boolean compilationSucceded = ContikiMoteTypeDialog.compileLibrary(        moteTypeID, contikiBaseDir, filesToCompile, false,        ContikiMoteType.CommunicationStack.RIME,        null, System.err);    if (!libFile.exists() || !depFile.exists() || !mapFile.exists()) {      compilationSucceded = false;    }    if (compilationSucceded) {      // logger.info(">> Compilation complete");    } else {      logger.fatal(">> Error during compilation, aborting");      return false;    }    // Create mote type    logger.info("> Creating mote type");    ContikiMoteType moteType;    try {      moteType = new ContikiMoteType(moteTypeID);    } catch (MoteTypeCreationException e) {      logger.fatal("Exception when creating mote type: " + e);      return false;    }    moteType.setDescription("Mote type: " + filename);    moteType.setContikiBaseDir(contikiBaseDir.getPath());    moteType.setContikiCoreDir(contikiCoreDir.getPath());    moteType.setProjectDirs(new Vector<File>());    moteType.setCompilationFiles(filesToCompile);    moteType.setConfig(gui.getProjectConfig());    moteType.setProcesses(userProcesses);    moteType.setSensors(sensors);    moteType.setCoreInterfaces(coreInterfaces);    moteType.setMoteInterfaces(moteIntfClasses);    // Create simulation    logger.info("> Creating simulation");    Simulation simulation = new Simulation(gui);    simulation.setTitle("Quickstarted: " + filename);    simulation.setDelayTime(delayTime);    simulation.setSimulationTime(0);    simulation.setTickTime(1);    String radioMediumClassName = null;    try {      radioMediumClassName = gui.getProjectConfig().getStringArrayValue(          GUI.class, "RADIOMEDIUMS")[0];      Class<? extends RadioMedium> radioMediumClass = gui.tryLoadClass(gui,          RadioMedium.class, radioMediumClassName);      RadioMedium radioMedium = RadioMedium.generateRadioMedium(          radioMediumClass, simulation);      simulation.setRadioMedium(radioMedium);    } catch (Exception e) {      logger.fatal(">> Failed to load radio medium, aborting: "          + radioMediumClassName + ", " + e);      return false;    }    // Create nodes    logger.info("> Creating motes");    Vector<ContikiMote> motes = new Vector<ContikiMote>();    Random random = new Random();    int nextMoteID = 1;    int nextIP = 0;    for (int i = 0; i < numberOfNodes; i++) {      ContikiMote mote = (ContikiMote) moteType.generateMote(simulation);      // Set random position      if (mote.getInterfaces().getPosition() != null) {        mote.getInterfaces().getPosition().setCoordinates(            random.nextDouble() * areaSideLength,            random.nextDouble() * areaSideLength, 0);      }      // Set unique mote ID's      if (mote.getInterfaces().getMoteID() != null) {        mote.getInterfaces().getMoteID().setMoteID(nextMoteID++);      }      // Set unique IP address      if (mote.getInterfaces().getIPAddress() != null) {        mote.getInterfaces().getIPAddress().setIPNumber((char) 10,            (char) ((nextIP / (254 * 255)) % 255),            (char) ((nextIP / 254) % 255), (char) (nextIP % 254 + 1));        nextIP++;      }      motes.add(mote);    }    // Add mote type and motes to simulation    logger.info("> Adding motes and mote type to simulation");    simulation.addMoteType(moteType);    for (Mote mote : motes) {      simulation.addMote(mote);    }    // Add simulation to GUI    logger.info("> Adding simulation to GUI");    gui.setSimulation(simulation);    // Start plugins and try to place them wisely    logger.info("> Starting plugin and showing GUI");    VisPlugin plugin = (VisPlugin) gui.startPlugin(VisState.class, gui, simulation, null);    plugin.setLocation(350, 20);    plugin = (VisPlugin) gui.startPlugin(VisTraffic.class, gui, simulation, null);    plugin.setLocation(350, 340);    plugin = (VisPlugin) gui.startPlugin(LogListener.class, gui, simulation, null);    plugin.setLocation(20, 420);    frame.setJMenuBar(gui.createMenuBar());    // Finally show GUI    frame.setVisible(true);    if (simulationStarting) {      simulation.startSimulation();    }    return true;  }  //// PROJECT CONFIG AND EXTENDABLE PARTS METHODS ////  /**   * Register new mote type class.   *   * @param moteTypeClass   *          Class to register   */  public void registerMoteType(Class<? extends MoteType> moteTypeClass) {    moteTypeClasses.add(moteTypeClass);  }  /**   * Unregister all mote type classes.   */  public void unregisterMoteTypes() {    moteTypeClasses.clear();  }  /**   * @return All registered mote type classes   */  public Vector<Class<? extends MoteType>> getRegisteredMoteTypes() {    return moteTypeClasses;  }  /**   * Register new IP distributor class   *   * @param ipDistributorClass   *          Class to register   * @return True if class was registered   */  public boolean registerIPDistributor(      Class<? extends IPDistributor> ipDistributorClass) {    // Check that vector constructor exists    try {      ipDistributorClass.getConstructor(new Class[] { Vector.class });    } catch (Exception e) {      logger.fatal("No vector constructor found of IP distributor: "          + ipDistributorClass);      return false;    }    ipDistributorClasses.add(ipDistributorClass);    return true;  }  /**   * Unregister all IP distributors.   */  public void unregisterIPDistributors() {    ipDistributorClasses.clear();

⌨️ 快捷键说明

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