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
/*
 * This file is part of Futures ZMQ.
 *
 * Copyright © 2018 Riley Trautman
 *
 * Futures ZMQ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Futures ZMQ 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Futures ZMQ.  If not, see <http://www.gnu.org/licenses/>.
 */

//! This module contains useful types for working with ZeroMQ Sockets.

pub mod config;
pub mod types;

use std::{fmt, sync::Arc};

use async_zmq_types::{InnerSocket, IntoInnerSocket, Multipart, SocketBuilder};
use zmq;

use crate::{
    async_types::{
        MultipartRequest, MultipartResponse, MultipartSink, MultipartSinkStream, MultipartStream,
    },
    polling::{LocalSession, SockId},
    RecvFuture, SendFuture,
};

/// Defines the raw Socket type. This type should never be interacted with directly, except to
/// create new instances of wrapper types.
pub struct Socket {
    sock: SockId,
    session: LocalSession,
}

impl Socket {
    /// Start a new Socket Config builder
    pub fn builder<T>(ctx: Arc<zmq::Context>) -> SocketBuilder<'static, T>
    where
        T: IntoInnerSocket,
    {
        SocketBuilder::new(ctx)
    }

    /// Retrieve a Reference-Counted Pointer to self's socket.
    pub fn inner(self) -> SockId {
        self.sock
    }

    /// Create a new socket from a given Sock and File
    ///
    /// This assumes that `sock` is already configured properly. Please don't call this directly
    /// unless you know what you're doing.
    pub fn from_sock_and_session(sock: SockId, session: LocalSession) -> Self {
        Socket { sock, session }
    }

    pub(crate) fn recv_msg(&self) -> RecvFuture {
        self.session.recv(&self.sock)
    }

    pub(crate) fn send_msg(&self, multipart: Multipart) -> SendFuture {
        self.session.send(&self.sock, multipart)
    }
}

impl<T> InnerSocket<T> for Socket
where
    T: IntoInnerSocket + From<Self>,
{
    type Request = MultipartRequest<T>;
    type Response = MultipartResponse<T>;

    type Sink = MultipartSink<T>;
    type Stream = MultipartStream<T>;

    type SinkStream = MultipartSinkStream<T>;

    fn send(self, multipart: Multipart) -> Self::Request {
        MultipartRequest::new(self, multipart)
    }

    fn recv(self) -> Self::Response {
        MultipartResponse::new(self)
    }

    fn stream(self) -> Self::Stream {
        MultipartStream::new(self)
    }

    fn sink(self, buffer_size: usize) -> Self::Sink {
        MultipartSink::new(self, buffer_size)
    }

    fn sink_stream(self, buffer_size: usize) -> Self::SinkStream {
        MultipartSinkStream::new(self, buffer_size)
    }
}

impl From<(SockId, LocalSession)> for Socket {
    fn from((sock, session): (SockId, LocalSession)) -> Self {
        Socket { sock, session }
    }
}

impl fmt::Debug for Socket {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Socket({:?})", self.sock)
    }
}

impl fmt::Display for Socket {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Socket({})", self.sock)
    }
}