-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIAMQPWorkerSSL.cpp
196 lines (182 loc) · 5.73 KB
/
IAMQPWorkerSSL.cpp
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
#include "agent/IAMQPWorkerSSL.hpp"
#include "agent/IConnectionHandler.hpp"
#include "agent/IWorker.hpp"
#include "agent/SymbolMaps.hpp"
#include <string>
#include <cstdint>
#include <amqpcpp.h>
#include <json/json.h>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
agent::IAMQPWorkerSSL::IAMQPWorkerSSL(
unsigned int _id,
IWorker *_iworker,
std::string _host,
std::uint16_t _port,
const std::string &_user,
const std::string &_pass,
const std::string &_vhost,
const std::string &_name,
const std::string &__queue,
const std::string &__exchange,
const std::string &__key,
const int __queueFlags,
const int __exchangeFlags,
std::uint32_t __prefetch,
AMQP::ExchangeType __exchangeType,
const std::string &__product,
const std::string &__version,
const std::string &__copyright,
const std::string &__information,
const std::string &__privateKeyFile,
const std::string &__certificateFile,
const std::string &__caLocation)
: _worker(_iworker),
_creds(_user, _pass),
_connection(this, _creds, _vhost),
_channel(&_connection),
_logger(nullptr),
_queue(__queue),
_exchange(__exchange),
_key(__key),
_queueFlags(__queueFlags),
_exchangeFlags(__exchangeFlags),
_prefetch(__prefetch),
_exchangeType(__exchangeType),
IConnectionHandlerSSL(
_id,
_host,
_port,
_name,
__privateKeyFile,
__certificateFile,
__caLocation,
__product,
__version,
__copyright,
__information)
{
// Create the logger first
_logger = spdlog::get(GetName());
if (_logger == nullptr)
_logger = spdlog::stdout_color_mt(GetName());
// Declare the queue and exchange and bind them
InitializeQueue();
// Set the worker callbacks
SetConsumerCallbacks();
// Start the threads
try
{
Run();
}
catch(const std::exception& e)
{
_logger->error("Exception caught: {}", e.what());
}
catch(...)
{
_logger->error("Anonymous exception caught");
}
}
agent::IAMQPWorkerSSL::IAMQPWorkerSSL(
unsigned int _id,
IWorker *_iworker,
Json::Value _config)
: _worker(_iworker),
_creds(
_config["credentials"]["username"].asString(),
_config["credentials"]["password"].asString()),
_connection(
this,
_creds,
_config["host"]["vhost"].asString()),
_channel(&_connection),
_logger(nullptr),
_queue(_config["settings"]["queue"].asString()),
_exchange(_config["settings"]["exchange"].asString()),
_key(_config["settings"]["key"].asString()),
_queueFlags([_config](){
int total = 0;
for (auto flag : _config["settings"]["queueFlags"])
total |= allBitFlags[flag.asString()];
return total; }()),
_exchangeFlags([_config](){
int total = 0;
for (auto flag : _config["settings"]["exchangeFlags"])
total |= allBitFlags[flag.asString()];
return total; }()),
_prefetch(_config["settings"]["prefetch"].asUInt()),
_exchangeType(exchangeTypeMap[_config["settings"]["exchangeType"].asString()]),
IConnectionHandlerSSL(
_id,
_config["host"]["host"].asString(),
_config["host"]["port"].asUInt(),
_config["name"].asString(),
_config["credentials"]["privateKeyFile"].asString(),
_config["credentials"]["certificateFile"].asString(),
_config["credentials"]["caLocation"].asString(),
_config["information"]["product"].asString(),
_config["information"]["version"].asString(),
_config["information"]["copyright"].asString(),
_config["information"]["information"].asString()
)
{
// Create the logger first
_logger = spdlog::get(GetName());
if (_logger == nullptr)
_logger = spdlog::stdout_color_mt(GetName());
// Declare the queue and exchange and bind them
InitializeQueue();
// Set the worker callbacks
SetConsumerCallbacks();
// Start the threads
try
{
Run();
}
catch(const std::exception& e)
{
_logger->error("Exception caught: {}", e.what());
}
catch(...)
{
_logger->error("Anonymous exception caught");
}
}
agent::IAMQPWorkerSSL::~IAMQPWorkerSSL()
{
_channel.close();
}
void agent::IAMQPWorkerSSL::InitializeQueue()
{
_channel.declareQueue(_queue, _queueFlags);
_channel.declareExchange(_exchange, _exchangeType, _exchangeFlags);
_channel.bindQueue(_exchange, _queue, _key);
_channel.setQos(_prefetch);
}
void agent::IAMQPWorkerSSL::SetConsumerCallbacks()
{
// Set the consumer callbacks here
_channel.consume(
_queue,
_key
).onReceived(
[this](const AMQP::Message& message, uint64_t tag, bool redelivered) {
_logger->info("[onReceived] Received message {}", tag);
_worker->AddMessage(message.body(), message.bodySize());
}
).onComplete(
[this](uint64_t tag, bool result) {
_logger->info("[onComplete] Finished message {}", tag);
_channel.ack(tag);
}
).onError(
[this](const char* message) {
_logger->error("[onError] {}", message);
}
);
}
void agent::IAMQPWorkerSSL::AddMessage(const void* _msg, std::uint32_t _size, std::string _exchange, std::string _key)
{
_channel.publish(_exchange, _key, static_cast<const char*>(_msg), _size, 0);
}