summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java
blob: b0b035bb277c219672699d45b46290f85e25d063 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                                 


                           
                               
                                       
                                                  
                                                    

                                          
   
                                                                                                                 
   
                           
 
 
                                                                                  




                                   




                                                                             
           


                                            
                                                                                  

                                                                              
                                
                                           
                                       
 
                                                                                                                      

                                   
                                           
                                 
                                                      
                                         

         
                                            
         

                                          
                                              

                                                   
         
 



                                          








                                                                                    
                 
                                                                  
         
 
                                              
         

                                          
                                              

                                                     
         
 
                                                   
         

                                          
                                              

                                                                                                                                     
         
 



                                                 
                                              

                                                                                                                                     
         
 
                                                            
         
                                                                       
                               
                                              

                                                                                                                              

         

                                                      

                                                                                                             
                 

         




                                   



                                                   
                                              

                                                                                                        





                                                 
                                              

                                                                


                                         
         
                                                                                   




                                                                                    
                                                                                   
                                          

         
                                     
         
                                      
         



                                        
                                                                      


                                    
 








                                                    







                                                                                 
                                      
         
                               
         
 
                                                    
         
                                       
         
 
                                                    
         


                                          

                                                                
                                         
                 
                              
         
 



                                          




                                                     










                                                                                                                             
 
package org.openslx.imagemaster.serverconnection;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.openslx.imagemaster.Globals;
import org.openslx.imagemaster.crcchecker.CrcFile;
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.
 */
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 long timestamp; // 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 ImageFile imageFile = null;
	private CrcFile crcFile = null;

	protected UploadingImage(String token, int[] initialBlockStatus, long timestamp, String uuid, String filename)
	{
		this.token = token;
		this.timestamp = timestamp;
		this.uuid = uuid;
		this.blockStatus = initialBlockStatus;
		this.filename = filename;
	}

	protected void setValid( int index )
	{
		if ( blockStatus == null )
			return;
		synchronized ( blockStatus ) {
			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 ) {
					missingBlocks.add( block );
				}
			}
		}
		getDbImage().updateMissingBlocks( missingBlocks );
	}

	protected void setMissing( int index )
	{
		if ( blockStatus == null )
			return;
		synchronized ( blockStatus ) {
			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
		}
	}

	protected void setNeedsCheck( int index )
	{
		if ( blockStatus == null )
			return;
		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;
		synchronized ( blockStatus ) {
			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];
		}
	}

	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 ) );
		}
	}

	protected boolean needsCheck( int index )
	{
		if ( blockStatus == null )
			return false;
		synchronized ( blockStatus ) {
			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 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 );
		}
		return imageFile;
	}

	protected CrcFile getCrcFile()
	{
		return crcFile;
	}

	protected void setCrcFile( CrcFile crcFile )
	{
		this.crcFile = crcFile;
	}

	public int getAmountOfBlocksNeedingRequest()
	{
		if ( blockStatus == null )
			return 0;
		int result = 0;
		for ( int i = 0; i < blockStatus.length; i++ ) {
			if ( needsRequest( i ) )
				result++;
		}
		return result;
	}

	public boolean allBlocksValid()
	{
		if ( blockStatus == null )
			return false;
		synchronized ( blockStatus ) {
			for ( int i : blockStatus ) {
				if ( i != 200 )
					return false;
			}
		}
		return true;
	}

	@Override
	public String toString()
	{
		return "UUID: " + uuid + ", filename " + filename + "\nmissing blocks " + getAmountOfBlocksNeedingRequest() +
				", number of blocks " + getNumberOfBlocks() + ", token " + getToken();

	}
}