2018-05-29 10:18:12 -07:00
|
|
|
//! The `fetch_stage` batches input from a UDP socket and sends it to a channel.
|
|
|
|
|
2018-06-27 11:33:56 -07:00
|
|
|
use packet::PacketRecycler;
|
2018-05-29 10:18:12 -07:00
|
|
|
use std::net::UdpSocket;
|
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
|
use std::sync::mpsc::channel;
|
2018-05-29 20:20:28 -07:00
|
|
|
use std::sync::Arc;
|
2018-05-29 10:18:12 -07:00
|
|
|
use std::thread::JoinHandle;
|
2018-06-27 11:33:56 -07:00
|
|
|
use streamer::{self, PacketReceiver};
|
2018-05-29 10:18:12 -07:00
|
|
|
|
|
|
|
pub struct FetchStage {
|
2018-06-13 21:52:23 -07:00
|
|
|
pub thread_hdls: Vec<JoinHandle<()>>,
|
2018-05-29 10:18:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FetchStage {
|
2018-07-02 14:45:16 -07:00
|
|
|
pub fn new(
|
|
|
|
socket: UdpSocket,
|
|
|
|
exit: Arc<AtomicBool>,
|
|
|
|
packet_recycler: PacketRecycler,
|
|
|
|
) -> (Self, PacketReceiver) {
|
2018-06-13 21:52:23 -07:00
|
|
|
Self::new_multi_socket(vec![socket], exit, packet_recycler)
|
|
|
|
}
|
|
|
|
pub fn new_multi_socket(
|
|
|
|
sockets: Vec<UdpSocket>,
|
|
|
|
exit: Arc<AtomicBool>,
|
2018-06-27 11:33:56 -07:00
|
|
|
packet_recycler: PacketRecycler,
|
2018-07-02 14:45:16 -07:00
|
|
|
) -> (Self, PacketReceiver) {
|
2018-05-29 10:18:12 -07:00
|
|
|
let (packet_sender, packet_receiver) = channel();
|
2018-06-13 21:52:23 -07:00
|
|
|
let thread_hdls: Vec<_> = sockets
|
|
|
|
.into_iter()
|
|
|
|
.map(|socket| {
|
|
|
|
streamer::receiver(
|
|
|
|
socket,
|
|
|
|
exit.clone(),
|
|
|
|
packet_recycler.clone(),
|
|
|
|
packet_sender.clone(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
2018-05-29 10:18:12 -07:00
|
|
|
|
2018-07-02 14:45:16 -07:00
|
|
|
(FetchStage { thread_hdls }, packet_receiver)
|
2018-05-29 10:18:12 -07:00
|
|
|
}
|
|
|
|
}
|