diff options
author | Nils Schwabe | 2014-08-18 14:18:31 +0200 |
---|---|---|
committer | Nils Schwabe | 2014-08-18 14:18:31 +0200 |
commit | a88927b894494f18a36fa78c06fd7568c7fa4420 (patch) | |
tree | 49777168d0b7e20d96b4f326ac43c213fc4e1f8a | |
parent | Change some classes / methods to fit camelCase (diff) | |
download | masterserver-a88927b894494f18a36fa78c06fd7568c7fa4420.tar.gz masterserver-a88927b894494f18a36fa78c06fd7568c7fa4420.tar.xz masterserver-a88927b894494f18a36fa78c06fd7568c7fa4420.zip |
Add support for updating images with new versions (not tested)
3 files changed, 120 insertions, 8 deletions
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/CrcScheduler.java b/src/main/java/org/openslx/imagemaster/serverconnection/CrcScheduler.java new file mode 100644 index 0000000..68ae95d --- /dev/null +++ b/src/main/java/org/openslx/imagemaster/serverconnection/CrcScheduler.java @@ -0,0 +1,75 @@ +package org.openslx.imagemaster.serverconnection; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; + +import org.apache.log4j.Logger; +import org.openslx.imagemaster.Globals; +import org.openslx.imagemaster.crcchecker.CrcChecker; + +/** + * Class to schedule crc checks. + */ +public class CrcScheduler extends TimerTask +{ + + private static Logger log = Logger.getLogger( CrcScheduler.class ); + + @Override + public void run() + { + log.debug( "Starting checks..." ); + List<UploadingImage> list = ImageProcessor.getImagesToCheck(); + log.debug( list ); + Iterator<UploadingImage> iter = list.iterator(); + while ( iter.hasNext() ) { + UploadingImage image = iter.next(); + log.debug( "Checking blocks of " + image.getDbImage().imageName ); + CrcChecker crcChecker = new CrcChecker( image.getImageFile(), image.getCrcFile() ); + log.debug( "CRCFile is valid: " + crcChecker.hasValidCrcFile() ); + if ( !crcChecker.hasValidCrcFile() ) { + image.setCrcFile( null ); // set crc file to null, so that the image processor will try to write it again. + crcChecker.done(); + continue; + } + for ( int block = 0; block < image.getNumberOfBlocks(); block++ ) { + if ( image.needsCheck( block ) ) { + try { + if ( crcChecker.checkBlock( block ) ) { + image.setValid( block ); + log.debug( block + " was valid" ); + } else { + image.setNeedsRequest( block ); + log.debug( block + " was NOT valid" ); + } + } catch ( IOException e ) { + if ( e.getMessage().equalsIgnoreCase( "crc" ) ) { + image.setCrcFile( null ); // set crc file to null, so that the imageprocessor will try to write it again. + image.updateDb(); + crcChecker.done(); + break; + } else { + // could not read image file + log.error( "Could not read from image file on disk. Pleas contact the server administrator. Image: '" + image.getImageFile().toString() + "'" ); + } + } + } + } + image.updateDb(); // writes valid blocks to database + crcChecker.done(); // closes image file + } + log.debug( "... done" ); + } + + public static void startScheduling() + { + Timer timer = new Timer( "CRCScheduler" ); + + // start now and fire every 60 s + timer.schedule( new CrcScheduler(), 0, Globals.getCrcSchedulingInterval() * 1000L ); + } + +} diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java index a738259..d68887d 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java @@ -1,5 +1,6 @@ package org.openslx.imagemaster.serverconnection; +import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -126,23 +127,21 @@ public class ImageProcessor } // insert new image - if ( DbImage.exists( imageData.uuid ) ) { - throw new ImageDataException( ImageDataError.INVALID_DATA, "UUID already existing." ); - } if ( !CrcFile.sumsAreValid( crcSums ) ) throw new UploadException( UploadError.INVALID_CRC, "CRC sums were invalid." ); - filepath = Globals.getImageDir() + "/" + uuid + ".vmdk"; // TODO: needs to be saved in another way... (Because of the versions.) + filepath = generateFilepathOfImage( imageData, !isUpdate ); token = RandomString.generate( 100, false ); nBlocks = Util.getNumberOfBlocks( imageData.fileSize, Globals.blockSize ); allBlocks = new int[ nBlocks ]; // initialize array with all zeros which mean that this block is missing image = new UploadingImage( token, allBlocks, System.currentTimeMillis(), uuid, filepath ); + image.setDbImage( i ); // set the dbImage (it doesn't matter if the image is null because the uploadingImage is creating it then uploadingImages.put( uuid, image ); } CrcFile crcFile; try { // try to write crc file ... - crcFile = CrcFile.writeCrcFile( crcSums, Globals.getImageDir() + "/" + uuid + ".crc" ); //TODO: same here ^ + crcFile = CrcFile.writeCrcFile( crcSums, generateFilepathOfCrcFile( imageData ) ); } catch ( IOException e ) { // ... and keep it in ram if it fails crcFile = new CrcFile( crcSums ); @@ -151,7 +150,7 @@ public class ImageProcessor ConnectionHandler.addConnection( token, filepath, Connection.UPLOADING ).image = image; if ( isUpdate ) { - // TODO: update db + i.updateVersion( i.imageVersion, Util.getNumberOfBlocks( i.fileSize, Globals.blockSize ) ); } else { DbImage.insert( imageData, System.currentTimeMillis(), token, nBlocks, serverSessionId, filepath ); } @@ -298,6 +297,33 @@ public class ImageProcessor // for the downloader return null; } + + /** + * Generates the filePath of an image. + * And creates the folder if wanted. + * The crc file is found under filePath + ".crc" + * + * @param imageData The data of the image + * @param createFolder If you want the folder to be created + * @return The filePath of the given image + */ + public static String generateFilepathOfImage( ImageData imageData, boolean createFolder ) + { + String result = Globals.getImageDir() + "/" + imageData.uuid + "/"; + if ( createFolder ) { + File f = new File( result ); + if ( !f.exists() ) { + f.mkdirs(); + } + } + result += imageData.imageName + "-v" + String.valueOf( imageData.imageVersion ) + ".vmdk"; + return result; + } + + public static String generateFilepathOfCrcFile( ImageData imageData ) + { + return generateFilepathOfImage( imageData, false ) + ".crc"; + } /** * Checks pending uploads in database and adds them to process list again. @@ -312,7 +338,7 @@ public class ImageProcessor try { if ( !crcFile.isValid() ) { continue; - // UploadingImage object contains an CRCFile = null which invokes the ImageProcessor to retry to save it + // UploadingImage object will contain a CRCFile = null which invokes the ImageProcessor to retry to save it } } catch ( IOException e ) { continue; @@ -324,6 +350,8 @@ public class ImageProcessor } log.info( "Added " + list.size() + " pending upload(s) to process list again." ); } + + public static void init() { diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java b/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java index b8a045e..0e431fa 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java @@ -69,7 +69,7 @@ public class UploadingImage } } } - dbImage.updateMissingBlocks( missingBlocks ); + getDbImage().updateMissingBlocks( missingBlocks ); } protected void setMissing( int index ) @@ -163,6 +163,15 @@ public class UploadingImage return this.dbImage; } + protected void setDbImage( DbImage dbImage ) + { + if ( dbImage != null ) { + return; + } else { + this.dbImage = dbImage; + } + } + protected ImageFile getImageFile() { if ( imageFile == null ) { |