This repository has been archived by the owner on Sep 23, 2019. It is now read-only.
/
NachrichtenDispo.cs
148 lines (120 loc) · 4.45 KB
/
NachrichtenDispo.cs
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NLog;
using RailNet.Clients.Ecos.Network;
namespace RailNet.Clients.Ecos.Basic
{
/// <summary>
/// NachrichtenDispo wird vom BasicClient genutzt um auf den NetworkClient zuzugreifen.
/// Der NachrichtenDispo ordnet gesendeten Befehlen die passenden Antworten zu. Im NachrichtenDispo
/// findet das "awaiten" der Antwort auf Nachrichten statt.
/// </summary>
public class NachrichtenDispo : INachrichtenDispo
{
private readonly Logger logger = LogManager.GetCurrentClassLogger();
private readonly INetworkClient _networkClient;
private readonly IObservable<BasicResponse> incomingMessages;
private readonly IObservable<BasicEvent> incomingEvents;
/// <summary>
/// Eine IObservable von Serverseitigen Events
/// </summary>
public IObservable<BasicEvent> IncomingEvents
{
get { return incomingEvents; }
}
public NachrichtenDispo(INetworkClient networkClient)
{
_networkClient = networkClient;
// Rx :)
incomingMessages = Observable.FromEventPattern<MessageReceivedEventHandler, MessageReceivedEventArgs>(
h => _networkClient.MessageReceivedEvent += h,
h => _networkClient.MessageReceivedEvent -= h)
.Select(x => x.EventArgs.Content)
.Where(ValidateMessage)
.Where(x => GetMessageType(x) == MessageType.Reply)
.Select(ParseResponse);
incomingEvents = Observable.FromEventPattern<MessageReceivedEventHandler, MessageReceivedEventArgs>(
h => _networkClient.MessageReceivedEvent += h,
h => _networkClient.MessageReceivedEvent -= h)
.Select(x => x.EventArgs.Content)
.Where(ValidateMessage)
.Where(x => GetMessageType(x) == MessageType.Event)
.Select(ParseEvent);
}
#if DEBUG
private const int Timeout = 5;
#else
private const int Timeout = 2;
#endif
/// <summary>
/// Von ausserhalb Aufgerufene Methode zum senden einer Nachricht.
/// Sendet eine Nachricht und liefert die Antwort der Nachricht asynchron.
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
public Task<BasicResponse> SendCommandAsync(string command)
{
if (!_networkClient.Connected)
throw new IOException("Client nicht verbunden!");
var result = incomingMessages
.Where(reply => reply.Command == command)
.Timeout(TimeSpan.FromSeconds(Timeout))
.Take(1)
.ToTask();
_networkClient.SendMessage(command);
return result;
}
private BasicResponse ParseResponse(string[] message)
{
var response = new BasicResponse(message);
response.ExtractError();
return response;
}
private BasicEvent ParseEvent(string[] message)
{
var Event = new BasicEvent(message);
Event.ExtractError();
return Event;
}
// TODO: Mehr Validation noetig?
private bool ValidateMessage(string[] message)
{
return HasBeginAndEnd(message);
}
private bool HasBeginAndEnd(string[] message)
{
bool isValid = true;
if (!message[0].StartsWith("<") || !message[0].EndsWith(">"))
isValid = false;
if (!message.Last().StartsWith("<END"))
isValid = false;
return isValid;
}
private MessageType GetMessageType(string[] message)
{
if (message.Length < 2)
return MessageType.Undefined;
if (message[0].StartsWith("<REPLY "))
return MessageType.Reply;
if (message[0].StartsWith("<EVENT "))
return MessageType.Event;
return MessageType.Undefined;
}
}
internal enum MessageType
{
Undefined = 0,
Reply,
Event
}
}