commit 6b4c34a20564f7bb6f49112fbc8842c93c2da3c4
Author: Eliška Mourycová <eliska.mourycova@seznam.cz>
Date:   Sun May 16 13:58:21 2021 +0200

    Re #8836. Added XML protocol building / parsing.

diff --git a/Server/ServerApp/Connection/ConnectionListener.cs b/Server/ServerApp/Connection/ConnectionListener.cs
index ea750f6..eb32549 100644
--- a/Server/ServerApp/Connection/ConnectionListener.cs
+++ b/Server/ServerApp/Connection/ConnectionListener.cs
@@ -2,6 +2,7 @@
 // Author: Eliska Mourycova
 //
 
+using ServerApp.Connection.XMLProtocolHandler;
 using System;
 using System.IO;
 using System.Net;
@@ -290,6 +291,12 @@ namespace ServerApp.Connection
     //  }
 
 
+    class ClientRequest
+	{
+        HttpListenerContext context;
+        string request;
+    }
+
     public class ConnectionListener
     {
         private readonly int PORT;
@@ -321,6 +328,7 @@ namespace ServerApp.Connection
                 //Console.ReadLine();
 
                 HttpListenerContext context = server.GetContext();
+                
 
 
                 if (context.Request.HttpMethod == "GET") // when client says download
@@ -348,6 +356,12 @@ namespace ServerApp.Connection
                     int rand = new Random().Next(1, 10);
                     string msg = "This is a response from the server :) " + rand;
 
+                    Response xmlResp = Response.Randomize();
+                    var xml = XmlCommunication.Serialize(xmlResp);
+                    //Console.WriteLine(xml);
+
+                    msg = xml;
+
                     byte[] buffer = Encoding.UTF8.GetBytes(msg);
 
                     response.ContentLength64 = buffer.Length;
diff --git a/Server/ServerApp/Connection/XMLProtocolHandler/Buildings.cs b/Server/ServerApp/Connection/XMLProtocolHandler/Buildings.cs
new file mode 100644
index 0000000..97dff76
--- /dev/null
+++ b/Server/ServerApp/Connection/XMLProtocolHandler/Buildings.cs
@@ -0,0 +1,16 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ServerApp.Connection.XMLProtocolHandler
+{
+	public class Buildings
+	{
+		public static string[] buildings = new string[] {
+		"FST+FEK", "FDU", "FAV", "FEL", "REK", "MENZA", "LIB", "CIV", "UNI14",
+		"DOM", "HUS", "CHOD", "JUNG", "KLAT", "KOLL", "RIEG", "SADY", "SED+VEL",
+		"TES", "TYL", "KARMA", "KBORY", "KLOCH", "KKLAT" };
+	}
+}
diff --git a/Server/ServerApp/Connection/XMLProtocolHandler/Date.cs b/Server/ServerApp/Connection/XMLProtocolHandler/Date.cs
new file mode 100644
index 0000000..cd16477
--- /dev/null
+++ b/Server/ServerApp/Connection/XMLProtocolHandler/Date.cs
@@ -0,0 +1,68 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Xml.Serialization;
+
+namespace ServerApp.Connection.XMLProtocolHandler
+{
+    public class Date : IComparable<Date>
+    {
+        [XmlIgnore]
+        public static int MIN_HOUR = 7;
+        [XmlIgnore]
+        public static int MAX_HOUR = 18;
+
+        [XmlElement]
+        public int day;
+
+        [XmlElement]
+        public int month;
+
+        [XmlElement]
+        public int year;
+
+        [XmlElement]
+        public int hour;
+
+        public static Date Randomize()
+        {
+            Date d = new Date();
+
+            d.day = DateTime.Now.Day;
+            d.month = DateTime.Now.Month;
+            d.year = DateTime.Now.Year;
+            d.hour = new Random().Next(7, 18);
+
+            return d;
+        }
+
+        public Date Clone()
+        {
+            Date clone = new Date();
+            clone.day = day;
+            clone.month = month;
+            clone.year = year;
+            clone.hour = hour;
+            return clone;
+        }
+
+        public int CompareTo(Date other)
+        {
+            if (year < other.year) return -1;
+            if (year > other.year) return 1;
+
+            if (month < other.month) return -1;
+            if (month > other.month) return 1;
+
+            if (day < other.day) return -1;
+            if (day > other.day) return 1;
+
+            if (hour < other.hour) return -1;
+            if (hour > other.hour) return 1;
+
+            return 0;
+        }
+    }
+}
diff --git a/Server/ServerApp/Connection/XMLProtocolHandler/Prediction.cs b/Server/ServerApp/Connection/XMLProtocolHandler/Prediction.cs
new file mode 100644
index 0000000..7f8eff1
--- /dev/null
+++ b/Server/ServerApp/Connection/XMLProtocolHandler/Prediction.cs
@@ -0,0 +1,19 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Xml.Serialization;
+
+namespace ServerApp.Connection.XMLProtocolHandler
+{
+    public class Prediction
+    {
+        [XmlElement]
+        public Date dateTime;
+
+        [XmlArray("rushLevels")]
+        [XmlArrayItem("rushLevel")]
+        public double[] predictions;
+    }
+}
diff --git a/Server/ServerApp/Connection/XMLProtocolHandler/Request.cs b/Server/ServerApp/Connection/XMLProtocolHandler/Request.cs
new file mode 100644
index 0000000..41f228e
--- /dev/null
+++ b/Server/ServerApp/Connection/XMLProtocolHandler/Request.cs
@@ -0,0 +1,80 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Xml.Serialization;
+
+namespace ServerApp.Connection.XMLProtocolHandler
+{
+    [XmlRoot("Request")]
+    public class Request
+    {
+        [XmlElement]
+        public bool useWeather;
+
+
+        [XmlElement]
+        public double temperature;
+
+        [XmlElement]
+        public double wind;
+
+        [XmlElement]
+        public double rain;
+
+        [XmlElement]
+        public WeatherCondition weather;
+
+
+        [XmlElement]
+        public bool useEndDate;
+
+
+        [XmlElement]
+        public Date start;
+
+        [XmlElement]
+        public Date end;
+
+
+        public static Request Randomize()
+        {
+            Request r = new Request();
+            Random rand = new Random();
+
+            r.useWeather = (rand.Next(0, 100)) < 50 ? true : false;
+
+            if (r.useWeather)
+            {
+                r.temperature = rand.Next(0, 40);
+                r.wind = rand.Next(0, 100);
+                r.rain = rand.Next(0, 100);
+                r.weather = (WeatherCondition)rand.Next(0, 3);
+            }
+
+            r.start = Date.Randomize();
+
+            r.useEndDate = (rand.Next(0, 100) < 50) ? true : false;
+
+            if (r.useEndDate)
+            {
+                r.end = Date.Randomize();
+                if (r.start.CompareTo(r.end) < 0)
+                {
+                    Date tmp = r.start;
+                    r.start = r.end;
+                    r.end = tmp;
+                }
+                else if (r.start.CompareTo(r.end) == 0)
+                {
+                    r.useEndDate = false;
+                }
+            }
+
+            return r;
+        }
+
+    }
+
+}
diff --git a/Server/ServerApp/Connection/XMLProtocolHandler/Response.cs b/Server/ServerApp/Connection/XMLProtocolHandler/Response.cs
new file mode 100644
index 0000000..17db15a
--- /dev/null
+++ b/Server/ServerApp/Connection/XMLProtocolHandler/Response.cs
@@ -0,0 +1,75 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Xml.Serialization;
+
+namespace ServerApp.Connection.XMLProtocolHandler
+{
+    [XmlRoot("Response")]
+    public class Response
+    {
+        [XmlElement]
+        public int hoursPerSegment;
+
+        [XmlArray("predictions")]
+        [XmlArrayItem("prediction")]
+        public Prediction[] predicitons;
+
+        public static Response Randomize()
+        {
+            Response r = new Response();
+            Random rand = new Random();
+
+            r.hoursPerSegment = 3;
+
+            Date start = Date.Randomize();
+            Date end = start.Clone();
+
+            end.day += rand.Next(0, 2);
+            end.hour += rand.Next(0, 18);
+
+            // If end gets generated before start, swap them
+            if (start.day == end.day && start.hour > end.hour)
+            {
+                Date tmp = start;
+                start = end;
+                end = tmp;
+            }
+
+            List<Prediction> predictionList = new List<Prediction>();
+            Date curr = start;
+
+            while (curr.CompareTo(end) <= 0)
+            {
+                Prediction p = new Prediction();
+
+                p.dateTime = curr;
+                p.predictions = new double[Buildings.buildings.Length];
+
+                for (int i = 0; i < p.predictions.Length; i++)
+                    p.predictions[i] = rand.Next(-1, 100);
+
+                predictionList.Add(p);
+
+                Date next = curr.Clone();
+                next.hour += r.hoursPerSegment;
+
+                if (next.hour > 18)
+                {
+                    next.day++;
+                    next.hour = 7;
+                }
+
+                curr = next;
+            }
+
+            r.predicitons = predictionList.ToArray();
+
+            return r;
+        }
+
+    }
+
+}
diff --git a/Server/ServerApp/Connection/XMLProtocolHandler/WeatherCondition.cs b/Server/ServerApp/Connection/XMLProtocolHandler/WeatherCondition.cs
new file mode 100644
index 0000000..e7304d1
--- /dev/null
+++ b/Server/ServerApp/Connection/XMLProtocolHandler/WeatherCondition.cs
@@ -0,0 +1,13 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ServerApp.Connection.XMLProtocolHandler
+{
+	public enum WeatherCondition
+	{
+		Sunny, Cloudy, Overcast, Dark
+	}
+}
diff --git a/Server/ServerApp/Connection/XMLProtocolHandler/XmlCommunication.cs b/Server/ServerApp/Connection/XMLProtocolHandler/XmlCommunication.cs
new file mode 100644
index 0000000..bfdcbf5
--- /dev/null
+++ b/Server/ServerApp/Connection/XMLProtocolHandler/XmlCommunication.cs
@@ -0,0 +1,34 @@
+﻿using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Xml.Serialization;
+
+namespace ServerApp.Connection.XMLProtocolHandler
+{
+    public class XmlCommunication
+    {
+        public static string Serialize<T>(T toSerialize)
+        {
+            var xml = new XmlSerializer(toSerialize.GetType());
+
+            using (var stream = new StringWriter())
+            {
+                xml.Serialize(stream, toSerialize);
+                return stream.ToString();
+            }
+        }
+
+        public static T Deserialize<T>(T toDeserialize, string xmlString)
+        {
+            using (var stream = new StringReader(xmlString))
+            {
+                var xml = new XmlSerializer(toDeserialize.GetType());
+                T deserialized = (T)xml.Deserialize(stream);
+                return deserialized;
+            }
+        }
+    }
+}
diff --git a/Server/ServerApp/Program.cs b/Server/ServerApp/Program.cs
index b3ace2a..ebc6347 100644
--- a/Server/ServerApp/Program.cs
+++ b/Server/ServerApp/Program.cs
@@ -1,4 +1,5 @@
 ﻿using ServerApp.Connection;
+using ServerApp.Connection.XMLProtocolHandler;
 using ServerApp.DataDownload;
 using ServerApp.Parser.Parsers;
 using ServerApp.Predictor;
@@ -46,14 +47,14 @@ namespace ServerApp
 			//DataParser p = new DataParser("data/");
 			//p.Parse();
 
-
+			#region UNCOMMENT
 			//test scenario -data download:
 			DataDownloader dd = new DataDownloader(config.DataRootDir, config.DataWebsite, config.DownloadedFilesNaming);
 			dd.OverwriteExisting = false;
 			List<string> savedFiles = new List<string>();
-			savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new Date(1, 2019), new Date(12, 2020)));
-			savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new Date(1, 2017), new Date(12, 2020)));
-			savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new Date(1, 2017), new Date(12, 2020)));
+			savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new DataDownload.Date(1, 2019), new DataDownload.Date(12, 2020)));
+			savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2020)));
+			savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2020)));
 
 
 
@@ -69,7 +70,7 @@ namespace ServerApp
 				Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
 			}
 
-			List<string> retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], new Date(10, 2019), new Date(12, 2020));
+			List<string> retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], new DataDownload.Date(10, 2019), new DataDownload.Date(12, 2020));
 			Console.WriteLine("Retrieved data: ");
 			foreach (string s in retrievedData)
 			{
@@ -79,6 +80,25 @@ namespace ServerApp
 			// test - connection:
 			ConnectionListener cl = new ConnectionListener(int.Parse(config.Port));
 			cl.StartListening();
+			#endregion
+
+			#region XML_TEST
+			//Response response = Response.Randomize();
+			//var xml = XmlCommunication.Serialize(response);
+			//Console.WriteLine(xml);
+			//Response responseDeserialized = XmlCommunication.Deserialize(response, xml);
+
+			//Console.WriteLine("------");
+
+
+			//Request request = Request.Randomize();
+			//xml = XmlCommunication.Serialize(request);
+			//Console.WriteLine(xml);
+			//Request requestDeserialized = XmlCommunication.Deserialize(request, xml);
+			#endregion
+
+
+
 
 			Console.ReadLine();
         }
diff --git a/Server/ServerApp/ServerApp.csproj b/Server/ServerApp/ServerApp.csproj
index 2abcf83..c7a2a76 100644
--- a/Server/ServerApp/ServerApp.csproj
+++ b/Server/ServerApp/ServerApp.csproj
@@ -131,6 +131,13 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Connection\ConnectionListener.cs" />
+    <Compile Include="Connection\XMLProtocolHandler\Buildings.cs" />
+    <Compile Include="Connection\XMLProtocolHandler\Date.cs" />
+    <Compile Include="Connection\XMLProtocolHandler\Prediction.cs" />
+    <Compile Include="Connection\XMLProtocolHandler\Request.cs" />
+    <Compile Include="Connection\XMLProtocolHandler\Response.cs" />
+    <Compile Include="Connection\XMLProtocolHandler\WeatherCondition.cs" />
+    <Compile Include="Connection\XMLProtocolHandler\XmlCommunication.cs" />
     <Compile Include="DataDownload\DataDownloader.cs" />
     <Compile Include="DataDownload\Date.cs" />
     <Compile Include="Parser\InputData\CsvDataLoader.cs" />
