-
Notifications
You must be signed in to change notification settings - Fork 11
/
KafkaClient.cs
239 lines (205 loc) · 9.43 KB
/
KafkaClient.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
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
namespace Opc.Ua.Cloud.Publisher.Configuration
{
using Confluent.Kafka;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Opc.Ua.Cloud.Publisher.Interfaces;
using Opc.Ua.Cloud.Publisher.Models;
using System;
using System.Text;
using System.Threading.Tasks;
public class KafkaClient : IBrokerClient
{
private IProducer<Null, string> _producer = null;
private IProducer<Null, string> _altProducer = null;
private IConsumer<Ignore, byte[]> _consumer = null;
private readonly ILogger _logger;
private readonly ICommandProcessor _commandProcessor;
public KafkaClient(ILoggerFactory loggerFactory, ICommandProcessor commandProcessor)
{
_logger = loggerFactory.CreateLogger("KafkaClient");
_commandProcessor = commandProcessor;
}
public void Connect(bool altBroker = false)
{
try
{
// disconnect if still connected
if (_producer != null)
{
_producer.Flush();
_producer.Dispose();
_producer = null;
Diagnostics.Singleton.Info.ConnectedToBroker = false;
}
if (_altProducer != null)
{
_altProducer.Flush();
_altProducer.Dispose();
_altProducer = null;
}
if (_consumer != null)
{
_consumer.Close();
_consumer.Dispose();
_consumer = null;
}
if (string.IsNullOrEmpty(Settings.Instance.BrokerUrl))
{
// no broker URL configured = nothing to connect to!
_logger.LogError("Broker URL not configured. Cannot connect to broker!");
return;
}
// create Kafka client
var config = new ProducerConfig
{
BootstrapServers = Settings.Instance.BrokerUrl + ":" + Settings.Instance.BrokerPort,
MessageTimeoutMs = 10000,
SecurityProtocol = SecurityProtocol.SaslSsl,
SaslMechanism = SaslMechanism.Plain,
SaslUsername = Settings.Instance.BrokerUsername,
SaslPassword = Settings.Instance.BrokerPassword
};
_producer = new ProducerBuilder<Null, string>(config).Build();
if (Settings.Instance.UseAltBrokerForMetadata)
{
var altConfig = new ProducerConfig
{
BootstrapServers = Settings.Instance.AltBrokerUrl + ":" + Settings.Instance.AltBrokerPort,
MessageTimeoutMs = 10000,
SecurityProtocol = SecurityProtocol.SaslSsl,
SaslMechanism = SaslMechanism.Plain,
SaslUsername = Settings.Instance.AltBrokerUsername,
SaslPassword = Settings.Instance.AltBrokerPassword
};
_altProducer = new ProducerBuilder<Null, string>(altConfig).Build();
}
if (!string.IsNullOrEmpty(Settings.Instance.BrokerCommandTopic))
{
var conf = new ConsumerConfig
{
GroupId = Settings.Instance.PublisherName,
BootstrapServers = Settings.Instance.BrokerUrl + ":" + Settings.Instance.BrokerPort,
AutoOffsetReset = AutoOffsetReset.Earliest,
SecurityProtocol = SecurityProtocol.SaslSsl,
SaslMechanism = SaslMechanism.Plain,
SaslUsername = Settings.Instance.BrokerUsername,
SaslPassword = Settings.Instance.BrokerPassword
};
_consumer = new ConsumerBuilder<Ignore, byte[]>(conf).Build();
_consumer.Subscribe(Settings.Instance.BrokerCommandTopic);
_ = Task.Run(() => HandleCommand());
}
Diagnostics.Singleton.Info.ConnectedToBroker = true;
_logger.LogInformation("Connected to Kafka broker.");
}
catch (Exception ex)
{
_logger.LogCritical("Failed to connect to Kafka broker: " + ex.Message);
}
}
public void Publish(byte[] payload)
{
Message<Null, string> message = new()
{
Headers = new Headers() { { "Content-Type", Encoding.UTF8.GetBytes("application/json") } },
Value = Encoding.UTF8.GetString(payload)
};
_producer.ProduceAsync(Settings.Instance.BrokerMessageTopic, message).GetAwaiter().GetResult();
}
public void PublishMetadata(byte[] payload)
{
Message<Null, string> message = new()
{
Headers = new Headers() { { "Content-Type", Encoding.UTF8.GetBytes("application/json") } },
Value = Encoding.UTF8.GetString(payload)
};
if (Settings.Instance.UseAltBrokerForMetadata)
{
_altProducer.ProduceAsync(Settings.Instance.BrokerMetadataTopic, message).GetAwaiter().GetResult();
}
else
{
_producer.ProduceAsync(Settings.Instance.BrokerMetadataTopic, message).GetAwaiter().GetResult();
}
}
public void PublishResponse(byte[] payload)
{
Message<Null, string> message = new()
{
Headers = new Headers() { { "Content-Type", Encoding.UTF8.GetBytes("application/json") } },
Value = Encoding.UTF8.GetString(payload)
};
_producer.ProduceAsync(Settings.Instance.BrokerResponseTopic, message).GetAwaiter().GetResult();
}
// handles all incoming commands form the cloud
private void HandleCommand()
{
while (true)
{
ResponseModel response = new()
{
TimeStamp = DateTime.UtcNow,
};
try
{
ConsumeResult<Ignore, byte[]> result = _consumer.Consume();
string requestPayload = Encoding.UTF8.GetString(result.Message.Value);
_logger.LogInformation($"Received method call with topic: {result.Topic} and payload: {requestPayload}");
// parse the message
RequestModel request = JsonConvert.DeserializeObject<RequestModel>(requestPayload);
// discard messages that are older than 15 seconds
if (request.TimeStamp < DateTime.UtcNow.AddSeconds(-15))
{
_logger.LogInformation($"Discarding old message with timestamp {request.TimeStamp}");
continue;
}
response.CorrelationId = request.CorrelationId;
// route this to the right handler
if (request.Command == "publishnodes")
{
response.Status = Encoding.UTF8.GetString(_commandProcessor.PublishNodes(requestPayload));
response.Success = true;
}
else if (request.Command == "unpublishnodes")
{
response.Status = Encoding.UTF8.GetString(_commandProcessor.UnpublishNodes(requestPayload));
response.Success = true;
}
else if (request.Command == "unpublishallnodes")
{
response.Status = Encoding.UTF8.GetString(_commandProcessor.UnpublishAllNodes());
response.Success = true;
}
else if (request.Command == "getpublishednodes")
{
response.Status = Encoding.UTF8.GetString(_commandProcessor.GetPublishedNodes());
response.Success = true;
}
else if (request.Command == "getinfo")
{
response.Status = Encoding.UTF8.GetString(_commandProcessor.GetInfo());
response.Success = true;
}
else
{
_logger.LogError("Unknown command received: " + result.Topic);
response.Status = "Unkown command " + result.Topic;
response.Success = false;
}
// send reponse to Kafka broker
Publish(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response)));
}
catch (Exception ex)
{
_logger.LogError(ex, "HandleMessageAsync");
response.Status = ex.Message;
response.Success = false;
// send error to Kafka broker
Publish(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response)));
}
}
}
}
}