diff options
author | Nils Schwabe | 2014-07-04 12:50:59 +0200 |
---|---|---|
committer | Nils Schwabe | 2014-07-04 12:50:59 +0200 |
commit | d1ab55e39b392e62ca1f1e41e3c70cf48d676885 (patch) | |
tree | 2fcf410f3a7ff4a3acae9ad5502fc933f8887e2c /src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java | |
parent | Fix that Globals are initialized before servers are starting up (diff) | |
download | masterserver-d1ab55e39b392e62ca1f1e41e3c70cf48d676885.tar.gz masterserver-d1ab55e39b392e62ca1f1e41e3c70cf48d676885.tar.xz masterserver-d1ab55e39b392e62ca1f1e41e3c70cf48d676885.zip |
Change handling of connections and add support for download
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java | 88 |
1 files changed, 80 insertions, 8 deletions
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java index 71ef97b..db3702d 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java @@ -8,10 +8,15 @@ 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.DownloadInfos; import org.openslx.imagemaster.thrift.iface.ImageData; import org.openslx.imagemaster.thrift.iface.UploadInfos; import org.openslx.imagemaster.util.RandomString; +/** + * Processing the up- and download of images. + * Handles who is authorized and knows which blocks are missing / need to be sent + */ public class ImageProcessor { @@ -24,9 +29,16 @@ public class ImageProcessor /** * The uploading images. * Key: imageUUID, - * Value: imageInfos + * Value: uploadingImageInfos */ - private static HashMap<String, ImageInfos> uploadingImages = new HashMap<>(); + private static HashMap<String, UploadingImageInfos> uploadingImages = new HashMap<>(); + + /** + * The downloading clients. + * Key: serverSessionId + * Value: downloadingClientInfos + */ + private static HashMap<String, DownloadingClientInfos> downloadingClients = new HashMap<>(); /** * Checks if this image is already uploading and returns a new list with missing blocks if so. @@ -49,13 +61,13 @@ public class ImageProcessor if ( missing.isEmpty() ) { String token = uploadingImages.get( uuid ).getToken(); uploadDone( uuid ); - return new UploadInfos( token, missing ); + return new UploadInfos( null, missing ); } uploadingImages.get( uuid ).setLastSentBlocks( missing ); return new UploadInfos( uploadingImages.get( uuid ).getToken(), missing ); } - // insert new image and generate token + // insert new image and start listener synchronized ( uploadingImages ) { int nBlocks = (int)Math.ceil( imageData.fileSize / Globals.blockSize ); List<Integer> allBlocks = new LinkedList<>(); @@ -64,11 +76,11 @@ public class ImageProcessor } String token = RandomString.generate( 100, false ); String filepath = Globals.getImageDir() + "/" + uuid + ".vmdk"; - ConnectionHandler.addConnection( token, filepath ); + ConnectionHandler.addConnection( token, filepath, ConnectionData.UPLOADING ); // 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 ImageInfos( token, allBlocks, serverSessionId, new Timestamp( System.currentTimeMillis() ) ) ); + uploadingImages.put( uuid, new UploadingImageInfos( token, allBlocks, serverSessionId, new Timestamp( System.currentTimeMillis() ) ) ); DbImage.insert( imageData, System.currentTimeMillis(), token, allBlocks, serverSessionId, filepath ); List<Integer> missing = getMissingBlocks( uuid, AMOUNT ); @@ -81,6 +93,62 @@ public class ImageProcessor } } + public static DownloadInfos getDownloadInfos( String serverSessionId, String uuid, List<Integer> requestedBlocks ) + { + // check if server is already downloading + if ( downloadingClients.containsKey( serverSessionId ) ) { + DownloadingClientInfos client = downloadingClients.get( serverSessionId ); + + // remove download if done + if ( requestedBlocks.isEmpty() ) + { + downloadDone( serverSessionId, uuid ); + return new DownloadInfos(); + } + + if ( client.isDownloading( uuid ) ) + { + // client was downloading this image + // update the requested blocks + client.requestBlocks( uuid, requestedBlocks ); + return new DownloadInfos( client.getToken( uuid ) ); + } + + // server was downloading another image and now gets a new connection for this new download + String token = RandomString.generate( 100, false ); + String filepath = DbImage.getImageByUUID( uuid ).imagePath; + client.addDownload( uuid, requestedBlocks, token ); + + downloadingClients.put( serverSessionId, client ); + ConnectionHandler.addConnection( token, filepath, ConnectionData.DOWNLOADING ); + return new DownloadInfos( token ); + } + + // insert new client and start listener + synchronized ( downloadingClients ) { + DownloadingClientInfos client = new DownloadingClientInfos(); + String token = RandomString.generate( 100, false ); + String filepath = DbImage.getImageByUUID( uuid ).imagePath; + client.addDownload( uuid, requestedBlocks, token ); + + downloadingClients.put( serverSessionId, client ); + ConnectionHandler.addConnection( token, filepath, ConnectionData.DOWNLOADING ); + return new DownloadInfos( token ); + } + } + + private static void downloadDone( String serverSessionId, String uuid ) + { + synchronized ( downloadingClients ) { + DownloadingClientInfos client = downloadingClients.get( serverSessionId ); + client.removeDownload( uuid ); + ConnectionHandler.removeConnection( client.getToken( uuid ) ); + if ( !client.hasDownloads() ) { + downloadingClients.remove( serverSessionId ); + } + } + } + /** * Returns a specified number of missing blocks. * @@ -128,10 +196,14 @@ public class ImageProcessor List<DbImage> list = DbImage.getUploadingImages(); for ( DbImage image : list ) { String token = image.token; - ConnectionHandler.addConnection( token, image.imagePath ); - ImageInfos infos = new ImageInfos( token, image.missingBlocks, image.serverSessionId, image.timestamp ); + ConnectionHandler.addConnection( token, image.imagePath, ConnectionData.UPLOADING ); + UploadingImageInfos infos = new UploadingImageInfos( token, image.missingBlocks, image.serverSessionId, image.timestamp ); uploadingImages.put( image.UUID, infos ); } log.info( "Added " + list.size() + " pending upload(s) to process list again." ); } + + public static void init() + { + } } |