|
1 | 1 | use anyhow::{bail, Context, Result}; |
2 | 2 | mod command; |
| 3 | +pub mod state; |
| 4 | + |
3 | 5 | pub use command::*; |
4 | 6 |
|
| 7 | +#[cfg(feature = "daemon")] |
| 8 | +pub use state::DaemonState; |
| 9 | + |
5 | 10 | pub const DAEMON_SOCKET_PATH: &str = "/tmp/xcodebase-daemon.socket"; |
6 | 11 | pub const DAEMON_BINARY: &str = |
7 | 12 | "/Users/tami5/repos/neovim/xcodebase.nvim/target/debug/xcodebase-daemon"; |
8 | 13 |
|
9 | 14 | pub struct Daemon { |
10 | 15 | #[cfg(feature = "daemon")] |
11 | | - state: std::sync::Arc<tokio::sync::Mutex<crate::state::State>>, |
| 16 | + pub state: std::sync::Arc<tokio::sync::Mutex<state::DaemonStateData>>, |
12 | 17 | #[cfg(feature = "daemon")] |
13 | | - listener: tokio::net::UnixListener, |
| 18 | + pub listener: tokio::net::UnixListener, |
14 | 19 | } |
15 | 20 |
|
16 | | -#[cfg(feature = "daemon")] |
17 | 21 | impl Daemon { |
18 | | - pub fn default() -> Self { |
19 | | - if std::fs::metadata(DAEMON_SOCKET_PATH).is_ok() { |
20 | | - std::fs::remove_file(DAEMON_SOCKET_PATH).ok(); |
21 | | - } |
22 | | - |
23 | | - tracing::info!("Started"); |
24 | | - |
| 22 | + #[cfg(feature = "daemon")] |
| 23 | + pub fn new() -> Self { |
25 | 24 | Self { |
26 | 25 | state: Default::default(), |
27 | 26 | listener: tokio::net::UnixListener::bind(DAEMON_SOCKET_PATH).unwrap(), |
28 | 27 | } |
29 | 28 | } |
30 | 29 |
|
31 | | - /// Run Main daemon server loop |
32 | | - pub async fn run(&mut self) -> ! { |
33 | | - use tokio::io::AsyncReadExt; |
34 | | - |
35 | | - loop { |
36 | | - let state = self.state.clone(); |
37 | | - let (mut s, _) = self.listener.accept().await.unwrap(); |
38 | | - tokio::spawn(async move { |
39 | | - // let mut current_state = state.lock().await; |
40 | | - // current_state.update_clients(); |
41 | | - |
42 | | - // trace!("Current State: {:?}", state.lock().await) |
43 | | - let mut string = String::default(); |
44 | | - |
45 | | - if let Err(e) = s.read_to_string(&mut string).await { |
46 | | - tracing::error!("[Read Error]: {:?}", e); |
47 | | - return; |
48 | | - }; |
49 | | - |
50 | | - if string.len() == 0 { |
51 | | - return; |
52 | | - } |
53 | | - |
54 | | - let msg = DaemonCommand::parse(string.as_str().trim()); |
55 | | - |
56 | | - if let Err(e) = msg { |
57 | | - tracing::error!("[Parse Error]: {:?}", e); |
58 | | - return; |
59 | | - }; |
60 | | - |
61 | | - let msg = msg.unwrap(); |
62 | | - if let Err(e) = msg.handle(state.clone()).await { |
63 | | - tracing::error!("[Failure]: Cause: ({:?}), Message: {:?}", e, msg); |
64 | | - return; |
65 | | - }; |
66 | | - |
67 | | - crate::watch::update(state, msg).await; |
68 | | - }); |
69 | | - } |
70 | | - } |
71 | | -} |
72 | | - |
73 | | -impl Daemon { |
74 | 30 | /// Spawn new instance of the server via running binaray is a child process |
75 | 31 | pub fn spawn() -> Result<()> { |
76 | 32 | std::process::Command::new(DAEMON_BINARY) |
|
0 commit comments