forked from EFEducationFirstMobile/librabbitmq-objc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AMQPMessage.m
119 lines (102 loc) · 5.93 KB
/
AMQPMessage.m
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
//
// AMQPMessage.m
// Objective-C wrapper for librabbitmq-c
//
// Copyright 2009 Max Wolter. All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#import "AMQPMessage.h"
# define AMQP_BYTES_TO_NSSTRING(x) [[NSString alloc] initWithBytes:x.bytes length:x.len encoding:NSUTF8StringEncoding]
@implementation AMQPMessage
+ (AMQPMessage*)messageFromBody:(amqp_bytes_t)theBody withDeliveryProperties:(amqp_basic_deliver_t *)theDeliveryProperties withMessageProperties:(amqp_basic_properties_t *)theMessageProperties receivedAt:(NSDate *)receiveTimestamp
{
AMQPMessage *message = [[AMQPMessage alloc] initWithBody:theBody withDeliveryProperties:theDeliveryProperties withMessageProperties:theMessageProperties receivedAt:receiveTimestamp];
return message;
}
- (id)initWithBody:(amqp_bytes_t)theBody withDeliveryProperties:(amqp_basic_deliver_t *)theDeliveryProperties withMessageProperties:(amqp_basic_properties_t *)theMessageProperties receivedAt:(NSDate *)receiveTimestamp
{
if (!theMessageProperties) {
return nil;
}
if ((self = [super init])) {
if (theDeliveryProperties) {
_consumerTag = AMQP_BYTES_TO_NSSTRING(theDeliveryProperties->consumer_tag);
_deliveryTag = theDeliveryProperties->delivery_tag;
_redelivered = theDeliveryProperties->redelivered;
_exchangeName = AMQP_BYTES_TO_NSSTRING(theDeliveryProperties->exchange);
_routingKey = AMQP_BYTES_TO_NSSTRING(theDeliveryProperties->routing_key);
}
if (theMessageProperties->_flags & AMQP_BASIC_CONTENT_TYPE_FLAG) { _contentType = AMQP_BYTES_TO_NSSTRING(theMessageProperties->content_type); } else { _contentType = nil; }
if (theMessageProperties->_flags & AMQP_BASIC_CONTENT_ENCODING_FLAG) { _contentEncoding = AMQP_BYTES_TO_NSSTRING(theMessageProperties->content_encoding); } else { _contentEncoding = nil; }
if (theMessageProperties->_flags & AMQP_BASIC_HEADERS_FLAG) { _headers = theMessageProperties->headers; } else { _headers = AMQP_EMPTY_TABLE; }
if (theMessageProperties->_flags & AMQP_BASIC_DELIVERY_MODE_FLAG) { _deliveryMode = theMessageProperties->delivery_mode; } else { _deliveryMode = 0; }
if (theMessageProperties->_flags & AMQP_BASIC_PRIORITY_FLAG) { _priority = theMessageProperties->priority; } else { _priority = 0; }
if (theMessageProperties->_flags & AMQP_BASIC_CORRELATION_ID_FLAG) { _correlationID = AMQP_BYTES_TO_NSSTRING(theMessageProperties->correlation_id); } else { _correlationID = nil; }
if (theMessageProperties->_flags & AMQP_BASIC_REPLY_TO_FLAG) { _replyToQueueName = AMQP_BYTES_TO_NSSTRING(theMessageProperties->reply_to); } else { _replyToQueueName = nil; }
if (theMessageProperties->_flags & AMQP_BASIC_EXPIRATION_FLAG) { _expiration = AMQP_BYTES_TO_NSSTRING(theMessageProperties->expiration); } else { _expiration = nil; }
if (theMessageProperties->_flags & AMQP_BASIC_MESSAGE_ID_FLAG) { _messageID = AMQP_BYTES_TO_NSSTRING(theMessageProperties->message_id); } else { _messageID = nil; }
if (theMessageProperties->_flags & AMQP_BASIC_TIMESTAMP_FLAG) { _timestamp = theMessageProperties->timestamp; } else { _timestamp = 0; }
if (theMessageProperties->_flags & AMQP_BASIC_TYPE_FLAG) { _type = AMQP_BYTES_TO_NSSTRING(theMessageProperties->type); } else { _type = nil; }
if (theMessageProperties->_flags & AMQP_BASIC_USER_ID_FLAG) { _userID = AMQP_BYTES_TO_NSSTRING(theMessageProperties->user_id); } else { _userID = nil; }
if (theMessageProperties->_flags & AMQP_BASIC_APP_ID_FLAG) { _appID = AMQP_BYTES_TO_NSSTRING(theMessageProperties->app_id); } else { _appID = nil; }
if (theMessageProperties->_flags & AMQP_BASIC_CLUSTER_ID_FLAG) { _clusterID = AMQP_BYTES_TO_NSSTRING(theMessageProperties->cluster_id); } else { _clusterID = nil; }
_read = NO;
_receivedAt = [receiveTimestamp copy];
if (!_contentType || [_contentType isEqualToString:@"t"]) {
_body = AMQP_BYTES_TO_NSSTRING(theBody);
}
else if ([_contentType isEqualToString:@"b"]) {
_data = [NSData dataWithBytes:theBody.bytes length:theBody.len];
}
else {
_body = AMQP_BYTES_TO_NSSTRING(theBody);
}
}
return self;
}
- (id)initWithAMQPMessage:(AMQPMessage *)theMessage
{
if ((self = [super init])) {
_body = [theMessage.body copy];
_data = [theMessage.data copy];
_consumerTag = [theMessage.consumerTag copy];
_deliveryTag = theMessage.deliveryTag;
_redelivered = theMessage.redelivered;
_exchangeName = [theMessage.exchangeName copy];
_routingKey = [theMessage.routingKey copy];
_contentType = [theMessage.contentType copy];
_contentEncoding = [theMessage.contentEncoding copy];
_headers = theMessage.headers;
_deliveryMode = theMessage.deliveryMode;
_priority = theMessage.priority;
_correlationID = [theMessage.correlationID copy];
_replyToQueueName = [theMessage.replyToQueueName copy];
_expiration = [theMessage.expiration copy];
_messageID = [theMessage.messageID copy];
_timestamp = theMessage.timestamp;
_type = [theMessage.type copy];
_userID = [theMessage.userID copy];
_appID = [theMessage.appID copy];
_clusterID = [theMessage.clusterID copy];
_read = theMessage.read;
_receivedAt = [theMessage.receivedAt copy];
}
return self;
}
- (id)copyWithZone:(NSZone*)zone
{
AMQPMessage *newMessage = [[AMQPMessage allocWithZone:zone] initWithAMQPMessage:self];
return newMessage;
}
@end