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
use amqpr_codec::{Frame, FrameHeader, FramePayload, AmqpString};
use amqpr_codec::content_body::ContentBodyPayload;
use amqpr_codec::content_header::{ContentHeaderPayload, Properties};
use amqpr_codec::method::MethodPayload;
use amqpr_codec::method::basic::{BasicClass, PublishMethod};

use bytes::Bytes;

use futures::{Future, Sink, Poll, Async};
use futures::sink::Send;

use common::Should;


/// Publish an item to AMQP server.
/// If you want to publish a lot number of items, please consider to use `publish_sink` function.
/// Returned item is `Future` which will be completed when finish to send.
pub fn publish<S>(channel_id: u16, socket: S, item: PublishItem) -> Published<S>
where
    S: Sink<SinkItem = Frame>,
{
    let (meta, header, body) = (item.meta, item.header, item.body);

    let declare = PublishMethod {
        reserved1: 0,
        exchange: meta.exchange,
        routing_key: meta.routing_key,
        mandatory: meta.is_mandatory,
        immediate: meta.is_immediate,
    };

    let frame = Frame {
        header: FrameHeader { channel: channel_id },
        payload: FramePayload::Method(MethodPayload::Basic(BasicClass::Publish(declare))),
    };

    debug!("Sending publish method : {:?}", frame);

    Published {
        state: SendingContentState::SendingPublishMethod(
            socket.send(frame),
            Should::new(header),
            Should::new(body),
        ),
        channel_id: channel_id,
    }
}



/// A meta option of `Publish` message on AMQP.
#[derive(Clone, Debug)]
pub struct PublishOption {
    pub exchange: AmqpString,
    pub routing_key: AmqpString,
    pub is_mandatory: bool,
    pub is_immediate: bool,
}


#[derive(Clone, Debug)]
pub struct PublishItem {
    pub meta: PublishOption,
    pub header: Properties,
    pub body: Bytes,
}



// Published struct {{{
pub struct Published<S>
where
    S: Sink<SinkItem = Frame>,
{
    state: SendingContentState<S>,
    channel_id: u16,
}

pub enum SendingContentState<S>
where
    S: Sink<SinkItem = Frame>,
{
    SendingPublishMethod(Send<S>, Should<Properties>, Should<Bytes>),
    SendingContentHeader(Send<S>, Should<Bytes>),
    SendingContentBody(Send<S>),
}


impl<S> Future for Published<S>
where
    S: Sink<SinkItem = Frame>,
{
    type Item = S;
    type Error = S::SinkError;

    fn poll(&mut self) -> Poll<S, S::SinkError> {

        use self::SendingContentState::*;
        self.state = match &mut self.state {
            &mut SendingPublishMethod(ref mut sending, ref mut properties, ref mut bytes) => {
                let socket = try_ready!(sending.poll());
                let header = ContentHeaderPayload {
                    class_id: 60,
                    body_size: bytes.as_ref().len() as u64,
                    properties: properties.take(),
                };
                let frame = Frame {
                    header: FrameHeader { channel: self.channel_id },
                    payload: FramePayload::ContentHeader(header),
                };
                debug!("Sent publish method");
                SendingContentHeader(socket.send(frame), bytes.clone())
            }

            &mut SendingContentHeader(ref mut sending, ref mut bytes) => {
                let socket = try_ready!(sending.poll());
                let frame = {
                    let payload = ContentBodyPayload { bytes: bytes.take() };
                    Frame {
                        header: FrameHeader { channel: self.channel_id },
                        payload: FramePayload::ContentBody(payload),
                    }
                };
                debug!("Sent content header");
                SendingContentBody(socket.send(frame))
            }

            &mut SendingContentBody(ref mut sending) => {
                let socket = try_ready!(sending.poll());
                debug!("Sent content body");
                return Ok(Async::Ready(socket));
            }
        };

        self.poll()
    }
}
// }}}