-
Notifications
You must be signed in to change notification settings - Fork 4
/
rabbit.h
343 lines (296 loc) · 9.07 KB
/
rabbit.h
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
* Rabbit.h
*
* Wrap a rabbitmq connection
*
* @author Toon Schoenmakers <[email protected]>
* @copyright 2015 - 2016 Copernica BV
*/
/**
* Include guard
*/
#pragma once
/**
* Dependencies
*/
#include <phpcpp.h>
#include <amqpcpp.h>
#include <copernica/nosql.h>
#include "json/object.h"
#include "descriptors.h"
#include "loop.h"
#include "tcphandler.h"
/**
* Class definition
*/
class Rabbit : private AMQP::TcpHandler, public TcpHandler
{
private:
/**
* The AMQP address
* @var AMQP::Address
*/
AMQP::Address _address;
/**
* The name of the exchange to which messages are published
* @var std::string
*/
std::string _exchange;
/**
* The name of the queues for mapreduce, regular and race jobs
* @var std::string
*/
std::string _mapreduce;
std::string _races;
std::string _jobs;
/**
* The file descriptors used by the connection
* @var Descriptors
*/
Descriptors _descriptors;
/**
* The underlying TCP connection
* @var AMQP::TcpConnection
*/
std::unique_ptr<AMQP::TcpConnection> _rabbit;
/**
* The error that was discovered
* @var std::string
*/
std::string _error;
/**
* The nosql connection
* @var NoSql::Connection
*/
std::unique_ptr<Copernica::NoSql::Connection> _nosql;
/**
* Should we also use this rabbitmq connection as the feedback channel?
* @var bool
*/
bool _feedback = false;
/**
* Called when connection is in error state
* @param connection The connection that entered the error state
* @param message Error message
*/
virtual void onError(AMQP::TcpConnection *connection, const char *message) override
{
// store the error
_error.assign(message);
// reset the connection
_rabbit = nullptr;
}
/**
* Called when connection is established
* @param connection The connection that can now be used
*/
virtual void onConnected(AMQP::TcpConnection *connection) override
{
// store the connection
_rabbit.reset(connection);
}
/**
* Callback called when connection is closed
* @param connection The connection that was closed and that is now unusable
*/
virtual void onClosed(AMQP::TcpConnection *connection) override
{
// reset connection
_rabbit = nullptr;
}
/**
* Monitor a filedescriptor for readability or writability
* @param connection The TCP connection object that is reporting
* @param fd The filedescriptor to be monitored
* @param flags Should the object be monitored for readability or writability?
*/
virtual void monitor(AMQP::TcpConnection *connection, int fd, int flags) override
{
// add the filedescriptor to the loop
_descriptors.add(fd, flags);
}
/**
* Create the AMQP connection
* @return bool
*/
bool connect()
{
// not necessary if already connected
if (_rabbit) return true;
// create the connection (it will the stored as a member in the onConnected() method)
auto *connection = new AMQP::TcpConnection(this, _address);
// keep running the event loop, until the connection is valid
while (!_rabbit && _error.empty())
{
// construct the event loop
Loop loop(_descriptors);
// run it
loop.step(connection);
}
// was the connection stored as member?
if (_rabbit) return true;
// report error to PHP space
if (!_error.empty()) Php::warning << _error << std::endl;
// done
return false;
}
public:
/**
* Constructor
*
* Watch out: throws an exception when connection could not be established
*
* @param address rabbitmq address
* @param exchange rabbitmq exchange
* @param mapreduce queue for mapreduce jobs
* @param races queue for race jobs
* @param jobs queue for regular jobs
* @param feedback use this connection also to receive the feedback / results
*
* @throws std::runtime_error
*
* @todo why std::string?
*/
Rabbit(std::string address, std::string exchange, std::string mapreduce, std::string races, std::string jobs, bool feedback) :
_address(std::move(address)),
_exchange(std::move(exchange)),
_mapreduce(std::move(mapreduce)),
_races(std::move(races)),
_jobs(std::move(jobs)),
_feedback(feedback)
{
// construct a connection
auto *connection = new AMQP::TcpConnection(this, _address);
// keep running the event loop, until the connection is valid
while (!_rabbit && _error.empty())
{
// construct the event loop
Loop loop(_descriptors);
// run it
loop.step(connection);
}
// was the connection stored as member?
if (_rabbit) return;
// we apparently have a failure
delete connection;
// connection was reset, this means that the onError() method was called
throw std::runtime_error(_error);
}
/**
* Destructor
*/
virtual ~Rabbit()
{
// nothing to do for us when we're not connected
if (!_rabbit) return;
// close the connection
_rabbit->close();
// create the event loop
Loop loop(_descriptors);
// run the loop (it only contains the connection, so it runs until the connection is closed)
loop.run(_rabbit.get());
};
/**
* Expose the filedescriptors
* @return Descriptors
*/
virtual const Descriptors &descriptors() const override
{
// expose member
return _descriptors;
}
/**
* Method that is called when a filedescriptor becomes active
* @param fd the filedescriptor that is active
* @param flags type of activity (readable or writalble)
*/
virtual void process(int fd, int flags) override
{
// pass on to the underlying AMQP connection
if (_rabbit != nullptr) _rabbit->process(fd, flags);
}
/**
* Method to publish a JSON encoded message to the mapreduce queue
* @param json The JSON data to be published
* @return bool
*/
bool mapreduce(const JSON::Object &json)
{
// publish to the mapreduce queue
return publish(_mapreduce, json);
}
/**
* Method to publish a JSON encoded message to the race queue
* @param json The JSON data to be published
* @return bool
*/
bool race(const JSON::Object &json)
{
// publish to the race queue
return publish(_races, json);
}
/**
* Method to publish a JSON encoded message to the jobs queue
* @param json The JSON data to be published
* @return bool
*/
bool job(const JSON::Object &json)
{
// publish to the mapreduce queue
return publish(_jobs, json);
}
/**
* Method to publish a JSON encoded message to the queue
* @param queue The name of the queue to publish to
* @param json The JSON data to be published
* @return bool
*/
bool publish(const std::string &queue, const JSON::Object &json)
{
// create the connection to the RabbitMQ server
if (!connect()) return false;
// create temporary channel, so that possible errors do not affect the connection
AMQP::TcpChannel channel(_rabbit.get());
// publish the json
channel.publish(_exchange, queue, json.toString());
// done
return true;
}
/**
* Flush the connection
* This runs the event loop until everything has been sent
*/
void flush()
{
// flush is pointless without a connection
if (!_rabbit) return;
// create an event loop with just these file descriptors
Loop loop(_descriptors);
// step through the loop as long as we have active channels
// we're working in such a way that all the channels drop away the second
// they're no longer needed, meaning that we've pushed/retrieved everything
// from rabbitmq the second we have no channels left
while (_rabbit->channels() > 0) loop.step(_rabbit.get());
}
/**
* Expose the connection
* Returns nullptr on error
* @return AMQP::TcpConnection
*/
AMQP::TcpConnection *connection()
{
// connect object
if (!connect()) return nullptr;
// retrieve the connection
return _rabbit.get();
}
/**
* Is it ok to use this rabbitmq connection for feedback too?
* @return bool
*/
bool feedback() const
{
// expose member
return _feedback;
}
};