Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 92897198

Přidáno uživatelem Eliška Mourycová před téměř 4 roky(ů)

Re #8944. Prepared testing class for DataDownloader.

Zobrazit rozdíly:

Server/ServerApp/Connection/ConnectionListener.cs
1
//
2
// Author: Eliska Mourycova
3
//
4

  
5
using ServerApp.Connection.XMLProtocolHandler;
6
using ServerApp.Predictor;
7
using System;
8
using System.Collections.Concurrent;
9
using System.Collections.Generic;
10
using System.IO;
11
using System.Net;
12
using System.Net.Sockets;
13
using System.Text;
14
using System.Threading;
15
using System.Web;
16

  
17
namespace ServerApp.Connection
18
{
19

  
20
	//public class HttpListenerCallbackState
21
	//{
22
	//    private readonly HttpListener _listener;
23
	//    private readonly AutoResetEvent _listenForNextRequest;
24

  
25
	//    public HttpListenerCallbackState(HttpListener listener)
26
	//    {
27
	//        if (listener == null) throw new ArgumentNullException("listener");
28
	//        _listener = listener;
29
	//        _listenForNextRequest = new AutoResetEvent(false);
30
	//    }
31

  
32
	//    public HttpListener Listener { get { return _listener; } }
33
	//    public AutoResetEvent ListenForNextRequest { get { return _listenForNextRequest; } }
34
	//}
35

  
36
	//public class HttpRequestHandler
37
	//{
38
	//    private int requestCounter = 0;
39
	//    private ManualResetEvent stopEvent = new ManualResetEvent(false);
40
	//    private int PORT;
41

  
42
	//    public HttpRequestHandler(int port)
43
	//    {
44
	//        this.PORT = port;
45
	//    }
46

  
47
	//    public void ListenAsynchronously(/*IEnumerable<string> prefixes*/)
48
	//    {
49
	//        HttpListener listener = new HttpListener();
50

  
51
	//        //foreach (string s in prefixes)
52
	//        //{
53
	//        //    listener.Prefixes.Add(s);
54
	//        //}
55

  
56
	//        listener.Prefixes.Add($"http://localhost:{PORT}/");
57

  
58
	//        listener.Start();
59
	//        Console.WriteLine("Listening...");
60
	//        HttpListenerCallbackState state = new HttpListenerCallbackState(listener);
61
	//        ThreadPool.QueueUserWorkItem(Listen, state);
62
	//    }
63

  
64
	//    public void StopListening()
65
	//    {
66
	//        stopEvent.Set();
67
	//    }
68

  
69

  
70
	//    private void Listen(object state)
71
	//    {
72
	//        HttpListenerCallbackState callbackState = (HttpListenerCallbackState)state;
73

  
74
	//        while (callbackState.Listener.IsListening)
75
	//        {
76
	//            callbackState.Listener.BeginGetContext(new AsyncCallback(ListenerCallback), callbackState);
77
	//            int n = WaitHandle.WaitAny(new WaitHandle[] { callbackState.ListenForNextRequest, stopEvent });
78

  
79
	//            if (n == 1)
80
	//            {
81
	//                // stopEvent was signalled 
82
	//                callbackState.Listener.Stop();
83
	//                break;
84
	//            }
85
	//        }
86
	//    }
87

  
88
	//    private void ListenerCallback(IAsyncResult ar)
89
	//    {
90
	//        HttpListenerCallbackState callbackState = (HttpListenerCallbackState)ar.AsyncState;
91
	//        HttpListenerContext context = null;
92

  
93
	//        int requestNumber = Interlocked.Increment(ref requestCounter);
94

  
95
	//        try
96
	//        {
97
	//            context = callbackState.Listener.EndGetContext(ar);
98
	//        }
99
	//        catch (Exception ex)
100
	//        {
101
	//            return;
102
	//        }
103
	//        finally
104
	//        {
105
	//            callbackState.ListenForNextRequest.Set();
106
	//        }
107

  
108
	//        if (context == null) return;
109

  
110

  
111
	//        HttpListenerRequest request = context.Request;
112

  
113
	//        if (request.HasEntityBody)
114
	//        {
115
	//            using (System.IO.StreamReader sr = new System.IO.StreamReader(request.InputStream, request.ContentEncoding))
116
	//            {
117
	//                string requestData = sr.ReadToEnd();
118

  
119
	//                //Stuff I do with the request happens here  
120
	//            }
121
	//        }
122

  
123

  
124
	//        try
125
	//        {
126
	//            using (HttpListenerResponse response = context.Response)
127
	//            {
128
	//                Console.WriteLine("dealing with response");
129
	//                //Thread.Sleep(10000);
130
	//                //response stuff happens here  
131
	//                string responseString = "Ok";
132

  
133
	//                byte[] buffer = Encoding.UTF8.GetBytes(responseString);
134
	//                response.ContentLength64 = buffer.LongLength;
135
	//                response.OutputStream.Write(buffer, 0, buffer.Length);
136
	//                response.Close();
137
	//            }
138
	//        }
139
	//        catch (Exception e)
140
	//        {
141
	//        }
142
	//    }
143
	//}
144

  
145

  
146

  
147

  
148

  
149

  
150

  
151

  
152

  
153

  
154
	public class ConnectionListener
155
	{
156
		private readonly int PORT;
157
		private IPredictionController predictionController;
158

  
159
		public ConnectionListener(int port, IPredictionController predictionController)
160
		{
161
			this.PORT = port;
162
			this.predictionController = predictionController;
163
		}
164

  
165
		public void StartListening()
166
		{
167
			string ip = GetLocalIPAddress();
168
			Console.WriteLine("ip : " + ip);
169
			HttpListener server = new HttpListener();
170
			server.Prefixes.Add($"https://{ip}:{PORT}/");
171
			//server.Prefixes.Add($"http://localhost:{PORT}/");
172

  
173
			server.Start();
174

  
175
			Console.WriteLine("Listening...");
176

  
177
			while (true)
178
			{
179
				Console.WriteLine("Waiting for a client request...");
180

  
181
				HttpListenerContext context = server.GetContext();
182

  
183

  
184

  
185
				if (context.Request.HttpMethod == "GET") // when client says download
186
				{
187
					Console.WriteLine("received GET request");
188

  
189
					HttpListenerResponse response = context.Response;
190
					response.AddHeader("Access-Control-Allow-Credentials", "true");
191
					response.AddHeader("Access-Control-Expose-Headers", "ETag");
192
					response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
193
					response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
194
					response.AddHeader("Access-Control-Allow-Origin", "*");
195

  
196

  
197

  
198
					//string page = Directory.GetCurrentDirectory() + context.Request.Url.LocalPath;
199
					// if (page == string.Empty)
200
					//     page = "test.txt";
201
					// 
202
					// TextReader tr = new StreamReader(page);
203
					// string msg = tr.ReadToEnd();
204

  
205
					int rand = new Random().Next(1, 10);
206
					string msg = "This is a response from the server :) " + rand;
207

  
208
					Response xmlResp = Response.Randomize();
209
					var xml = XmlCommunication.Serialize(xmlResp);
210
					//Console.WriteLine(xml);
211

  
212
					msg = xml;
213

  
214
					byte[] buffer = Encoding.UTF8.GetBytes(msg);
215

  
216
					response.ContentLength64 = buffer.Length;
217
					Stream st = response.OutputStream;
218
					st.Write(buffer, 0, buffer.Length);
219

  
220

  
221
					context.Response.Close();
222
				}
223

  
224
				else if (context.Request.HttpMethod == "POST") // when client says upload
225
				{
226
					Console.WriteLine("received POST request");
227

  
228
					string text;
229
					using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
230
						text = reader.ReadToEnd();
231
					Console.WriteLine("Received request:");
232
					string requestXML = HttpUtility.UrlDecode(text);
233
					Console.WriteLine(requestXML);
234

  
235
					// prepare the type:
236
					Request newRequest = new Request();
237
					Request requestDeserialized = null;
238
					try
239
					{
240
						requestDeserialized = XmlCommunication.Deserialize(newRequest, requestXML);
241
					}
242
					catch
243
					{
244
						Console.WriteLine("wrong format of request!");
245

  
246
						// don't bother to send anything:
247
						context.Response.Close();
248
						continue;
249
					}
250
					
251

  
252
					// response headers:
253
					HttpListenerResponse response = context.Response;
254
					response.AddHeader("Access-Control-Allow-Credentials", "true");
255
					response.AddHeader("Access-Control-Expose-Headers", "ETag");
256
					response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
257
					response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
258
					response.AddHeader("Access-Control-Allow-Origin", "*");
259

  
260
					// get the prediction from the model:
261
					Response responsePrediction = predictionController.Predict(requestDeserialized);
262

  
263
					int rand = new Random().Next(1, 10);
264
					string msg = "OK " + rand;
265

  
266
					// build the xml from the prediction: - TODO mbe an exception can happen here as well? - it shouldnt
267
					msg = XmlCommunication.Serialize(responsePrediction);
268

  
269

  
270
					// send the response:
271
					byte[] buffer = Encoding.UTF8.GetBytes(msg);
272
					response.ContentLength64 = buffer.Length;
273
					Stream st = response.OutputStream;
274
					st.Write(buffer, 0, buffer.Length);
275

  
276
					// close
277
					context.Response.Close(); // ?? Works better with this, if this line is absent, the client gets 'stuck' after approximately 6 messages
278
				}
279
				else
280
				{
281
					Console.WriteLine("received request other than GET or POST");
282
					Console.WriteLine("method: " + context.Request.HttpMethod);
283
					Console.WriteLine("headers: " + context.Request.Headers);
284
					Console.WriteLine("whole request?: " + context.Request);
285
					Console.WriteLine("user: " + context.User);
286

  
287

  
288

  
289
					//HttpListenerResponse response = context.Response;
290
					//response.AddHeader("Access-Control-Allow-Credentials", "true");
291
					//response.AddHeader("Access-Control-Expose-Headers", "ETag");
292
					//response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time, Origin, Content-Type, X-Auth-Token");
293
					//response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
294
					//response.AddHeader("Access-Control-Allow-Origin", "*");
295

  
296
					//string msg = "Preflight check? ";
297

  
298
					//byte[] buffer = Encoding.UTF8.GetBytes(msg);
299

  
300
					//response.ContentLength64 = buffer.Length;
301
					//Stream st = response.OutputStream;
302
					//st.Write(buffer, 0, buffer.Length);
303

  
304

  
305
					//context.Response.Close(); // ??
306

  
307
				}
308
			}
309
		}
310

  
311
		private string GetLocalIPAddress()
312
		{
313
			var host = Dns.GetHostEntry(Dns.GetHostName());
314
			foreach (var ip in host.AddressList)
315
			{
316
				if (ip.AddressFamily == AddressFamily.InterNetwork)
317
				{
318
					return ip.ToString();
319
				}
320
			}
321
			throw new Exception("No network adapters with an IPv4 address in the system!");
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
	class ClientService
353
	{
354
		public static readonly int MAX_IDS = 1000000; // ??
355
		public static int IDs = 0;
356
		public int UniqueID;
357
		public Request ClientRequest;
358
		public Response ClientResponse;
359
    }
360

  
361
    public class ConnectionListenerMulti
362
    {
363
        private readonly int PORT;
364
        private ConcurrentDictionary<int, ClientService> readyRequests;
365
        private ConcurrentQueue<ClientService> waitingRequests;
366

  
367

  
368
        public ConnectionListenerMulti(int port)
369
        {
370
            this.PORT = port;
371
        }
372

  
373

  
374
		private void RequestHandler()
375
		{
376
			while (true)
377
			{
378
				if (!waitingRequests.IsEmpty)
379
				{
380
					Console.WriteLine("handling a request for 10s...");
381
					ClientService nextService;
382
					bool deqResultOk = waitingRequests.TryDequeue(out nextService);
383
					// nextService has ID and request filled out
384
					Thread.Sleep(10000);
385

  
386
					Console.WriteLine("finished building response, adding it to the dictionary...");
387
					//ClientService service = new ClientService();
388
					//service.UniqueID = GetNextID();
389
					//service.ClientRequest = Request.Randomize();
390
					nextService.ClientResponse = Response.Randomize();
391
					bool resultOk = readyRequests.TryAdd(nextService.UniqueID, nextService);
392
					if (!resultOk)
393
					{
394
						Console.WriteLine("ID " + nextService.UniqueID + " is being used!");
395
						// do nothing? - keep it in the queue and let the client try later perhaps?
396
					}
397
					else
398
					{
399
						// added to dictionary
400
						// -> remove it from the queue
401
						
402
					}
403
				}
404
				else
405
				{
406
					Thread.Sleep(100); // ??
407
				}
408
				
409
			}
410
			
411

  
412
		}
413

  
414
		private int GetNextID()
415
		{
416
			int id = (ClientService.IDs++) % ClientService.MAX_IDS; // we presume that after a million requests, the first ids are free again, if not, tough luck
417
			return id;
418
		}
419

  
420
        public void StartListening()
421
        {
422
            string ip = GetLocalIPAddress();
423
            Console.WriteLine("ip : " + ip);
424
            HttpListener server = new HttpListener();
425
            //server.Prefixes.Add($"https://{ip}:{PORT}/");
426
            server.Prefixes.Add($"http://localhost:{PORT}/");
427

  
428
            server.Start();
429

  
430
            Console.WriteLine("Listening...");
431

  
432
			waitingRequests = new ConcurrentQueue<ClientService>();
433
			readyRequests = new ConcurrentDictionary<int, ClientService>();
434

  
435

  
436
			Thread requestHandlerThread = new Thread(RequestHandler);
437
			requestHandlerThread.Start();
438

  
439
			while (true)
440
            {
441
                Console.WriteLine("Waiting for a client request...");
442
				//Console.ReadLine();
443

  
444
				HttpListenerContext context = server.GetContext();
445
				//Thread.Sleep(10000);
446

  
447
				//IAsyncResult result = server.BeginGetContext(new AsyncCallback(ListenerCallback), server);
448
				//Console.WriteLine("Waiting for request to be processed asyncronously.");
449
				//result.AsyncWaitHandle.WaitOne();
450
				//Console.WriteLine("Request processed asyncronously.");
451

  
452

  
453

  
454
				if (context.Request.HttpMethod == "GET") // when client says download
455
				{
456
					Console.WriteLine("received GET request");
457

  
458
					HttpListenerResponse response = context.Response;
459
					response.AddHeader("Access-Control-Allow-Credentials", "true");
460
					response.AddHeader("Access-Control-Expose-Headers", "ETag");
461
					response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
462
					response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
463
					response.AddHeader("Access-Control-Allow-Origin", "*");
464

  
465

  
466

  
467
					//string page = Directory.GetCurrentDirectory() + context.Request.Url.LocalPath;
468
					// if (page == string.Empty)
469
					//     page = "test.txt";
470
					// 
471
					// TextReader tr = new StreamReader(page);
472
					// string msg = tr.ReadToEnd();
473

  
474
					int rand = new Random().Next(1, 10);
475
					string msg = "This is a response from the server :) " + rand;
476

  
477
					Response xmlResp = Response.Randomize();
478
					var xml = XmlCommunication.Serialize(xmlResp);
479
					//Console.WriteLine(xml);
480

  
481
					msg = xml;
482

  
483
					byte[] buffer = Encoding.UTF8.GetBytes(msg);
484

  
485
					response.ContentLength64 = buffer.Length;
486
					Stream st = response.OutputStream;
487
					st.Write(buffer, 0, buffer.Length);
488

  
489

  
490
					context.Response.Close();
491
				}
492

  
493
				else if (context.Request.HttpMethod == "POST") // when client says upload
494
				{
495
					Console.WriteLine("received POST request");
496

  
497
					string text;
498
					using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
499
						text = reader.ReadToEnd();
500
					Console.WriteLine("Received request:");
501
					string requestXML = HttpUtility.UrlDecode(text);
502
					Console.WriteLine(requestXML);
503

  
504
					Request newRequest = new Request();
505
					Request requestDeserialized = XmlCommunication.Deserialize(newRequest, requestXML);
506

  
507
					ClientService newService = new ClientService();
508
					newService.UniqueID = GetNextID();
509
					newService.ClientRequest = requestDeserialized;
510
					// save the request to the queue
511
					waitingRequests.Enqueue(newService);
512

  
513

  
514

  
515
					// SEND THE ID: - TODO xml format also
516
					HttpListenerResponse response = context.Response;
517
					response.AddHeader("Access-Control-Allow-Credentials", "true");
518
					response.AddHeader("Access-Control-Expose-Headers", "ETag");
519
					response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
520
					response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
521
					response.AddHeader("Access-Control-Allow-Origin", "*");
522

  
523

  
524

  
525
					int rand = new Random().Next(1, 10);
526
					string msg = "" + newService.UniqueID;
527

  
528
					byte[] buffer = Encoding.UTF8.GetBytes(msg);
529

  
530
					response.ContentLength64 = buffer.Length;
531
					Stream st = response.OutputStream;
532
					st.Write(buffer, 0, buffer.Length);
533

  
534

  
535
					context.Response.Close(); // ?? Works better with this, if this line is absent, the client gets 'stuck' after approximately 6 messages
536
				}
537
				else
538
				{
539
					Console.WriteLine("received request other than GET or POST");
540
					Console.WriteLine("method: " + context.Request.HttpMethod);
541
					Console.WriteLine("headers: " + context.Request.Headers);
542
					Console.WriteLine("whole request?: " + context.Request);
543
					Console.WriteLine("user: " + context.User);
544

  
545

  
546

  
547
					//HttpListenerResponse response = context.Response;
548
					//response.AddHeader("Access-Control-Allow-Credentials", "true");
549
					//response.AddHeader("Access-Control-Expose-Headers", "ETag");
550
					//response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time, Origin, Content-Type, X-Auth-Token");
551
					//response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
552
					//response.AddHeader("Access-Control-Allow-Origin", "*");
553

  
554
					//string msg = "Preflight check? ";
555

  
556
					//byte[] buffer = Encoding.UTF8.GetBytes(msg);
557

  
558
					//response.ContentLength64 = buffer.Length;
559
					//Stream st = response.OutputStream;
560
					//st.Write(buffer, 0, buffer.Length);
561

  
562

  
563
					//context.Response.Close(); // ??
564

  
565
				}
566
			}
567
        }
568

  
569

  
570

  
571

  
572

  
573

  
574

  
575

  
576

  
577

  
578

  
579
		private void ListenerCallback(IAsyncResult result)
580
		{
581
			HttpListener listener = (HttpListener)result.AsyncState;
582
			// Call EndGetContext to complete the asynchronous operation.
583
			HttpListenerContext context = listener.EndGetContext(result);
584

  
585
			Console.WriteLine("callbacking...");
586

  
587
			Thread.Sleep(10000);
588

  
589
			if (context.Request.HttpMethod == "GET") // when client says download
590
			{
591
				Console.WriteLine("received GET request");
592

  
593
				HttpListenerResponse response = context.Response;
594
				response.AddHeader("Access-Control-Allow-Credentials", "true");
595
				response.AddHeader("Access-Control-Expose-Headers", "ETag");
596
				response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
597
				response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
598
				response.AddHeader("Access-Control-Allow-Origin", "*");
599

  
600

  
601

  
602
				//string page = Directory.GetCurrentDirectory() + context.Request.Url.LocalPath;
603
				// if (page == string.Empty)
604
				//     page = "test.txt";
605
				// 
606
				// TextReader tr = new StreamReader(page);
607
				// string msg = tr.ReadToEnd();
608

  
609
				int rand = new Random().Next(1, 10);
610
				string msg = "This is a response from the server :) " + rand;
611

  
612
				Response xmlResp = Response.Randomize();
613
				var xml = XmlCommunication.Serialize(xmlResp);
614
				//Console.WriteLine(xml);
615

  
616
				msg = xml;
617

  
618
				byte[] buffer = Encoding.UTF8.GetBytes(msg);
619

  
620
				response.ContentLength64 = buffer.Length;
621
				Stream st = response.OutputStream;
622
				st.Write(buffer, 0, buffer.Length);
623

  
624

  
625
				context.Response.Close();
626
			}
627

  
628
			else if (context.Request.HttpMethod == "POST") // when client says upload
629
			{
630
				Console.WriteLine("received POST request");
631

  
632
				string text;
633
				using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
634
					text = reader.ReadToEnd();
635
				Console.WriteLine("Received request:");
636
				Console.WriteLine(HttpUtility.UrlDecode(text));
637

  
638

  
639

  
640
				HttpListenerResponse response = context.Response;
641
				response.AddHeader("Access-Control-Allow-Credentials", "true");
642
				response.AddHeader("Access-Control-Expose-Headers", "ETag");
643
				response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
644
				response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
645
				response.AddHeader("Access-Control-Allow-Origin", "*");
646

  
647

  
648

  
649
				int rand = new Random().Next(1, 10);
650
				string msg = "OK " + rand;
651

  
652
				byte[] buffer = Encoding.UTF8.GetBytes(msg);
653

  
654
				response.ContentLength64 = buffer.Length;
655
				Stream st = response.OutputStream;
656
				st.Write(buffer, 0, buffer.Length);
657

  
658

  
659
				context.Response.Close(); // ?? Works better with this, if this line is absent, the client gets 'stuck' after approximately 6 messages
660
			}
661
			else
662
			{
663
				Console.WriteLine("received request other than GET or POST");
664
				Console.WriteLine("method: " + context.Request.HttpMethod);
665
				Console.WriteLine("headers: " + context.Request.Headers);
666
				Console.WriteLine("whole request?: " + context.Request);
667
				Console.WriteLine("user: " + context.User);
668

  
669

  
670

  
671
				//HttpListenerResponse response = context.Response;
672
				//response.AddHeader("Access-Control-Allow-Credentials", "true");
673
				//response.AddHeader("Access-Control-Expose-Headers", "ETag");
674
				//response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time, Origin, Content-Type, X-Auth-Token");
675
				//response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
676
				//response.AddHeader("Access-Control-Allow-Origin", "*");
677

  
678
				//string msg = "Preflight check? ";
679

  
680
				//byte[] buffer = Encoding.UTF8.GetBytes(msg);
681

  
682
				//response.ContentLength64 = buffer.Length;
683
				//Stream st = response.OutputStream;
684
				//st.Write(buffer, 0, buffer.Length);
685

  
686

  
687
				//context.Response.Close(); // ??
688

  
689
			}
690

  
691

  
692

  
693

  
694

  
695

  
696

  
697

  
698

  
699

  
700
			//HttpListenerRequest request = context.Request;
701
			//// Obtain a response object.
702
			//HttpListenerResponse response = context.Response;
703
			//// Construct a response.
704
			//string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
705
			//byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
706
			//// Get a response stream and write the response to it.
707
			//response.ContentLength64 = buffer.Length;
708
			//System.IO.Stream output = response.OutputStream;
709
			//output.Write(buffer, 0, buffer.Length);
710
			//// You must close the output stream.
711
			//output.Close();
712
		}
713

  
714

  
715
		private string GetLocalIPAddress()
716
        {
717
            var host = Dns.GetHostEntry(Dns.GetHostName());
718
            foreach (var ip in host.AddressList)
719
            {
720
                if (ip.AddressFamily == AddressFamily.InterNetwork)
721
                {
722
                    return ip.ToString();
723
                }
724
            }
725
            throw new Exception("No network adapters with an IPv4 address in the system!");
726
        }
727

  
728
    }
729

  
730
}
Server/ServerApp/Connection/ConnectionListenerMulti.cs
1
//
2
// Author: Eliska Mourycova
3
//
4

  
5
using ServerApp.Connection.XMLProtocolHandler;
6
using ServerApp.Predictor;
7
using System;
8
using System.Collections.Concurrent;
9
using System.Collections.Generic;
10
using System.IO;
11
using System.Net;
12
using System.Net.Sockets;
13
using System.Text;
14
using System.Threading;
15
using System.Web;
16

  
17
namespace ServerApp.Connection
18
{
19

  
20
	//public class HttpListenerCallbackState
21
	//{
22
	//    private readonly HttpListener _listener;
23
	//    private readonly AutoResetEvent _listenForNextRequest;
24

  
25
	//    public HttpListenerCallbackState(HttpListener listener)
26
	//    {
27
	//        if (listener == null) throw new ArgumentNullException("listener");
28
	//        _listener = listener;
29
	//        _listenForNextRequest = new AutoResetEvent(false);
30
	//    }
31

  
32
	//    public HttpListener Listener { get { return _listener; } }
33
	//    public AutoResetEvent ListenForNextRequest { get { return _listenForNextRequest; } }
34
	//}
35

  
36
	//public class HttpRequestHandler
37
	//{
38
	//    private int requestCounter = 0;
39
	//    private ManualResetEvent stopEvent = new ManualResetEvent(false);
40
	//    private int PORT;
41

  
42
	//    public HttpRequestHandler(int port)
43
	//    {
44
	//        this.PORT = port;
45
	//    }
46

  
47
	//    public void ListenAsynchronously(/*IEnumerable<string> prefixes*/)
48
	//    {
49
	//        HttpListener listener = new HttpListener();
50

  
51
	//        //foreach (string s in prefixes)
52
	//        //{
53
	//        //    listener.Prefixes.Add(s);
54
	//        //}
55

  
56
	//        listener.Prefixes.Add($"http://localhost:{PORT}/");
57

  
58
	//        listener.Start();
59
	//        Console.WriteLine("Listening...");
60
	//        HttpListenerCallbackState state = new HttpListenerCallbackState(listener);
61
	//        ThreadPool.QueueUserWorkItem(Listen, state);
62
	//    }
63

  
64
	//    public void StopListening()
65
	//    {
66
	//        stopEvent.Set();
67
	//    }
68

  
69

  
70
	//    private void Listen(object state)
71
	//    {
72
	//        HttpListenerCallbackState callbackState = (HttpListenerCallbackState)state;
73

  
74
	//        while (callbackState.Listener.IsListening)
75
	//        {
76
	//            callbackState.Listener.BeginGetContext(new AsyncCallback(ListenerCallback), callbackState);
77
	//            int n = WaitHandle.WaitAny(new WaitHandle[] { callbackState.ListenForNextRequest, stopEvent });
78

  
79
	//            if (n == 1)
80
	//            {
81
	//                // stopEvent was signalled 
82
	//                callbackState.Listener.Stop();
83
	//                break;
84
	//            }
85
	//        }
86
	//    }
87

  
88
	//    private void ListenerCallback(IAsyncResult ar)
89
	//    {
90
	//        HttpListenerCallbackState callbackState = (HttpListenerCallbackState)ar.AsyncState;
91
	//        HttpListenerContext context = null;
92

  
93
	//        int requestNumber = Interlocked.Increment(ref requestCounter);
94

  
95
	//        try
96
	//        {
97
	//            context = callbackState.Listener.EndGetContext(ar);
98
	//        }
99
	//        catch (Exception ex)
100
	//        {
101
	//            return;
102
	//        }
103
	//        finally
104
	//        {
105
	//            callbackState.ListenForNextRequest.Set();
106
	//        }
107

  
108
	//        if (context == null) return;
109

  
110

  
111
	//        HttpListenerRequest request = context.Request;
112

  
113
	//        if (request.HasEntityBody)
114
	//        {
115
	//            using (System.IO.StreamReader sr = new System.IO.StreamReader(request.InputStream, request.ContentEncoding))
116
	//            {
117
	//                string requestData = sr.ReadToEnd();
118

  
119
	//                //Stuff I do with the request happens here  
120
	//            }
121
	//        }
122

  
123

  
124
	//        try
125
	//        {
126
	//            using (HttpListenerResponse response = context.Response)
127
	//            {
128
	//                Console.WriteLine("dealing with response");
129
	//                //Thread.Sleep(10000);
130
	//                //response stuff happens here  
131
	//                string responseString = "Ok";
132

  
133
	//                byte[] buffer = Encoding.UTF8.GetBytes(responseString);
134
	//                response.ContentLength64 = buffer.LongLength;
135
	//                response.OutputStream.Write(buffer, 0, buffer.Length);
136
	//                response.Close();
137
	//            }
138
	//        }
139
	//        catch (Exception e)
140
	//        {
141
	//        }
142
	//    }
143
	//}
144

  
145

  
146

  
147

  
148

  
149

  
150

  
151

  
152

  
153

  
154
	public class ConnectionListener
155
	{
156
		private readonly int PORT;
157
		private IPredictionController predictionController;
158

  
159
		public ConnectionListener(int port, IPredictionController predictionController)
160
		{
161
			this.PORT = port;
162
			this.predictionController = predictionController;
163
		}
164

  
165
		public void StartListening()
166
		{
167
			string ip = GetLocalIPAddress();
168
			Console.WriteLine("ip : " + ip);
169
			HttpListener server = new HttpListener();
170
			//server.Prefixes.Add($"https://{ip}:{PORT}/");
171
			server.Prefixes.Add($"http://localhost:{PORT}/");
172

  
173
			server.Start();
174

  
175
			Console.WriteLine("Listening...");
176

  
177
			while (true)
178
			{
179
				Console.WriteLine("Waiting for a client request...");
180

  
181
				HttpListenerContext context = server.GetContext();
182

  
183

  
184

  
185
				if (context.Request.HttpMethod == "GET") // when client says download
186
				{
187
					Console.WriteLine("received GET request");
188

  
189
					HttpListenerResponse response = context.Response;
190
					response.AddHeader("Access-Control-Allow-Credentials", "true");
191
					response.AddHeader("Access-Control-Expose-Headers", "ETag");
192
					response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
193
					response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
194
					response.AddHeader("Access-Control-Allow-Origin", "*");
195

  
196

  
197

  
198
					//string page = Directory.GetCurrentDirectory() + context.Request.Url.LocalPath;
199
					// if (page == string.Empty)
200
					//     page = "test.txt";
201
					// 
202
					// TextReader tr = new StreamReader(page);
203
					// string msg = tr.ReadToEnd();
204

  
205
					int rand = new Random().Next(1, 10);
206
					string msg = "This is a response from the server :) " + rand;
207

  
208
					Response xmlResp = Response.Randomize();
209
					var xml = XmlCommunication.Serialize(xmlResp);
210
					//Console.WriteLine(xml);
211

  
212
					msg = xml;
213

  
214
					byte[] buffer = Encoding.UTF8.GetBytes(msg);
215

  
216
					response.ContentLength64 = buffer.Length;
217
					Stream st = response.OutputStream;
218
					st.Write(buffer, 0, buffer.Length);
219

  
220

  
221
					context.Response.Close();
222
				}
223

  
224
				else if (context.Request.HttpMethod == "POST") // when client says upload
225
				{
226
					Console.WriteLine("received POST request");
227

  
228
					string text;
229
					using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
230
						text = reader.ReadToEnd();
231
					Console.WriteLine("Received request:");
232
					string requestXML = HttpUtility.UrlDecode(text);
233
					Console.WriteLine(requestXML);
234

  
235
					// prepare the type:
236
					Request newRequest = new Request();
237
					Request requestDeserialized = null;
238
					try
239
					{
240
						requestDeserialized = XmlCommunication.Deserialize(newRequest, requestXML);
241
					}
242
					catch
243
					{
244
						Console.WriteLine("wrong format of request!");
245

  
246
						// don't bother to send anything:
247
						continue;
248
					}
249
					
250

  
251
					// response headers:
252
					HttpListenerResponse response = context.Response;
253
					response.AddHeader("Access-Control-Allow-Credentials", "true");
254
					response.AddHeader("Access-Control-Expose-Headers", "ETag");
255
					response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
256
					response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
257
					response.AddHeader("Access-Control-Allow-Origin", "*");
258

  
259
					// get the prediction from the model:
260
					Response responsePrediction = predictionController.Predict(requestDeserialized);
261

  
262
					int rand = new Random().Next(1, 10);
263
					string msg = "OK " + rand;
264

  
265
					// build the xml from the prediction: - TODO mbe an exception can happen here as well? - it shouldnt
266
					msg = XmlCommunication.Serialize(responsePrediction);
267

  
268

  
269
					// send the response:
270
					byte[] buffer = Encoding.UTF8.GetBytes(msg);
271
					response.ContentLength64 = buffer.Length;
272
					Stream st = response.OutputStream;
273
					st.Write(buffer, 0, buffer.Length);
274

  
275
					// close
276
					context.Response.Close(); // ?? Works better with this, if this line is absent, the client gets 'stuck' after approximately 6 messages
277
				}
278
				else
279
				{
280
					Console.WriteLine("received request other than GET or POST");
281
					Console.WriteLine("method: " + context.Request.HttpMethod);
282
					Console.WriteLine("headers: " + context.Request.Headers);
283
					Console.WriteLine("whole request?: " + context.Request);
284
					Console.WriteLine("user: " + context.User);
285

  
286

  
287

  
288
					//HttpListenerResponse response = context.Response;
289
					//response.AddHeader("Access-Control-Allow-Credentials", "true");
290
					//response.AddHeader("Access-Control-Expose-Headers", "ETag");
291
					//response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time, Origin, Content-Type, X-Auth-Token");
292
					//response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
293
					//response.AddHeader("Access-Control-Allow-Origin", "*");
294

  
295
					//string msg = "Preflight check? ";
296

  
297
					//byte[] buffer = Encoding.UTF8.GetBytes(msg);
298

  
299
					//response.ContentLength64 = buffer.Length;
300
					//Stream st = response.OutputStream;
301
					//st.Write(buffer, 0, buffer.Length);
302

  
303

  
304
					//context.Response.Close(); // ??
305

  
306
				}
307
			}
308
		}
309

  
310
		private string GetLocalIPAddress()
311
		{
312
			var host = Dns.GetHostEntry(Dns.GetHostName());
313
			foreach (var ip in host.AddressList)
314
			{
315
				if (ip.AddressFamily == AddressFamily.InterNetwork)
316
				{
317
					return ip.ToString();
318
				}
319
			}
320
			throw new Exception("No network adapters with an IPv4 address in the system!");
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
	class ClientService
352
	{
353
		public static readonly int MAX_IDS = 1000000; // ??
354
		public static int IDs = 0;
355
		public int UniqueID;
356
		public Request ClientRequest;
357
		public Response ClientResponse;
358
    }
359

  
360
    public class ConnectionListenerMulti
361
    {
362
        private readonly int PORT;
363
        private ConcurrentDictionary<int, ClientService> readyRequests;
364
        private ConcurrentQueue<ClientService> waitingRequests;
365

  
366

  
367
        public ConnectionListenerMulti(int port)
368
        {
369
            this.PORT = port;
370
        }
371

  
372

  
373
		private void RequestHandler()
374
		{
375
			while (true)
376
			{
377
				if (!waitingRequests.IsEmpty)
378
				{
379
					Console.WriteLine("handling a request for 10s...");
380
					ClientService nextService;
381
					bool deqResultOk = waitingRequests.TryDequeue(out nextService);
382
					// nextService has ID and request filled out
383
					Thread.Sleep(10000);
384

  
385
					Console.WriteLine("finished building response, adding it to the dictionary...");
386
					//ClientService service = new ClientService();
387
					//service.UniqueID = GetNextID();
388
					//service.ClientRequest = Request.Randomize();
389
					nextService.ClientResponse = Response.Randomize();
390
					bool resultOk = readyRequests.TryAdd(nextService.UniqueID, nextService);
391
					if (!resultOk)
392
					{
393
						Console.WriteLine("ID " + nextService.UniqueID + " is being used!");
394
						// do nothing? - keep it in the queue and let the client try later perhaps?
395
					}
396
					else
397
					{
398
						// added to dictionary
399
						// -> remove it from the queue
400
						
401
					}
402
				}
403
				else
404
				{
405
					Thread.Sleep(100); // ??
406
				}
407
				
408
			}
409
			
410

  
411
		}
412

  
413
		private int GetNextID()
414
		{
415
			int id = (ClientService.IDs++) % ClientService.MAX_IDS; // we presume that after a million requests, the first ids are free again, if not, tough luck
416
			return id;
417
		}
418

  
419
        public void StartListening()
420
        {
421
            string ip = GetLocalIPAddress();
422
            Console.WriteLine("ip : " + ip);
423
            HttpListener server = new HttpListener();
424
            //server.Prefixes.Add($"https://{ip}:{PORT}/");
425
            server.Prefixes.Add($"http://localhost:{PORT}/");
426

  
427
            server.Start();
428

  
429
            Console.WriteLine("Listening...");
430

  
431
			waitingRequests = new ConcurrentQueue<ClientService>();
432
			readyRequests = new ConcurrentDictionary<int, ClientService>();
433

  
434

  
435
			Thread requestHandlerThread = new Thread(RequestHandler);
436
			requestHandlerThread.Start();
437

  
438
			while (true)
439
            {
440
                Console.WriteLine("Waiting for a client request...");
441
				//Console.ReadLine();
442

  
443
				HttpListenerContext context = server.GetContext();
444
				//Thread.Sleep(10000);
445

  
446
				//IAsyncResult result = server.BeginGetContext(new AsyncCallback(ListenerCallback), server);
447
				//Console.WriteLine("Waiting for request to be processed asyncronously.");
448
				//result.AsyncWaitHandle.WaitOne();
449
				//Console.WriteLine("Request processed asyncronously.");
450

  
451

  
452

  
453
				if (context.Request.HttpMethod == "GET") // when client says download
454
				{
455
					Console.WriteLine("received GET request");
456

  
457
					HttpListenerResponse response = context.Response;
458
					response.AddHeader("Access-Control-Allow-Credentials", "true");
459
					response.AddHeader("Access-Control-Expose-Headers", "ETag");
460
					response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
461
					response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
462
					response.AddHeader("Access-Control-Allow-Origin", "*");
463

  
464

  
465

  
466
					//string page = Directory.GetCurrentDirectory() + context.Request.Url.LocalPath;
467
					// if (page == string.Empty)
468
					//     page = "test.txt";
469
					// 
470
					// TextReader tr = new StreamReader(page);
471
					// string msg = tr.ReadToEnd();
472

  
473
					int rand = new Random().Next(1, 10);
474
					string msg = "This is a response from the server :) " + rand;
475

  
476
					Response xmlResp = Response.Randomize();
477
					var xml = XmlCommunication.Serialize(xmlResp);
478
					//Console.WriteLine(xml);
479

  
480
					msg = xml;
481

  
482
					byte[] buffer = Encoding.UTF8.GetBytes(msg);
483

  
484
					response.ContentLength64 = buffer.Length;
485
					Stream st = response.OutputStream;
486
					st.Write(buffer, 0, buffer.Length);
487

  
488

  
489
					context.Response.Close();
490
				}
491

  
492
				else if (context.Request.HttpMethod == "POST") // when client says upload
493
				{
494
					Console.WriteLine("received POST request");
495

  
496
					string text;
497
					using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
498
						text = reader.ReadToEnd();
499
					Console.WriteLine("Received request:");
500
					string requestXML = HttpUtility.UrlDecode(text);
501
					Console.WriteLine(requestXML);
502

  
503
					Request newRequest = new Request();
504
					Request requestDeserialized = XmlCommunication.Deserialize(newRequest, requestXML);
505

  
506
					ClientService newService = new ClientService();
507
					newService.UniqueID = GetNextID();
508
					newService.ClientRequest = requestDeserialized;
509
					// save the request to the queue
510
					waitingRequests.Enqueue(newService);
511

  
512

  
513

  
514
					// SEND THE ID: - TODO xml format also
515
					HttpListenerResponse response = context.Response;
516
					response.AddHeader("Access-Control-Allow-Credentials", "true");
517
					response.AddHeader("Access-Control-Expose-Headers", "ETag");
518
					response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
519
					response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
520
					response.AddHeader("Access-Control-Allow-Origin", "*");
521

  
522

  
523

  
524
					int rand = new Random().Next(1, 10);
525
					string msg = "" + newService.UniqueID;
526

  
527
					byte[] buffer = Encoding.UTF8.GetBytes(msg);
528

  
529
					response.ContentLength64 = buffer.Length;
530
					Stream st = response.OutputStream;
531
					st.Write(buffer, 0, buffer.Length);
532

  
533

  
534
					context.Response.Close(); // ?? Works better with this, if this line is absent, the client gets 'stuck' after approximately 6 messages
535
				}
536
				else
537
				{
538
					Console.WriteLine("received request other than GET or POST");
539
					Console.WriteLine("method: " + context.Request.HttpMethod);
540
					Console.WriteLine("headers: " + context.Request.Headers);
541
					Console.WriteLine("whole request?: " + context.Request);
542
					Console.WriteLine("user: " + context.User);
543

  
544

  
545

  
546
					//HttpListenerResponse response = context.Response;
547
					//response.AddHeader("Access-Control-Allow-Credentials", "true");
548
					//response.AddHeader("Access-Control-Expose-Headers", "ETag");
549
					//response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time, Origin, Content-Type, X-Auth-Token");
550
					//response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
551
					//response.AddHeader("Access-Control-Allow-Origin", "*");
552

  
553
					//string msg = "Preflight check? ";
554

  
555
					//byte[] buffer = Encoding.UTF8.GetBytes(msg);
556

  
557
					//response.ContentLength64 = buffer.Length;
558
					//Stream st = response.OutputStream;
559
					//st.Write(buffer, 0, buffer.Length);
560

  
561

  
562
					//context.Response.Close(); // ??
563

  
564
				}
565
			}
566
        }
567

  
568

  
569

  
570

  
571

  
572

  
573

  
574

  
575

  
576

  
577

  
578
		private void ListenerCallback(IAsyncResult result)
579
		{
580
			HttpListener listener = (HttpListener)result.AsyncState;
581
			// Call EndGetContext to complete the asynchronous operation.
582
			HttpListenerContext context = listener.EndGetContext(result);
583

  
584
			Console.WriteLine("callbacking...");
585

  
586
			Thread.Sleep(10000);
587

  
588
			if (context.Request.HttpMethod == "GET") // when client says download
589
			{
590
				Console.WriteLine("received GET request");
591

  
592
				HttpListenerResponse response = context.Response;
593
				response.AddHeader("Access-Control-Allow-Credentials", "true");
594
				response.AddHeader("Access-Control-Expose-Headers", "ETag");
595
				response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
596
				response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
597
				response.AddHeader("Access-Control-Allow-Origin", "*");
598

  
599

  
600

  
601
				//string page = Directory.GetCurrentDirectory() + context.Request.Url.LocalPath;
602
				// if (page == string.Empty)
603
				//     page = "test.txt";
604
				// 
605
				// TextReader tr = new StreamReader(page);
606
				// string msg = tr.ReadToEnd();
607

  
608
				int rand = new Random().Next(1, 10);
609
				string msg = "This is a response from the server :) " + rand;
610

  
611
				Response xmlResp = Response.Randomize();
612
				var xml = XmlCommunication.Serialize(xmlResp);
613
				//Console.WriteLine(xml);
614

  
615
				msg = xml;
616

  
617
				byte[] buffer = Encoding.UTF8.GetBytes(msg);
618

  
619
				response.ContentLength64 = buffer.Length;
620
				Stream st = response.OutputStream;
621
				st.Write(buffer, 0, buffer.Length);
622

  
623

  
624
				context.Response.Close();
625
			}
626

  
627
			else if (context.Request.HttpMethod == "POST") // when client says upload
628
			{
629
				Console.WriteLine("received POST request");
630

  
631
				string text;
632
				using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
633
					text = reader.ReadToEnd();
634
				Console.WriteLine("Received request:");
635
				Console.WriteLine(HttpUtility.UrlDecode(text));
636

  
637

  
638

  
639
				HttpListenerResponse response = context.Response;
640
				response.AddHeader("Access-Control-Allow-Credentials", "true");
641
				response.AddHeader("Access-Control-Expose-Headers", "ETag");
642
				response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
643
				response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
644
				response.AddHeader("Access-Control-Allow-Origin", "*");
645

  
646

  
647

  
648
				int rand = new Random().Next(1, 10);
649
				string msg = "OK " + rand;
650

  
651
				byte[] buffer = Encoding.UTF8.GetBytes(msg);
652

  
653
				response.ContentLength64 = buffer.Length;
654
				Stream st = response.OutputStream;
655
				st.Write(buffer, 0, buffer.Length);
656

  
657

  
658
				context.Response.Close(); // ?? Works better with this, if this line is absent, the client gets 'stuck' after approximately 6 messages
659
			}
660
			else
661
			{
662
				Console.WriteLine("received request other than GET or POST");
663
				Console.WriteLine("method: " + context.Request.HttpMethod);
664
				Console.WriteLine("headers: " + context.Request.Headers);
665
				Console.WriteLine("whole request?: " + context.Request);
666
				Console.WriteLine("user: " + context.User);
667

  
668

  
669

  
670
				//HttpListenerResponse response = context.Response;
671
				//response.AddHeader("Access-Control-Allow-Credentials", "true");
672
				//response.AddHeader("Access-Control-Expose-Headers", "ETag");
673
				//response.AddHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time, Origin, Content-Type, X-Auth-Token");
674
				//response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
675
				//response.AddHeader("Access-Control-Allow-Origin", "*");
676

  
677
				//string msg = "Preflight check? ";
678

  
679
				//byte[] buffer = Encoding.UTF8.GetBytes(msg);
680

  
681
				//response.ContentLength64 = buffer.Length;
682
				//Stream st = response.OutputStream;
683
				//st.Write(buffer, 0, buffer.Length);
684

  
685

  
686
				//context.Response.Close(); // ??
687

  
688
			}
689

  
690

  
691

  
692

  
693

  
694

  
695

  
696

  
697

  
698

  
699
			//HttpListenerRequest request = context.Request;
700
			//// Obtain a response object.
701
			//HttpListenerResponse response = context.Response;
702
			//// Construct a response.
703
			//string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
704
			//byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
705
			//// Get a response stream and write the response to it.
706
			//response.ContentLength64 = buffer.Length;
707
			//System.IO.Stream output = response.OutputStream;
708
			//output.Write(buffer, 0, buffer.Length);
709
			//// You must close the output stream.
710
			//output.Close();
711
		}
712

  
713

  
714
		private string GetLocalIPAddress()
715
        {
716
            var host = Dns.GetHostEntry(Dns.GetHostName());
717
            foreach (var ip in host.AddressList)
718
            {
719
                if (ip.AddressFamily == AddressFamily.InterNetwork)
720
                {
721
                    return ip.ToString();
722
                }
723
            }
724
            throw new Exception("No network adapters with an IPv4 address in the system!");
725
        }
726

  
727
    }
728

  
729
}
Server/ServerApp/Program.cs
19 19
		public string DataWebsite { get; set; }
20 20
		public string DownloadedFilesNaming { get; set; }
21 21
		public string DataRootDir { get; set; }
22
		public string Port { get; set; }
22
		public string Port { get; set; } // TODO port cannot be configurable?
23 23
	}
24 24

  
25 25
    public class Program
......
57 57
			DataDownloader dd = DataDownloadAndRetrievalTest(config);
58 58

  
59 59
			// xml building test
60
			XMLTest();
60
			//XMLTest();
61 61

  
62 62

  
63 63

  
... Rozdílový soubor je zkrácen, protože jeho délka přesahuje max. limit.

Také k dispozici: Unified diff