summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/serverconnection/CrcScheduler.java
blob: 031a807602968ff8854a339851ca07148edb2756 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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()
	{
		List<UploadingImage> list = ImageProcessor.getImagesToCheck();
		if ( list == null || list.isEmpty() )
			return;
		log.debug( "Starting checks: " + list );
		Iterator<UploadingImage> 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.getUuid() );
			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 );
	}

}