-
Notifications
You must be signed in to change notification settings - Fork 38
/
AMQPMessage.m
157 lines (134 loc) · 6.52 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
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
//
// AMQPMessage.m
// This file is part of librabbitmq-objc.
// Copyright (C) 2014 *Prof. MAAD* aka Max Wolter
// librabbitmq-objc is released under the terms of the GNU Lesser General Public License Version 3.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#import "AMQPMessage.h"
# import <amqp.h>
# import <amqp_framing.h>
# define AMQP_BYTES_TO_NSSTRING(x) [[NSString alloc] initWithBytes:x.bytes length:x.len encoding:NSUTF8StringEncoding]
@implementation AMQPMessage
@synthesize body;
@synthesize contentType;
@synthesize contentEncoding;
@synthesize headers;
@synthesize deliveryMode;
@synthesize priority;
@synthesize correlationID;
@synthesize replyToQueueName;
@synthesize expiration;
@synthesize messageID;
@synthesize timestamp;
@synthesize type;
@synthesize userID;
@synthesize appID;
@synthesize clusterID;
@synthesize consumerTag;
@synthesize deliveryTag;
@synthesize redelivered;
@synthesize exchangeName;
@synthesize routingKey;
@synthesize read;
@synthesize receivedAt;
+ (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 autorelease];
}
- (id)initWithBody:(amqp_bytes_t)theBody withDeliveryProperties:(amqp_basic_deliver_t*)theDeliveryProperties withMessageProperties:(amqp_basic_properties_t*)theMessageProperties receivedAt:(NSDate*)receiveTimestamp
{
if(!theDeliveryProperties || !theMessageProperties) { return nil; }
if(self = [super init])
{
body = AMQP_BYTES_TO_NSSTRING(theBody);
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];
}
return self;
}
- (id)initWithAMQPMessage:(AMQPMessage*)theMessage
{
if(self = [super init])
{
body = [theMessage.body 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;
}
- (void)dealloc
{
[body release];
[consumerTag release];
[exchangeName release];
[routingKey release];
[contentType release];
[contentEncoding release];
[correlationID release];
[replyToQueueName release];
[expiration release];
[messageID release];
[type release];
[userID release];
[appID release];
[clusterID release];
[receivedAt release];
[super dealloc];
}
- (id)copyWithZone:(NSZone*)zone
{
AMQPMessage *newMessage = [[AMQPMessage allocWithZone:zone] initWithAMQPMessage:self];
return newMessage;
}
@end