-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use bitcoincore_zmq::subscribe_single_blocking; | ||
use core::ops::ControlFlow; | ||
|
||
fn main() { | ||
let callback = |msg| { | ||
match msg { | ||
Ok(msg) => println!("Received message: {msg}"), | ||
Err(err) => { | ||
// Do this to exit and return the error | ||
return ControlFlow::Break(err); | ||
} | ||
} | ||
|
||
ControlFlow::Continue(()) | ||
}; | ||
|
||
match subscribe_single_blocking("tcp://127.0.0.1:28359", callback) { | ||
Ok(ControlFlow::Break(err)) => { | ||
// Callback exited by returning ControlFlow::Break | ||
println!("Error receiving message: {err}"); | ||
} | ||
Err(err) => { | ||
println!("Unable to connect: {err}"); | ||
} | ||
Ok(ControlFlow::Continue(v)) => { | ||
// unreachable | ||
match v {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use bitcoincore_zmq::subscribe_multi; | ||
|
||
fn main() { | ||
let rx = subscribe_multi(&["tcp://127.0.0.1:28332", "tcp://127.0.0.1:28333"]).unwrap(); | ||
|
||
for msg in rx { | ||
match msg { | ||
Ok(msg) => println!("Received message: {msg}"), | ||
Err(err) => println!("Error receiving message: {err}"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use bitcoincore_zmq::subscribe_multi; | ||
use std::{ | ||
sync::{Arc, Mutex}, | ||
thread, | ||
}; | ||
|
||
/// Use 4 threads to handle messages | ||
const POOL_THREADS: usize = 4; | ||
|
||
fn main() { | ||
let mut threads = Vec::new(); | ||
|
||
let rx = Arc::new(Mutex::new( | ||
subscribe_multi(&["tcp://127.0.0.1:28332", "tcp://127.0.0.1:28333"]).unwrap(), | ||
)); | ||
|
||
for id in 0..POOL_THREADS { | ||
let rx = rx.clone(); | ||
|
||
threads.push(thread::spawn(move || { | ||
while let Ok(msg) = { rx.lock().unwrap().recv() } { | ||
match msg { | ||
Ok(msg) => println!("Thread {id}: Received message: {msg}"), | ||
Err(err) => println!("Thread {id}: Error receiving message: {err}"), | ||
} | ||
} | ||
})); | ||
} | ||
|
||
for t in threads { | ||
t.join().unwrap(); | ||
} | ||
} |