diff options
3 files changed, 82 insertions, 14 deletions
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java b/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java index 97a8c37..b1fce15 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java @@ -1,9 +1,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 +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 ); + } + } diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java index 272b924..05c094b 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java @@ -2,6 +2,7 @@ package org.openslx.imagemaster.serverconnection; import java.sql.Timestamp; import java.util.HashMap; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -32,6 +33,11 @@ public class ImageProcessor * Value: uploadingImageInfos */ private static HashMap<String, UploadingImageInfos> uploadingImages = new HashMap<>(); + + /** + * The UUIDs of the images that need to be checked by the crc checker. + */ + private static List<String> imagesToCheck = new LinkedList<>(); /** * The downloading clients. @@ -62,7 +68,7 @@ public class ImageProcessor uploadDone( uuid ); return new UploadInfos( null, missing ); } - uploadingImages.get( uuid ).setLastSentBlocks( missing ); + uploadingImages.get( uuid ).addNotCheckedBlocks( missing ); return new UploadInfos( uploadingImages.get( uuid ).getToken(), missing ); } @@ -79,15 +85,16 @@ public class ImageProcessor // TODO: proper synchronization, interface is multi threaded. // should synchronize operations on the map (use concurrent map) and then synchronize on the uploading image // when handing the missing blocks etc... - uploadingImages.put( uuid, new UploadingImageInfos( token, allBlocks, serverSessionId, new Timestamp( System.currentTimeMillis() ), uuid ) ); + uploadingImages.put( uuid, new UploadingImageInfos( token, allBlocks, serverSessionId, new Timestamp( System.currentTimeMillis() ), uuid, filepath, "CRCFILE" ) ); DbImage.insert( imageData, System.currentTimeMillis(), token, allBlocks, serverSessionId, filepath ); - + imagesToCheck.add( uuid ); + List<Integer> missing = getMissingBlocks( uuid, AMOUNT ); if ( missing.isEmpty() ) { // TODO: if this is empty, check if there are pending blocks and if so, request them again uploadDone( uuid ); } - uploadingImages.get( uuid ).setLastSentBlocks( missing ); + uploadingImages.get( uuid ).addNotCheckedBlocks( missing ); return new UploadInfos( token, missing ); } } @@ -169,7 +176,7 @@ public class ImageProcessor } synchronized ( image ) { - image.setLastSentBlocks( result ); + image.addNotCheckedBlocks( result ); } return result; @@ -183,6 +190,9 @@ public class ImageProcessor */ private static void uploadDone( String uuid ) { + synchronized (imagesToCheck) { + imagesToCheck.remove( uuid ); + } UploadingImageInfos image; synchronized ( uploadingImages ) { image = uploadingImages.remove( uuid ); @@ -192,6 +202,17 @@ public class ImageProcessor // remove the connection so that it can be used by a new client ConnectionHandler.removeConnection( image.getToken() ); } + + public static List<UploadingImageInfos> getImagesToCheck() + { + List<UploadingImageInfos> result = new LinkedList<>(); + Iterator<String> iter = imagesToCheck.iterator(); + while ( iter.hasNext() ) { + result.add( uploadingImages.get( iter.next() ) ); + } + + return result; + } /** * Checks pending uploads in database and adds them to process list again. @@ -202,7 +223,7 @@ public class ImageProcessor for ( DbImage image : list ) { String token = image.token; ConnectionHandler.addConnection( token, image.imagePath, ConnectionData.UPLOADING ); - UploadingImageInfos infos = new UploadingImageInfos( token, image.missingBlocks, image.serverSessionId, image.timestamp, image.uuid ); + UploadingImageInfos infos = new UploadingImageInfos( token, image.missingBlocks, image.serverSessionId, image.timestamp, image.uuid, image.imagePath, "CRCFILE" ); uploadingImages.put( image.uuid, infos ); } log.info( "Added " + list.size() + " pending upload(s) to process list again." ); diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImageInfos.java b/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImageInfos.java index 547916f..210586f 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImageInfos.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImageInfos.java @@ -12,7 +12,6 @@ import org.openslx.imagemaster.db.DbImage; */ public class UploadingImageInfos { - /** * Token for the satellite. */ @@ -25,19 +24,22 @@ public class UploadingImageInfos * The list of blocks that the satellite received last. * (This could be used to tell the CRCChecker to check these blocks. */ - private List<Integer> lastSentBlocks = new LinkedList<>(); + private List<Integer> notCheckedBlocks = new LinkedList<>(); private String serverSessionId; private Timestamp ts; // when did the server something for the last time private DbImage dbImage = null; // the DB representation of this image private String uuid; + private String filename; + private String crcFilename; - protected UploadingImageInfos(String token, List<Integer> missingBlocks, String serverSessionId, Timestamp ts, String uuid) + protected UploadingImageInfos(String token, List<Integer> missingBlocks, String serverSessionId, Timestamp ts, String uuid, String filename, String crcFilename) { this.token = token; this.missingBlocks = missingBlocks; this.serverSessionId = serverSessionId; this.ts = ts; this.uuid = uuid; + this.crcFilename = crcFilename; } protected void removeBlock( int number ) @@ -50,14 +52,14 @@ public class UploadingImageInfos this.missingBlocks.removeAll( list ); } - protected void setLastSentBlocks( List<Integer> list ) + protected void addNotCheckedBlocks( List<Integer> list ) { - this.lastSentBlocks = list; + this.notCheckedBlocks = list; } - protected List<Integer> getLastSentBlocks() + protected List<Integer> getNotCheckedBlocks() { - return this.lastSentBlocks; + return this.notCheckedBlocks; } protected String getToken() @@ -87,4 +89,14 @@ public class UploadingImageInfos } return this.dbImage; } + + protected String getFilename() + { + return this.filename; + } + + protected String getCrcFilename() + { + return this.crcFilename; + } } |