-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetUpload.cs
155 lines (135 loc) · 5.34 KB
/
NetUpload.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
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
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using log4net;
using Newtonsoft.Json;
namespace crash
{
public class NetUploadArgs
{
public string Hostname { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
class NetUpload
{
ILog Log = null;
private volatile bool running, active;
private int failures;
private NetUploadArgs args = new NetUploadArgs();
ConcurrentQueue<Spectrum> sendq = new ConcurrentQueue<Spectrum>();
public NetUpload(ILog log, ref ConcurrentQueue<Spectrum> sendQueue)
{
Log = log;
Log.Info("Creating Net Uploader");
running = true;
active = false;
failures = 0;
sendQueue = sendq;
}
public void DoWork()
{
while (running)
{
if (active)
{
// Upload spectrums to server
while (sendq.Count > 0)
{
Spectrum spec;
if (sendq.TryDequeue(out spec))
{
if (String.IsNullOrEmpty(args.Hostname))
continue;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args.Hostname + "/spectrums");
string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(args.Username + ":" + args.Password));
request.Headers.Add("Authorization", "Basic " + credentials);
request.Timeout = 8000;
request.Method = WebRequestMethods.Http.Post;
request.Accept = "application/json";
APISpectrum apiSpec = new APISpectrum(spec);
Log.Info("Sending " + apiSpec.SessionName + ":" + apiSpec.SessionIndex);
var jsonRequest = JsonConvert.SerializeObject(apiSpec);
var sendData = Encoding.UTF8.GetBytes(jsonRequest);
request.ContentType = "application/json";
request.ContentLength = sendData.Length;
using (var stream = request.GetRequestStream())
stream.Write(sendData, 0, sendData.Length);
string recvData;
HttpStatusCode code = Utils.GetResponseData(request, out recvData);
if (code == HttpStatusCode.OK)
{
failures = 0;
Log.Info(apiSpec.SessionName + ":" + apiSpec.SessionIndex + " uploaded successfully");
}
else if (code == HttpStatusCode.RequestTimeout)
{
failures++;
Log.Error("Request timeout");
if(failures > 8)
{
Log.Error("Request timeout has happened 8 times. Giving up uploading");
Deactivate();
}
}
else
{
failures++;
Log.Error(code.ToString() + ": " + recvData);
if (failures > 8)
{
Log.Error("Upload request has failed 8 times. Giving up uploading");
Deactivate();
}
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
}
}
}
else
{
while (sendq.Count > 0)
{
Spectrum spec;
sendq.TryDequeue(out spec);
}
}
Thread.Sleep(50);
}
}
public void Activate(NetUploadArgs a)
{
args.Hostname = a.Hostname;
args.Username = a.Username;
args.Password = a.Password;
active = true;
failures = 0;
}
public void Deactivate()
{
args.Hostname = String.Empty;
args.Username = String.Empty;
args.Password = String.Empty;
active = false;
}
public void RequestStop()
{
running = false;
}
public bool IsRunning()
{
return running;
}
}
}