summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java
blob: dc15dae156930e74244444fd1bba327022bc5b33 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package org.openslx.imagemaster.serverconnection;

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

	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 int getTimesTransmitted( int index )
	{
		return Math.abs( blockStatus[index] );
	}

	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 long getTimestamp()
	{
		return this.timestamp;
	}

	protected DbImage getDbImage()
	{
		if ( dbImage == null ) {
			dbImage = DbImage.getImageByUUID( this.uuid );
		}
		return this.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 getAmountOfMissingBlocks()
	{
		if ( blockStatus == null )
			return 0;
		int result = 0;
		for ( int i : blockStatus ) {
			if ( blockStatus[i] == missing )
				result++;
		}
		return result;
	}
}