Changeset 1299


Ignore:
Timestamp:
Feb 25, 2011, 2:26:46 PM (11 years ago)
Author:
Nicklas Nordborg
Message:

Implemented progress reporting during the download phase. When the download is complete the files are uploaded to the BASE file system.

Location:
extensions/net.sf.basedb.torrent/trunk/src/main/net/sf/basedb/clients/torrent/service
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.torrent/trunk/src/main/net/sf/basedb/clients/torrent/service/TorrentManager.java

    r1255 r1299  
    3232import java.io.OutputStream;
    3333import java.util.Arrays;
     34import java.util.List;
    3435
    3536import org.slf4j.Logger;
     
    3738
    3839import net.sf.basedb.core.DbControl;
     40import net.sf.basedb.core.Directory;
    3941import net.sf.basedb.core.Job;
     42import net.sf.basedb.util.ChainedProgressReporter;
    4043import net.sf.basedb.util.FileUtil;
     44import net.sf.basedb.util.Values;
    4145
    4246/**
     
    260264 
    261265  /**
     266    Report download progress to the BASE job.
     267    The download is considered to use between 0-90% of
     268    the total job time. If the torrent isn't currently
     269    downloading, nothing is done.
     270  */
     271  public void reportDownloadProgress(DbControl dc)
     272  {
     273    TorrentState state = getState();
     274    if (state == TorrentState.DOWNLOADING || state == TorrentState.DOWNLOAD_COMPLETE)
     275    {
     276      Torrent t = getTorrent();
     277      // 0-90% for downloading
     278      long size = t.getFileLength();
     279      long remain = t.getRemainingBytes();
     280      if (log.isDebugEnabled())
     281      {
     282        log.debug("torrent=" + getName() + "; state=" + state + "; size=" + size + "; remain="+remain + "; tracker: " + t.getTracker().getInterval() + ";" + t.getTracker().getMinInterval() + ";" + t.getTracker().getRemainingMillisUntilUpdate());
     283      }
     284      int completed = (int)(90 * (size - remain) / size);
     285      Job job = getJob(dc);
     286      if (remain == 0)
     287      {
     288        job.setProgress(90, "Download complete");
     289      }
     290      else
     291      {
     292        job.setProgress(completed, "Downloaded " +
     293            Values.formatBytes(size - remain) + " of " + Values.formatBytes(size));
     294      }
     295    }
     296  }
     297 
     298  /**
     299    Copy the downloaded files to BASE.
     300  */
     301  public void copyToBase(DbControl dc)
     302    throws IOException
     303  {
     304    TorrentState state = getState();
     305    if (state != TorrentState.DOWNLOAD_COMPLETE) return;
     306   
     307    this.state = TorrentState.UPLOADING;
     308    File downloadDir = getDownloadDir();
     309    Job job = getJob(dc);
     310    Directory uploadDir = (Directory)job.getParameterValue("directory");
     311    ChainedProgressReporter progress = new ChainedProgressReporter(job.getProgressReporter(null));
     312    progress.setRange(90, 100);
     313    List files = FileUtil.uploadFiles(dc, uploadDir, downloadDir, null, true, progress);
     314    dc.refreshItem(job); // Needed since the progress reporter has modified the state in the database
     315    job.doneOk(files.size() + " file(s) uploaded successfully");
     316  }
     317 
     318  public void copyToBaseAsync()
     319  {
     320    Thread t = new Thread(new Runnable()
     321    {
     322      @Override
     323      public void run()
     324      {
     325        DbControl dc = service.newDbControl();
     326        try
     327        {
     328          copyToBase(dc);
     329          dc.commit();
     330          state = TorrentState.UPLOAD_COMPLETE;
     331        }
     332        catch (Exception ex)
     333        {
     334          close(ex);
     335        }
     336        finally
     337        {
     338          if (dc != null) dc.close();
     339        }
     340
     341      }
     342    });
     343   
     344    t.start();
     345  }
     346 
     347  /**
    262348    Close this torrent manager and remove all temporary working files.
    263349    This method can be called any time, but it is intentded to be used
  • extensions/net.sf.basedb.torrent/trunk/src/main/net/sf/basedb/clients/torrent/service/TorrentService.java

    r1255 r1299  
    4343import net.sf.basedb.core.ConfigurationException;
    4444import net.sf.basedb.core.DbControl;
     45import net.sf.basedb.core.Job;
    4546import net.sf.basedb.core.SessionControl;
     47import net.sf.basedb.core.Application.Pinger;
    4648import net.sf.basedb.util.FileUtil;
    4749import net.sf.basedb.util.Values;
     
    108110  private List<TorrentManager> torrentManagers;
    109111  private SessionControl sc;
     112  private Pinger pinger;
    110113  private volatile TimerTask serviceTask;
    111114 
     
    166169        sc = Application.newSessionControl(null, "local-bittorrent", null);
    167170        sc.login(username, password, "Bittorrent download service", false);
     171        pinger = Application.newPinger(sc);
    168172        log.debug("Login successful");
    169173
     
    202206    if (serviceTask != null) serviceTask.cancel();
    203207    serviceTask = null;
     208    if (pinger != null) pinger.stop();
     209    pinger = null;
    204210    if (sc != null) closeSessionControl();
    205211    sc = null;
     
    288294  synchronized void remove(TorrentManager manager)
    289295  {
     296    try
     297    {
     298      btClient.stopTorrent(manager.getTorrent());
     299    }
     300    catch (Exception ex)
     301    {
     302      log.warn("Could not stop download of " + manager.getName(), ex);
     303    }
    290304    torrentManagers.remove(manager);
    291305    if (torrentManagers.size() == 0)
     
    300314    may be modified at any time by other threads.
    301315  */
    302   List<TorrentManager> getManagers()
     316  synchronized List<TorrentManager> getManagers()
    303317  {
    304318    return new ArrayList<TorrentManager>(torrentManagers);
     
    321335  {
    322336
     337    // Start the job + register signal receiver
     338    DbControl dc = sc.newDbControl();
     339    try
     340    {
     341      // Load the job and register it as started
     342      Job job = manager.getJob(dc);
     343      //String handlerId = signalReciever.registerSignalHandler(manager);
     344      //job.setSignalTransporter(signalReciever.getSignalTransporterClass(), handlerId);
     345      job.start("Starting download...", "torrent-service@" + Application.getHostName(), null);
     346      dc.commit();
     347    }
     348    finally
     349    {
     350      if (dc != null) dc.close();
     351    }
     352   
    323353    try
    324354    {
  • extensions/net.sf.basedb.torrent/trunk/src/main/net/sf/basedb/clients/torrent/service/TorrentServiceStateCheckerTask.java

    r1255 r1299  
    2626import java.util.List;
    2727import java.util.TimerTask;
     28
     29import net.sf.basedb.core.DbControl;
    2830
    2931import org.slf4j.Logger;
     
    7476    List<TorrentManager> all = service.getManagers();
    7577    log.debug("Checking " + all.size() + " torrent managers");
    76     for (TorrentManager manager : all)
     78    if (all.size() == 0) return; // early exit
     79
     80    DbControl dc = service.newDbControl();
     81    try
    7782    {
    78       TorrentState state = manager.getState();
    79       log.debug("Manager: " + manager.getName() + "; State: " + state);
    80      
    81       if (state == TorrentState.INITIAL)
     83      for (TorrentManager manager : all)
    8284      {
    83         // Do nothing; waiting for more configuration
     85        TorrentState state = manager.getState();
     86        log.debug("Manager: " + manager.getName() + "; State: " + state);
     87       
     88        if (state == TorrentState.INITIAL)
     89        {
     90          // Do nothing; waiting for more configuration
     91        }
     92        else if (state == TorrentState.READY_TO_DOWNLOAD)
     93        {
     94          // Start the download
     95          service.startDownload(manager);
     96        }
     97        else if (state == TorrentState.DOWNLOADING)
     98        {
     99          // Report progress about the download
     100          manager.reportDownloadProgress(dc);
     101        }
     102        else if (state == TorrentState.DOWNLOAD_COMPLETE)
     103        {
     104          // Upload the files to the BASE file system
     105          manager.reportDownloadProgress(dc);
     106          manager.copyToBaseAsync();
     107        }
     108        else if (state == TorrentState.UPLOADING)
     109        {
     110          // Wait...
     111        }
     112        else if (state == TorrentState.UPLOAD_COMPLETE)
     113        {
     114          // Success!!
     115          manager.close();
     116        }
     117       
    84118      }
    85       else if (state == TorrentState.READY_TO_DOWNLOAD)
    86       {
    87         // Start the download
    88         service.startDownload(manager);
    89       }
    90      
    91       else if (state == TorrentState.DOWNLOAD_COMPLETE)
    92       {
    93         // For new --- fix this later
    94         manager.close();
    95       }
    96      
     119      dc.commit();
    97120    }
    98    
    99 
    100 
     121    finally
     122    {
     123      if (dc != null) dc.close();
     124    }
    101125  }
    102126  // -------------------------
  • extensions/net.sf.basedb.torrent/trunk/src/main/net/sf/basedb/clients/torrent/service/TorrentState.java

    r1255 r1299  
    5959 
    6060  /**
     61    The files have been successfully copied to the BASE file system.
     62  */
     63  UPLOAD_COMPLETE,
     64 
     65  /**
    6166    Something went wrong and the torrent and temporary files
    6267    should be deleted. The error information should be
Note: See TracChangeset for help on using the changeset viewer.