summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/filetransfer/FileUploadWorker.java
blob: d88cd90f5038043d9fbd1fd3d4e6e98591281715 (plain) (tree)
1
2
3


                                                 



















                                                                                                 
                                                                                         


                                                            



                                                                                                                                                                              
 


                                                                                                                                              






                                                                                                                   
                                                 







                                                                                                                                                                             

                                                                                                                       

                                                                                           
                                                                                          





                                                                                                                                                                                                            





                                                                                           
                                                              








                                                                                     
package org.openslx.satellitedaemon.filetransfer;

import java.util.List;

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( "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" );

					continue;

				}
				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 );

				log.info( "upInfos.getMissingBlocks().size() = " + upInfos.getMissingBlocks().size() );

				// continue sending Blocks until getMissingBlocks is empty.
				while ( !upInfos.getMissingBlocks().isEmpty() ) {
					List<Integer> blocks = upInfos.getMissingBlocks();
					log.info( "Anzahl angeforderter Blöcke : " + blocks.size() );
					log.info( blocks );
					for ( int i = 0; i < blocks.size(); i++ ) {
						u.sendRange( blocks.get( i ) * Globals.getPropertyInt( PropInt.BLOCKSIZE ), ( ( blocks.get( i ) + 1 ) * Globals.getPropertyInt( PropInt.BLOCKSIZE ) - 1 ) );
						u.sendFile( image.path );
						log.info( "Block number " + blocks.get(i) + " uploaded.");
					}
					upInfos = ThriftConnection.getUploadInfos( imDat );
				}
				u.close();
			}
			try {
				Thread.sleep( 1 * 60 * 1000 );
				//				Thread.sleep( 1000 );
			} catch ( InterruptedException e ) {
				Thread.currentThread().interrupt();
				return;
			}
		}

	}
}