diff options
author | Nils Schwabe | 2014-07-23 16:06:47 +0200 |
---|---|---|
committer | Nils Schwabe | 2014-07-23 16:06:47 +0200 |
commit | a956b282692a5d84fd7efac1b1018cccf41653bf (patch) | |
tree | 7b595a24a0a00a28e252d7dbd9b434ad59d9d8c3 /src | |
parent | Fix some bugs while testing the connection (diff) | |
download | masterserver-a956b282692a5d84fd7efac1b1018cccf41653bf.tar.gz masterserver-a956b282692a5d84fd7efac1b1018cccf41653bf.tar.xz masterserver-a956b282692a5d84fd7efac1b1018cccf41653bf.zip |
Add "synchronized" to UploadingImage
Add method to calc the amount of blocks to Util
Diffstat (limited to 'src')
5 files changed, 57 insertions, 27 deletions
diff --git a/src/main/java/org/openslx/imagemaster/db/DbImage.java b/src/main/java/org/openslx/imagemaster/db/DbImage.java index 386c509..099738f 100644 --- a/src/main/java/org/openslx/imagemaster/db/DbImage.java +++ b/src/main/java/org/openslx/imagemaster/db/DbImage.java @@ -8,6 +8,7 @@ import java.util.List; import org.openslx.imagemaster.Globals; import org.openslx.imagemaster.serverconnection.UploadingImage; import org.openslx.imagemaster.thrift.iface.ImageData; +import org.openslx.imagemaster.util.Util; public class DbImage { @@ -73,11 +74,11 @@ public class DbImage this.token = token; String[] parts = missingBlocksList.split( ";" ); - blockStatus = new int[ (int)Math.ceil( this.fileSize / Globals.blockSize ) ]; // initialize array to ones + blockStatus = new int[ Util.getNumberOfBlocks( fileSize, Globals.blockSize ) ]; // initialize array to ones for ( int i : blockStatus ) { blockStatus[i] = UploadingImage.valid; } - for ( int i = 0; i < parts.length; i++ ) { // do not copy the last empty string (1;2;3;) -> "1","2","3","" + for ( int i = 0; i < parts.length; i++ ) { blockStatus[i] = UploadingImage.missing; } diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java b/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java index c47eb04..c76fb79 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/CRCScheduler.java @@ -55,9 +55,7 @@ public class CRCScheduler extends TimerTask Timer timer = new Timer( "CRCScheduler" ); // start now and fire every 60 s - //timer.schedule( new CRCScheduler(), 0, 60000L ); - - timer.schedule( new CRCScheduler(), 0L, 30000L ); + timer.schedule( new CRCScheduler(), 0, 60000L ); } } diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java index a27d543..b872b25 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java @@ -22,6 +22,7 @@ import org.openslx.imagemaster.thrift.iface.UploadError; import org.openslx.imagemaster.thrift.iface.UploadException; import org.openslx.imagemaster.thrift.iface.UploadInfos; import org.openslx.imagemaster.util.RandomString; +import org.openslx.imagemaster.util.Util; /** * Processing the up- and download of images. @@ -109,8 +110,8 @@ public class ImageProcessor throw new UploadException( UploadError.INVALID_CRC, "CRC sums were invalid." ); filepath = Globals.getImageDir() + "/" + uuid + ".vmdk"; token = RandomString.generate( 100, false ); - nBlocks = crcSums.size() - 1; - allBlocks = new int[ nBlocks ]; // initalize array with all zeros which mean that this block is missing + nBlocks = Util.getNumberOfBlocks( imageData.fileSize, Globals.blockSize ); + allBlocks = new int[ nBlocks ]; // initialize array with all zeros which mean that this block is missing image = new UploadingImage( token, allBlocks, System.currentTimeMillis(), uuid, filepath ); uploadingImages.put( uuid, image ); } diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java b/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java index 8cbaecf..feb2a1e 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java @@ -50,7 +50,9 @@ public class UploadingImage { if ( blockStatus == null ) return; - blockStatus[index] = valid; + synchronized(blockStatus) { + blockStatus[index] = valid; + } } protected void updateDb() @@ -60,10 +62,12 @@ public class UploadingImage List<Integer> missingBlocks = new ArrayList<>(); - for (int block = 0; block < blockStatus.length; block++) { - if (blockStatus[block] != valid) { - missingBlocks.add( block ); - } + synchronized(blockStatus) { + for (int block = 0; block < blockStatus.length; block++) { + if (blockStatus[block] != valid) { + missingBlocks.add( block ); + } + } } dbImage.updateMissingBlocks( missingBlocks ); } @@ -72,33 +76,43 @@ public class UploadingImage { if ( blockStatus == null ) return; - blockStatus[index] = missing; + synchronized(blockStatus) { + blockStatus[index] = missing; + } } protected void setNeedsRequest( int index ) { if ( blockStatus == null ) return; - blockStatus[index] *= ( blockStatus[index] < missing ) ? -1 : 1; // switch to positive value if needed + synchronized(blockStatus) { + blockStatus[index] *= ( blockStatus[index] < missing ) ? -1 : 1; // switch to positive value if needed + } } protected void setNeedsCheck( int index ) { if ( blockStatus == null ) return; - blockStatus[index] *= ( blockStatus[index] > missing ) ? -1 : 1; // switch to negative value if needed + synchronized(blockStatus) { + blockStatus[index] *= ( blockStatus[index] > missing ) ? -1 : 1; // switch to negative value if needed + } } protected void increaseTransmittedTimes( int index ) { if ( blockStatus == null || blockStatus[index] == 200 ) return; - blockStatus[index] += ( blockStatus[index] <= missing ) ? -1 : 1; // increase in both directions + synchronized(blockStatus) { + blockStatus[index] += ( blockStatus[index] <= missing ) ? -1 : 1; // increase in both directions + } } protected int getTimesTransmitted( int index ) { - return Math.abs( blockStatus[index] ); + synchronized(blockStatus) { + return Math.abs( blockStatus[index] ); + } } protected String getToken() @@ -110,14 +124,18 @@ public class UploadingImage { if ( blockStatus == null ) return false; - return ( ( blockStatus[index] >= missing ) && ( blockStatus[index] != valid ) ); + synchronized(blockStatus) { + return ( ( blockStatus[index] >= missing ) && ( blockStatus[index] != valid ) ); + } } protected boolean needsCheck( int index ) { if ( blockStatus == null ) return false; - return ( blockStatus[index] < missing ); + synchronized(blockStatus) { + return ( blockStatus[index] < missing ); + } } protected int getNumberOfBlocks() @@ -179,9 +197,11 @@ public class UploadingImage { if ( blockStatus == null ) return false; - for ( int i : blockStatus ) { - if ( i != 200 ) - return false; + synchronized(blockStatus) { + for ( int i : blockStatus ) { + if ( i != 200 ) + return false; + } } return true; } diff --git a/src/main/java/org/openslx/imagemaster/util/Util.java b/src/main/java/org/openslx/imagemaster/util/Util.java index 9d5c5fb..df7cea4 100644 --- a/src/main/java/org/openslx/imagemaster/util/Util.java +++ b/src/main/java/org/openslx/imagemaster/util/Util.java @@ -32,7 +32,7 @@ public class Util System.exit( 2 ); } } - + /** * Check if String is null or empty, abort program if so. * An optional message to be printed can be passed. A stack trace @@ -42,7 +42,7 @@ public class Util * @param something The string you want to check * @param message The message to be printed if "something" is null or empty */ - public static void notNullOrEmptyFatal( String something, String message) + public static void notNullOrEmptyFatal( String something, String message ) { if ( something == null || something.isEmpty() ) { if ( message != null ) @@ -92,15 +92,25 @@ public class Util /** * Tries to parse an int. Returns 0 on error. + * * @param s The strig to parse * @return The parsed int or 0 on error */ - public static int tryToParseInt(String s) { + public static int tryToParseInt( String s ) + { try { return Integer.parseInt( s ); - } catch (NumberFormatException e) { + } catch ( NumberFormatException e ) { return 0; } } - + + public static int getNumberOfBlocks( long fileSize, int blockSize ) + { + int blocks = (int) ( fileSize / blockSize ); + if ( fileSize % blockSize != 0 ) + blocks++; + return blocks; + } + } |