diff options
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java | 172 |
1 files changed, 89 insertions, 83 deletions
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java b/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java index 169877c..78d5f2b 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java @@ -1,5 +1,6 @@ package org.openslx.imagemaster.serverconnection; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -10,16 +11,14 @@ import org.openslx.imagemaster.crcchecker.ImageFile; import org.openslx.imagemaster.db.DbImage; /** - * Helper class for ImageProcessor and ConnectionHandler to save some infos about the images in the process list. + * Helper class for ImageProcessor and ConnectionHandler to save some infos about the images in the + * process list. */ public class UploadingImage { public static final Logger log = Logger.getLogger( UploadingImage.class ); - /** - * Token for the satellite. - */ - private String token; + /** * The status list of the blocks. * x = 0 block is missing @@ -27,177 +26,164 @@ public class UploadingImage * x > 0 block is invalid and was transmitted x times (needs request) * x < 0 block is invalid and was transmitted x times (needs check) */ - private int[] blockStatus = null; - public static final int valid = 200; - public static final int missing = 0; - private long timestamp; // when did the server something for the last time + private final int[] blockStatus; + /** + * Remember last position in blockStatus array that was returned, so we don't always + * iterate from the beginning. + */ + private int lastStatusPos = 0; + + public static final int VALID = 200; + public static final int MISSING = 0; + private DbImage dbImage = null; // the DB representation of this image - private String uuid; - private String filename; + /** + * Class for accessing the file (read blocks from it) + */ private ImageFile imageFile = null; private CrcFile crcFile = null; - protected UploadingImage(String token, int[] initialBlockStatus, long timestamp, String uuid, String filename) + protected UploadingImage( String uuid ) { - this.token = token; - this.timestamp = timestamp; - this.uuid = uuid; - this.blockStatus = initialBlockStatus; - this.filename = filename; + this.dbImage = DbImage.getImageByUuid( uuid ); + if ( this.dbImage == null ) + throw new RuntimeException( "Unknown image " + uuid + " on UploadingImage creation" ); + this.blockStatus = this.dbImage.blockStatus; } protected void setValid( int index ) { - if ( blockStatus == null ) - return; synchronized ( blockStatus ) { - blockStatus[index] = valid; + blockStatus[index] = VALID; } } protected void updateDb() { - if ( blockStatus == null ) - return; - List<Integer> missingBlocks = new ArrayList<>(); - synchronized ( blockStatus ) { for ( int block = 0; block < blockStatus.length; block++ ) { - if ( blockStatus[block] != valid ) { + if ( blockStatus[block] != VALID ) { missingBlocks.add( block ); } } } - getDbImage().updateMissingBlocks( missingBlocks ); + dbImage.updateMissingBlocks( missingBlocks ); } protected void setMissing( int index ) { - if ( blockStatus == null ) - return; synchronized ( blockStatus ) { - blockStatus[index] = missing; + blockStatus[index] = MISSING; } } protected void setNeedsRequest( int index ) { - if ( blockStatus == null ) - return; synchronized ( blockStatus ) { - blockStatus[index] *= ( blockStatus[index] < missing ) ? -1 : 1; // switch to positive value if needed + blockStatus[index] = Math.abs( blockStatus[index] ); // switch to positive value if needed } } protected void setNeedsCheck( int index ) { - if ( blockStatus == null ) - return; synchronized ( blockStatus ) { - blockStatus[index] *= ( blockStatus[index] > missing ) ? -1 : 1; // switch to negative value if needed + blockStatus[index] = -Math.abs( blockStatus[index] ); // switch to negative value if needed } } protected void increaseTransmittedTimes( int index ) { - if ( blockStatus == null || blockStatus[index] == 200 ) - return; synchronized ( blockStatus ) { - blockStatus[index] += ( blockStatus[index] <= missing ) ? -1 : 1; // increase in both directions + if ( blockStatus[index] == 200 ) + return; + blockStatus[index] += ( blockStatus[index] <= MISSING ) ? -1 : 1; // increase in both directions } } protected int getTimesTransmitted( int index ) { synchronized ( blockStatus ) { - return ( blockStatus[index] > 0 ) ? blockStatus[index] : ( -1 ) * blockStatus[index]; + return Math.abs( blockStatus[index] ); } } - protected String getToken() - { - return this.token; - } - protected boolean needsRequest( int index ) { - if ( blockStatus == null ) - return false; synchronized ( blockStatus ) { - return ( ( blockStatus[index] >= missing ) && ( blockStatus[index] != valid ) ); + return ( ( blockStatus[index] >= MISSING ) && ( blockStatus[index] != VALID ) ); } } protected boolean needsCheck( int index ) { - if ( blockStatus == null ) - return false; synchronized ( blockStatus ) { - return ( blockStatus[index] < missing ); + return ( blockStatus[index] < MISSING ); } } protected int getNumberOfBlocks() { - /////////////////////////////////////////////////////////////////// -// ArrayList<Integer> l = new ArrayList<Integer>( blockStatus.length ); -// for ( int i : blockStatus ) { -// l.add( i ); -// } -// log.debug( l ); - /////////////////////////////////////////////////////////////////// return blockStatus.length; } - + protected int getNextMissingBlock() { - // TODO: handle intern status of current uploading block. - for (int i = 0; i < blockStatus.length; i++) { - if (blockStatus[i] == 0) - return i; + synchronized ( blockStatus ) { + for ( int i = 0; i < blockStatus.length; i++ ) { + int index = ( i + lastStatusPos ) % blockStatus.length; + if ( blockStatus[index] == MISSING ) + return lastStatusPos = index; + } + for ( int index = 0; index < blockStatus.length; index++ ) { + if ( blockStatus[index] > MISSING && blockStatus[index] < VALID ) + return lastStatusPos = index; + } + for ( int index = 0; index < blockStatus.length; index++ ) { + if ( blockStatus[index] < MISSING ) + return lastStatusPos = index; + } } return -1; } + /* protected long getTimestamp() { return this.timestamp; } - - protected DbImage getDbImage() - { - if ( dbImage == null ) { - dbImage = DbImage.getImageByUuid( this.uuid ); - } - return this.dbImage; - } - - protected void setDbImage( DbImage dbImage ) - { - if ( dbImage != null ) { - return; - } else { - this.dbImage = dbImage; - } - } + */ protected ImageFile getImageFile() { if ( imageFile == null ) { - imageFile = new ImageFile( filename, Globals.blockSize ); + imageFile = new ImageFile( dbImage.getAbsolutePath(), Globals.blockSize ); } return imageFile; } protected CrcFile getCrcFile() { + if ( crcFile == null ) { + try { + crcFile = new CrcFile( dbImage.getAbsolutePath() + ".crc" ); + } catch ( IOException e ) { + // Not found... return null + } + } return crcFile; } protected void setCrcFile( CrcFile crcFile ) { - this.crcFile = crcFile; + if ( getCrcFile() == null ) { + this.crcFile = crcFile; + try { + crcFile.writeCrcFile( dbImage.getAbsolutePath() + ".crc" ); + } catch ( IOException e ) { + log.error( "Could not write crc list to file", e ); + } + } } public int getAmountOfBlocksNeedingRequest() @@ -228,8 +214,28 @@ public class UploadingImage @Override public String toString() { - return "UUID: " + uuid + ", filename " + filename + "\nmissing blocks " + getAmountOfBlocksNeedingRequest() + - ", number of blocks " + getNumberOfBlocks() + ", token " + getToken(); + return "UUID: " + dbImage.uuid + ", filename " + dbImage.imagePath + "\nmissing blocks " + getAmountOfBlocksNeedingRequest() + + ", number of blocks " + getNumberOfBlocks(); + + } + public String getAbsolutePath() + { + return dbImage.getAbsolutePath(); + } + + public long getFileSize() + { + return dbImage.fileSize; + } + + public String getUuid() + { + return dbImage.uuid; + } + + public void updateMissingBlocks( List<Integer> missingBlocks ) + { + dbImage.updateMissingBlocks( missingBlocks ); } } |