blob: 7ad08dbbb244059dbc00178cd7cd4c510f5af729 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package org.openslx.satellitedaemon.filetransfer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.openslx.filetransfer.Downloader;
import org.openslx.filetransfer.FileRange;
import org.openslx.filetransfer.WantRangeCallback;
import org.openslx.imagemaster.thrift.iface.DownloadData;
import org.openslx.satellitedaemon.Globals;
import org.openslx.satellitedaemon.Identity;
import org.openslx.satellitedaemon.db.DbImage;
import org.openslx.satellitedaemon.db.DbImage.Status;
public class FileDownloadWorker implements Runnable
{
private static Logger log = Logger.getLogger( FileDownloadWorker.class );
@Override
public void run()
{
while ( true ) {
List<DbImage> imageList = DbImage.getAllMarkedForDownload();
log.info( "imageList Contains " + imageList.size() + " items." );
for ( final DbImage image : imageList ) {
List<Integer> range = new ArrayList<Integer>();
for ( long i = 0; i < ( image.fileSize / Globals.BLOCKSIZE ); i++ ) {
range.add( (int)i );
}
DownloadData downInfos = ThriftConnection.getDownloadInfos( image );
if ( downInfos == null ) {
log.error( "The DownloadInfos returned by ThriftConnection class are null" );
continue;
}
// create new instance of Downloader.
Downloader d;
try {
d = new Downloader( Globals.getMasterserverHost(),
downInfos.port, Globals.getMasterServerSslContext(), downInfos.token );
} catch ( IOException e ) {
e.printStackTrace();
continue;
}
// start downloading process.
d.download( Globals.getImageFolder() + "/" + image.path, new WantRangeCallback() {
long pos = 0;
long size = image.fileSize;
@Override
public FileRange get()
{
// get start of range.
if ( pos >= size ) {
log.debug( "Download completed." );
image.updateStatus( Status.successfully_decentralized );
return null;
}
long startOfRange = pos;
long endOfRange = Math.min( pos + Globals.BLOCKSIZE, image.fileSize );
FileRange range = new FileRange( startOfRange, endOfRange );
pos += Globals.BLOCKSIZE;
return range;
}
} );
}
try {
Thread.sleep( 5 * 60 * 1000 );
} catch ( InterruptedException e ) {
Thread.currentThread().interrupt();
return;
}
}
}
}
|