diff options
author | Nils Schwabe | 2014-07-04 10:20:04 +0200 |
---|---|---|
committer | Nils Schwabe | 2014-07-04 10:20:04 +0200 |
commit | 63af3084df7d6ea17defe104844a7298cda7b459 (patch) | |
tree | a27364921ff16c4fe2dc7ea9a9f7d424a10c1a64 /src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java | |
parent | Add implementation for the new up- and download protocoll (diff) | |
download | masterserver-63af3084df7d6ea17defe104844a7298cda7b459.tar.gz masterserver-63af3084df7d6ea17defe104844a7298cda7b459.tar.xz masterserver-63af3084df7d6ea17defe104844a7298cda7b459.zip |
Make use of the filetransfer classes
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java | 68 |
1 files changed, 39 insertions, 29 deletions
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java index d4ae717..c7ee973 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java @@ -14,6 +14,7 @@ import org.openslx.imagemaster.util.RandomString; public class ImageProcessor { + private static Logger log = Logger.getLogger( ImageProcessor.class ); /** @@ -22,14 +23,15 @@ public class ImageProcessor private static final int AMOUNT = 20; /** * The uploading images. - * Key: imageUUID, - * Value: imageInfos + * 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 @@ -38,9 +40,9 @@ public class ImageProcessor { // 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 ); @@ -52,19 +54,20 @@ public class ImageProcessor 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); + 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 + 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 ); - + String filepath = Globals.getImageDir() + "/" + uuid + ".vmdk"; + ConnectionHandler.addConnection( token, filepath ); + uploadingImages.put( uuid, new ImageInfos( token, allBlocks, serverSessionId, new Timestamp( System.currentTimeMillis() ) ) ); + DbImage.insert( imageData, System.currentTimeMillis(), token, allBlocks, serverSessionId, filepath ); + List<Integer> missing = getMissingBlocks( uuid, AMOUNT ); if ( missing.isEmpty() ) { uploadDone( uuid ); @@ -73,51 +76,58 @@ public class ImageProcessor 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 + * @return The missing blocks */ 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 ); + private static void uploadDone( String uuid ) + { + String token; + synchronized ( uploadingImages ) { + token = uploadingImages.remove( uuid ).getToken(); } + DbImage.updateMissingBlocks( uuid, null ); // file was already downloaded in the right location by the updownloader class. + // remove the connection so that it can be used by a new client + ConnectionHandler.removeConnection( token ); } - + /** * Checks pending uploads in database and adds them to process list again. */ - public static void checkUploading() { + 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); + for ( DbImage image : list ) { + String token = image.token; + ConnectionHandler.addConnection( token, image.imagePath ); + 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."); + log.info( "Added " + list.size() + " pending upload(s) to process list again." ); } } |