summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/thrifthelper/TransferStatusWrapper.java
blob: bb84990a2fc01ffeb3b18ab563660e0bbdd27ef9 (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
package org.openslx.thrifthelper;

public class TransferStatusWrapper
{
	
	public static enum BlockStatus {
		COMPLETE, MISSING, UPLOADING, QUEUED_FOR_COPYING, COPYING, HASHING;
	}
	
	// 0 = complete, 1 = missing, 2 = uploading, 3 = queued for copying, 4 = copying, 5 = hashing (server side)
	private byte[] blocks = null;
	
	public TransferStatusWrapper(byte[] blocks) {
		this.blocks = blocks;
	}
	
	public void setBlocks(byte[] blocks) {
		this.blocks = blocks;
	}
	
	public boolean isComplete() {
		for (byte block : blocks) {
			if (block != 0)
				return false;
		}
		return true;
	}
	
	public float getComplete() {
		int done = 0;
		for (byte block : blocks) {
			if (block == 0 || block == 5) {
				done++;
			}
		}
		return ((float)done / (float)blocks.length);
	}
	
	public static class Progress {
		public float done;
		public float potentiallyDone;
	}
	
	/**
	 * Return completeness of transfer, guaranteed and estimated.
	 * .done will be what is known to be completed and good,
	 * .potentiallyDone will be what is queued for copying and still
	 * hashing, so can be considered and optimistic estimate.
	 * Values are returned as float between 0 and 1.
	 * .done + .potentiallyDone will never be > 1.
	 * @param progress array with length >= 2
	 */
	public void getCompleteEx(Progress progress) {
		int done = 0, potentialDone = 0;
		for (byte block : blocks) {
			if (block == 0) {
				done++;
			} else if (block == 3 || block == 4 || block == 5) {
				potentialDone++;
			}
		}
		progress.done = ((float)done / (float)blocks.length);
		progress.potentiallyDone = ((float)(done + potentialDone) / (float)blocks.length);
	}
	
	public float getPercentComplete() {
		 return getComplete() * 100f;
	}
	
	public byte[] getBlocks() {
		return this.blocks;
	}
	
	public boolean isEmpty() {
		return blocks == null || blocks.length == 0;
	}
	
	public int getBlockCount() {
		return blocks.length;
	}
	
	public BlockStatus get(int index) {
		switch (blocks[index]) {
		case 0:
			return BlockStatus.COMPLETE;
		case 1:
			return BlockStatus.MISSING;
		case 2:
			return BlockStatus.UPLOADING;
		case 3:
			return BlockStatus.QUEUED_FOR_COPYING;
		case 4:
			return BlockStatus.COPYING;
		}
		return null;
	}

}