Skip to content

Commit 296126d

Browse files
committed
Breaking change: send/receive data, not strings.
Issue #46 [#124585511]
1 parent 4319b93 commit 296126d

20 files changed

+144
-131
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Test-driven from Swift and implemented in Objective-C, to avoid burdening Object
2121
- [x] PKCS12 client certificates using the [TLS auth mechanism plugin](https://github.com/rabbitmq/rabbitmq-auth-mechanism-ssl)
2222
- [ ] [PKCS12 client certificates using chained CAs](https://github.com/rabbitmq/rabbitmq-objc-client/issues/74)
2323
- [ ] [Publisher confirmations](https://github.com/rabbitmq/rabbitmq-objc-client/issues/68)
24-
- [ ] [Publish and consume messages as data](https://github.com/rabbitmq/rabbitmq-objc-client/issues/46)
24+
- [x] [Publish and consume messages as data](https://github.com/rabbitmq/rabbitmq-objc-client/issues/46)
2525
- [ ] [Connection closure when broker doesn't send heartbeats fast enough](https://github.com/rabbitmq/rabbitmq-objc-client/issues/41)
2626
- [ ] [Customisable consumer hooks](https://github.com/rabbitmq/rabbitmq-objc-client/issues/71)
2727
- [ ] [basic.return support](https://github.com/rabbitmq/rabbitmq-objc-client/issues/72)
@@ -90,9 +90,9 @@ this list.
9090
```swift
9191
let q = ch.queue("myqueue")
9292
q.subscribe { m in
93-
print("Received: \(m.content)")
93+
print("Received: \(m.body)")
9494
}
95-
q.publish("foo")
95+
q.publish("foo".dataUsingEncoding(NSUTF8StringEncoding)!)
9696
```
9797

9898
1. Close the connection when done:

RMQClient/RMQAllocatedChannel.m

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ - (void)basicCancel:(NSString *)consumerTag {
242242
options:RMQBasicCancelNoOptions]];
243243
}
244244

245-
- (void)basicPublish:(NSString *)message
245+
- (void)basicPublish:(NSData *)body
246246
routingKey:(NSString *)routingKey
247247
exchange:(NSString *)exchange
248248
properties:(NSArray<RMQValue *> *)properties
@@ -251,8 +251,7 @@ - (void)basicPublish:(NSString *)message
251251
exchange:[[RMQShortstr alloc] init:exchange]
252252
routingKey:[[RMQShortstr alloc] init:routingKey]
253253
options:options];
254-
NSData *contentBodyData = [message dataUsingEncoding:NSUTF8StringEncoding];
255-
RMQContentBody *contentBody = [[RMQContentBody alloc] initWithData:contentBodyData];
254+
RMQContentBody *contentBody = [[RMQContentBody alloc] initWithData:body];
256255

257256
NSData *bodyData = contentBody.amqEncoded;
258257

@@ -282,15 +281,13 @@ - (void)basicGet:(NSString *)queue
282281
options:options]
283282
completionHandler:^(RMQFrameset *frameset) {
284283
RMQBasicGetOk *getOk = (RMQBasicGetOk *)frameset.method;
285-
NSString *messageContent = [[NSString alloc] initWithData:frameset.contentData
286-
encoding:NSUTF8StringEncoding];
287-
RMQMessage *message = [[RMQMessage alloc] initWithContent:messageContent
288-
consumerTag:@""
289-
deliveryTag:@(getOk.deliveryTag.integerValue)
290-
redelivered:getOk.options & RMQBasicGetOkRedelivered
291-
exchangeName:getOk.exchange.stringValue
292-
routingKey:getOk.routingKey.stringValue
293-
properties:frameset.contentHeader.properties];
284+
RMQMessage *message = [[RMQMessage alloc] initWithBody:frameset.contentData
285+
consumerTag:@""
286+
deliveryTag:@(getOk.deliveryTag.integerValue)
287+
redelivered:getOk.options & RMQBasicGetOkRedelivered
288+
exchangeName:getOk.exchange.stringValue
289+
routingKey:getOk.routingKey.stringValue
290+
properties:frameset.contentHeader.properties];
294291
userCompletionHandler(message);
295292
}];
296293
}
@@ -439,16 +436,15 @@ - (void)handleConfirmation:(RMQFrameset *)frameset {
439436

440437
- (void)handleBasicDeliver:(RMQFrameset *)frameset {
441438
RMQBasicDeliver *deliver = (RMQBasicDeliver *)frameset.method;
442-
NSString *content = [[NSString alloc] initWithData:frameset.contentData encoding:NSUTF8StringEncoding];
443439
RMQConsumer *consumer = self.consumers[deliver.consumerTag.stringValue];
444440
if (consumer) {
445-
RMQMessage *message = [[RMQMessage alloc] initWithContent:content
446-
consumerTag:deliver.consumerTag.stringValue
447-
deliveryTag:@(deliver.deliveryTag.integerValue)
448-
redelivered:deliver.options & RMQBasicDeliverRedelivered
449-
exchangeName:deliver.exchange.stringValue
450-
routingKey:deliver.routingKey.stringValue
451-
properties:frameset.contentHeader.properties];
441+
RMQMessage *message = [[RMQMessage alloc] initWithBody:frameset.contentData
442+
consumerTag:deliver.consumerTag.stringValue
443+
deliveryTag:@(deliver.deliveryTag.integerValue)
444+
redelivered:deliver.options & RMQBasicDeliverRedelivered
445+
exchangeName:deliver.exchange.stringValue
446+
routingKey:deliver.routingKey.stringValue
447+
properties:frameset.contentHeader.properties];
452448
consumer.handler(message);
453449
}
454450
}

RMQClient/RMQChannel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797

9898
- (void)basicCancel:(nonnull NSString *)consumerTag;
9999

100-
- (void)basicPublish:(nonnull NSString *)message
100+
- (void)basicPublish:(nonnull NSData *)body
101101
routingKey:(nonnull NSString *)routingKey
102102
exchange:(nonnull NSString *)exchange
103103
properties:(nonnull NSArray<RMQValue *> *)properties

RMQClient/RMQExchange.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,19 @@
7272
- (void)unbind:(RMQExchange *)source;
7373
- (void)delete:(RMQExchangeDeleteOptions)options;
7474
- (void)delete;
75-
- (void)publish:(NSString *)message
75+
- (void)publish:(NSData *)body
7676
routingKey:(NSString *)routingKey
7777
properties:(NSArray <RMQValue<RMQBasicValue> *> *)properties
7878
options:(RMQBasicPublishOptions)options;
79-
- (void)publish:(NSString *)message
79+
- (void)publish:(NSData *)body
8080
routingKey:(NSString *)key
8181
persistent:(BOOL)isPersistent
8282
options:(RMQBasicPublishOptions)options;
83-
- (void)publish:(NSString *)message
83+
- (void)publish:(NSData *)body
8484
routingKey:(NSString *)key
8585
persistent:(BOOL)isPersistent;
86-
- (void)publish:(NSString *)message
86+
- (void)publish:(NSData *)body
8787
routingKey:(NSString *)key;
88-
- (void)publish:(NSString *)message;
88+
- (void)publish:(NSData *)body;
8989

9090
@end

RMQClient/RMQExchange.m

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,50 +106,50 @@ - (void)delete {
106106
[self delete:RMQExchangeDeleteNoOptions];
107107
}
108108

109-
- (void)publish:(NSString *)message
109+
- (void)publish:(NSData *)body
110110
routingKey:(NSString *)routingKey
111111
properties:(NSArray<RMQValue<RMQBasicValue> *> *)properties
112112
options:(RMQBasicPublishOptions)options {
113-
[self.channel basicPublish:message
113+
[self.channel basicPublish:body
114114
routingKey:routingKey
115115
exchange:self.name
116116
properties:properties
117117
options:options];
118118
}
119119

120-
- (void)publish:(NSString *)message
120+
- (void)publish:(NSData *)body
121121
routingKey:(NSString *)key
122122
persistent:(BOOL)isPersistent
123123
options:(RMQBasicPublishOptions)options {
124124
NSMutableArray *properties = [NSMutableArray new];
125125
if (isPersistent) {
126126
[properties addObject:[[RMQBasicDeliveryMode alloc] init:2]];
127127
}
128-
[self.channel basicPublish:message
128+
[self.channel basicPublish:body
129129
routingKey:key
130130
exchange:self.name
131131
properties:properties
132132
options:options];
133133
}
134134

135-
- (void)publish:(NSString *)message
135+
- (void)publish:(NSData *)body
136136
routingKey:(NSString *)key
137137
persistent:(BOOL)isPersistent {
138-
[self publish:message
138+
[self publish:body
139139
routingKey:key
140140
persistent:isPersistent
141141
options:RMQBasicPublishNoOptions];
142142
}
143143

144-
- (void)publish:(NSString *)message
144+
- (void)publish:(NSData *)body
145145
routingKey:(NSString *)key {
146-
[self publish:message
146+
[self publish:body
147147
routingKey:key
148148
persistent:NO];
149149
}
150150

151-
- (void)publish:(NSString *)message {
152-
[self publish:message
151+
- (void)publish:(NSData *)body {
152+
[self publish:body
153153
routingKey:@""];
154154
}
155155

RMQClient/RMQMessage.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,21 @@
5353
#import "RMQBasicProperties.h"
5454

5555
@interface RMQMessage : RMQValue
56-
@property (nonatomic, readonly) NSString *content;
56+
@property (nonatomic, readonly) NSData *body;
5757
@property (nonatomic, readonly) NSString *consumerTag;
5858
@property (nonatomic, readonly) NSNumber *deliveryTag;
5959
@property (nonatomic, readonly) BOOL isRedelivered;
6060
@property (nonatomic, readonly) NSString *exchangeName;
6161
@property (nonatomic, readonly) NSString *routingKey;
6262
@property (nonatomic, readonly) NSArray *properties;
6363

64-
- (instancetype)initWithContent:(NSString *)content
65-
consumerTag:(NSString *)consumerTag
66-
deliveryTag:(NSNumber *)deliveryTag
67-
redelivered:(BOOL)isRedelivered
68-
exchangeName:(NSString *)exchangeName
69-
routingKey:(NSString *)routingKey
70-
properties:(NSArray<RMQValue<RMQBasicValue> *> *)properties;
64+
- (instancetype)initWithBody:(NSData *)body
65+
consumerTag:(NSString *)consumerTag
66+
deliveryTag:(NSNumber *)deliveryTag
67+
redelivered:(BOOL)isRedelivered
68+
exchangeName:(NSString *)exchangeName
69+
routingKey:(NSString *)routingKey
70+
properties:(NSArray<RMQValue<RMQBasicValue> *> *)properties;
7171

7272
- (NSString *)appID;
7373
- (NSString *)contentType;

RMQClient/RMQMessage.m

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
#import "RMQMessage.h"
5353

5454
@interface RMQMessage ()
55-
@property (nonatomic, readwrite) NSString *content;
55+
@property (nonatomic, readwrite) NSData *body;
5656
@property (nonatomic, readwrite) NSString *consumerTag;
5757
@property (nonatomic, readwrite) NSNumber *deliveryTag;
5858
@property (nonatomic, readwrite) BOOL isRedelivered;
@@ -63,16 +63,16 @@ @interface RMQMessage ()
6363

6464
@implementation RMQMessage
6565

66-
- (instancetype)initWithContent:(NSString *)content
67-
consumerTag:(NSString *)consumerTag
68-
deliveryTag:(NSNumber *)deliveryTag
69-
redelivered:(BOOL)isRedelivered
70-
exchangeName:(NSString *)exchangeName
71-
routingKey:(NSString *)routingKey
72-
properties:(NSArray<RMQValue<RMQBasicValue> *> *)properties {
66+
- (instancetype)initWithBody:(NSData *)body
67+
consumerTag:(NSString *)consumerTag
68+
deliveryTag:(NSNumber *)deliveryTag
69+
redelivered:(BOOL)isRedelivered
70+
exchangeName:(NSString *)exchangeName
71+
routingKey:(NSString *)routingKey
72+
properties:(NSArray<RMQValue<RMQBasicValue> *> *)properties {
7373
self = [super init];
7474
if (self) {
75-
self.content = content;
75+
self.body = body;
7676
self.consumerTag = consumerTag;
7777
self.deliveryTag = deliveryTag;
7878
self.isRedelivered = isRedelivered;

RMQClient/RMQQueue.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@
7575
- (void)unbind:(RMQExchange *)exchange;
7676
- (void)delete:(RMQQueueDeleteOptions)options;
7777
- (void)delete;
78-
- (void)publish:(NSString *)message
78+
- (void)publish:(NSData *)body
7979
properties:(NSArray <RMQValue<RMQBasicValue> *> *)properties
8080
options:(RMQBasicPublishOptions)options;
81-
- (void)publish:(NSString *)message
81+
- (void)publish:(NSData *)body
8282
persistent:(BOOL)isPersistent
8383
options:(RMQBasicPublishOptions)options;
84-
- (void)publish:(NSString *)message
84+
- (void)publish:(NSData *)body
8585
persistent:(BOOL)isPersistent;
86-
- (void)publish:(NSString *)message;
86+
- (void)publish:(NSData *)body;
8787
- (void)pop:(RMQConsumerDeliveryHandler)handler;
8888
- (RMQConsumer *)subscribe:(RMQConsumerDeliveryHandler)handler;
8989
- (RMQConsumer *)subscribe:(RMQBasicConsumeOptions)options

RMQClient/RMQQueue.m

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,37 +104,37 @@ - (void)delete {
104104
[self delete:RMQQueueDeleteNoOptions];
105105
}
106106

107-
- (void)publish:(NSString *)message
107+
- (void)publish:(NSData *)data
108108
properties:(NSArray<RMQValue<RMQBasicValue> *> *)properties
109109
options:(RMQBasicPublishOptions)options {
110-
[self.channel basicPublish:message
110+
[self.channel basicPublish:data
111111
routingKey:self.name
112112
exchange:@""
113113
properties:properties
114114
options:options];
115115
}
116116

117-
- (void)publish:(NSString *)message
117+
- (void)publish:(NSData *)body
118118
persistent:(BOOL)isPersistent
119119
options:(RMQBasicPublishOptions)options {
120120
NSMutableArray *properties = [NSMutableArray new];
121121
if (isPersistent) {
122122
[properties addObject:[[RMQBasicDeliveryMode alloc] init:2]];
123123
}
124-
[self.channel basicPublish:message
124+
[self.channel basicPublish:body
125125
routingKey:self.name
126126
exchange:@""
127127
properties:properties
128128
options:options];
129129
}
130130

131-
- (void)publish:(NSString *)message
131+
- (void)publish:(NSData *)body
132132
persistent:(BOOL)isPersistent {
133-
[self publish:message persistent:isPersistent options:RMQBasicPublishNoOptions];
133+
[self publish:body persistent:isPersistent options:RMQBasicPublishNoOptions];
134134
}
135135

136-
- (void)publish:(NSString *)message {
137-
[self publish:message persistent:NO];
136+
- (void)publish:(NSData *)body {
137+
[self publish:body persistent:NO];
138138
}
139139

140140
- (void)pop:(RMQConsumerDeliveryHandler)handler {

0 commit comments

Comments
 (0)