Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/tools/compilembed/mbedonlinecompile.cs

raw
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Net;
6 using System.Text;
7 using System.Threading.Tasks;
8 using System.Web;
9 using System.Web.Script.Serialization;
10
11 namespace compilembed
12 {
13 public class CompileMessage
14 {
15 public string action { get; set; }
16 public string severity { get; set; }
17 public string type { get; set; }
18 public double percent { get; set; }
19 public object file { get; set; }
20 public string line { get; set; }
21 public string text { get; set; }
22 public string message { get; set; }
23 }
24
25 public class CompileResultData
26 {
27 public bool task_complete { get; set; }
28 public string task_status { get; set; }
29 public string task_id { get; set; }
30 public CompileMessage[] new_messages { get; set; }
31 public bool compilation_success { get; set; }
32 public string binary { get; set; }
33 public string program { get; set; }
34 }
35
36 public class CompileResult
37 {
38 public CompileResultData data { get; set; }
39 }
40
41 public class CompileStartResponse
42 {
43
44 public int code { get; set; }
45 public string[] errors { get; set; }
46 public CompileResult result { get; set; }
47 }
48 public class MBEDOnlineCompile
49 {
50 private string authInfo = string.Empty;
51 private string taskId = string.Empty;
52 private CompileStartResponse lastResponse = null;
53
54 public MBEDOnlineCompile(string userName, string password)
55 {
56 authInfo = userName + ":" + password;
57 authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
58 }
59
60 static string StringOrArrayToString(object item)
61 {
62 string result;
63
64 if (item is string)
65 {
66 result = (string)item;
67 }
68 else if (item is object[])
69 {
70 result = "[" + String.Join(", ", (object[])item) + "]";
71 }
72 else if (item != null)
73 {
74 result = item.GetType().ToString();
75 }
76 else
77 {
78 result = null;
79 }
80
81 return result;
82 }
83
84 public void StartCompile(string platform, string program = null, string repo = null, bool clean = false)
85 {
86 int retryCount = 5;
87 while (true)
88 {
89 try
90 {
91 WebRequest request = WebRequest.Create("https://os.mbed.com/api/v2/tasks/compiler/start/");
92 request.Headers["Authorization"] = "Basic " + authInfo;
93 request.Method = "POST";
94 request.ContentType = "application/x-www-form-urlencoded";
95 string requestPayload = string.Format("platform={0}&clean={1}", platform, clean ? "True" : "False");
96 if (!string.IsNullOrEmpty(program))
97 {
98 requestPayload += string.Format("&program={0}", program);
99 }
100 if (!string.IsNullOrEmpty(repo))
101 {
102 requestPayload += string.Format("&repo={0}", repo);
103 }
104 byte[] encodedPayload = Encoding.UTF8.GetBytes(requestPayload);
105 request.GetRequestStream().Write(encodedPayload, 0, Encoding.UTF8.GetBytes(requestPayload).Length);
106 WebResponse response = request.GetResponse();
107 CompileStartResponse compileResponse = new JavaScriptSerializer().Deserialize<CompileStartResponse>(new StreamReader(response.GetResponseStream()).ReadToEnd());
108 taskId = compileResponse.result.data.task_id;
109 lastResponse = compileResponse;
110
111 break;
112 }
113 catch (WebException webException)
114 {
115 retryCount--;
116 System.Console.WriteLine("Error sending request :{0}, retries left: {1}", webException.Message, retryCount);
117
118 if (retryCount == 0)
119 {
120 throw webException;
121 }
122 }
123 }
124 }
125
126 public bool PollStatus(ICollection<string> messages, out bool failed)
127 {
128 string requestURL = string.Format("https://os.mbed.com/api/v2/tasks/compiler/output/{0}", HttpUtility.UrlEncode(taskId));
129 bool complete = false;
130 failed = true;
131 CompileStartResponse compileResponse;
132
133 int retryCount = 5;
134 while (true)
135 {
136 try
137 {
138 WebRequest request = WebRequest.Create(requestURL);
139 request.Headers["Authorization"] = "Basic " + authInfo;
140 request.Method = "GET";
141
142 // 30s should be enough
143 request.Timeout = 30000;
144 WebResponse response = request.GetResponse();
145 string jsonResponse = new StreamReader(response.GetResponseStream()).ReadToEnd();
146
147 compileResponse = new JavaScriptSerializer().Deserialize<CompileStartResponse>(jsonResponse);
148 lastResponse = compileResponse;
149
150 foreach (CompileMessage msg in compileResponse.result.data.new_messages)
151 {
152 if (msg.type == "cc")
153 {
154 messages.Add(string.Format("{0}: {1}: {2}: {3}", msg.severity, StringOrArrayToString(msg.file), msg.line, msg.message));
155 messages.Add(string.Format("{0}", msg.text));
156 }
157 else if (msg.type == "progress")
158 {
159 messages.Add(string.Format("{0}: {1} ({2:F} %)", msg.action, StringOrArrayToString(msg.file), msg.percent));
160 }
161 else if (msg.type == "tool_error")
162 {
163 messages.Add(string.Format("{0}: {1}", msg.type, msg.message));
164 }
165 }
166
167 failed = !compileResponse.result.data.compilation_success;
168 if (failed && compileResponse.result.data.task_complete)
169 {
170 messages.Add(string.Format("Raw response: {0}", jsonResponse));
171 }
172
173 complete = compileResponse.result.data.task_complete;
174
175 break;
176 }
177 catch (WebException webException)
178 {
179 retryCount--;
180 System.Console.WriteLine("Error sending request :{0}, retries left: {1}", webException.Message, retryCount);
181
182 if (retryCount == 0)
183 {
184 throw webException;
185 }
186 }
187 }
188
189 if (retryCount < 0)
190 {
191 complete = true;
192 }
193
194 return complete;
195 }
196
197 public void DownloadBin(string downloadPath)
198 {
199 if (lastResponse == null ||
200 lastResponse.result == null ||
201 lastResponse.result.data == null ||
202 String.IsNullOrWhiteSpace(lastResponse.result.data.program) ||
203 String.IsNullOrWhiteSpace(lastResponse.result.data.binary))
204 {
205 return;
206 }
207
208 if (File.Exists(downloadPath))
209 {
210 File.Delete(downloadPath);
211 }
212
213 string requestQuery = string.Format("binary={0}&program={1}&repomode={2}&task_id={3}", lastResponse.result.data.binary, lastResponse.result.data.program, "True", taskId);
214 string requestUrl = String.Format("{0}?{1}", "https://os.mbed.com/api/v2/tasks/compiler/bin/", requestQuery);
215
216 int retryCount = 5;
217 while (true)
218 {
219 try
220 {
221 WebRequest request = WebRequest.Create(requestUrl);
222 request.Headers["Authorization"] = "Basic " + authInfo;
223 request.Method = "GET";
224
225 WebResponse response = request.GetResponse();
226
227 BinaryReader br = new BinaryReader(response.GetResponseStream());
228 FileStream fs = new FileStream(downloadPath, FileMode.CreateNew);
229
230 int bytesRead = 0;
231 byte[] buffer = new byte[256];
232
233 while ((bytesRead = br.Read(buffer, 0, buffer.Length)) > 0)
234 {
235 fs.Write(buffer, 0, bytesRead);
236 }
237
238 fs.Close();
239 br.Close();
240
241 break;
242 }
243 catch (WebException webException)
244 {
245 retryCount--;
246 System.Console.WriteLine("Error sending request :{0}, retries left: {1}", webException.Message, retryCount);
247
248 if (retryCount == 0)
249 {
250 throw webException;
251 }
252 }
253 }
254 }
255 }
256 }