summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java
blob: b1fce15e7c6f054228bc5751adf8d348abc1c4cd (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
package org.openslx.imagemaster.serverconnection;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.openslx.imagemaster.crcchecker.CRCChecker;

/**
 *	Class to schedule crc checks. 
 */
public class CRCScheduler extends TimerTask
{

	@Override
	public void run()
	{
		List<UploadingImageInfos> list = ImageProcessor.getImagesToCheck();
		Iterator<UploadingImageInfos> iter = list.iterator();
		while ( iter.hasNext() ) {
			UploadingImageInfos image = iter.next();
			List<Integer> blocks = image.getNotCheckedBlocks();
			List<Integer> finishedBlocks = new LinkedList<>();
			try {
				finishedBlocks = CRCChecker.checkCRC( image.getFilename(), image.getCrcFilename(), blocks );
			} catch ( IOException e ) {
				// TODO: Could not read crc file: tell this to client
			}
			image.removeBlocks( finishedBlocks );
		}
	}
	
	public static void startScheduling()
	{
		Timer timer = new Timer( "CRCScheduler" );
		
		// start now and fire every 60 s
		timer.schedule( new CRCScheduler(), 0, 60000 );
	}
	
}