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ry+https://github.com/rust-lang/crates.io-index"
checksum = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
dependencies = [
"cfg-if 0.1.10",
]
[[package]]
name = "crossbeam-epoch"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace"
dependencies = [
"autocfg",
"cfg-if 0.1.10",
"crossbeam-utils 0.7.2",
"lazy_static",
"maybe-uninit",
"memoffset",
"scopeguard",
]
[[package]]
"cfg-if",
"cfg-if 0.1.10",
"lazy_static",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec91540d98355f690a86367e566ecad2e9e579f230230eb7c21398372be73ea5"
dependencies = [
"autocfg",
"cfg-if 1.0.0",
"const_fn",
[[package]]
name = "flume"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9e818efa7776f4dd7df0e542f877f7a5a87bddd6a1a10f59a7732b71ffb9d55"
dependencies = [
"futures-core",
"futures-sink",
"rand",
"spinning_top",
]
[[package]]
name = "fs2"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "futures-core"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d674eaa0056896d5ada519900dbf97ead2e46a7b6621e8160d79e2f2e1e2784b"
[[package]]
name = "futures-sink"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d8764258ed64ebc5d9ed185cf86a95db5cac810269c5d20ececb32e0088abbd"
[[package]]
name = "fxhash"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c"
dependencies = [
"byteorder",
]
name = "parking_lot_core"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c361aa727dd08437f2f1447be8b59a33b0edd15e0fcee698f935613d9efbca9b"
dependencies = [
"cfg-if 0.1.10",
"cloudabi 0.1.0",
"instant",
"libc",
"redox_syscall",
"smallvec",
"winapi",
]
[[package]]
name = "sled"
version = "0.34.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f72c064e63fbca3138ad07f3588c58093f1684f3a99f60dcfa6d46b87e60fde7"
dependencies = [
"crc32fast",
"crossbeam-epoch",
"crossbeam-utils 0.7.2",
"fs2",
"fxhash",
"libc",
"log",
"parking_lot 0.11.0",
]
[[package]]
pub fn server(cache_path: PathBuf, socket_path: &PathBuf, data_dir: PathBuf) -> Result<(), Error> {
let server = server::new(cache_path, data_dir)?;
server.start(socket_path)?;
pub fn server(cache_dir: PathBuf, socket: PathBuf, data_dir: PathBuf) -> Result<(), Error> {
let server = server::builder(cache_dir, data_dir, socket)
.build()?
.run()?;
use super::Server;
use crate::store;
use crossbeam_utils::sync::WaitGroup;
use std::{
os::unix::net::UnixDatagram,
path::PathBuf,
sync::{
atomic::AtomicBool,
Arc,
},
};
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("can not open entries database: {0}")]
OpenEntriesDb(sled::Error),
#[error("no parent directory for socket path")]
NoSocketPathParent,
#[error("can not create socket parent directory: {0}")]
CreateSocketPathParent(std::io::Error),
#[error("can not bind to socket: {0}")]
BindSocket(std::io::Error),
}
pub struct Builder {
pub(super) cache_dir: PathBuf,
pub(super) data_dir: PathBuf,
pub(super) socket: PathBuf,
}
impl Builder {
pub fn build(self) -> Result<Server, Error> {
let entries = sled::open(self.cache_dir.join("entries")).map_err(Error::OpenEntriesDb)?;
let socket_path_parent = self.socket.parent().ok_or(Error::NoSocketPathParent)?;
std::fs::create_dir_all(socket_path_parent).map_err(Error::CreateSocketPathParent)?;
let socket = UnixDatagram::bind(&self.socket).map_err(Error::BindSocket)?;
let store = store::new(self.data_dir);
let stopping = Arc::new(AtomicBool::new(false));
let wait_group = WaitGroup::new();
Ok(Server {
entries,
socket,
socket_path: self.socket,
store,
stopping,
wait_group,
})
}
}
pub mod builder;
pub mod server;
pub use builder::{
Builder,
Error as BuilderError,
};
pub use server::{
builder,
Error as ServerError,
Server,
};
use super::Builder;
use crate::{
client,
message::Message,
store::Store,
};
use crossbeam_utils::sync::WaitGroup;
use flume::{
Receiver,
Sender,
};
use log::warn;
use std::{
os::unix::net::UnixDatagram,
path::PathBuf,
sync::{
atomic::{
AtomicBool,
Ordering,
},
Arc,
},
thread,
};
use thiserror::Error;
const BUFFER_SIZE: usize = 65_527;
#[derive(Error, Debug)]
pub enum Error {
#[error("can not receive message from socket: {0}")]
ReceiveFromSocket(std::io::Error),
#[error("can not send received data to processing: {0}")]
SendBuffer(flume::SendError<Vec<u8>>),
#[error("can not deserialize message: {0}")]
DeserializeMessage(bincode::Error),
#[error("can not receive data from channel: {0}")]
ReceiveData(flume::RecvError),
#[error("can not remove socket: {0}")]
RemoveSocket(std::io::Error),
#[error("can not setup ctrlc handler: {0}")]
SetupCtrlHandler(ctrlc::Error),
}
pub struct Server {
pub(super) entries: sled::Db,
pub(super) socket: UnixDatagram,
pub(super) socket_path: PathBuf,
pub(super) store: Store,
pub(super) stopping: Arc<AtomicBool>,
pub(super) wait_group: WaitGroup,
}
pub fn builder(cache_dir: PathBuf, data_dir: PathBuf, socket: PathBuf) -> Builder {
Builder {
cache_dir,
data_dir,
socket,
}
}
impl Server {
pub fn run(self) -> Result<(), Error> {
let data_sender =
Self::start_processor(Arc::clone(&self.stopping), self.wait_group.clone())?;
Self::start_receiver(
Arc::clone(&self.stopping),
self.wait_group.clone(),
self.socket,
data_sender,
)?;
Self::ctrl_c_watcher(self.stopping, self.socket_path.clone())?;
self.wait_group.wait();
std::fs::remove_file(&self.socket_path).map_err(Error::RemoveSocket)?;
Ok(())
}
fn ctrl_c_watcher(stopping: Arc<AtomicBool>, socket_path: PathBuf) -> Result<(), Error> {
ctrlc::set_handler(move || {
stopping.store(true, Ordering::SeqCst);
let client = client::new(socket_path.clone());
if let Err(err) = client.send(&Message::Stop) {
warn!("{}", err);
}
})
.map_err(Error::SetupCtrlHandler)?;
Ok(())
}
fn start_receiver(
stopping: Arc<AtomicBool>,
wait_group: WaitGroup,
socket: UnixDatagram,
data_sender: Sender<Vec<u8>>,
) -> Result<(), Error> {
thread::spawn(move || {
loop {
if dbg!(stopping.load(Ordering::SeqCst)) {
dbg!("break loop");
break;
}
if let Err(err) = Self::receive(&socket, &data_sender) {
warn!("{}", err)
}
}
drop(wait_group)
});
Ok(())
}
fn receive(socket: &UnixDatagram, data_sender: &Sender<Vec<u8>>) -> Result<(), Error> {
dbg!("receive");
let mut buffer = [0_u8; BUFFER_SIZE];
let (written, _) = socket
.recv_from(&mut buffer)
.map_err(Error::ReceiveFromSocket)?;
data_sender
.send(buffer[0..written].to_vec())
.map_err(Error::SendBuffer)?;
Ok(())
}
fn start_processor(
stopping: Arc<AtomicBool>,
wait_group: WaitGroup,
) -> Result<Sender<Vec<u8>>, Error> {
let (data_sender, data_receiver) = flume::unbounded();
thread::spawn(move || {
loop {
if dbg!(stopping.load(Ordering::SeqCst)) {
break;
}
if let Err(err) = Self::process(&stopping, &data_receiver) {
warn!("{}", err)
}
}
drop(wait_group)
});
Ok(data_sender)
}
fn process(stopping: &Arc<AtomicBool>, data_receiver: &Receiver<Vec<u8>>) -> Result<(), Error> {
let data = data_receiver.recv().map_err(Error::ReceiveData)?;
let message = bincode::deserialize(&data).map_err(Error::DeserializeMessage)?;
dbg!(&message);
match message {
Message::Stop => stopping.store(true, Ordering::SeqCst),
_ => todo!(),
}
Ok(())
}
}
let (data_sender, data_receiver) = flume::unbounded();
let (signal_sender, signal_receiver) = flume::unbounded();
let stopping = Arc::new(AtomicBool::new(false));
Self::ctrl_c_watcher(signal_sender)?;
Self::signal_watcher(signal_receiver, Arc::clone(&stopping));
Self::start_processor(data_receiver);
match Self::receive(&socket) {
Err(err) => warn!("{}", err),
Ok(message) => match self.process(message) {
Ok(state) => {
if state.is_stop() {
break;
}
}
if stopping.load(Ordering::SeqCst) {
break;
}
}
fn signal_watcher(signal_receiver: Receiver<RunState>, stopping: Arc<AtomicBool>) {
thread::spawn(move || loop {
match signal_receiver.recv() {
Ok(signal) => match signal {
RunState::Stop => stopping.store(true, Ordering::SeqCst),
RunState::Continue => continue,
},
Err(err) => warn!("{}", err),
}
});
Ok(message)
fn start_processor(data_receiver: Receiver<Vec<u8>>) -> Result<(), Error> {
loop {
let buffer = match data_receiver.recv() {
Ok(b) => b,
Err(err) => {
warn!("{}", err);
continue;
}
};
let message = bincode::deserialize(&buffer).map_err(Error::DeserializeMessage)?;
}