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. * CRC checks of uploading images are so done parallel to the connection. */ public class CrcScheduler extends TimerTask { private static Logger log = Logger.getLogger( CrcScheduler.class ); @Override public void run() { log.debug( "Starting checks..." ); List list = ImageProcessor.getImagesToCheck(); log.debug( list ); Iterator iter = list.iterator(); // iterate over the uploading images that need to be checked 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 ); } }