1255 |
22 Oct 10 |
nicklas |
1 |
/** |
1255 |
22 Oct 10 |
nicklas |
$Id $ |
1255 |
22 Oct 10 |
nicklas |
3 |
|
1255 |
22 Oct 10 |
nicklas |
Copyright (C) 2010 Nicklas Nordborg |
1255 |
22 Oct 10 |
nicklas |
5 |
|
1255 |
22 Oct 10 |
nicklas |
This file is part of Bittorent download service for BASE. |
1255 |
22 Oct 10 |
nicklas |
Available at http://baseplugins.thep.lu.se/ |
1255 |
22 Oct 10 |
nicklas |
8 |
|
1255 |
22 Oct 10 |
nicklas |
BASE is free software; you can redistribute it and/or |
1255 |
22 Oct 10 |
nicklas |
modify it under the terms of the GNU General Public License |
1255 |
22 Oct 10 |
nicklas |
as published by the Free Software Foundation; either version 2 |
1255 |
22 Oct 10 |
nicklas |
of the License, or (at your option) any later version. |
1255 |
22 Oct 10 |
nicklas |
13 |
|
1255 |
22 Oct 10 |
nicklas |
BASE is distributed in the hope that it will be useful, |
1255 |
22 Oct 10 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
1255 |
22 Oct 10 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1255 |
22 Oct 10 |
nicklas |
GNU General Public License for more details. |
1255 |
22 Oct 10 |
nicklas |
18 |
|
1255 |
22 Oct 10 |
nicklas |
You should have received a copy of the GNU General Public License |
1255 |
22 Oct 10 |
nicklas |
along with this program; if not, write to the Free Software |
1255 |
22 Oct 10 |
nicklas |
Foundation, Inc., 59 Temple Place - Suite 330, |
1255 |
22 Oct 10 |
nicklas |
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 |
This timer task is regiestered with the BASE core timer and |
1255 |
22 Oct 10 |
nicklas |
is scheduled to run at regular intervals. It's purpose |
1255 |
22 Oct 10 |
nicklas |
is the check the state of all {@link TorrentManager}:s registered |
1255 |
22 Oct 10 |
nicklas |
with the {@link TorrentService} and take appropriate action when |
1255 |
22 Oct 10 |
nicklas |
needed. Eg. |
1255 |
22 Oct 10 |
nicklas |
42 |
|
1255 |
22 Oct 10 |
nicklas |
<ul> |
1255 |
22 Oct 10 |
nicklas |
<li>{@link TorrentState#INITIAL}: Do nothing since the torrent is not yet completely registered |
1255 |
22 Oct 10 |
nicklas |
<li>{@link TorrentState#READY_TO_DOWNLOAD}: Start the download: .... |
1255 |
22 Oct 10 |
nicklas |
<li>{@link TorrentState#DOWNLOADING}: Report the progress to BASE: .... |
1255 |
22 Oct 10 |
nicklas |
<li>{@link TorrentState#DOWNLOAD_COMPLETE}: Start a thread that uploads the file to BASE |
1255 |
22 Oct 10 |
nicklas |
<li>{@link TorrentState#UPLOADING}: Do nothing (progress is reported by the uploader thread) |
1255 |
22 Oct 10 |
nicklas |
<li>{@link TorrentState#DONE}: Cleanup and remove files from working directory |
1255 |
22 Oct 10 |
nicklas |
<li>ERROR: ???? |
1255 |
22 Oct 10 |
nicklas |
</ul> |
1255 |
22 Oct 10 |
nicklas |
52 |
|
1255 |
22 Oct 10 |
nicklas |
@author nicklas |
1255 |
22 Oct 10 |
nicklas |
@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 |
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 |
// 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 |
// 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 |
// 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 |
// 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 |
// 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 |
// 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 |
// 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 |
} |