Beispiel #1
0
 public static Download createFrom(ObjectInputStream in, DownloadManager m) throws IOException {
   try {
     Hash h = new Hash();
     int r = in.read(h.array());
     String fn = in.readUTF();
     ArrayList<Integer> guids = (ArrayList<Integer>) in.readObject();
     if (T.t) {
       T.ass(r == h.array().length, "Incorrect length when deserializing download");
     }
     BlockStorage bs = BlockStorage.getById(m.getCore(), in.readInt());
     Download d = new Download(m, h, bs, fn, guids);
     d.fd = d.manager.getCore().getFileManager().getFd(h);
     return d;
   } catch (ClassNotFoundException e) {
     throw new IOException("Could not find class while deserializing: " + e);
   }
 }
Beispiel #2
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;
    }
  }