From 3f4f67ec1f25434e2607363e6014b7cd9455707b Mon Sep 17 00:00:00 2001 From: Nils Schwabe Date: Fri, 4 Jul 2014 14:10:40 +0200 Subject: Begin to implement crc scheduler... (not working) --- .../imagemaster/serverconnection/CRCScheduler.java | 37 +++++++++++++++++++++- .../serverconnection/ImageProcessor.java | 33 +++++++++++++++---- .../serverconnection/UploadingImageInfos.java | 26 +++++++++++---- 3 files changed, 82 insertions(+), 14 deletions(-) (limited to 'src/main/java/org/openslx') 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 list = ImageProcessor.getImagesToCheck(); + Iterator iter = list.iterator(); + while ( iter.hasNext() ) { + UploadingImageInfos image = iter.next(); + List blocks = image.getNotCheckedBlocks(); + List 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 uploadingImages = new HashMap<>(); + + /** + * The UUIDs of the images that need to be checked by the crc checker. + */ + private static List 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 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 getImagesToCheck() + { + List result = new LinkedList<>(); + Iterator 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 lastSentBlocks = new LinkedList<>(); + private List 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 missingBlocks, String serverSessionId, Timestamp ts, String uuid) + protected UploadingImageInfos(String token, List 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 list ) + protected void addNotCheckedBlocks( List list ) { - this.lastSentBlocks = list; + this.notCheckedBlocks = list; } - protected List getLastSentBlocks() + protected List 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; + } } -- cgit v1.2.3-55-g7522