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
use crate::{
    message::{BasicGetMessage, BasicReturnMessage},
    options::*,
    types::{Boolean, FieldTable, LongUInt, ShortUInt},
    BasicProperties, ConfirmationFuture, Consumer, Error, ExchangeKind, Queue, Result,
};
use futures::Future;
use lapin::{Channel as InnerChannel, Connection};

/// `Channel` provides methods to act on a channel, such as managing queues
#[derive(Clone)]
pub struct Channel {
    inner: InnerChannel,
}

impl Channel {
    /// create a channel
    pub fn create(conn: &Connection) -> impl Future<Item = Self, Error = Error> {
        let confirmation: ConfirmationFuture<InnerChannel> = conn.create_channel().into();
        confirmation.map(|inner| Channel { inner })
    }

    pub fn id(&self) -> u16 {
        self.inner.id()
    }

    /// request access
    ///
    /// returns a future that resolves once the access is granted
    pub fn access_request(
        &self,
        realm: &str,
        options: AccessRequestOptions,
    ) -> ConfirmationFuture<()> {
        self.inner.access_request(realm, options).into()
    }

    /// declares an exchange
    ///
    /// returns a future that resolves once the exchange is available
    pub fn exchange_declare(
        &self,
        name: &str,
        exchange_type: ExchangeKind,
        options: ExchangeDeclareOptions,
        arguments: FieldTable,
    ) -> ConfirmationFuture<()> {
        self.inner
            .exchange_declare(name, exchange_type, options, arguments)
            .into()
    }

    /// deletes an exchange
    ///
    /// returns a future that resolves once the exchange is deleted
    pub fn exchange_delete(
        &self,
        name: &str,
        options: ExchangeDeleteOptions,
    ) -> ConfirmationFuture<()> {
        self.inner.exchange_delete(name, options).into()
    }

    /// binds an exchange to another exchange
    ///
    /// returns a future that resolves once the exchanges are bound
    pub fn exchange_bind(
        &self,
        destination: &str,
        source: &str,
        routing_key: &str,
        options: ExchangeBindOptions,
        arguments: FieldTable,
    ) -> ConfirmationFuture<()> {
        self.inner
            .exchange_bind(destination, source, routing_key, options, arguments)
            .into()
    }

    /// unbinds an exchange from another one
    ///
    /// returns a future that resolves once the exchanges are unbound
    pub fn exchange_unbind(
        &self,
        destination: &str,
        source: &str,
        routing_key: &str,
        options: ExchangeUnbindOptions,
        arguments: FieldTable,
    ) -> ConfirmationFuture<()> {
        self.inner
            .exchange_unbind(destination, source, routing_key, options, arguments)
            .into()
    }

    /// declares a queue
    ///
    /// returns a future that resolves once the queue is available
    ///
    /// the `mandatory` and `ìmmediate` options can be set to true,
    /// but the return message will not be handled
    pub fn queue_declare(
        &self,
        name: &str,
        options: QueueDeclareOptions,
        arguments: FieldTable,
    ) -> ConfirmationFuture<Queue> {
        self.inner.queue_declare(name, options, arguments).into()
    }

    /// binds a queue to an exchange
    ///
    /// returns a future that resolves once the queue is bound to the exchange
    pub fn queue_bind(
        &self,
        name: &str,
        exchange: &str,
        routing_key: &str,
        options: QueueBindOptions,
        arguments: FieldTable,
    ) -> ConfirmationFuture<()> {
        self.inner
            .queue_bind(name, exchange, routing_key, options, arguments)
            .into()
    }

    /// unbinds a queue from the exchange
    ///
    /// returns a future that resolves once the queue is unbound from the exchange
    pub fn queue_unbind(
        &self,
        name: &str,
        exchange: &str,
        routing_key: &str,
        arguments: FieldTable,
    ) -> ConfirmationFuture<()> {
        self.inner
            .queue_unbind(name, exchange, routing_key, arguments)
            .into()
    }

    /// sets up confirm extension for this channel
    pub fn confirm_select(&self, options: ConfirmSelectOptions) -> ConfirmationFuture<()> {
        self.inner.confirm_select(options).into()
    }

    /// specifies quality of service for a channel
    pub fn basic_qos(
        &self,
        prefetch_count: ShortUInt,
        options: BasicQosOptions,
    ) -> ConfirmationFuture<()> {
        self.inner.basic_qos(prefetch_count, options).into()
    }

    /// publishes a message on a queue
    pub fn basic_publish(
        &self,
        exchange: &str,
        routing_key: &str,
        payload: Vec<u8>,
        options: BasicPublishOptions,
        properties: BasicProperties,
    ) -> ConfirmationFuture<()> {
        self.inner
            .basic_publish(exchange, routing_key, options, payload, properties)
            .into()
    }

    /// creates a consumer stream
    ///
    /// returns a future of a `Consumer` that resolves once the method succeeds
    ///
    /// `Consumer` implements `futures::Stream`, so it can be used with any of
    /// the usual combinators
    pub fn basic_consume(
        &self,
        queue: &str,
        consumer_tag: &str,
        options: BasicConsumeOptions,
        arguments: FieldTable,
    ) -> impl Future<Item = Consumer, Error = Error> {
        let confirmation: ConfirmationFuture<lapin::Consumer> = self
            .inner
            .basic_consume(queue, consumer_tag, options, arguments)
            .into();
        confirmation.map(Consumer)
    }

    pub fn basic_cancel(
        &self,
        consumer_tag: &str,
        options: BasicCancelOptions,
    ) -> ConfirmationFuture<()> {
        self.inner.basic_cancel(consumer_tag, options).into()
    }

    pub fn basic_recover(&self, options: BasicRecoverOptions) -> ConfirmationFuture<()> {
        self.inner.basic_recover(options).into()
    }

    pub fn basic_recover_async(&self, options: BasicRecoverAsyncOptions) -> ConfirmationFuture<()> {
        self.inner.basic_recover_async(options).into()
    }

    /// acks a message
    pub fn basic_ack(&self, delivery_tag: u64, multiple: bool) -> ConfirmationFuture<()> {
        self.inner
            .basic_ack(delivery_tag, BasicAckOptions { multiple })
            .into()
    }

    /// nacks a message
    pub fn basic_nack(
        &self,
        delivery_tag: u64,
        multiple: bool,
        requeue: bool,
    ) -> ConfirmationFuture<()> {
        self.inner
            .basic_nack(delivery_tag, BasicNackOptions { multiple, requeue })
            .into()
    }

    /// rejects a message
    pub fn basic_reject(
        &self,
        delivery_tag: u64,
        options: BasicRejectOptions,
    ) -> ConfirmationFuture<()> {
        self.inner.basic_reject(delivery_tag, options).into()
    }

    /// gets a message
    pub fn basic_get(
        &self,
        queue: &str,
        options: BasicGetOptions,
    ) -> ConfirmationFuture<Option<BasicGetMessage>> {
        self.inner.basic_get(queue, options).into()
    }

    /// Purge a queue.
    ///
    /// This method removes all messages from a queue which are not awaiting acknowledgment.
    pub fn queue_purge(
        &self,
        queue_name: &str,
        options: QueuePurgeOptions,
    ) -> ConfirmationFuture<LongUInt> {
        self.inner.queue_purge(queue_name, options).into()
    }

    /// Delete a queue.
    ///
    /// This method deletes a queue. When a queue is deleted any pending messages are sent to a dead-letter queue
    /// if this is defined in the server configuration, and all consumers on the queue are cancelled.
    ///
    /// If `if_unused` is set, the server will only delete the queue if it has no consumers.
    /// If the queue has consumers the server does not delete it but raises a channel exception instead.
    ///
    /// If `if_empty` is set, the server will only delete the queue if it has no messages.
    pub fn queue_delete(
        &self,
        queue_name: &str,
        options: QueueDeleteOptions,
    ) -> ConfirmationFuture<LongUInt> {
        self.inner.queue_delete(queue_name, options).into()
    }

    /// closes the channel
    pub fn close(&self, code: u16, message: &str) -> ConfirmationFuture<()> {
        self.inner.close(code, message).into()
    }

    /// update a channel flow
    pub fn channel_flow(&self, options: ChannelFlowOptions) -> ConfirmationFuture<Boolean> {
        self.inner.channel_flow(options).into()
    }

    pub fn tx_select(&self) -> ConfirmationFuture<()> {
        self.inner.tx_select().into()
    }

    pub fn tx_commit(&self) -> ConfirmationFuture<()> {
        self.inner.tx_commit().into()
    }

    pub fn tx_rollback(&self) -> ConfirmationFuture<()> {
        self.inner.tx_rollback().into()
    }

    /// When publishers confirm is enabled, wait for pending confirmations and return the nacked
    /// messages
    pub fn wait_for_confirms(&self) -> ConfirmationFuture<Vec<BasicReturnMessage>, Result<()>> {
        self.inner.wait_for_confirms().into()
    }
}