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
/*
 * This file is part of Async ZMQ Types.
 *
 * Copyright © 2018 Riley Trautman
 *
 * Async ZMQ Types 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.
 *
 * Async ZMQ Types 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 Async ZMQ Types.  If not, see <http://www.gnu.org/licenses/>.
 */

//! This module contains `SocketBuilder` and related types.

use std::{marker::PhantomData, sync::Arc};

use zmq;

use crate::{IntoInnerSocket, Pair, Sub, UnPair};

fn bind_all(sock: zmq::Socket, binds: &[&str]) -> zmq::Result<zmq::Socket> {
    for bind in binds {
        sock.bind(bind)?;
    }
    Ok(sock)
}

fn connect_all(sock: zmq::Socket, connects: &[&str]) -> zmq::Result<zmq::Socket> {
    for connect in connects {
        sock.connect(connect)?;
    }
    Ok(sock)
}

/// The root struct for a Socket builder
///
/// This struct contains a context and an identity.
pub struct SocketBuilder<'a, T>
where
    T: IntoInnerSocket,
{
    ctx: Arc<zmq::Context>,
    identity: Option<&'a [u8]>,
    _type: PhantomData<T>,
}

impl<'a, T> SocketBuilder<'a, T>
where
    T: IntoInnerSocket,
{
    /// Create a new Socket builder
    ///
    /// All sockets that are created through the Async ZMQ Types library will use this as the base
    /// for their socket builder (except PAIR sockets).
    pub fn new(ctx: Arc<zmq::Context>) -> Self {
        SocketBuilder {
            ctx,
            identity: None,
            _type: PhantomData,
        }
    }

    /// Give the socket a custom identity
    pub fn identity(self, identity: &'a [u8]) -> Self {
        SocketBuilder {
            ctx: self.ctx,
            identity: Some(identity),
            _type: self._type,
        }
    }

    /// Bind the socket to an address
    ///
    /// Since this is just part of the builder, and the socket doesn't exist yet, we store the
    /// address for later retrieval.
    pub fn bind(self, addr: &'a str) -> SockConfig<'a, T> {
        SockConfig {
            ctx: self.ctx,
            bind: vec![addr],
            connect: Vec::new(),
            identity: self.identity,
            _type: self._type,
        }
    }

    /// Connect the socket to an address
    ///
    /// Since this is just part of the builder, and the socket doesn't exist yet, we store the
    /// address for later retrieval.
    pub fn connect(self, addr: &'a str) -> SockConfig<'a, T> {
        SockConfig {
            ctx: self.ctx,
            bind: Vec::new(),
            connect: vec![addr],
            identity: self.identity,
            _type: self._type,
        }
    }
}

/// The final builder step for some socket types
///
/// This contains all the information required to contstruct a valid socket, except in the case of
/// SUB, which needs an additional `filter` parameter.
pub struct SockConfig<'a, T>
where
    T: IntoInnerSocket,
{
    pub ctx: Arc<zmq::Context>,
    pub bind: Vec<&'a str>,
    pub connect: Vec<&'a str>,
    pub identity: Option<&'a [u8]>,
    _type: PhantomData<T>,
}

impl<'a, T> SockConfig<'a, T>
where
    T: UnPair + IntoInnerSocket,
{
    /// Bind the `SockConfig` to an address, returning a `SockConfig`
    ///
    /// This allows for a single socket to be bound to multiple addresses.
    pub fn bind(mut self, addr: &'a str) -> Self {
        self.bind.push(addr);
        self
    }

    /// Connect the `SockConfig` to an address, returning a `SockConfig`
    ///
    /// This allows for a single socket to be connected to multiple addresses.
    pub fn connect(mut self, addr: &'a str) -> Self {
        self.connect.push(addr);
        self
    }

    pub fn do_build(self) -> Result<zmq::Socket, zmq::Error> {
        let SockConfig {
            ctx,
            bind,
            connect,
            identity,
            _type,
        } = self;

        let sock = ctx.socket(T::kind())?;
        if let Some(identity) = identity {
            sock.set_identity(identity)?;
        }
        let sock = bind_all(sock, &bind)?;
        let sock = connect_all(sock, &connect)?;

        Ok(sock)
    }
}

impl<'a, T> SockConfig<'a, T>
where
    T: IntoInnerSocket + Pair,
{
    /// Bind or Connect the socket to an address
    ///
    /// This method indicates that the resulting socket will be a PAIR socket.
    pub fn pair(self, addr: &'a str, bind: bool) -> PairConfig<'a> {
        PairConfig {
            ctx: self.ctx,
            addr,
            bind,
            identity: self.identity,
        }
    }
}

impl<'a, T> SockConfig<'a, T>
where
    T: IntoInnerSocket + Sub,
{
    /// Continue the building process into a SubConfig, for the SUB socket type which requires
    /// setting a subscription filter.
    pub fn filter(self, pattern: &'a [u8]) -> SubConfig<'a> {
        SubConfig {
            ctx: self.ctx,
            bind: self.bind,
            connect: self.connect,
            identity: self.identity,
            filter: vec![pattern],
        }
    }
}

/// The final builder step for the Sub socket type.
///
/// This contains all the information required to contstruct a valid SUB socket
pub struct SubConfig<'a> {
    pub ctx: Arc<zmq::Context>,
    pub bind: Vec<&'a str>,
    pub connect: Vec<&'a str>,
    pub filter: Vec<&'a [u8]>,
    pub identity: Option<&'a [u8]>,
}

impl<'a> SubConfig<'a> {
    /// Continue the building process into a SubConfig, for the SUB socket type which requires
    /// setting a subscription filter.
    pub fn filter(mut self, pattern: &'a [u8]) -> SubConfig<'a> {
        self.filter.push(pattern);

        SubConfig {
            ctx: self.ctx,
            bind: self.bind,
            connect: self.connect,
            identity: self.identity,
            filter: self.filter,
        }
    }

    /// Finalize the `SubConfig` into a `Sub` if the creation is successful, or into an Error
    /// if something went wrong.
    pub fn do_build(self) -> Result<zmq::Socket, zmq::Error> {
        let SubConfig {
            ctx,
            bind,
            connect,
            filter,
            identity,
        } = self;

        let sock = ctx.socket(zmq::SUB)?;
        if let Some(identity) = identity {
            sock.set_identity(identity)?;
        }
        let sock = bind_all(sock, &bind)?;
        let sock = connect_all(sock, &connect)?;
        for pattern in filter {
            sock.set_subscribe(pattern)?;
        }

        Ok(sock)
    }
}

/// The final builder step for the Pair socket type.
///
/// This contains all the information required to contstruct a valid PAIR socket
pub struct PairConfig<'a> {
    ctx: Arc<zmq::Context>,
    addr: &'a str,
    bind: bool,
    identity: Option<&'a [u8]>,
}

impl<'a> PairConfig<'a> {
    /// Construct a raw `Socket` type from the given `PairConfig`
    ///
    /// This build takes the same arguments as the `SockConfig`'s build method for convenience, but
    /// this should not be called with `zmq::SocketType`s other than `zmq::PAIR`. The `Pair`
    /// wrapper uses this builder, so it is better to use the Pair wrapper than directly building a
    /// PAIR socket.
    pub fn do_build(self) -> Result<zmq::Socket, zmq::Error> {
        let PairConfig {
            ctx,
            addr,
            bind,
            identity,
        } = self;

        let sock = ctx.socket(zmq::PAIR)?;
        if let Some(identity) = identity {
            sock.set_identity(identity)?;
        }
        if bind {
            sock.bind(addr)?;
        } else {
            sock.connect(addr)?;
        }

        Ok(sock)
    }
}