summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/filetransfer/FileUploadWorker.java
blob: 8e66ea9ca892c0e1e321920ba5ddd7aec942b4ea (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
82
83
84
85
package org.openslx.satellitedaemon.filetransfer;

import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.openslx.filetransfer.Uploader;
import org.openslx.imagemaster.thrift.iface.ImageData;
import org.openslx.imagemaster.thrift.iface.UploadInfos;
import org.openslx.satellitedaemon.Globals;
import org.openslx.satellitedaemon.Globals.PropInt;
import org.openslx.satellitedaemon.Globals.PropString;
import org.openslx.satellitedaemon.db.DbImage;

public class FileUploadWorker implements Runnable
{
	private static Logger log = Logger.getLogger( FileUploadWorker.class );

	@Override
	public void run()
	{
		while ( true ) {
			// This List contains all Images in the Database that should be uploaded.
			List<DbImage> imageList = DbImage.getAllMarkedForUpload();
			log.info( "FILTRANSFERUploadWorker: imageList Contains " + imageList.size() + " items." );

			// Upload one Image after the other.
			for ( DbImage image : imageList ) {
				// TODO: still some fields for ImageData, which i can't fill with info from DbImage.
				//				ImageData imDat = new ImageData( image.guid, image.rid,
				//						image.name, System.currentTimeMillis(), System.currentTimeMillis(), image.creator, "anyThing",
				//						true, false, "best", "theVeryBest", image.fileSize );

				ImageData imDat = new ImageData( UUID.randomUUID().toString(), image.rid,
						image.name, System.currentTimeMillis(), System.currentTimeMillis(), image.creator, "anyThing",
						true, false, "best", "theVeryBest", image.fileSize );

				// uploadInfo and ThriftAuthentication
				String crcPath = image.path.concat( ".crc" );
				UploadInfos upInfos = ThriftConnection.getUploadInfos( imDat, crcPath );
				if ( upInfos == null ) {
					log.error( "The UploadInfos returned by ThriftConnection Class are null" );

					return;

					// FIXME: And then..? If you just continue, you'll run into a null pointer exception

				}
				log.info( "Got upInfos. Trying to create Uploader with token: " + upInfos.token );

				// creating the uploader with the "context"-item.
				Uploader u = new Uploader( Globals.getPropertyString( PropString.FILETRANSFERSERVERIP ), upInfos.port, Globals.getMasterServerSslContext() );
				u.sendToken( upInfos.token );

				// continue sending Blocks until getMissingBlocks is empty.
				while ( !upInfos.getMissingBlocks().isEmpty() ) {
					// Send all Blocks from upInfos.getMissingBlocks() in ranges.
					List<Integer> blocks = upInfos.getMissingBlocks();
					int start = 0;
					for ( int i = 0; i < blocks.size() - 1; i++ ) {
						if ( blocks.get( i ) != ( blocks.get( i + 1 ) - 1 ) ) {
							u.sendRange( start * Globals.getPropertyInt( PropInt.BLOCKSIZE ), i * Globals.getPropertyInt( PropInt.BLOCKSIZE ) );
							u.sendFile( image.path );
							start = i + 1;
						}
						if ( i == blocks.size() - 2 ) {
							u.sendRange( start * Globals.getPropertyInt( PropInt.BLOCKSIZE ), ( blocks.size() - 1 ) * Globals.getPropertyInt( PropInt.BLOCKSIZE ) );
							u.sendFile( image.path );
						}
					}
					upInfos = ThriftConnection.getUploadInfos( imDat );
				}
				u.close();
			}
			try {
				Thread.sleep( 5 * 60 * 1000 );
				//				Thread.sleep( 1000 );
			} catch ( InterruptedException e ) {
				Thread.currentThread().interrupt();
				return;
			}
		}

	}
}