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

📄 gridkit.java

📁 一个开源的用java开发的遗传算法的封装好的工程
💻 JAVA
字号:
package org.jgap.distr.grid.util;

import org.jgap.util.*;
import java.io.*;
import java.net.*;
import org.jgap.distr.grid.request.*;

public class GridKit {
  public static String ensureDirectory(String a_currentDir, String a_subDir,
                                       String a_descr)
      throws IOException {
    if (a_currentDir == null || a_currentDir.length() < 1) {
      String currentDir = FileKit.getCurrentDir();
      // Set workdir, create it if it does not exist.
      // --------------------------------------------
      String workDir = FileKit.addSubDir(currentDir, a_subDir, true);
      File f = new File(workDir);
      if (!f.exists()) {
        if (!f.mkdirs()) {
          throw new RuntimeException("Creation of "
                                     + a_descr + " "
                                     + workDir
                                     + " failed!");
        }
      }
      return workDir;
    }
    else {
      return null;
    }
  }

  public static URLConnection getConnection(String a_url)
      throws Exception {
    URL url1 = new URL(a_url);
    URL url = new URL(url1.toExternalForm());
    URLConnection con = url.openConnection();
    con.setUseCaches(false);
    con.setDoInput(true);
    con.setDoOutput(true);
    return con;
  }

  public static VersionInfo isUpdateAvailable(String BASE_URL,
      String a_moduleName,
      String currentVersion)
      throws Exception {
    VersionInfo result;
    String url = BASE_URL + "getVersion=" + a_moduleName;
    addURLParameter(url, "version", currentVersion);
    URLConnection con = GridKit.getConnection(url);
    ObjectInputStream ois = new ObjectInputStream(con.getInputStream());
    result = (VersionInfo) ois.readObject();
    return result;
  }

  public static String addURLParameter(String a_requestURL, String a_key,
                                       long a_value) {
    String result = a_requestURL + "&" + a_key + "=" + a_value;
    return result;
  }

  public static String addURLParameter(String a_requestURL, String a_key,
                                       String a_value) {
    String result = a_requestURL + "&" + a_key + "=" + a_value;
    return result;
  }

  public static String retrieveModule(String BASE_URL,
                                      VersionInfo a_versionInfo,
                                      String a_destDir)
      throws Exception {
    String filename = a_versionInfo.filenameOfLib;
    if (!getFile(BASE_URL, filename, a_destDir)) {
      return null;
    }
    return filename;
  }

  public static void updateModule(String a_filename, String a_workDir,
                                  String a_libDir)
      throws Exception {
    String libDir = a_libDir;
    // Copy module to new location.
    // ----------------------------
    String sourceFileName = a_workDir + a_filename;
    FileKit.copyFile(sourceFileName, libDir);
    // Delete file in workdir.
    // -----------------------
    if (!FileKit.deleteFile(sourceFileName)) {
      /**@todo write log: delete manually*/
    }
  }

  public static boolean getFile(String BASE_URL, String a_sourceFilename,
                                String a_targetDir)
      throws Exception {
    String filename = FileKit.getFilename(a_sourceFilename);
    String destFilename = a_targetDir + filename;
    long offset;
    // If file already exists, determine start offset
    File f = new File(destFilename);
    if (f.exists()) {
      offset = f.length();
    }
    else {
      offset = 0;
    }
    String a_url = BASE_URL + "download=";
    String requestURL = a_url + a_sourceFilename;
    // Include start offset into request
    requestURL = GridKit.addURLParameter(requestURL, "offset", offset);
    URL url1 = new URL(requestURL);
    URL url = new URL(url1.toExternalForm());
    // Open connection
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    // Configure connection
    con.setUseCaches(false);
//    con.setDoInput(true);
    con.setDoOutput(true);
    // Receive result
    boolean append;
    if (offset == 0) {
      append = false;
    }
    else {
      append = true;
    }
    InputStream in = con.getInputStream();
    ObjectInputStream sis = new ObjectInputStream(new BufferedInputStream(in,
        1024 * 5));
    Status s;
//    BufferedInputStream ois = new BufferedInputStream(sis, 1024 * 5);
    FileOutputStream fos = new FileOutputStream(destFilename, append);
    long currentOffset = 0;
    try {
      int loopIndex = 0;
      do {
        s = (Status) sis.readObject();
        if (s.code == 0) {
          if (loopIndex == 0) {
            if (s.buffer == null || s.buffer.length < 1) {
              System.out.println("File already exists");
              return true;
            }
          }
        }
        else if (s.code < 0) {
          throw new IOException(s.description);
        }
        loopIndex++;
        fos.write(s.buffer);
        currentOffset += s.buffer.length;
        if (s.code == 0) {
          break;
        }
//        Thread.sleep(50);
      } while (true);
    } catch (SocketException sex) {
      System.err.println("Connection to server lost"
                         + " - file transfer interrupted (resum possible)");
      sex.printStackTrace();
      // Close file being able to resume it later
      fos.close();
      return false;
    }
    fos.close();
    System.out.println("File received: " + destFilename);
    return true;
  }

  public static void updateModuleLibrary(String BASE_URL, String a_moduleName,
      String a_libDir,
      String a_workDir)
      throws Exception {
    String currentVersion;
    String JGAPVersionNeeded;
    String filename;
    boolean isCoreModule;
    if (a_moduleName.equals("evolutionDistributed")) {
      /**@todo zuordnung irgendwo abspeichern*/
      filename = "evdistr.jar";
      isCoreModule = false;
    }
    else if (a_moduleName.equals("jgap")) {
      /**@todo zuordnung irgendwo abspeichern*/
      filename = "jgap.jar";
      isCoreModule = true;
    }
    else {
      throw new IllegalArgumentException("Unknown module " + a_moduleName);
    }
    filename = a_libDir + filename;
    // Determine if file available locally
    if (!FileKit.existsFile(filename)) {
      currentVersion = "none";
      System.out.println(" File not found: " + filename);
    }
    else {
      // If file exists: determine version of module
      if (isCoreModule) {
        currentVersion = FileKit.getVersionOfJGAP(filename);
      }
      else {
        currentVersion = FileKit.getVersionOfModule(filename);
        // Also determine minimal version of JGAP library that is
        // required for the module to work.
        // -------------------------------------------------------
        JGAPVersionNeeded = FileKit.getVersionOfJGAP(filename);
      }
    }
    // ask server if update required
    VersionInfo versionInfo = GridKit.isUpdateAvailable(BASE_URL, a_moduleName,
        currentVersion);
    if (versionInfo.currentVersion == null) {
      System.out.println("Module " + a_moduleName + " is unknown");
      return;
    }
    if (!currentVersion.equals(versionInfo.currentVersion)) {
      System.out.println("Newer module " + a_moduleName + " with version "
                         + versionInfo.currentVersion
                         + " available");
      String workDir = a_workDir;
      // Download the newest module version
      filename = GridKit.retrieveModule(BASE_URL, versionInfo, workDir);
      if (filename != null) {
        // Do the update
        GridKit.updateModule(filename, workDir, a_libDir);
      }
    }
    else {
      System.out.println("Module " + a_moduleName + " with version " +
                         currentVersion + " is up-to-date");
    }
  }

  public static void updateJGAPLibrary(String BASE_URL, String a_libDir,
                                       String a_workDir)
      throws Exception {
    updateModuleLibrary(BASE_URL, "jgap", a_libDir, a_workDir);
  }
}

⌨️ 快捷键说明

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