Example #1
4
  protected String getRallyXML(String apiUrl) throws Exception {
    String responseXML = "";

    DefaultHttpClient httpClient = new DefaultHttpClient();

    Base64 base64 = new Base64();
    String encodeString =
        new String(base64.encode((rallyApiHttpUsername + ":" + rallyApiHttpPassword).getBytes()));

    HttpGet httpGet = new HttpGet(apiUrl);
    httpGet.addHeader("Authorization", "Basic " + encodeString);
    HttpResponse response = httpClient.execute(httpGet);
    HttpEntity entity = response.getEntity();

    if (entity != null) {

      InputStreamReader reader = new InputStreamReader(entity.getContent());
      BufferedReader br = new BufferedReader(reader);

      StringBuilder sb = new StringBuilder();
      String line = "";
      while ((line = br.readLine()) != null) {
        sb.append(line);
      }

      responseXML = sb.toString();
    }

    log.debug("responseXML=" + responseXML);

    return responseXML;
  }
Example #2
0
  // function to do the join use case
  public static void share() throws Exception {
    HttpPost method = new HttpPost(url + "/share");
    String ipAddress = null;

    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
      NetworkInterface ni = (NetworkInterface) en.nextElement();
      if (ni.getName().equals("eth0")) {
        Enumeration<InetAddress> en2 = ni.getInetAddresses();
        while (en2.hasMoreElements()) {
          InetAddress ip = (InetAddress) en2.nextElement();
          if (ip instanceof Inet4Address) {
            ipAddress = ip.getHostAddress();
            break;
          }
        }
        break;
      }
    }

    method.setEntity(new StringEntity(username + ';' + ipAddress, "UTF-8"));
    try {
      ResponseHandler<String> responseHandler = new BasicResponseHandler();
      connIp = client.execute(method, responseHandler);
    } catch (IOException e) {
      System.err.println("Fatal transport error: " + e.getMessage());
      e.printStackTrace();
    }

    // get present time
    date = new Date();
    long start = date.getTime();

    // Execute the vishwa share process
    Process p = Runtime.getRuntime().exec("java -jar vishwa/JVishwa.jar " + connIp);

    String ch = "alive";
    System.out.println("Type kill to unjoin from the grid");

    while (!ch.equals("kill")) {
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      ch = in.readLine();
    }

    p.destroy();

    date = new Date();
    long end = date.getTime();
    long durationInt = end - start;

    String duration = String.valueOf(durationInt);
    method = new HttpPost(url + "/shareAck");
    method.setEntity(new StringEntity(username + ";" + duration, "UTF-8"));
    try {
      client.execute(method);
    } catch (IOException e) {
      System.err.println("Fatal transport error: " + e.getMessage());
      e.printStackTrace();
    }
  }
 /**
  * this is the primary method that sends a query to the CIPRes REST service. It expects to receive
  * an XML file, which it returns in Document if the root tag matc hes xmlRootTag
  */
 public Document cipresQuery(HttpClient httpclient, String URL, String xmlRootTag) {
   if (StringUtil.blank(URL)) return null;
   try {
     HttpGet httpget = new HttpGet(URL);
     httpget.addHeader("cipres-appkey", CIPRESkey);
     try {
       HttpResponse response = httpclient.execute(httpget);
       HttpEntity responseEntity = response.getEntity();
       InputStream instream = responseEntity.getContent();
       BufferedReader br = new BufferedReader(new InputStreamReader(instream));
       String line = "";
       StringBuffer sb = new StringBuffer();
       while ((line = br.readLine()) != null) {
         sb.append(line + StringUtil.lineEnding());
       }
       Document cipresResponseDoc = loadXMLFile(xmlRootTag, sb.toString());
       if (cipresResponseDoc != null && verbose) {
         ownerModule.logln(sb.toString());
       }
       if (cipresResponseDoc == null) {
         Document errorDoc = loadXMLFile(sb.toString());
         if (errorDoc != null) reportError(errorDoc, "Error in communicating with CIPRes", true);
       }
       EntityUtils.consume(response.getEntity());
       return cipresResponseDoc;
     } catch (IOException e) {
       Debugg.printStackTrace(e);
     } catch (Exception e) {
       Debugg.printStackTrace(e);
     }
   } catch (Exception e) {
     Debugg.printStackTrace(e);
   }
   return null;
 }
Example #4
0
  // function to do the compute use case
  public static void compute() throws Exception {
    HttpPost method = new HttpPost(url + "/compute");

    method.setEntity(new StringEntity(username, "UTF-8"));

    try {
      ResponseHandler<String> responseHandler = new BasicResponseHandler();
      connIp = client.execute(method, responseHandler);
    } catch (IOException e) {
      System.err.println("Fatal transport error: " + e.getMessage());
      e.printStackTrace();
    }

    System.out.println("Give the file name which has to be put in the grid for computation");

    // input of the file name to be computed
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String name = in.readLine();

    // get the absolute path of the current working directory
    File directory = new File(".");
    String pwd = directory.getAbsolutePath();

    // get present time
    date = new Date();
    long start = date.getTime();

    String cmd = "java -classpath " + pwd + "/vishwa/JVishwa.jar:. " + name + " " + connIp;
    System.out.println(cmd);

    // Execute the vishwa compute process
    Process p = Runtime.getRuntime().exec(cmd);

    // wait till the compute process is completed
    // check for the status code (0 for successful termination)
    int status = p.waitFor();

    if (status == 0) {
      System.out.println("Compute operation successful. Check the directory for results");
    }

    date = new Date();
    long end = date.getTime();
    long durationInt = end - start;

    String duration = String.valueOf(durationInt);
    method = new HttpPost(url + "/computeAck");
    method.setEntity(new StringEntity(username + ";" + duration, "UTF-8"));
    try {
      client.execute(method);
    } catch (IOException e) {
      System.err.println("Fatal transport error: " + e.getMessage());
      e.printStackTrace();
    }
  }
Example #5
0
  public static void main(String args[]) throws Exception {
    int choice;

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Vishwa Based Campus Compute Cloud");

    while (true) {
      System.out.println("");

      if (username != null) {
        System.out.println("1. Logout");
      } else {
        System.out.println("1. Login");
      }

      System.out.println("2. Join the Grid");
      System.out.println("3. Avail the compute facility");

      System.out.println("4. Quit");
      System.out.printf("Enter your choice: ");

      try {
        choice = Integer.parseInt(in.readLine());
      } catch (NumberFormatException ex) {
        System.out.println("WARNING: Enter a number only!");
        continue;
      }

      if (choice == 1) {
        if (username != null) {
          username = null;
        } else {
          System.out.printf("Enter your username: "******"Enter your password: "******"Successfully logged in!");
          } else {
            System.out.println("Unable to login. Please check your credentials.");
            username = null;
          }
        }
      } else if (choice == 2) {
        share();
      } else if (choice == 3) {
        compute();
      } else {
        System.exit(0);
      }
    }
  }
Example #6
0
  public String getEntityString(HttpEntity entity) throws Exception {
    StringBuilder sb = new StringBuilder();

    if (entity != null) {
      InputStreamReader reader = new InputStreamReader(entity.getContent());
      BufferedReader br = new BufferedReader(reader);

      String line = "";
      while ((line = br.readLine()) != null) {
        sb.append(line);
      }
    }

    return sb.toString();
  }
Example #7
0
  public String loadStream(InputStream is) {
    StringBuilder sb = new StringBuilder();

    try {
      InputStreamReader reader = new InputStreamReader(is);
      BufferedReader br = new BufferedReader(reader);

      String line = "";
      while ((line = br.readLine()) != null) {
        sb.append(line);
      }
    } catch (Exception e) {
      log.error("", e);
    }

    return sb.toString();
  }
Example #8
0
 private CIJob getJob(ApplicationInfo appInfo) throws PhrescoException {
   Gson gson = new Gson();
   try {
     BufferedReader br = new BufferedReader(new FileReader(getCIJobPath(appInfo)));
     CIJob job = gson.fromJson(br, CIJob.class);
     br.close();
     return job;
   } catch (FileNotFoundException e) {
     S_LOGGER.debug(e.getLocalizedMessage());
     return null;
   } catch (com.google.gson.JsonParseException e) {
     S_LOGGER.debug("it is already adpted project !!!!! " + e.getLocalizedMessage());
     return null;
   } catch (IOException e) {
     S_LOGGER.debug(e.getLocalizedMessage());
     return null;
   }
 }
Example #9
0
 public List<CIJob> getJobs(ApplicationInfo appInfo) throws PhrescoException {
   S_LOGGER.debug("GetJobs Called!");
   try {
     boolean adaptedProject = adaptExistingJobs(appInfo);
     S_LOGGER.debug("Project adapted for new feature => " + adaptedProject);
     Gson gson = new Gson();
     BufferedReader br = new BufferedReader(new FileReader(getCIJobPath(appInfo)));
     Type type = new TypeToken<List<CIJob>>() {}.getType();
     List<CIJob> jobs = gson.fromJson(br, type);
     br.close();
     return jobs;
   } catch (FileNotFoundException e) {
     S_LOGGER.debug("FileNotFoundException");
     return null;
   } catch (IOException e) {
     S_LOGGER.debug("IOException");
     throw new PhrescoException(e);
   }
 }
  /** The core method that initiates a job on CIPRes. */
  public boolean postJob(
      HttpClient httpclient, MultipartEntityBuilder builder, MesquiteString jobURL) {
    if (builder == null) return false;
    String URL = baseURL + "/job/" + username;
    HttpPost httppost = new HttpPost(URL);
    httppost.addHeader("cipres-appkey", CIPRESkey);

    // some of this from
    // http://stackoverflow.com/questions/18964288/upload-a-file-through-an-http-form-via-multipartentitybuilder-with-a-progress
    HttpEntity cipresEntity = builder.build();

    httppost.setEntity(cipresEntity);

    try {
      HttpResponse response = httpclient.execute(httppost);

      HttpEntity responseEntity = response.getEntity();
      InputStream instream = responseEntity.getContent();
      BufferedReader br = new BufferedReader(new InputStreamReader(instream));
      StringBuffer sb = new StringBuffer();
      String line = "";
      while ((line = br.readLine()) != null) {
        sb.append(line + StringUtil.lineEnding());
      }

      Document cipresResponseDoc = loadXMLFile("jobstatus", sb.toString()); // let's see how it went
      boolean success = false;
      if (cipresResponseDoc != null) {
        processJobSubmissionResponse(cipresResponseDoc, jobURL);
        if (verbose) ownerModule.logln(sb.toString());
        if (jobURL != null) success = StringUtil.notEmpty(jobURL.getValue());
        else success = true;
      } else {
        cipresResponseDoc = loadXMLFile(sb.toString());
        reportError(cipresResponseDoc, "Error with CIPRes run", false);
      }
      EntityUtils.consume(response.getEntity());
      return success;
    } catch (IOException e) {
      Debugg.printStackTrace(e);
    }
    return false;
  }