diff options
| author | Eskil Queseth <eskilq@kth.se> | 2021-03-26 01:10:09 +0100 |
|---|---|---|
| committer | Eskil Queseth <eskilq@kth.se> | 2021-03-26 01:10:09 +0100 |
| commit | 169eb4f49f3fd587e9c28f2209b2dd130425d866 (patch) | |
| tree | ee4e782ed6a9a367c9ac4834b91e622e55b22680 /mumd | |
| parent | d484c05e56194346944b295968c66ccc0e543534 (diff) | |
| download | mum-169eb4f49f3fd587e9c28f2209b2dd130425d866.tar.gz | |
re-add check for live server
Diffstat (limited to 'mumd')
| -rw-r--r-- | mumd/Cargo.toml | 2 | ||||
| -rw-r--r-- | mumd/src/main.rs | 36 |
2 files changed, 23 insertions, 15 deletions
diff --git a/mumd/Cargo.toml b/mumd/Cargo.toml index f271e69..7f0739b 100644 --- a/mumd/Cargo.toml +++ b/mumd/Cargo.toml @@ -36,7 +36,7 @@ opus = "0.2" serde = { version = "1.0", features = ["derive"] } strum = "0.20" strum_macros = "0.20" -tokio = { version = "1.0", features = ["macros", "rt", "rt-multi-thread", "sync", "net", "time", "io-util"] } +tokio = { version = "1.0", features = ["macros", "rt", "rt-multi-thread", "sync", "net", "time", "io-util", "fs"] } tokio-stream = "0.1.0" tokio-native-tls = "0.3" tokio-util = { version = "0.6", features = ["codec", "net"] } diff --git a/mumd/src/main.rs b/mumd/src/main.rs index d4151e3..5b22089 100644 --- a/mumd/src/main.rs +++ b/mumd/src/main.rs @@ -5,12 +5,12 @@ mod network; mod notify; mod state; -use futures_util::StreamExt; +use futures_util::{SinkExt, StreamExt}; //use ipc_channel::ipc::{self, IpcOneShotServer, IpcSender}; use log::*; use mumlib::command::{Command, CommandResponse}; use mumlib::setup_logger; -use tokio::{join, net::UnixListener, sync::{mpsc, oneshot}}; +use tokio::{join, net::{UnixListener, UnixStream}, sync::{mpsc, oneshot}}; use tokio_util::codec::{FramedRead, FramedWrite, LengthDelimitedCodec}; use bytes::{BufMut, BytesMut}; @@ -25,24 +25,32 @@ async fn main() { notify::init(); // check if another instance is live - /*let (tx_client, rx_client) = - ipc::channel::<mumlib::error::Result<Option<CommandResponse>>>().unwrap(); - if let Ok(server_name) = fs::read_to_string(mumlib::SOCKET_PATH) { - if let Ok(tx0) = IpcSender::connect(server_name) { - if tx0.send((Command::Ping, tx_client)).is_ok() { - match rx_client.recv() { - Ok(Ok(Some(CommandResponse::Pong))) => { + let connection = UnixStream::connect("/tmp/mumd").await; + match connection { + Ok(stream) => { + let (reader, writer) = stream.into_split(); + let mut reader = FramedRead::new(reader, LengthDelimitedCodec::new()); + let mut writer = FramedWrite::new(writer, LengthDelimitedCodec::new()); + let mut command = BytesMut::new(); + bincode::serialize_into((&mut command).writer(), &Command::Ping).unwrap(); + if let Ok(()) = writer.send(command.freeze()).await { + if let Some(Ok(buf)) = reader.next().await { + if let Ok(Ok::<Option<CommandResponse>, mumlib::error::Error>(Some(CommandResponse::Pong))) = bincode::deserialize(&buf) { error!("Another instance of mumd is already running"); return; - }, - resp => { - warn!("Ping with weird response. Continuing..."); - debug!("Response was {:?}", resp); } } } + debug!("a dead socket was found, removing"); + tokio::fs::remove_file("/tmp/mumd").await.unwrap(); } - }*/ + Err(e) => { + if matches!(e.kind(), std::io::ErrorKind::ConnectionRefused) { + debug!("a dead socket was found, removing"); + tokio::fs::remove_file("/tmp/mumd").await.unwrap(); + } + } + } let (command_sender, command_receiver) = mpsc::unbounded_channel(); |
