示例#1
0
  public void run() {
    // Create a file channel for the file
    try {
      _parent.addToActive(this);
      file = new RandomAccessFile(Globals.ourHome + outFile, "rwd");
      channel = file.getChannel();

      // Create a segment downloader for each segment and run them simultaneously
      for (int i = 0; i < nSeg; i++) {
        segDownloads.add(new SegDownloader(this, peers.get(i % peers.size()) + "/" + path, i));
        (new Thread(segDownloads.get(segDownloads.size() - 1))).start();
      }

      // Wait for a download thread to either finish or fail, and update our bookkeeping
      try {
        while (!done) {
          // Get a new segment downloader off the stopped list.
          SegDownloader s = bstopped.take();

          // Give a new path to every download thread that failed.
          if (s.status() == Dstatus.FAILED) {
            synchronized (s) {
              s.dlPath = getNewPath();
              removeStopped(s);
              s.notify();
            }
          }

          // Check if that segment finishes off the download.
          else if (s.status() == Dstatus.FINISHED) {
            if (nSeg == doneSegs) {
              done = true;
              percentDone = 100;
              System.out.println("Download " + Globals.ourHome + outFile + " Finished!");

              // "Notify" the waiters that we're done.
              while (waiters > 0 && waitToken.take()) {
                waiters--;
              }
            }
          } else throw new RuntimeException("Impossible");
        }
      } catch (InterruptedException e) {
        status = Dstatus.FAILED;
      }

    } catch (IOException e) {
      Log.info("Could not open file for download!");
      status = Dstatus.FAILED;
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      status = Dstatus.FAILED;
    }
  }
示例#2
0
 public void removeActive(SegDownloader s) {
   try {
     if (doneSegs < nSeg) active.take();
   } catch (InterruptedException e) {
     // Nothing
   }
 }