Repository navigation
Expand file tree
/
Copy pathBasicMessage.cpp
More file actions
462 lines (400 loc) · 16 KB
/
Copy pathBasicMessage.cpp
File metadata and controls
462 lines (400 loc) · 16 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* ***** BEGIN LICENSE BLOCK *****
* Version: MIT
*
* Copyright (c) 2010-2013 Alan Antonuk
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* ***** END LICENSE BLOCK *****
*/
// Put these first to avoid warnings about INT#_C macro redefinition
#include <amqp.h>
#include <amqp_framing.h>
#include "SimpleAmqpClient/BasicMessage.h"
#include "SimpleAmqpClient/TableImpl.h"
#include <cstring>
namespace AmqpClient {
namespace Detail {
class BasicMessageImpl {
public:
BasicMessageImpl() : m_properties(), m_body() {}
amqp_basic_properties_t m_properties;
amqp_bytes_t m_body;
amqp_pool_ptr_t m_table_pool;
};
}
BasicMessage::BasicMessage() : m_impl(new Detail::BasicMessageImpl) {
m_impl->m_body.bytes = NULL;
m_impl->m_body.len = 0;
m_impl->m_properties._flags = 0;
}
BasicMessage::BasicMessage(const std::string &body)
: m_impl(new Detail::BasicMessageImpl) {
Body(body);
m_impl->m_properties._flags = 0;
}
BasicMessage::BasicMessage(const amqp_bytes_t &body,
const amqp_basic_properties_t *properties)
: m_impl(new Detail::BasicMessageImpl) {
m_impl->m_body = body;
m_impl->m_properties = *properties;
if (ContentTypeIsSet())
m_impl->m_properties.content_type =
amqp_bytes_malloc_dup(m_impl->m_properties.content_type);
if (ContentEncodingIsSet())
m_impl->m_properties.content_encoding =
amqp_bytes_malloc_dup(m_impl->m_properties.content_encoding);
if (CorrelationIdIsSet())
m_impl->m_properties.correlation_id =
amqp_bytes_malloc_dup(m_impl->m_properties.correlation_id);
if (ReplyToIsSet())
m_impl->m_properties.reply_to =
amqp_bytes_malloc_dup(m_impl->m_properties.reply_to);
if (ExpirationIsSet())
m_impl->m_properties.expiration =
amqp_bytes_malloc_dup(m_impl->m_properties.expiration);
if (MessageIdIsSet())
m_impl->m_properties.message_id =
amqp_bytes_malloc_dup(m_impl->m_properties.message_id);
if (TypeIsSet())
m_impl->m_properties.type =
amqp_bytes_malloc_dup(m_impl->m_properties.type);
if (UserIdIsSet())
m_impl->m_properties.user_id =
amqp_bytes_malloc_dup(m_impl->m_properties.user_id);
if (AppIdIsSet())
m_impl->m_properties.app_id =
amqp_bytes_malloc_dup(m_impl->m_properties.app_id);
if (ClusterIdIsSet())
m_impl->m_properties.cluster_id =
amqp_bytes_malloc_dup(m_impl->m_properties.cluster_id);
if (HeaderTableIsSet())
m_impl->m_properties.headers = Detail::TableValueImpl::CopyTable(
m_impl->m_properties.headers, m_impl->m_table_pool);
}
BasicMessage::~BasicMessage() {
amqp_bytes_free(m_impl->m_body);
if (ContentTypeIsSet()) amqp_bytes_free(m_impl->m_properties.content_type);
if (ContentEncodingIsSet())
amqp_bytes_free(m_impl->m_properties.content_encoding);
if (CorrelationIdIsSet())
amqp_bytes_free(m_impl->m_properties.correlation_id);
if (ReplyToIsSet()) amqp_bytes_free(m_impl->m_properties.reply_to);
if (ExpirationIsSet()) amqp_bytes_free(m_impl->m_properties.expiration);
if (MessageIdIsSet()) amqp_bytes_free(m_impl->m_properties.message_id);
if (TypeIsSet()) amqp_bytes_free(m_impl->m_properties.type);
if (UserIdIsSet()) amqp_bytes_free(m_impl->m_properties.user_id);
if (AppIdIsSet()) amqp_bytes_free(m_impl->m_properties.app_id);
if (ClusterIdIsSet()) amqp_bytes_free(m_impl->m_properties.cluster_id);
}
const amqp_basic_properties_t *BasicMessage::getAmqpProperties() const {
return &m_impl->m_properties;
}
const amqp_bytes_t &BasicMessage::getAmqpBody() const { return m_impl->m_body; }
std::string BasicMessage::Body() const {
if (m_impl->m_body.bytes == NULL)
return std::string();
return std::string((char *)m_impl->m_body.bytes, m_impl->m_body.len);
}
void BasicMessage::Body(const std::string &body) {
if (NULL != m_impl->m_body.bytes) {
amqp_bytes_free(m_impl->m_body);
}
amqp_bytes_t body_bytes;
body_bytes.bytes = const_cast<char *>(body.data());
body_bytes.len = body.length();
m_impl->m_body = amqp_bytes_malloc_dup(body_bytes);
}
std::string BasicMessage::ContentType() const {
if (ContentTypeIsSet())
return std::string((char *)m_impl->m_properties.content_type.bytes,
m_impl->m_properties.content_type.len);
return std::string();
}
void BasicMessage::ContentType(const std::string &content_type) {
if (ContentTypeIsSet()) amqp_bytes_free(m_impl->m_properties.content_type);
m_impl->m_properties.content_type =
amqp_bytes_malloc_dup(amqp_cstring_bytes(content_type.c_str()));
m_impl->m_properties._flags |= AMQP_BASIC_CONTENT_TYPE_FLAG;
}
bool BasicMessage::ContentTypeIsSet() const {
return AMQP_BASIC_CONTENT_TYPE_FLAG ==
(m_impl->m_properties._flags & AMQP_BASIC_CONTENT_TYPE_FLAG);
}
void BasicMessage::ContentTypeClear() {
if (ContentTypeIsSet()) amqp_bytes_free(m_impl->m_properties.content_type);
m_impl->m_properties._flags &= ~AMQP_BASIC_CONTENT_TYPE_FLAG;
}
std::string BasicMessage::ContentEncoding() const {
if (ContentEncodingIsSet())
return std::string((char *)m_impl->m_properties.content_encoding.bytes,
m_impl->m_properties.content_encoding.len);
return std::string();
}
void BasicMessage::ContentEncoding(const std::string &content_encoding) {
if (ContentEncodingIsSet())
amqp_bytes_free(m_impl->m_properties.content_encoding);
m_impl->m_properties.content_encoding =
amqp_bytes_malloc_dup(amqp_cstring_bytes(content_encoding.c_str()));
m_impl->m_properties._flags |= AMQP_BASIC_CONTENT_ENCODING_FLAG;
}
bool BasicMessage::ContentEncodingIsSet() const {
return AMQP_BASIC_CONTENT_ENCODING_FLAG ==
(m_impl->m_properties._flags & AMQP_BASIC_CONTENT_ENCODING_FLAG);
}
void BasicMessage::ContentEncodingClear() {
if (ContentEncodingIsSet())
amqp_bytes_free(m_impl->m_properties.content_encoding);
m_impl->m_properties._flags &= ~AMQP_BASIC_CONTENT_ENCODING_FLAG;
}
BasicMessage::delivery_mode_t BasicMessage::DeliveryMode() const {
if (DeliveryModeIsSet())
return (delivery_mode_t)m_impl->m_properties.delivery_mode;
return (delivery_mode_t)0;
}
void BasicMessage::DeliveryMode(delivery_mode_t delivery_mode) {
m_impl->m_properties.delivery_mode = static_cast<uint8_t>(delivery_mode);
m_impl->m_properties._flags |= AMQP_BASIC_DELIVERY_MODE_FLAG;
}
bool BasicMessage::DeliveryModeIsSet() const {
return AMQP_BASIC_DELIVERY_MODE_FLAG ==
(m_impl->m_properties._flags & AMQP_BASIC_DELIVERY_MODE_FLAG);
}
void BasicMessage::DeliveryModeClear() {
m_impl->m_properties._flags &= ~AMQP_BASIC_DELIVERY_MODE_FLAG;
}
boost::uint8_t BasicMessage::Priority() const {
if (PriorityIsSet())
return m_impl->m_properties.priority;
return 0;
}
void BasicMessage::Priority(boost::uint8_t priority) {
m_impl->m_properties.priority = priority;
m_impl->m_properties._flags |= AMQP_BASIC_PRIORITY_FLAG;
}
bool BasicMessage::PriorityIsSet() const {
return AMQP_BASIC_PRIORITY_FLAG ==
(m_impl->m_properties._flags & AMQP_BASIC_PRIORITY_FLAG);
}
void BasicMessage::PriorityClear() {
m_impl->m_properties._flags &= ~AMQP_BASIC_PRIORITY_FLAG;
}
std::string BasicMessage::CorrelationId() const {
if (CorrelationIdIsSet())
return std::string((char *)m_impl->m_properties.correlation_id.bytes,
m_impl->m_properties.correlation_id.len);
return std::string();
}
void BasicMessage::CorrelationId(const std::string &correlation_id) {
if (CorrelationIdIsSet())
amqp_bytes_free(m_impl->m_properties.correlation_id);
m_impl->m_properties.correlation_id =
amqp_bytes_malloc_dup(amqp_cstring_bytes(correlation_id.c_str()));
m_impl->m_properties._flags |= AMQP_BASIC_CORRELATION_ID_FLAG;
}
bool BasicMessage::CorrelationIdIsSet() const {
return AMQP_BASIC_CORRELATION_ID_FLAG ==
(m_impl->m_properties._flags & AMQP_BASIC_CORRELATION_ID_FLAG);
}
void BasicMessage::CorrelationIdClear() {
if (CorrelationIdIsSet())
amqp_bytes_free(m_impl->m_properties.correlation_id);
m_impl->m_properties._flags &= ~AMQP_BASIC_CORRELATION_ID_FLAG;
}
std::string BasicMessage::ReplyTo() const {
if (ReplyToIsSet())
return std::string((char *)m_impl->m_properties.reply_to.bytes,
m_impl->m_properties.reply_to.len);
return std::string();
}
void BasicMessage::ReplyTo(const std::string &reply_to) {
if (ReplyToIsSet()) amqp_bytes_free(m_impl->m_properties.reply_to);
m_impl->m_properties.reply_to =
amqp_bytes_malloc_dup(amqp_cstring_bytes(reply_to.c_str()));
m_impl->m_properties._flags |= AMQP_BASIC_REPLY_TO_FLAG;
}
bool BasicMessage::ReplyToIsSet() const {
return AMQP_BASIC_REPLY_TO_FLAG ==
(m_impl->m_properties._flags & AMQP_BASIC_REPLY_TO_FLAG);
}
void BasicMessage::ReplyToClear() {
if (ReplyToIsSet()) amqp_bytes_free(m_impl->m_properties.reply_to);
m_impl->m_properties._flags &= ~AMQP_BASIC_REPLY_TO_FLAG;
}
std::string BasicMessage::Expiration() const {
if (ExpirationIsSet())
return std::string((char *)m_impl->m_properties.expiration.bytes,
m_impl->m_properties.expiration.len);
return std::string();
}
void BasicMessage::Expiration(const std::string &expiration) {
if (ExpirationIsSet()) amqp_bytes_free(m_impl->m_properties.expiration);
m_impl->m_properties.expiration =
amqp_bytes_malloc_dup(amqp_cstring_bytes(expiration.c_str()));
m_impl->m_properties._flags |= AMQP_BASIC_EXPIRATION_FLAG;
}
bool BasicMessage::ExpirationIsSet() const {
return AMQP_BASIC_EXPIRATION_FLAG ==
(m_impl->m_properties._flags & AMQP_BASIC_EXPIRATION_FLAG);
}
void BasicMessage::ExpirationClear() {
if (ExpirationIsSet()) amqp_bytes_free(m_impl->m_properties.expiration);
m_impl->m_properties._flags &= ~AMQP_BASIC_EXPIRATION_FLAG;
}
std::string BasicMessage::MessageId() const {
if (MessageIdIsSet())
return std::string((char *)m_impl->m_properties.message_id.bytes,
m_impl->m_properties.message_id.len);
return std::string();
}
void BasicMessage::MessageId(const std::string &message_id) {
if (MessageIdIsSet()) amqp_bytes_free(m_impl->m_properties.message_id);
m_impl->m_properties.message_id =
amqp_bytes_malloc_dup(amqp_cstring_bytes(message_id.c_str()));
m_impl->m_properties._flags |= AMQP_BASIC_MESSAGE_ID_FLAG;
}
bool BasicMessage::MessageIdIsSet() const {
return AMQP_BASIC_MESSAGE_ID_FLAG ==
(m_impl->m_properties._flags & AMQP_BASIC_MESSAGE_ID_FLAG);
}
void BasicMessage::MessageIdClear() {
if (MessageIdIsSet()) amqp_bytes_free(m_impl->m_properties.message_id);
m_impl->m_properties._flags &= ~AMQP_BASIC_MESSAGE_ID_FLAG;
}
boost::uint64_t BasicMessage::Timestamp() const {
if (TimestampIsSet())
return m_impl->m_properties.timestamp;
return 0;
}
void BasicMessage::Timestamp(boost::uint64_t timestamp) {
m_impl->m_properties.timestamp = timestamp;
m_impl->m_properties._flags |= AMQP_BASIC_TIMESTAMP_FLAG;
}
bool BasicMessage::TimestampIsSet() const {
return AMQP_BASIC_TIMESTAMP_FLAG ==
(m_impl->m_properties._flags & AMQP_BASIC_TIMESTAMP_FLAG);
}
void BasicMessage::TimestampClear() {
m_impl->m_properties._flags &= ~AMQP_BASIC_TIMESTAMP_FLAG;
}
std::string BasicMessage::Type() const {
if (TypeIsSet())
return std::string((char *)m_impl->m_properties.type.bytes,
m_impl->m_properties.type.len);
return std::string();
}
void BasicMessage::Type(const std::string &type) {
if (TypeIsSet()) amqp_bytes_free(m_impl->m_properties.type);
m_impl->m_properties.type =
amqp_bytes_malloc_dup(amqp_cstring_bytes(type.c_str()));
m_impl->m_properties._flags |= AMQP_BASIC_TYPE_FLAG;
}
bool BasicMessage::TypeIsSet() const {
return AMQP_BASIC_TYPE_FLAG ==
(m_impl->m_properties._flags & AMQP_BASIC_TYPE_FLAG);
}
void BasicMessage::TypeClear() {
if (TypeIsSet()) amqp_bytes_free(m_impl->m_properties.type);
m_impl->m_properties._flags &= ~AMQP_BASIC_TYPE_FLAG;
}
std::string BasicMessage::UserId() const {
if (UserIdIsSet())
return std::string((char *)m_impl->m_properties.user_id.bytes,
m_impl->m_properties.user_id.len);
return std::string();
}
void BasicMessage::UserId(const std::string &user_id) {
if (UserIdIsSet()) amqp_bytes_free(m_impl->m_properties.user_id);
m_impl->m_properties.user_id =
amqp_bytes_malloc_dup(amqp_cstring_bytes(user_id.c_str()));
m_impl->m_properties._flags |= AMQP_BASIC_USER_ID_FLAG;
}
bool BasicMessage::UserIdIsSet() const {
return AMQP_BASIC_USER_ID_FLAG ==
(m_impl->m_properties._flags & AMQP_BASIC_USER_ID_FLAG);
}
void BasicMessage::UserIdClear() {
if (UserIdIsSet()) amqp_bytes_free(m_impl->m_properties.user_id);
m_impl->m_properties._flags &= ~AMQP_BASIC_USER_ID_FLAG;
}
std::string BasicMessage::AppId() const {
if (AppIdIsSet())
return std::string((char *)m_impl->m_properties.app_id.bytes,
m_impl->m_properties.app_id.len);
return std::string();
}
void BasicMessage::AppId(const std::string &app_id) {
if (AppIdIsSet()) amqp_bytes_free(m_impl->m_properties.app_id);
m_impl->m_properties.app_id =
amqp_bytes_malloc_dup(amqp_cstring_bytes(app_id.c_str()));
m_impl->m_properties._flags |= AMQP_BASIC_APP_ID_FLAG;
}
bool BasicMessage::AppIdIsSet() const {
return AMQP_BASIC_APP_ID_FLAG ==
(m_impl->m_properties._flags & AMQP_BASIC_APP_ID_FLAG);
}
void BasicMessage::AppIdClear() {
if (AppIdIsSet()) amqp_bytes_free(m_impl->m_properties.app_id);
m_impl->m_properties._flags &= ~AMQP_BASIC_APP_ID_FLAG;
}
std::string BasicMessage::ClusterId() const {
if (ClusterIdIsSet())
return std::string((char *)m_impl->m_properties.cluster_id.bytes,
m_impl->m_properties.cluster_id.len);
return std::string();
}
void BasicMessage::ClusterId(const std::string &cluster_id) {
if (ClusterIdIsSet()) amqp_bytes_free(m_impl->m_properties.cluster_id);
m_impl->m_properties.cluster_id =
amqp_bytes_malloc_dup(amqp_cstring_bytes(cluster_id.c_str()));
m_impl->m_properties._flags |= AMQP_BASIC_CLUSTER_ID_FLAG;
}
bool BasicMessage::ClusterIdIsSet() const {
return AMQP_BASIC_CLUSTER_ID_FLAG ==
(m_impl->m_properties._flags & AMQP_BASIC_CLUSTER_ID_FLAG);
}
void BasicMessage::ClusterIdClear() {
if (ClusterIdIsSet()) amqp_bytes_free(m_impl->m_properties.cluster_id);
m_impl->m_properties._flags &= ~AMQP_BASIC_CLUSTER_ID_FLAG;
}
Table BasicMessage::HeaderTable() const {
if (HeaderTableIsSet())
return Detail::TableValueImpl::CreateTable(m_impl->m_properties.headers);
return Table();
}
void BasicMessage::HeaderTable(const Table &header_table) {
m_impl->m_properties.headers = Detail::TableValueImpl::CreateAmqpTable(
header_table, m_impl->m_table_pool);
m_impl->m_properties._flags |= AMQP_BASIC_HEADERS_FLAG;
}
bool BasicMessage::HeaderTableIsSet() const {
return AMQP_BASIC_HEADERS_FLAG ==
(m_impl->m_properties._flags & AMQP_BASIC_HEADERS_FLAG);
}
void BasicMessage::HeaderTableClear() {
if (HeaderTableIsSet()) {
m_impl->m_table_pool.reset();
m_impl->m_properties.headers.num_entries = 0;
m_impl->m_properties.headers.entries = NULL;
}
m_impl->m_properties._flags &= ~AMQP_BASIC_HEADERS_FLAG;
}
} // namespace AmqpClient