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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * 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/>.
 */

#[cfg(unix)]
use std::os::unix::io::{AsRawFd, RawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, RawSocket};

use std::{
    fmt,
    io::{self, Read, Write},
    net::{TcpListener, TcpStream},
    sync::{mpsc, Arc, Mutex},
    thread,
};

use async_zmq_types::Multipart;
use futures::{executor::Notify, sync::oneshot, Async, Future, Poll};
use log::{error, info, trace};
use zmq::Socket;

use crate::error::Error;

mod poll_thread;
mod pollable;

use self::{poll_thread::PollThread, pollable::Pollable};

pub struct SockId(usize, Arc<Mutex<SockIdInner>>);

impl SockId {
    fn new(id: usize, tx: Sender) -> Self {
        SockId(id, Arc::new(Mutex::new(SockIdInner(id, tx))))
    }
}

impl fmt::Debug for SockId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "SockId({}, _)", self.0)
    }
}

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

struct SockIdInner(usize, Sender);

impl Drop for SockIdInner {
    fn drop(&mut self) {
        trace!("Dropping {}", self.0);
        let _ = self.1.send(Request::DropSocket(self.0));
    }
}

pub(crate) enum Request {
    Init(Socket, oneshot::Sender<SockId>),
    SendMessage(usize, Multipart, oneshot::Sender<Response>),
    ReceiveMessage(usize, oneshot::Sender<Response>),
    DropSocket(usize),
    Done,
}

pub(crate) enum Response {
    Sent,
    Received(Multipart),
    Full(Multipart),
    Error(Error),
}

struct Channel {
    tx: TcpStream,
    rx: TcpStream,
}

impl Channel {
    fn notify(&self) {
        self.drain();

        if let Err(e) = (&self.tx).write(&[1]) {
            error!("Error notifying channel, {}", e);
        }
    }

    fn drain(&self) -> bool {
        let mut new_data = false;
        loop {
            match (&self.rx).read(&mut [0; 32]) {
                Ok(_) => {
                    new_data = true;
                }
                Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => break,
                Err(e) => panic!("I/O error: {}", e),
            }
        }
        new_data
    }

    #[cfg(unix)]
    fn read_fd(&self) -> RawFd {
        self.rx.as_raw_fd()
    }

    #[cfg(windows)]
    fn read_fd(&self) -> RawSocket {
        self.rx.as_raw_socket()
    }
}

#[derive(Clone)]
pub(crate) struct Sender {
    tx: mpsc::Sender<Request>,
    channel: Arc<Channel>,
}

impl Sender {
    fn send(&self, request: Request) {
        if let Err(_) = self.tx.send(request) {
            error!("Error sending request");
        }
        self.channel.notify();
    }
}

pub(crate) struct Receiver {
    rx: mpsc::Receiver<Request>,
    channel: Arc<Channel>,
}

impl Receiver {
    fn try_recv(&self) -> Option<Request> {
        self.rx.try_recv().ok()
    }

    /// Returns whether there are messages to look at
    fn drain(&self) -> bool {
        self.channel.drain()
    }
}

/// A local copy of Session
///
/// This is useful so we don't invoke mutex locks to send commands to the poll thread
pub struct LocalSession {
    sender: Sender,
    #[allow(dead_code)]
    session: Session,
}

impl LocalSession {
    pub fn send(&self, id: &SockId, msg: Multipart) -> SendFuture {
        let (tx, rx) = oneshot::channel();

        self.sender.send(Request::SendMessage(id.0, msg, tx));

        SendFuture { rx }
    }

    pub fn recv(&self, id: &SockId) -> RecvFuture {
        let (tx, rx) = oneshot::channel();

        self.sender.send(Request::ReceiveMessage(id.0, tx));

        RecvFuture { rx }
    }

    pub fn init(&self, sock: Socket) -> InitFuture {
        let (tx, rx) = oneshot::channel();

        self.sender.send(Request::Init(sock, tx));

        InitFuture { rx }
    }
}

#[derive(Clone)]
pub struct Session {
    inner: Arc<Mutex<Option<InnerSession>>>,
}

impl Session {
    pub fn new() -> Self {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();

        let conn1 = TcpStream::connect(&addr).unwrap();
        let conn2 = listener.accept().unwrap().0;

        drop(listener);

        conn1.set_nonblocking(true).unwrap();
        conn2.set_nonblocking(true).unwrap();

        let channel = Arc::new(Channel {
            tx: conn1,
            rx: conn2,
        });

        let (tx, rx) = mpsc::channel();

        let tx = Sender {
            tx: tx.clone(),
            channel: channel.clone(),
        };
        let rx = Receiver {
            rx: rx,
            channel: channel,
        };

        let tx2 = tx.clone();

        thread::spawn(move || {
            PollThread::new(tx2, rx).run();
        });

        Session {
            inner: InnerSession::init(tx),
        }
    }

    pub fn shutdown(&self) {
        *self.inner.lock().unwrap() = None;
    }

    pub fn local_session(&self) -> LocalSession {
        let session = self.clone();
        let sender = self.inner.lock().unwrap().as_ref().unwrap().tx.clone();

        LocalSession { sender, session }
    }
}

pub struct SendFuture {
    rx: oneshot::Receiver<Response>,
}

impl Future for SendFuture {
    type Item = Option<Multipart>;
    type Error = Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.rx.poll()? {
            Async::Ready(res) => match res {
                Response::Sent => Ok(Async::Ready(None)),
                Response::Full(msg) => Ok(Async::Ready(Some(msg))),
                Response::Error(e) => Err(e),
                _ => panic!("Response kind was not sent"),
            },
            Async::NotReady => Ok(Async::NotReady),
        }
    }
}

pub struct RecvFuture {
    rx: oneshot::Receiver<Response>,
}

impl Future for RecvFuture {
    type Item = Multipart;
    type Error = Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.rx.poll()? {
            Async::Ready(res) => match res {
                Response::Received(msg) => Ok(Async::Ready(msg)),
                Response::Error(e) => Err(e),
                _ => panic!("Response kind was not received"),
            },
            Async::NotReady => Ok(Async::NotReady),
        }
    }
}

pub struct InitFuture {
    rx: oneshot::Receiver<SockId>,
}

impl Future for InitFuture {
    type Item = SockId;
    type Error = Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        Ok(self.rx.poll()?)
    }
}

struct InnerSession {
    tx: Sender,
}

impl InnerSession {
    fn init(tx: Sender) -> Arc<Mutex<Option<Self>>> {
        Arc::new(Mutex::new(Some(InnerSession { tx })))
    }
}

impl Drop for InnerSession {
    fn drop(&mut self) {
        info!("Dropping session");
        self.tx.send(Request::Done);
    }
}

#[derive(Clone)]
struct NotifyCanceled {
    channel: Arc<Channel>,
}

impl NotifyCanceled {
    fn new(channel: Arc<Channel>) -> Self {
        NotifyCanceled { channel }
    }
}

impl Notify for NotifyCanceled {
    fn notify(&self, _id: usize) {
        self.channel.notify();
    }
}

struct CheckCanceled<'a> {
    sender: &'a mut oneshot::Sender<Response>,
}

impl<'a> Future for CheckCanceled<'a> {
    type Item = ();
    type Error = ();

    fn poll(&mut self) -> Poll<(), ()> {
        self.sender.poll_cancel()
    }
}