summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/filetransfer/FileUploadWorker.java
blob: 4876e6c7a5227bfd21e08885de737320fcb96eb6 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                                 
                           
                      



                                                      
                                                               
                                                       
                                                     
                                           
                                            
                                              
                                                     
                                             
 


                                                                               

                 


                                

                                                                                       
                                                                                  
                                                                                         

                                                            
                                                           
                                                                

                                                                                                              
                                                                                                                                       
 
                                                                                          
 
                                                             
                                                                                                                      
                                                                                       
















                                                                                                                                                       

                                                                                                                   
                                                 
                                 

                                                                                               

                                                                                 
                                           


                                                                                                                           
                                                           

                                                            
                                 
 
                                                        



                                                                                            
                                 
 

                             
                                                              
                                                        
                                                            






                                                                   
package org.openslx.satellitedaemon.filetransfer;

import java.io.IOException;
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.ImageDataException;
import org.openslx.imagemaster.thrift.iface.UploadData;
import org.openslx.imagemaster.thrift.iface.UserInfo;
import org.openslx.satellitedaemon.Globals;
import org.openslx.satellitedaemon.Identity;
import org.openslx.satellitedaemon.db.DbImage;
import org.openslx.satellitedaemon.db.DbImage.Status;
import org.openslx.satellitedaemon.db.DbUser;

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 ) {
				ImageData imDat = new ImageData(
						image.guid, image.rid,
						image.name, image.createTime, image.changeTime, image.creator,
						image.contentOperatingSystem, image.isValid, image.isDeleted, "best", image.fileSize );

				String path = Globals.getImageFolder() + "/" + image.path;

				log.info( imDat.toString() );
				// ThriftConnection.getUploadInfos returns uploadInfo and handles ThriftAuthentication
				DbUser dbUser = DbUser.getUserById( imDat.ownerLogin );
				if (dbUser == null) {
					image.updateStatus( Status.only_local );
					log.warn( "Unknown user from database." );	
				} else {
   				UserInfo userInfo = new UserInfo( dbUser.userId, dbUser.firstName, dbUser.lastName, dbUser.email, dbUser.institution );
   				ThriftConnection.publishUser( userInfo );
				}
				
				UploadData upInfos;
				try {
					upInfos = ThriftConnection.getUploadInfos( imDat, path );
				} catch ( ImageDataException e1 ) {
					log.error( "Invalid image data. Not known by master.", e1 );
					// mark image as only_local, that this image will be skipped next time.
					image.updateStatus( Status.only_local );
					continue;
				}
				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;
				try {
					u = new Uploader( Globals.getMasterserverHost(),
							upInfos.port, Globals.getMasterServerSslContext(), upInfos.token );
				} catch ( IOException e ) {
					e.printStackTrace();
					continue;
				}

				// Start upload process.
				if ( u.upload( path ) ) {
					u.close( null );
					log.info( "Uploaded image successfuly." );
					image.updateStatus( Status.successfully_published );
				}

			}
			try {
				Thread.sleep( 1 * 60 * 1000 );
				// Thread.sleep( 1000 );
			} catch ( InterruptedException e ) {
				Thread.currentThread().interrupt();
				return;
			}
		}

	}
}