extensions/net.sf.basedb.torrent/trunk/src/main/net/sf/basedb/clients/torrent/service/TorrentServiceStateCheckerTask.java

Code
Comments
Other
Rev Date Author Line
1255 22 Oct 10 nicklas 1 /**
1255 22 Oct 10 nicklas 2   $Id $
1255 22 Oct 10 nicklas 3
1255 22 Oct 10 nicklas 4   Copyright (C) 2010 Nicklas Nordborg
1255 22 Oct 10 nicklas 5
1255 22 Oct 10 nicklas 6   This file is part of Bittorent download service for BASE.
1255 22 Oct 10 nicklas 7   Available at http://baseplugins.thep.lu.se/
1255 22 Oct 10 nicklas 8
1255 22 Oct 10 nicklas 9   BASE is free software; you can redistribute it and/or
1255 22 Oct 10 nicklas 10   modify it under the terms of the GNU General Public License
1255 22 Oct 10 nicklas 11   as published by the Free Software Foundation; either version 2
1255 22 Oct 10 nicklas 12   of the License, or (at your option) any later version.
1255 22 Oct 10 nicklas 13
1255 22 Oct 10 nicklas 14   BASE is distributed in the hope that it will be useful,
1255 22 Oct 10 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
1255 22 Oct 10 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1255 22 Oct 10 nicklas 17   GNU General Public License for more details.
1255 22 Oct 10 nicklas 18
1255 22 Oct 10 nicklas 19   You should have received a copy of the GNU General Public License
1255 22 Oct 10 nicklas 20   along with this program; if not, write to the Free Software
1255 22 Oct 10 nicklas 21   Foundation, Inc., 59 Temple Place - Suite 330,
1255 22 Oct 10 nicklas 22   Boston, MA  02111-1307, USA.
1255 22 Oct 10 nicklas 23 */
1255 22 Oct 10 nicklas 24 package net.sf.basedb.clients.torrent.service;
1255 22 Oct 10 nicklas 25
1255 22 Oct 10 nicklas 26 import java.util.List;
1255 22 Oct 10 nicklas 27 import java.util.TimerTask;
1255 22 Oct 10 nicklas 28
1299 25 Feb 11 nicklas 29 import net.sf.basedb.core.DbControl;
1299 25 Feb 11 nicklas 30
1255 22 Oct 10 nicklas 31 import org.slf4j.Logger;
1255 22 Oct 10 nicklas 32 import org.slf4j.LoggerFactory;
1255 22 Oct 10 nicklas 33
1255 22 Oct 10 nicklas 34
1255 22 Oct 10 nicklas 35
1255 22 Oct 10 nicklas 36 /**
1255 22 Oct 10 nicklas 37   This timer task is regiestered with the BASE core timer and
1255 22 Oct 10 nicklas 38   is scheduled to run at regular intervals. It's purpose
1255 22 Oct 10 nicklas 39   is the check the state of all {@link TorrentManager}:s registered
1255 22 Oct 10 nicklas 40   with the {@link TorrentService} and take appropriate action when
1255 22 Oct 10 nicklas 41   needed. Eg.
1255 22 Oct 10 nicklas 42   
1255 22 Oct 10 nicklas 43   <ul>
1255 22 Oct 10 nicklas 44   <li>{@link TorrentState#INITIAL}: Do nothing since the torrent is not yet completely registered
1255 22 Oct 10 nicklas 45   <li>{@link TorrentState#READY_TO_DOWNLOAD}: Start the download: ....
1255 22 Oct 10 nicklas 46   <li>{@link TorrentState#DOWNLOADING}: Report the progress to BASE: ....
1255 22 Oct 10 nicklas 47   <li>{@link TorrentState#DOWNLOAD_COMPLETE}: Start a thread that uploads the file to BASE
1255 22 Oct 10 nicklas 48   <li>{@link TorrentState#UPLOADING}: Do nothing (progress is reported by the uploader thread)
1255 22 Oct 10 nicklas 49   <li>{@link TorrentState#DONE}: Cleanup and remove files from working directory
1255 22 Oct 10 nicklas 50   <li>ERROR: ????
1255 22 Oct 10 nicklas 51   </ul>
1255 22 Oct 10 nicklas 52   
1255 22 Oct 10 nicklas 53   @author nicklas
1255 22 Oct 10 nicklas 54   @since 1.0
1255 22 Oct 10 nicklas 55 */
1255 22 Oct 10 nicklas 56 public class TorrentServiceStateCheckerTask 
1255 22 Oct 10 nicklas 57   extends TimerTask 
1255 22 Oct 10 nicklas 58 {
1255 22 Oct 10 nicklas 59
1255 22 Oct 10 nicklas 60   private static final Logger log = LoggerFactory.getLogger(TorrentServiceStateCheckerTask.class);
1255 22 Oct 10 nicklas 61
1255 22 Oct 10 nicklas 62   private final TorrentService service;
1255 22 Oct 10 nicklas 63   TorrentServiceStateCheckerTask(TorrentService service)
1255 22 Oct 10 nicklas 64   {
1255 22 Oct 10 nicklas 65     this.service = service;
1255 22 Oct 10 nicklas 66   }
1255 22 Oct 10 nicklas 67   
1255 22 Oct 10 nicklas 68   /*
1255 22 Oct 10 nicklas 69     From the TimerTask class
1255 22 Oct 10 nicklas 70     ------------------------
1255 22 Oct 10 nicklas 71   */
1255 22 Oct 10 nicklas 72   @Override
1255 22 Oct 10 nicklas 73   public void run() 
1255 22 Oct 10 nicklas 74   {
1255 22 Oct 10 nicklas 75     // Clone the list since the actions below may cause it to be modified
1255 22 Oct 10 nicklas 76     List<TorrentManager> all = service.getManagers();
1255 22 Oct 10 nicklas 77     log.debug("Checking " + all.size() + " torrent managers");
1299 25 Feb 11 nicklas 78     if (all.size() == 0) return; // early exit
1299 25 Feb 11 nicklas 79
1299 25 Feb 11 nicklas 80     DbControl dc = service.newDbControl();
1299 25 Feb 11 nicklas 81     try
1255 22 Oct 10 nicklas 82     {
1299 25 Feb 11 nicklas 83       for (TorrentManager manager : all)
1255 22 Oct 10 nicklas 84       {
1299 25 Feb 11 nicklas 85         TorrentState state = manager.getState();
1299 25 Feb 11 nicklas 86         log.debug("Manager: " + manager.getName() + "; State: " + state);
1299 25 Feb 11 nicklas 87         
1299 25 Feb 11 nicklas 88         if (state == TorrentState.INITIAL)
1299 25 Feb 11 nicklas 89         {
1299 25 Feb 11 nicklas 90           // Do nothing; waiting for more configuration
1299 25 Feb 11 nicklas 91         }
1299 25 Feb 11 nicklas 92         else if (state == TorrentState.READY_TO_DOWNLOAD)
1299 25 Feb 11 nicklas 93         {
1299 25 Feb 11 nicklas 94           // Start the download
1299 25 Feb 11 nicklas 95           service.startDownload(manager);
1299 25 Feb 11 nicklas 96         }
1299 25 Feb 11 nicklas 97         else if (state == TorrentState.DOWNLOADING)
1299 25 Feb 11 nicklas 98         {
1299 25 Feb 11 nicklas 99           // Report progress about the download
1299 25 Feb 11 nicklas 100           manager.reportDownloadProgress(dc);
1299 25 Feb 11 nicklas 101         }
1299 25 Feb 11 nicklas 102         else if (state == TorrentState.DOWNLOAD_COMPLETE)
1299 25 Feb 11 nicklas 103         {
1299 25 Feb 11 nicklas 104           // Upload the files to the BASE file system
1299 25 Feb 11 nicklas 105           manager.reportDownloadProgress(dc);
1299 25 Feb 11 nicklas 106           manager.copyToBaseAsync();
1299 25 Feb 11 nicklas 107         }
1299 25 Feb 11 nicklas 108         else if (state == TorrentState.UPLOADING)
1299 25 Feb 11 nicklas 109         {
1299 25 Feb 11 nicklas 110           // Wait...
1299 25 Feb 11 nicklas 111         }
1299 25 Feb 11 nicklas 112         else if (state == TorrentState.UPLOAD_COMPLETE)
1299 25 Feb 11 nicklas 113         {
1299 25 Feb 11 nicklas 114           // Success!! 
1299 25 Feb 11 nicklas 115           manager.close();
1299 25 Feb 11 nicklas 116         }
1299 25 Feb 11 nicklas 117         
1255 22 Oct 10 nicklas 118       }
1299 25 Feb 11 nicklas 119       dc.commit();
1255 22 Oct 10 nicklas 120     }
1299 25 Feb 11 nicklas 121     finally
1299 25 Feb 11 nicklas 122     {
1299 25 Feb 11 nicklas 123       if (dc != null) dc.close();
1299 25 Feb 11 nicklas 124     }
1255 22 Oct 10 nicklas 125   }
1255 22 Oct 10 nicklas 126   // -------------------------
1255 22 Oct 10 nicklas 127 }