-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathAMQPConsumer.m
126 lines (97 loc) · 3.61 KB
/
AMQPConsumer.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
//
// AMQPConsumer.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 "AMQPConsumer.h"
# import <amqp.h>
# import <amqp_framing.h>
# import <string.h>
# import <stdlib.h>
# import "AMQPChannel.h"
# import "AMQPQueue.h"
# import "AMQPMessage.h"
@implementation AMQPConsumer
@synthesize internalConsumer = consumer;
@synthesize channel;
@synthesize queue;
- (id)initForQueue:(AMQPQueue*)theQueue onChannel:(AMQPChannel*)theChannel useAcknowledgements:(BOOL)ack isExclusive:(BOOL)exclusive receiveLocalMessages:(BOOL)local
{
if(self = [super init])
{
channel = [theChannel retain];
queue = [theQueue retain];
amqp_basic_consume_ok_t *response = amqp_basic_consume(channel.connection.internalConnection, channel.internalChannel, queue.internalQueue, AMQP_EMPTY_BYTES, !local, !ack, exclusive);
[channel.connection checkLastOperation:@"Failed to start consumer"];
consumer = amqp_bytes_malloc_dup(response->consumer_tag);
}
return self;
}
- (void)dealloc
{
amqp_bytes_free(consumer);
[channel release];
[queue release];
[super dealloc];
}
- (AMQPMessage*)pop
{
amqp_frame_t frame;
int result = -1;
size_t receivedBytes = 0;
size_t bodySize = -1;
amqp_bytes_t body;
amqp_basic_deliver_t *delivery;
amqp_basic_properties_t *properties;
AMQPMessage *message = nil;
amqp_maybe_release_buffers(channel.connection.internalConnection);
while(!message)
{
// a complete message delivery consists of at least three frames:
// Frame #1: method frame with method basic.deliver
result = amqp_simple_wait_frame(channel.connection.internalConnection, &frame);
if(result != 0) { return nil; }
if(frame.frame_type != AMQP_FRAME_METHOD || frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD) { continue; }
delivery = (amqp_basic_deliver_t*)frame.payload.method.decoded;
// Frame #2: header frame containing body size
result = amqp_simple_wait_frame(channel.connection.internalConnection, &frame);
if(result != 0) { return nil; }
if(frame.frame_type != AMQP_FRAME_HEADER)
{
return nil;
}
properties = (amqp_basic_properties_t*)frame.payload.properties.decoded;
bodySize = frame.payload.properties.body_size;
receivedBytes = 0;
body = amqp_bytes_malloc(bodySize);
// Frame #3+: body frames
while(receivedBytes < bodySize)
{
result = amqp_simple_wait_frame(channel.connection.internalConnection, &frame);
if(result != 0) { return nil; }
if(frame.frame_type != AMQP_FRAME_BODY)
{
return nil;
}
receivedBytes += frame.payload.body_fragment.len;
memcpy(body.bytes, frame.payload.body_fragment.bytes, frame.payload.body_fragment.len);
}
message = [AMQPMessage messageFromBody:body withDeliveryProperties:delivery withMessageProperties:properties receivedAt:[NSDate date]];
amqp_bytes_free(body);
}
return message;
}
@end