-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
378 lines (299 loc) · 11.7 KB
/
Program.cs
File metadata and controls
378 lines (299 loc) · 11.7 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Configuration;
using System.Threading;
using System.Linq;
namespace MapServerKeepALive
{
class Program
{
static void Main(string[] args)
{
string home = Directory.GetCurrentDirectory();
string Tserver = "";
string server = "";
string TTokenURL = "";
string TUser = "";
string TDomain = "";
string TPassword = "";
string Tretry = "";
int retry = 1;
string Txy = "";
string Tx = "";
string Ty = "";
double x = 0;
double y = 0;
bool export_map = false;
int c = args.GetUpperBound(0);
// Loop through arguments
for (int n = 0; n < c; n++)
{
string thisKey = args[n].ToLower();
string thisVal = args[n + 1].TrimEnd().TrimStart();
// eval the key
switch (thisKey)
{
case "-server":
Tserver = thisVal;
break;
case "-retry":
Tretry = thisVal;
break;
case "-exportmap":
Txy = thisVal;
if (Txy != "") export_map = true;
string[] coord = thisVal.Split(';');
Tx = coord[0];
Ty = coord[1];
break;
case "-user":
TUser = thisVal;
break;
case "-password":
TPassword = thisVal;
break;
case "-tokenurl":
TTokenURL = thisVal;
break;
case "-domain":
TDomain = thisVal;
break;
default:
break;
}
}
if (Tserver == "") return;
server = Tserver;
if (Tretry != "") retry = Convert.ToInt32(Tretry);
if (Tx != "" && Ty != "")
{
x = Convert.ToDouble(Tx);
y = Convert.ToDouble(Ty);
}
string Token = "";
if (TTokenURL != "" && TPassword != "" && TUser != "")
{
Token = GetToken(TTokenURL, TUser, TPassword);
if (Token.Contains("Token Error:"))
{
Console.WriteLine(Token);
Environment.Exit(-1);
}
}
NetworkCredential webcredentials = new NetworkCredential();
if (TPassword != "" && TUser != "" && TDomain != "")
{
webcredentials.UserName = TUser;
webcredentials.Password = TPassword;
webcredentials.Domain = TDomain;
}
WebClient client = new WebClient();
if (webcredentials.Domain != "") client.Credentials = webcredentials;
string json = "";
try
{
if (Token != "")
{
json = client.DownloadString(new Uri(server + "?f=json&token=" + Token));
}
else
{
json = client.DownloadString(new Uri(server + "?f=json"));
}
}
catch (WebException webEx)
{
if (webEx.Message.Contains("401")) Console.WriteLine("Server returned a 401, did you forget to include your windows login details?");
Console.WriteLine(webEx.Message);
Environment.Exit(-1);
}
if (json.ToLower().Contains("error"))
{
Console.WriteLine(json);
Environment.Exit(-1);
}
client.Dispose();
Hashtable root;
ArrayList folders;
ArrayList services;
root = (Hashtable)Procurios.Public.JSON.JsonDecode(json);
folders = (ArrayList)root["folders"];
services = (ArrayList)root["services"];
ProcessFolder(server, retry, services, export_map, x, y, Token, webcredentials);
int folder_count = folders.Count;
for (int n = 0; n < folder_count; n++)
{
string folder = (string)folders[n];
client = new WebClient();
if (webcredentials.Domain != "") client.Credentials = webcredentials;
json = "";
try
{
if (Token != "")
{
json = client.DownloadString(new Uri(server + "/" + folder + "?f=json&token=" + Token));
}
else
{
json = client.DownloadString(new Uri(server + "/" + folder + "?f=json"));
}
}
catch (WebException webEx)
{
Console.WriteLine(webEx.Message);
}
if (json.ToLower().Contains("error"))
{
Console.WriteLine(json);
}
client.Dispose();
root = (Hashtable)Procurios.Public.JSON.JsonDecode(json);
services = (ArrayList)root["services"];
ProcessFolder(server, retry, services, export_map, x, y, Token, webcredentials);
}
Console.WriteLine("Done!");
}
public static bool ProcessFolder(string server, int retry, ArrayList services, bool export_map, double x, double y, string Token, NetworkCredential webcredentials)
{
int service_count = services.Count;
for (int n = 0; n < service_count; n++)
{
Hashtable service = (Hashtable)services[n];
string mstype = (string)service["type"];
string msname = (string)service["name"];
if (mstype == "MapServer")
{
int c = 0;
bool result = false;
while (result == false && c <= retry)
{
if (c > 0)
{
Console.WriteLine("Retry #" + c.ToString() + " in 5 seconds");
Thread.Sleep(5000);
}
result = PingMapServer(server + "/" + msname + "/" + mstype, Token, webcredentials);
if (export_map == true) result = ExportMapServer(server + "/" + msname + "/" + mstype, x, y, Token, webcredentials);
c++;
}
}
}
return true;
}
public static bool PingMapServer(string mapserver, string Token, NetworkCredential webcredentials)
{
WebClient client = new WebClient();
if (webcredentials.Domain != "") client.Credentials = webcredentials;
Stopwatch sw = Stopwatch.StartNew();
string json = "";
try
{
if (Token != "")
{
json = client.DownloadString(new Uri(mapserver + "?f=json&token=" + Token));
}
else
{
json = client.DownloadString(new Uri(mapserver + "?f=json"));
}
}
catch (WebException webEx)
{
Console.WriteLine("Failed to ping " + mapserver + "");
Console.WriteLine(webEx.Message);
return false;
}
if (json.ToLower().Contains("error"))
{
Console.WriteLine("Failed to ping " + mapserver + "");
Console.WriteLine(json);
return false;
}
sw.Stop();
client.Dispose();
double seconds = sw.ElapsedMilliseconds;
Console.WriteLine("Successfully pinged " + mapserver + " in " + seconds.ToString() + "ms");
return true;
}
public static bool ExportMapServer(string mapserver, double x, double y, string Token, NetworkCredential webcredentials)
{
string home = Directory.GetCurrentDirectory();
string ExeFriendlyName = System.AppDomain.CurrentDomain.FriendlyName;
string[] ExeNameBits = ExeFriendlyName.Split('.');
string ExeName = ExeNameBits[0];
WebClient client = new WebClient();
if (webcredentials.Domain != "") client.Credentials = webcredentials;
double XMin = x - 1000;
double XMax = x + 1000;
double YMin = y - 1000;
double YMax = y + 1000;
string imgurl = mapserver + "/export?";
imgurl = imgurl + "dpi=96";
imgurl = imgurl + "&size=500,500";
imgurl = imgurl + "&f=image";
imgurl = imgurl + "&format=png32";
imgurl = imgurl + "&bbox=" + XMin.ToString() + "," + YMin.ToString() + "," + XMax.ToString() + "," + YMax.ToString();
if (Token != "")
{
imgurl = imgurl + "&token=" + Token;
}
Stopwatch sw = Stopwatch.StartNew();
try
{
client.DownloadFile(new Uri(imgurl), home + "\\TempImage.png");
}
catch (WebException webEx)
{
Console.WriteLine(imgurl + " " + webEx.Message);
}
sw.Stop();
double seconds = sw.ElapsedMilliseconds;
client.Dispose();
try
{
//Check if the file is bigger than 1K
FileInfo FI = new FileInfo(home + "\\TempImage.png");
long FL = FI.Length;
if (FL < 1000)
{
seconds = 99999; //Probably timeout or error
Console.WriteLine("Failed to export map " + mapserver + " in " + seconds.ToString() + "ms");
return false;
}
else
{
Console.WriteLine("Successfully exported map " + mapserver + " in " + seconds.ToString() + "ms");
return true;
}
}
catch
{
seconds = 99999; //Probably timeout or error
Console.WriteLine("Failed to export map " + mapserver + " in " + seconds.ToString() + "ms");
return false;
}
}
public static string GetToken(string tokenurl, string username, string password)
{
string url = tokenurl + "?request=getToken&username=" + username + "&password=" + password;
System.Net.WebRequest request = System.Net.WebRequest.Create(url);
string myToken = "";
try
{
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader readStream = new System.IO.StreamReader(responseStream);
myToken = readStream.ReadToEnd();
}
catch (WebException we)
{
myToken = "Token Error: " + we.Message;
}
return myToken;
}
}
}