package org.openslx.imagemaster.serverconnection; import java.sql.Timestamp; import org.apache.log4j.Logger; import org.openslx.imagemaster.db.DbImage; /** * Helper class for ImageProcessor 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 * x = 200 block arrived and is valid * 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 Timestamp ts; // when did the server something for the last time private DbImage dbImage = null; // the DB representation of this image private String uuid; private String filename; private String crcFilename; protected UploadingImage(String token, int[] initialBlockStatus, Timestamp ts, String uuid, String filename, String crcFilename) { this.token = token; this.ts = ts; this.uuid = uuid; this.crcFilename = crcFilename; this.blockStatus = initialBlockStatus; } protected void setValid( int index ) { if ( blockStatus == null ) return; blockStatus[index] = valid; } protected void setMissing( int index ) { if ( blockStatus == null ) return; 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 } protected void setNeedsCheck( int index ) { if ( blockStatus == null ) return; blockStatus[index] *= ( blockStatus[index] > missing ) ? -1 : 1; // switch to negative value if needed } protected void increaseTransmittedTimes( int index ) { if ( blockStatus == null || blockStatus[index] == 0 || blockStatus[index] == 200 ) return; blockStatus[index] += ( blockStatus[index] < missing ) ? -1 : 1; // increase in both directions } protected String getToken() { return this.token; } protected boolean needsRequest( int index ) { if ( blockStatus == null ) return false; return ( blockStatus[index] > missing && blockStatus[index] != valid ); } protected boolean needsCheck( int index ) { if ( blockStatus == null ) return false; return ( blockStatus[index] < missing ); } protected int getNumberOfBlocks() { return blockStatus.length; } protected Timestamp getTimestamp() { return this.ts; } protected DbImage getDbImage() { if ( dbImage == null ) { dbImage = DbImage.getImageByUUID( this.uuid ); } return this.dbImage; } protected String getFilename() { return this.filename; } protected String getCrcFilename() { return this.crcFilename; } public int getAmountOfMissingBlocks() { if ( blockStatus == null ) return 0; int result = 0; for ( int i : blockStatus ) { if ( blockStatus[i] == missing ) result++; } return result; } }