Revize 6b4c34a2
Přidáno uživatelem Eliška Mourycová před více než 3 roky(ů)
Server/ServerApp/Connection/ConnectionListener.cs | ||
---|---|---|
2 | 2 |
// Author: Eliska Mourycova |
3 | 3 |
// |
4 | 4 |
|
5 |
using ServerApp.Connection.XMLProtocolHandler; |
|
5 | 6 |
using System; |
6 | 7 |
using System.IO; |
7 | 8 |
using System.Net; |
... | ... | |
290 | 291 |
// } |
291 | 292 |
|
292 | 293 |
|
294 |
class ClientRequest |
|
295 |
{ |
|
296 |
HttpListenerContext context; |
|
297 |
string request; |
|
298 |
} |
|
299 |
|
|
293 | 300 |
public class ConnectionListener |
294 | 301 |
{ |
295 | 302 |
private readonly int PORT; |
... | ... | |
321 | 328 |
//Console.ReadLine(); |
322 | 329 |
|
323 | 330 |
HttpListenerContext context = server.GetContext(); |
331 |
|
|
324 | 332 |
|
325 | 333 |
|
326 | 334 |
if (context.Request.HttpMethod == "GET") // when client says download |
... | ... | |
348 | 356 |
int rand = new Random().Next(1, 10); |
349 | 357 |
string msg = "This is a response from the server :) " + rand; |
350 | 358 |
|
359 |
Response xmlResp = Response.Randomize(); |
|
360 |
var xml = XmlCommunication.Serialize(xmlResp); |
|
361 |
//Console.WriteLine(xml); |
|
362 |
|
|
363 |
msg = xml; |
|
364 |
|
|
351 | 365 |
byte[] buffer = Encoding.UTF8.GetBytes(msg); |
352 | 366 |
|
353 | 367 |
response.ContentLength64 = buffer.Length; |
Server/ServerApp/Connection/XMLProtocolHandler/Buildings.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Threading.Tasks; |
|
6 |
|
|
7 |
namespace ServerApp.Connection.XMLProtocolHandler |
|
8 |
{ |
|
9 |
public class Buildings |
|
10 |
{ |
|
11 |
public static string[] buildings = new string[] { |
|
12 |
"FST+FEK", "FDU", "FAV", "FEL", "REK", "MENZA", "LIB", "CIV", "UNI14", |
|
13 |
"DOM", "HUS", "CHOD", "JUNG", "KLAT", "KOLL", "RIEG", "SADY", "SED+VEL", |
|
14 |
"TES", "TYL", "KARMA", "KBORY", "KLOCH", "KKLAT" }; |
|
15 |
} |
|
16 |
} |
Server/ServerApp/Connection/XMLProtocolHandler/Date.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Threading.Tasks; |
|
6 |
using System.Xml.Serialization; |
|
7 |
|
|
8 |
namespace ServerApp.Connection.XMLProtocolHandler |
|
9 |
{ |
|
10 |
public class Date : IComparable<Date> |
|
11 |
{ |
|
12 |
[XmlIgnore] |
|
13 |
public static int MIN_HOUR = 7; |
|
14 |
[XmlIgnore] |
|
15 |
public static int MAX_HOUR = 18; |
|
16 |
|
|
17 |
[XmlElement] |
|
18 |
public int day; |
|
19 |
|
|
20 |
[XmlElement] |
|
21 |
public int month; |
|
22 |
|
|
23 |
[XmlElement] |
|
24 |
public int year; |
|
25 |
|
|
26 |
[XmlElement] |
|
27 |
public int hour; |
|
28 |
|
|
29 |
public static Date Randomize() |
|
30 |
{ |
|
31 |
Date d = new Date(); |
|
32 |
|
|
33 |
d.day = DateTime.Now.Day; |
|
34 |
d.month = DateTime.Now.Month; |
|
35 |
d.year = DateTime.Now.Year; |
|
36 |
d.hour = new Random().Next(7, 18); |
|
37 |
|
|
38 |
return d; |
|
39 |
} |
|
40 |
|
|
41 |
public Date Clone() |
|
42 |
{ |
|
43 |
Date clone = new Date(); |
|
44 |
clone.day = day; |
|
45 |
clone.month = month; |
|
46 |
clone.year = year; |
|
47 |
clone.hour = hour; |
|
48 |
return clone; |
|
49 |
} |
|
50 |
|
|
51 |
public int CompareTo(Date other) |
|
52 |
{ |
|
53 |
if (year < other.year) return -1; |
|
54 |
if (year > other.year) return 1; |
|
55 |
|
|
56 |
if (month < other.month) return -1; |
|
57 |
if (month > other.month) return 1; |
|
58 |
|
|
59 |
if (day < other.day) return -1; |
|
60 |
if (day > other.day) return 1; |
|
61 |
|
|
62 |
if (hour < other.hour) return -1; |
|
63 |
if (hour > other.hour) return 1; |
|
64 |
|
|
65 |
return 0; |
|
66 |
} |
|
67 |
} |
|
68 |
} |
Server/ServerApp/Connection/XMLProtocolHandler/Prediction.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Threading.Tasks; |
|
6 |
using System.Xml.Serialization; |
|
7 |
|
|
8 |
namespace ServerApp.Connection.XMLProtocolHandler |
|
9 |
{ |
|
10 |
public class Prediction |
|
11 |
{ |
|
12 |
[XmlElement] |
|
13 |
public Date dateTime; |
|
14 |
|
|
15 |
[XmlArray("rushLevels")] |
|
16 |
[XmlArrayItem("rushLevel")] |
|
17 |
public double[] predictions; |
|
18 |
} |
|
19 |
} |
Server/ServerApp/Connection/XMLProtocolHandler/Request.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Threading.Tasks; |
|
6 |
using System.Xml.Serialization; |
|
7 |
|
|
8 |
namespace ServerApp.Connection.XMLProtocolHandler |
|
9 |
{ |
|
10 |
[XmlRoot("Request")] |
|
11 |
public class Request |
|
12 |
{ |
|
13 |
[XmlElement] |
|
14 |
public bool useWeather; |
|
15 |
|
|
16 |
|
|
17 |
[XmlElement] |
|
18 |
public double temperature; |
|
19 |
|
|
20 |
[XmlElement] |
|
21 |
public double wind; |
|
22 |
|
|
23 |
[XmlElement] |
|
24 |
public double rain; |
|
25 |
|
|
26 |
[XmlElement] |
|
27 |
public WeatherCondition weather; |
|
28 |
|
|
29 |
|
|
30 |
[XmlElement] |
|
31 |
public bool useEndDate; |
|
32 |
|
|
33 |
|
|
34 |
[XmlElement] |
|
35 |
public Date start; |
|
36 |
|
|
37 |
[XmlElement] |
|
38 |
public Date end; |
|
39 |
|
|
40 |
|
|
41 |
public static Request Randomize() |
|
42 |
{ |
|
43 |
Request r = new Request(); |
|
44 |
Random rand = new Random(); |
|
45 |
|
|
46 |
r.useWeather = (rand.Next(0, 100)) < 50 ? true : false; |
|
47 |
|
|
48 |
if (r.useWeather) |
|
49 |
{ |
|
50 |
r.temperature = rand.Next(0, 40); |
|
51 |
r.wind = rand.Next(0, 100); |
|
52 |
r.rain = rand.Next(0, 100); |
|
53 |
r.weather = (WeatherCondition)rand.Next(0, 3); |
|
54 |
} |
|
55 |
|
|
56 |
r.start = Date.Randomize(); |
|
57 |
|
|
58 |
r.useEndDate = (rand.Next(0, 100) < 50) ? true : false; |
|
59 |
|
|
60 |
if (r.useEndDate) |
|
61 |
{ |
|
62 |
r.end = Date.Randomize(); |
|
63 |
if (r.start.CompareTo(r.end) < 0) |
|
64 |
{ |
|
65 |
Date tmp = r.start; |
|
66 |
r.start = r.end; |
|
67 |
r.end = tmp; |
|
68 |
} |
|
69 |
else if (r.start.CompareTo(r.end) == 0) |
|
70 |
{ |
|
71 |
r.useEndDate = false; |
|
72 |
} |
|
73 |
} |
|
74 |
|
|
75 |
return r; |
|
76 |
} |
|
77 |
|
|
78 |
} |
|
79 |
|
|
80 |
} |
Server/ServerApp/Connection/XMLProtocolHandler/Response.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Threading.Tasks; |
|
6 |
using System.Xml.Serialization; |
|
7 |
|
|
8 |
namespace ServerApp.Connection.XMLProtocolHandler |
|
9 |
{ |
|
10 |
[XmlRoot("Response")] |
|
11 |
public class Response |
|
12 |
{ |
|
13 |
[XmlElement] |
|
14 |
public int hoursPerSegment; |
|
15 |
|
|
16 |
[XmlArray("predictions")] |
|
17 |
[XmlArrayItem("prediction")] |
|
18 |
public Prediction[] predicitons; |
|
19 |
|
|
20 |
public static Response Randomize() |
|
21 |
{ |
|
22 |
Response r = new Response(); |
|
23 |
Random rand = new Random(); |
|
24 |
|
|
25 |
r.hoursPerSegment = 3; |
|
26 |
|
|
27 |
Date start = Date.Randomize(); |
|
28 |
Date end = start.Clone(); |
|
29 |
|
|
30 |
end.day += rand.Next(0, 2); |
|
31 |
end.hour += rand.Next(0, 18); |
|
32 |
|
|
33 |
// If end gets generated before start, swap them |
|
34 |
if (start.day == end.day && start.hour > end.hour) |
|
35 |
{ |
|
36 |
Date tmp = start; |
|
37 |
start = end; |
|
38 |
end = tmp; |
|
39 |
} |
|
40 |
|
|
41 |
List<Prediction> predictionList = new List<Prediction>(); |
|
42 |
Date curr = start; |
|
43 |
|
|
44 |
while (curr.CompareTo(end) <= 0) |
|
45 |
{ |
|
46 |
Prediction p = new Prediction(); |
|
47 |
|
|
48 |
p.dateTime = curr; |
|
49 |
p.predictions = new double[Buildings.buildings.Length]; |
|
50 |
|
|
51 |
for (int i = 0; i < p.predictions.Length; i++) |
|
52 |
p.predictions[i] = rand.Next(-1, 100); |
|
53 |
|
|
54 |
predictionList.Add(p); |
|
55 |
|
|
56 |
Date next = curr.Clone(); |
|
57 |
next.hour += r.hoursPerSegment; |
|
58 |
|
|
59 |
if (next.hour > 18) |
|
60 |
{ |
|
61 |
next.day++; |
|
62 |
next.hour = 7; |
|
63 |
} |
|
64 |
|
|
65 |
curr = next; |
|
66 |
} |
|
67 |
|
|
68 |
r.predicitons = predictionList.ToArray(); |
|
69 |
|
|
70 |
return r; |
|
71 |
} |
|
72 |
|
|
73 |
} |
|
74 |
|
|
75 |
} |
Server/ServerApp/Connection/XMLProtocolHandler/WeatherCondition.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Threading.Tasks; |
|
6 |
|
|
7 |
namespace ServerApp.Connection.XMLProtocolHandler |
|
8 |
{ |
|
9 |
public enum WeatherCondition |
|
10 |
{ |
|
11 |
Sunny, Cloudy, Overcast, Dark |
|
12 |
} |
|
13 |
} |
Server/ServerApp/Connection/XMLProtocolHandler/XmlCommunication.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.IO; |
|
4 |
using System.Linq; |
|
5 |
using System.Text; |
|
6 |
using System.Threading.Tasks; |
|
7 |
using System.Xml.Serialization; |
|
8 |
|
|
9 |
namespace ServerApp.Connection.XMLProtocolHandler |
|
10 |
{ |
|
11 |
public class XmlCommunication |
|
12 |
{ |
|
13 |
public static string Serialize<T>(T toSerialize) |
|
14 |
{ |
|
15 |
var xml = new XmlSerializer(toSerialize.GetType()); |
|
16 |
|
|
17 |
using (var stream = new StringWriter()) |
|
18 |
{ |
|
19 |
xml.Serialize(stream, toSerialize); |
|
20 |
return stream.ToString(); |
|
21 |
} |
|
22 |
} |
|
23 |
|
|
24 |
public static T Deserialize<T>(T toDeserialize, string xmlString) |
|
25 |
{ |
|
26 |
using (var stream = new StringReader(xmlString)) |
|
27 |
{ |
|
28 |
var xml = new XmlSerializer(toDeserialize.GetType()); |
|
29 |
T deserialized = (T)xml.Deserialize(stream); |
|
30 |
return deserialized; |
|
31 |
} |
|
32 |
} |
|
33 |
} |
|
34 |
} |
Server/ServerApp/Program.cs | ||
---|---|---|
1 | 1 |
using ServerApp.Connection; |
2 |
using ServerApp.Connection.XMLProtocolHandler; |
|
2 | 3 |
using ServerApp.DataDownload; |
3 | 4 |
using ServerApp.Parser.Parsers; |
4 | 5 |
using ServerApp.Predictor; |
... | ... | |
46 | 47 |
//DataParser p = new DataParser("data/"); |
47 | 48 |
//p.Parse(); |
48 | 49 |
|
49 |
|
|
50 |
#region UNCOMMENT |
|
50 | 51 |
//test scenario -data download: |
51 | 52 |
DataDownloader dd = new DataDownloader(config.DataRootDir, config.DataWebsite, config.DownloadedFilesNaming); |
52 | 53 |
dd.OverwriteExisting = false; |
53 | 54 |
List<string> savedFiles = new List<string>(); |
54 |
savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new Date(1, 2019), new Date(12, 2020)));
|
|
55 |
savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new Date(1, 2017), new Date(12, 2020)));
|
|
56 |
savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new Date(1, 2017), new Date(12, 2020)));
|
|
55 |
savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new DataDownload.Date(1, 2019), new DataDownload.Date(12, 2020)));
|
|
56 |
savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2020)));
|
|
57 |
savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2020)));
|
|
57 | 58 |
|
58 | 59 |
|
59 | 60 |
|
... | ... | |
69 | 70 |
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); |
70 | 71 |
} |
71 | 72 |
|
72 |
List<string> retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], new Date(10, 2019), new Date(12, 2020));
|
|
73 |
List<string> retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], new DataDownload.Date(10, 2019), new DataDownload.Date(12, 2020));
|
|
73 | 74 |
Console.WriteLine("Retrieved data: "); |
74 | 75 |
foreach (string s in retrievedData) |
75 | 76 |
{ |
... | ... | |
79 | 80 |
// test - connection: |
80 | 81 |
ConnectionListener cl = new ConnectionListener(int.Parse(config.Port)); |
81 | 82 |
cl.StartListening(); |
83 |
#endregion |
|
84 |
|
|
85 |
#region XML_TEST |
|
86 |
//Response response = Response.Randomize(); |
|
87 |
//var xml = XmlCommunication.Serialize(response); |
|
88 |
//Console.WriteLine(xml); |
|
89 |
//Response responseDeserialized = XmlCommunication.Deserialize(response, xml); |
|
90 |
|
|
91 |
//Console.WriteLine("------"); |
|
92 |
|
|
93 |
|
|
94 |
//Request request = Request.Randomize(); |
|
95 |
//xml = XmlCommunication.Serialize(request); |
|
96 |
//Console.WriteLine(xml); |
|
97 |
//Request requestDeserialized = XmlCommunication.Deserialize(request, xml); |
|
98 |
#endregion |
|
99 |
|
|
100 |
|
|
101 |
|
|
82 | 102 |
|
83 | 103 |
Console.ReadLine(); |
84 | 104 |
} |
Server/ServerApp/ServerApp.csproj | ||
---|---|---|
131 | 131 |
</ItemGroup> |
132 | 132 |
<ItemGroup> |
133 | 133 |
<Compile Include="Connection\ConnectionListener.cs" /> |
134 |
<Compile Include="Connection\XMLProtocolHandler\Buildings.cs" /> |
|
135 |
<Compile Include="Connection\XMLProtocolHandler\Date.cs" /> |
|
136 |
<Compile Include="Connection\XMLProtocolHandler\Prediction.cs" /> |
|
137 |
<Compile Include="Connection\XMLProtocolHandler\Request.cs" /> |
|
138 |
<Compile Include="Connection\XMLProtocolHandler\Response.cs" /> |
|
139 |
<Compile Include="Connection\XMLProtocolHandler\WeatherCondition.cs" /> |
|
140 |
<Compile Include="Connection\XMLProtocolHandler\XmlCommunication.cs" /> |
|
134 | 141 |
<Compile Include="DataDownload\DataDownloader.cs" /> |
135 | 142 |
<Compile Include="DataDownload\Date.cs" /> |
136 | 143 |
<Compile Include="Parser\InputData\CsvDataLoader.cs" /> |
Také k dispozici: Unified diff
Re #8836. Added XML protocol building / parsing.