summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/serverconnection/UploadingImage.java
blob: fb3d3d9888a15f87af45b04d16f33f89c6a70aa8 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package org.openslx.imagemaster.serverconnection;

import java.io.IOException;
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 );

	/**
	 * 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 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
	/**
	 * Class for accessing the file (read blocks from it)
	 */
	private ImageFile imageFile = null;
	private CrcFile crcFile = null;

	protected UploadingImage( String uuid )
	{
		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 )
	{
		synchronized ( blockStatus ) {
			blockStatus[index] = VALID;
		}
	}

	protected void updateDb()
	{
		List<Integer> missingBlocks = new ArrayList<>();
		synchronized ( blockStatus ) {
			for ( int block = 0; block < blockStatus.length; block++ ) {
				if ( blockStatus[block] != VALID ) {
					missingBlocks.add( block );
				}
			}
		}
		dbImage.updateMissingBlocks( missingBlocks );
	}

	protected void setMissing( int index )
	{
		synchronized ( blockStatus ) {
			blockStatus[index] = MISSING;
		}
	}

	protected void setNeedsRequest( int index )
	{
		synchronized ( blockStatus ) {
			blockStatus[index] = Math.abs( blockStatus[index] ); // switch to positive value if needed
		}
	}

	protected void setNeedsCheck( int index )
	{
		synchronized ( blockStatus ) {
			if ( crcFile == null ) {
				blockStatus[index] = VALID;
			} else {
				blockStatus[index] = -Math.abs( blockStatus[index] ); // switch to negative value if needed
			}
		}
	}

	protected void increaseTransmittedTimes( int index )
	{
		synchronized ( blockStatus ) {
			if ( blockStatus[index] == VALID )
				return;
			blockStatus[index] += ( blockStatus[index] <= MISSING ) ? -1 : 1;	// increase in both directions
		}
	}

	protected int getTimesTransmitted( int index )
	{
		synchronized ( blockStatus ) {
			return Math.abs( blockStatus[index] );
		}
	}

	protected boolean needsRequest( int index )
	{
		synchronized ( blockStatus ) {
			return ( ( blockStatus[index] >= MISSING ) && ( blockStatus[index] != VALID ) );
		}
	}

	protected boolean needsCheck( int index )
	{
		synchronized ( blockStatus ) {
			return ( blockStatus[index] < MISSING );
		}
	}

	protected int getNumberOfBlocks()
	{
		return blockStatus.length;
	}

	protected int getNextMissingBlock()
	{
		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 ImageFile getImageFile()
	{
		if ( imageFile == null ) {
			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 )
	{
		if ( crcFile == null )
			return;
		if ( getCrcFile() == null && crcFile.isValid() ) {
			this.crcFile = crcFile;
			crcFile.writeCrcFile( dbImage.getAbsolutePath() + ".crc" );
		}
	}

	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: " + 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 );
	}
}