Skip to content

Commit

Permalink
feat: implement tracking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bysensa committed Jan 19, 2023
1 parent 7457c20 commit a84c2de
Show file tree
Hide file tree
Showing 17 changed files with 1,960 additions and 93 deletions.
1,506 changes: 1,439 additions & 67 deletions rust/Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
members = [
"core",
"server",
"mac_watcher"
"yad_mac_observer",
"yad_mac_agent",
"yad_tracking_scope"
]


Expand Down
11 changes: 0 additions & 11 deletions rust/mac_watcher/Cargo.toml

This file was deleted.

14 changes: 0 additions & 14 deletions rust/mac_watcher/src/lib.rs

This file was deleted.

15 changes: 15 additions & 0 deletions rust/yad_mac_agent/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "yad-mac-agent"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1"
cacao = "0.3.2"
async-std = { version = "1", features=["tokio1"] }
salvo = "0.37"
yad-mac-observer = { path = "../yad_mac_observer" }
lazy_static = "1.4"
flume = "0.10"
41 changes: 41 additions & 0 deletions rust/yad_mac_agent/src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
mod server;

use crate::app::server::YadAgentServer;
use async_std::task::JoinHandle;
use cacao::appkit::*;
use lazy_static::lazy_static;
use std::cell::RefCell;
use std::future::Future;
use yad_mac_observer::MacEventObserver;

pub struct YadAgentApp {
observer: RefCell<MacEventObserver>,
server: YadAgentServer,
}

impl YadAgentApp {
pub fn new() -> Self {
YadAgentApp {
observer: RefCell::new(MacEventObserver::new()),
server: YadAgentServer::new(),
}
}

pub fn spawn<F>(future: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
async_std::task::spawn(future)
}
}

impl AppDelegate for YadAgentApp {
fn did_finish_launching(&self) {
self.observer.borrow_mut().start();
}

fn will_terminate(&self) {
self.observer.borrow_mut().stop();
}
}
45 changes: 45 additions & 0 deletions rust/yad_mac_agent/src/app/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::app::YadAgentApp;
// use crate::RUNTIME;
use async_std::task::JoinHandle;
use flume::{bounded, Receiver, Sender};
use lazy_static::lazy_static;
use salvo::prelude::*;

lazy_static! {
static ref SHUTDOWN_CHANNEL: (Sender<()>, Receiver<()>) = bounded(1);
}

pub struct YadAgentServer {
handle: JoinHandle<()>,
}

impl YadAgentServer {
pub fn new() -> Self {
let handle = YadAgentApp::spawn(Self::init_server());
YadAgentServer { handle }
}

async fn init_server() {
let router = Router::new().get(hello_world);
let server = Server::new(TcpListener::bind("127.0.0.1:7878")).serve_with_graceful_shutdown(
router,
async {
SHUTDOWN_CHANNEL
.1
.recv_async()
.await
.expect("Shutdown signal received");
},
);
server.await;
}

pub fn shutdown() {
SHUTDOWN_CHANNEL.0.send_async(());
}
}

#[handler]
async fn hello_world(res: &mut Response) {
res.render("Hello world!");
}
18 changes: 18 additions & 0 deletions rust/yad_mac_agent/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
mod app;

// use crate::app::RUNTIME;
use cacao::appkit::*;
use salvo::prelude::*;
use std::thread;
use yad_mac_observer::MacEventObserver;

fn main() {
let mut app = App::new("dev.bysensa.yad.agent", app::YadAgentApp::new());
thread::spawn(|| {
let receiver = MacEventObserver::receiver();
while let Ok(event) = receiver.recv() {
dbg!(event);
}
});
app.run();
}
20 changes: 20 additions & 0 deletions rust/yad_mac_observer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "yad-mac-observer"
version = "0.1.0"
edition = "2021"

[[example]]
name = "usage"

[dependencies]
objc = "0.2.7"
objc_id = "0.1.1"
cocoa = "0.24.1"
cacao = "0.3.2"
objc-foundation = "0.1.1"
serde = { version = "1", features = ["derive"] }
lazy_static = "1.4.0"
flume = "0.10.14"
chrono = "0.4.23"
#https://docs.rs/thingbuf/0.1.3/thingbuf/mpsc/index.html
#thingbuf = "0.1.3"
20 changes: 20 additions & 0 deletions rust/yad_mac_observer/examples/usage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::thread;
use yad_mac_observer::MacEventObserver;

#[derive(Default)]
struct ExampleApp {}

impl cacao::appkit::AppDelegate for ExampleApp {}

fn main() {
let app = cacao::appkit::App::new("dev.bysensa.yad", ExampleApp::default());
let event_receiver = MacEventObserver::receiver();
let mut observer = MacEventObserver::new();
observer.start();
let _handle = thread::spawn(move || {
while let Ok(event) = event_receiver.recv() {
dbg!(event);
}
});
app.run();
}
Loading

0 comments on commit a84c2de

Please sign in to comment.