package org.openslx.satellitedaemon.filetransfer;
import java.io.File;
import java.io.IOException;
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.UploadData;
import org.openslx.satellitedaemon.Globals;
import org.openslx.satellitedaemon.db.DbImage;
import org.openslx.satellitedaemon.db.DbImage.Status;
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", // TODO: fields in databases need to fit somehow...
// true, false, "best", "theVeryBest", image.fileSize);
// Only for testing because a random UUID is used. Later the method above should be used.
ImageData imDat = new ImageData(
UUID.randomUUID().toString(), image.rid,
image.name, (System.currentTimeMillis() / 1000),
(System.currentTimeMillis() / 1000), image.creator, "anyThing",
true, false, "best", "theVeryBest", image.fileSize );
String path = Globals.getImageFolder() + "/" + image.path;
// ThriftConnection.getUploadInfos returns uploadInfo and handles ThriftAuthentication
UploadData upInfos = ThriftConnection.getUploadInfos( imDat, path );
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;
}
}
}
}