diff options
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/serverconnection')
| -rw-r--r-- | src/main/java/org/openslx/imagemaster/serverconnection/ImageInfos.java | 73 | ||||
| -rw-r--r-- | src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java | 123 |
2 files changed, 196 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ImageInfos.java b/src/main/java/org/openslx/imagemaster/serverconnection/ImageInfos.java new file mode 100644 index 0000000..1e84978 --- /dev/null +++ b/src/main/java/org/openslx/imagemaster/serverconnection/ImageInfos.java @@ -0,0 +1,73 @@ +package org.openslx.imagemaster.serverconnection; + +import java.sql.Timestamp; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +/** + * Helper class for ImageProcessor to save some infos about the images in the process list. + */ +public class ImageInfos +{ + /** + * Token for the satellite. + */ + private String token; + /** + * The missing blocks that need to be uploaded by the satellite. + */ + private List<Integer> missingBlocks; + /** + * 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 String serverSessionId; + private Timestamp ts; // when did the server something for the last time + + protected ImageInfos(String token, List<Integer> missingBlocks, String serverSessionId, Timestamp ts) + { + this.token = token; + this.missingBlocks = missingBlocks; + this.serverSessionId = serverSessionId; + this.ts = ts; + } + + protected void removeBlock( int number ) + { + this.missingBlocks.remove( number ); + } + + protected void removeBlocks( Collection<Integer> list ) + { + this.missingBlocks.removeAll( list ); + } + + protected void setLastSentBlocks(List<Integer> list) { + this.lastSentBlocks = list; + } + + protected List<Integer> getLastSentBlocks() { + return this.lastSentBlocks; + } + + protected String getToken() + { + return this.token; + } + + protected List<Integer> getMissingBlocks() + { + return this.missingBlocks; + } + + protected String getServerSessionId() + { + return this.serverSessionId; + } + + protected Timestamp getTimestamp() { + return this.ts; + } +} diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java new file mode 100644 index 0000000..d4ae717 --- /dev/null +++ b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java @@ -0,0 +1,123 @@ +package org.openslx.imagemaster.serverconnection; + +import java.sql.Timestamp; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; + +import org.apache.log4j.Logger; +import org.openslx.imagemaster.Globals; +import org.openslx.imagemaster.db.DbImage; +import org.openslx.imagemaster.thrift.iface.ImageData; +import org.openslx.imagemaster.thrift.iface.UploadInfos; +import org.openslx.imagemaster.util.RandomString; + +public class ImageProcessor +{ + private static Logger log = Logger.getLogger( ImageProcessor.class ); + + /** + * The amount of blocks that is return in UploadInfos (after request of satellite) + */ + private static final int AMOUNT = 20; + /** + * The uploading images. + * Key: imageUUID, + * Value: imageInfos + */ + private static HashMap<String, ImageInfos> uploadingImages = new HashMap<>(); + + /** + * Checks if this image is already uploading and returns a new list with missing blocks if so. + * Puts the new image into processing list else. + * @param serverSessionId The uploading server + * @param imageData The data of the image + * @return + */ + public static UploadInfos getUploadInfos( String serverSessionId, ImageData imageData ) + { + // check image data + // TODO: do security checks + + String uuid = imageData.uuid; + + // check if image is already uploading + if ( uploadingImages.containsKey( uuid ) ) { + List<Integer> missing = getMissingBlocks( uuid, AMOUNT ); + if ( missing.isEmpty() ) { + String token = uploadingImages.get( uuid ).getToken(); + uploadDone( uuid ); + return new UploadInfos( token, missing ); + } + uploadingImages.get( uuid ).setLastSentBlocks( missing ); + return new UploadInfos( uploadingImages.get( uuid ).getToken(), missing ); + } + + // insert new image and generate token + synchronized (uploadingImages) { + int nBlocks = (int)Math.ceil(imageData.fileSize / Globals.blockSize); + List<Integer> allBlocks = new LinkedList<>(); + for (int i = 0; i < nBlocks; i++) { // fill empty list with all block numbers + allBlocks.add( i ); + } + String token = RandomString.generate( 100, false ); + // TODO: init updownloader class + uploadingImages.put( uuid, new ImageInfos(token, allBlocks, serverSessionId, new Timestamp(System.currentTimeMillis())) ); + DbImage.insert( imageData, System.currentTimeMillis(), token, allBlocks, serverSessionId ); + + List<Integer> missing = getMissingBlocks( uuid, AMOUNT ); + if ( missing.isEmpty() ) { + uploadDone( uuid ); + } + uploadingImages.get( uuid ).setLastSentBlocks( missing ); + return new UploadInfos( token, missing ); + } + } + + /** + * Returns a specified number of missing blocks. + * @param imageUUID The image of which you want to get the missing blocks from + * @param amount The amount of blocks that you want to get + * @return The missing blocksht + */ + private static List<Integer> getMissingBlocks( String imageUUID, int amount ) + { + List<Integer> list = uploadingImages.get( imageUUID ).getMissingBlocks(); + List<Integer> result = new LinkedList<>(); + + if ( amount > list.size() ) + amount = list.size(); + + for ( int i = 0; i < amount; i++ ) { + result.add( list.get( i ) ); + } + return result; + } + + /** + * Is triggered when an upload of an image is done. + * Removes image from process list, updates db entry and moves file on hard drive. + * @param uuid + */ + private static void uploadDone( String uuid ) { + synchronized(uploadingImages) { + uploadingImages.remove( uuid ); + DbImage.updateMissingBlocks( uuid, null ); + } + // file was already downloaded in the right location by the updownloader class. + } + + /** + * Checks pending uploads in database and adds them to process list again. + */ + public static void checkUploading() { + List<DbImage> list = DbImage.getUploadingImages(); + for (DbImage image : list) { + // TODO: init updownloader class + String token = RandomString.generate( 100, false ); + ImageInfos infos = new ImageInfos(token, image.missingBlocks, image.serverSessionId, image.timestamp); + uploadingImages.put( image.UUID, infos); + } + log.info("Added " + list.size() + " pending upload(s) to process list again."); + } +} |
