This commit is contained in:
Richard Ramos 2024-11-09 16:38:04 +07:00
parent 434b5c1e73
commit 251f2836fa
No known key found for this signature in database
GPG Key ID: 1CE87DB518195760
4 changed files with 2157 additions and 59 deletions

2149
examples/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -2,4 +2,4 @@
members = [ members = [
"basic" "basic"
, "tic-tac-toe"] , "tic-tac-toe", "tic-tac-toe-gui"]

View File

@ -73,32 +73,32 @@ impl TicTacToeApp {
// println!("funciona ?"); // println!("funciona ?");
let mut cloned = Arc::clone(&self.val); // let mut cloned = Arc::clone(&self.val);
let tx_clone = self.tx.clone();
// Establish a closure that handles the incoming messages // Establish a closure that handles the incoming messages
self.waku.ctx.waku_set_event_callback(|response| { self.waku.ctx.waku_set_event_callback(move |response| {
// if let Ok(mut tx) = tx_clone.try_lock() {
// // Lock succeeded, proceed to send the message
// if tx.try_send(msg.to_string()).is_err() {
// eprintln!("Failed to send message to async task");
// }
// } else {
// eprintln!("Failed to acquire lock on tx_clone");
// }
// if let Ok(mut aa) = cloned.try_lock() {
// if let Ok(mut tx) = tx_clone.try_lock() { // }
// Lock succeeded, proceed to send the message
// if tx.try_send(msg.to_string()).is_err() {
// eprintln!("Failed to send message to async task");
// }
// } else {
// eprintln!("Failed to acquire lock on tx_clone");
// }
if let Ok(mut aa) = cloned.try_lock() {
} // match cloned.lock() {
// Ok(mut data) => {
// match cloned.lock() { // *data = "Modified Value".to_string();
// Ok(mut data) => { // println!("Thread updated value to: {}", data);
// *data = "Modified Value".to_string(); // },
// println!("Thread updated value to: {}", data); // Err(e) => {
// }, // eprintln!("Failed to lock the mutex in thread: {}", e);
// Err(e) => { // }
// eprintln!("Failed to lock the mutex in thread: {}", e); // }
// }
// }
if let LibwakuResponse::Success(v) = response { if let LibwakuResponse::Success(v) = response {
@ -118,14 +118,14 @@ impl TicTacToeApp {
println!("::::::::::::::::::::::::::::::::::::::::::::::::::::"); println!("::::::::::::::::::::::::::::::::::::::::::::::::::::");
// Send the message to the main thread // Send the message to the main thread
// if let Ok(mut tx) = tx_clone.try_lock() { if let Ok(mut tx) = tx_clone.try_lock() {
// Lock succeeded, proceed to send the message // Lock succeeded, proceed to send the message
// if tx.try_send(msg.to_string()).is_err() { if tx.try_send(msg.to_string()).is_err() {
// eprintln!("Failed to send message to async task"); eprintln!("Failed to send message to async task");
// } }
// } else { } else {
// eprintln!("Failed to acquire lock on tx_clone"); eprintln!("Failed to acquire lock on tx_clone");
// } }
// Deserialize the JSON into the GameState struct // Deserialize the JSON into the GameState struct
// Lock the game_state and update it // Lock the game_state and update it
@ -356,6 +356,9 @@ fn main() -> eframe::Result<()> {
// Listen for messages in the main thread // Listen for messages in the main thread
tokio::spawn(async move { tokio::spawn(async move {
unsafe {
waku_sys::waku_setup();
}
while let Some(msg) = rx.recv().await { while let Some(msg) = rx.recv().await {
println!("Main thread received: {}", msg); println!("Main thread received: {}", msg);
// Handle the received message, e.g., update the UI or game state // Handle the received message, e.g., update the UI or game state

View File

@ -43,7 +43,7 @@ pub struct WakuMessageEvent {
impl WakuNodeContext { impl WakuNodeContext {
/// Register callback to act as event handler and receive application events, /// Register callback to act as event handler and receive application events,
/// which are used to react to asynchronous events in Waku /// which are used to react to asynchronous events in Waku
pub fn waku_set_event_callback<F: FnMut(LibwakuResponse)>(&self, mut closure: F) { pub fn waku_set_event_callback<F: FnMut(LibwakuResponse) + 'static>(&self, mut closure: F) {
unsafe { unsafe {
let cb = get_trampoline(&closure); let cb = get_trampoline(&closure);
waku_sys::waku_set_event_callback(self.obj_ptr, cb, &mut closure as *mut _ as *mut c_void) waku_sys::waku_set_event_callback(self.obj_ptr, cb, &mut closure as *mut _ as *mut c_void)