2018-05-29 10:18:12 -07:00
|
|
|
//! The `fetch_stage` batches input from a UDP socket and sends it to a channel.
|
|
|
|
|
2018-12-07 19:16:27 -08:00
|
|
|
use crate::service::Service;
|
|
|
|
use crate::streamer::{self, PacketReceiver};
|
2018-05-29 10:18:12 -07:00
|
|
|
use std::net::UdpSocket;
|
2018-07-09 13:53:18 -07:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2018-05-29 10:18:12 -07:00
|
|
|
use std::sync::mpsc::channel;
|
2018-05-29 20:20:28 -07:00
|
|
|
use std::sync::Arc;
|
2018-07-03 21:14:08 -07:00
|
|
|
use std::thread::{self, JoinHandle};
|
2018-05-29 10:18:12 -07:00
|
|
|
|
|
|
|
pub struct FetchStage {
|
2018-07-09 13:53:18 -07:00
|
|
|
exit: Arc<AtomicBool>,
|
2018-07-03 21:14:08 -07:00
|
|
|
thread_hdls: Vec<JoinHandle<()>>,
|
2018-05-29 10:18:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FetchStage {
|
2018-12-07 19:01:28 -08:00
|
|
|
#[allow(clippy::new_ret_no_self)]
|
2018-09-18 08:02:57 -07:00
|
|
|
pub fn new(sockets: Vec<UdpSocket>, exit: Arc<AtomicBool>) -> (Self, PacketReceiver) {
|
2018-09-06 14:13:40 -07:00
|
|
|
let tx_sockets = sockets.into_iter().map(Arc::new).collect();
|
2018-09-18 08:02:57 -07:00
|
|
|
Self::new_multi_socket(tx_sockets, exit)
|
2018-06-13 21:52:23 -07:00
|
|
|
}
|
2019-01-24 12:46:40 -08:00
|
|
|
fn new_multi_socket(
|
2018-08-28 16:32:40 -07:00
|
|
|
sockets: Vec<Arc<UdpSocket>>,
|
2018-06-13 21:52:23 -07:00
|
|
|
exit: Arc<AtomicBool>,
|
2018-07-02 14:45:16 -07:00
|
|
|
) -> (Self, PacketReceiver) {
|
2018-08-28 16:32:40 -07:00
|
|
|
let (sender, receiver) = channel();
|
2018-06-13 21:52:23 -07:00
|
|
|
let thread_hdls: Vec<_> = sockets
|
|
|
|
.into_iter()
|
2018-09-24 17:13:49 -07:00
|
|
|
.map(|socket| streamer::receiver(socket, exit.clone(), sender.clone(), "fetch-stage"))
|
2018-09-18 08:02:57 -07:00
|
|
|
.collect();
|
2018-05-29 10:18:12 -07:00
|
|
|
|
2018-12-07 19:01:28 -08:00
|
|
|
(Self { exit, thread_hdls }, receiver)
|
2018-07-09 13:53:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close(&self) {
|
|
|
|
self.exit.store(true, Ordering::Relaxed);
|
2018-05-29 10:18:12 -07:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 21:14:08 -07:00
|
|
|
|
|
|
|
impl Service for FetchStage {
|
2018-09-13 14:00:17 -07:00
|
|
|
type JoinReturnType = ();
|
2018-07-03 21:14:08 -07:00
|
|
|
|
|
|
|
fn join(self) -> thread::Result<()> {
|
2018-09-13 14:00:17 -07:00
|
|
|
for thread_hdl in self.thread_hdls {
|
2018-07-03 21:14:08 -07:00
|
|
|
thread_hdl.join()?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|