diff options
-rw-r--r-- | pom.xml | 4 | ||||
-rw-r--r-- | src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java | 31 | ||||
-rw-r--r-- | src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java | 69 |
3 files changed, 90 insertions, 14 deletions
@@ -91,7 +91,7 @@ <version>4.11</version> <scope>test</scope> </dependency> -<!-- <dependency> + <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> @@ -102,7 +102,7 @@ <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> <scope>compile</scope> - </dependency> --> + </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java b/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java index 9276ed9..aa750f2 100644 --- a/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java +++ b/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java @@ -9,6 +9,7 @@ import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.util.List; +import java.util.UUID; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; @@ -19,7 +20,6 @@ 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; @@ -36,17 +36,21 @@ public class FtpUploadWorker implements Runnable // This List contains all Images in the Database that should be uploaded. List<DbImage> imageList = DbImage.getAllMarkedForUpload(); log.info( "FtpUploadWorker: 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, +// 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 ); char[] passphrase = Globals.getPropertyString( PropString.FTPSKEYSTOREPWD ).toCharArray(); KeyStore keystore; try { - + // All the necessary KeyStore handling for the "context"-item. // TODO: Don't do this again and again for every image, but once before the loop (or in the constructor) // Or better yet in a central spot, as the downloadworker needs the context too. Maybe something like Globals.getMasterServerSslContext() @@ -59,17 +63,26 @@ public class FtpUploadWorker implements Runnable context.init( null, trustManagers, null ); // uploadInfo and ThriftAuthentication - UploadInfos upInfos = ThriftConnection.getUploadInfos( imDat ); + 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.FTPSERVERIP ), Globals.getPropertyInt( PropInt.FTPPORT ), context ); + Uploader u = new Uploader( Globals.getPropertyString( PropString.FTPSERVERIP ), upInfos.port, context ); 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(); @@ -89,6 +102,7 @@ public class FtpUploadWorker implements Runnable // FIXME: Should be inside loop, otherwise you'll only ever upload the first 20 blocks upInfos = ThriftConnection.getUploadInfos( imDat ); + // TODO: Use more local try/catch-blocks, not a big one around everything, so you know what exactly caused the exception // and can handle it properly. There are roughly 3 types of error/exception you can encounter: // 1) Expected exception: Something you expect to go wrong, so you handle it in the catch block and let the code flow continue @@ -120,6 +134,7 @@ public class FtpUploadWorker implements Runnable } try { Thread.sleep( 5 * 60 * 1000 ); + // Thread.sleep( 1000 ); } catch ( InterruptedException e ) { Thread.currentThread().interrupt(); return; diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java b/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java index c831435..cacd6c8 100644 --- a/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java +++ b/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java @@ -16,6 +16,7 @@ import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; +import org.openslx.imagemaster.crcchecker.CRCFile; import org.openslx.imagemaster.thrift.iface.DownloadInfos; import org.openslx.imagemaster.thrift.iface.ImageData; import org.openslx.imagemaster.thrift.iface.ImageServer; @@ -39,14 +40,73 @@ public class ThriftConnection private static ImageServer.Client client = null; private static ServerSessionData sSD = null; private static Logger log = Logger.getLogger( ThriftConnection.class ); + private static CRCFile crc = null; - /** + * Method for getting UploadeInfos + * + * !! on the first Call !! + * + * when the CRCsum need to be transfered. * The method calls getConnection() to check if the connection is ok - * and to get the ServerSessionData. If connection is ok, it returns ftpCredential. + * and to get the ServerSessionData. If connection is ok, it calls + * submitImage with CRCsum in List<Integer>. * * @return returns 'null' if there is a problem. */ + public static UploadInfos getUploadInfos( ImageData imDat, String filename ) + { + ImageServer.Client theClient = null; + try { + theClient = getConnection(); + if ( theClient == null ) { + log.error( "Client was null!" ); + return null; + } + // .submitImage needs the List<Integer> from CRCFile.getCRCs() only + // on the first time called. null afterwards. + log.info( "First call of submitImage following..." ); + crc = new CRCFile(filename); + log.info( "Made CRCFile from " + filename ); + return theClient.submitImage( sSD.sessionId, imDat, crc.getCrcSums() ); + } catch ( TException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( UnrecoverableKeyException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( InvalidKeyException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( NoSuchAlgorithmException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( CertificateException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( FileNotFoundException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( KeyStoreException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( SignatureException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( IOException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + /** + * Method for getting UploadeInfos when CRCsum was already transfered on first call. + * The method calls getConnection() to check if the connection is ok + * and to get the ServerSessionData. If connection is ok, it calls + * submitImage with sSD.sessionId, imDat and !!null!! + * @return returns 'null' if there is a problem. + */ public static UploadInfos getUploadInfos( ImageData imDat ) { ImageServer.Client theClient = null; @@ -56,8 +116,9 @@ public class ThriftConnection log.error( "Client was null!" ); return null; } - - return theClient.submitImage( sSD.sessionId, imDat ); + // .submitImage needs the List<Integer> from CRCFile.getCRCs() only + // on the first time called. null afterwards. + return theClient.submitImage( sSD.sessionId, imDat, crc.getCrcSums()); } catch ( TException e ) { // TODO Auto-generated catch block e.printStackTrace(); |