/
UploaderWorker.cs
88 lines (75 loc) · 2.63 KB
/
UploaderWorker.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Security.Cryptography;
using Amazon;
using Amazon.Glacier;
using Amazon.Glacier.Model;
using System.Diagnostics;
namespace Glacierizer
{
class UploaderWorker
{
private static short NUM_RETRIES = 3;
private GlacierAPIInterface api;
private TransferMetric metric;
private byte[] data;
private string checksum;
private long start;
private long end;
private string uploadId;
private long lastBytesCount;
public UploaderWorker(ref GlacierAPIInterface api, ref TransferMetric metric, ref byte[] data, string checksum, long start, long end, string uploadId)
{
this.api = api;
this.metric = metric;
this.start = start;
this.end = end;
this.uploadId = uploadId;
this.lastBytesCount = 0;
this.data = data;
this.checksum = checksum;
}
private void OnTransferProgress(Object sender, EventArgs e)
{
try
{
string tmp = e.ToString();
System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(tmp, "Bytes transferred: ([0-9]+)");
int numBytes = int.Parse(match.Groups[1].Value);
// Add the delta to the total then save the new byte count
metric.incrementTransferredBytes(numBytes - lastBytesCount);
lastBytesCount = numBytes;
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
}
public void Run()
{
short retries = 0;
while (retries++ < NUM_RETRIES)
{
try
{
GlacierFilePart part = new GlacierFilePart(ref data, checksum, start, end, uploadId);
api.UploadPart(part, OnTransferProgress);
metric.incrementTransferredParts();
return;
}
catch (Exception ex)
{
// Remove what we've uploaded so far from the metrics just to be precise.
metric.removeTransferredBytes(lastBytesCount);
// Reset the last byte count to zero or we're going to mess things up later.
lastBytesCount = 0;
Console.WriteLine("Upload exception: " + ex.Message);
}
}
}
}
}