Skip to content

Commit

Permalink
UTEXKER-562: accept closures as handlers in the Context
Browse files Browse the repository at this point in the history
  • Loading branch information
denisxor committed Oct 18, 2021
1 parent 7354a74 commit bd8bbd5
Show file tree
Hide file tree
Showing 16 changed files with 426 additions and 335 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pretty_env_logger = "0.4"
galvanic-assert = "0.8"
chrono = "0.4"
memoffset = "0.6.4"
dyn-clone = "1.0.4"

[dev-dependencies]
tempfile = "3.1"
Expand Down
4 changes: 2 additions & 2 deletions examples/basic_publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ fn main() {

println!("Using CnC file: {}", context.cnc_file_name());

context.set_new_publication_handler(on_new_publication_handler);
context.set_error_handler(error_handler);
context.set_new_publication_handler(Box::new(on_new_publication_handler));
context.set_error_handler(Box::new(error_handler));
context.set_pre_touch_mapped_memory(true);

let aeron = Aeron::new(context);
Expand Down
14 changes: 6 additions & 8 deletions examples/basic_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ fn parse_cmd_line() -> Settings {
Settings::new()
}

fn on_new_subscription_handler(channel: CString, stream_id: i32, correlation_id: i64) {
println!("Subscription: {} {} {}", channel.to_str().unwrap(), stream_id, correlation_id);
}

fn available_image_handler(image: &Image) {
println!(
"Available image correlation_id={} session_id={} at position={} from {}",
Expand Down Expand Up @@ -132,10 +128,12 @@ fn main() {

println!("Using CnC file: {}", context.cnc_file_name());

context.set_new_subscription_handler(on_new_subscription_handler);
context.set_available_image_handler(available_image_handler);
context.set_unavailable_image_handler(unavailable_image_handler);
context.set_error_handler(error_handler);
context.set_new_subscription_handler(Box::new(|channel: CString, stream_id: i32, correlation_id: i64| {
println!("Subscription: {} {} {}", channel.to_str().unwrap(), stream_id, correlation_id)
}));
context.set_available_image_handler(Box::new(available_image_handler));
context.set_unavailable_image_handler(Box::new(unavailable_image_handler));
context.set_error_handler(Box::new(error_handler));
context.set_pre_touch_mapped_memory(true);

let aeron = Aeron::new(context);
Expand Down
10 changes: 5 additions & 5 deletions examples/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ fn main() {

println!("Using CnC file: {}", context.cnc_file_name());

context.set_new_subscription_handler(on_new_subscription_handler);
context.set_new_publication_handler(on_new_publication_handler);
context.set_available_image_handler(available_image_handler);
context.set_unavailable_image_handler(unavailable_image_handler);
context.set_error_handler(error_handler);
context.set_new_subscription_handler(Box::new(on_new_subscription_handler));
context.set_new_publication_handler(Box::new(on_new_publication_handler));
context.set_available_image_handler(Box::new(available_image_handler));
context.set_unavailable_image_handler(Box::new(unavailable_image_handler));
context.set_error_handler(Box::new(error_handler));
context.set_pre_touch_mapped_memory(true);
//context.set_use_conductor_agent_invoker(true); // start it in one thread for debugging

Expand Down
10 changes: 5 additions & 5 deletions examples/throughput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ fn main() {

println!("Using CnC file: {}", context.cnc_file_name());

context.set_new_subscription_handler(on_new_subscription_handler);
context.set_new_publication_handler(on_new_publication_handler);
context.set_available_image_handler(available_image_handler);
context.set_unavailable_image_handler(unavailable_image_handler);
context.set_error_handler(error_handler);
context.set_new_subscription_handler(Box::new(on_new_subscription_handler));
context.set_new_publication_handler(Box::new(on_new_publication_handler));
context.set_available_image_handler(Box::new(available_image_handler));
context.set_unavailable_image_handler(Box::new(unavailable_image_handler));
context.set_error_handler(Box::new(error_handler));
context.set_pre_touch_mapped_memory(true);
//context.set_use_conductor_agent_invoker(true); // start it in one thread for debugging

Expand Down
16 changes: 8 additions & 8 deletions src/aeron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ impl Aeron {
&mut self,
channel: CString,
stream_id: i32,
on_available_image_handler: OnAvailableImage,
on_unavailable_image_handler: OnUnavailableImage,
on_available_image_handler: Box<dyn OnAvailableImage>,
on_unavailable_image_handler: Box<dyn OnUnavailableImage>,
) -> Result<i64, AeronError> {
self.conductor.lock().expect("Mutex poisoned").add_subscription(
channel,
Expand Down Expand Up @@ -401,7 +401,7 @@ impl Aeron {
*
* @param handler to be added to the available counters list.
*/
pub fn add_available_counter_handler(&mut self, handler: OnAvailableCounter) {
pub fn add_available_counter_handler(&mut self, handler: Box<dyn OnAvailableCounter>) {
let _ignored = self
.conductor
.lock()
Expand All @@ -414,7 +414,7 @@ impl Aeron {
*
* @param handler to be removed from the available counters list.
*/
pub fn remove_available_counter_handler(&mut self, handler: OnAvailableCounter) {
pub fn remove_available_counter_handler(&mut self, handler: Box<dyn OnAvailableCounter>) {
let _ignored = self
.conductor
.lock()
Expand All @@ -427,7 +427,7 @@ impl Aeron {
*
* @param handler to be added to the unavailable counters list.
*/
pub fn add_unavailable_counter_handler(&mut self, handler: OnUnavailableCounter) {
pub fn add_unavailable_counter_handler(&mut self, handler: Box<dyn OnUnavailableCounter>) {
let _ignored = self
.conductor
.lock()
Expand All @@ -440,7 +440,7 @@ impl Aeron {
*
* @param handler to be removed from the unavailable counters list.
*/
pub fn remove_unavailable_counter_handler(&mut self, handler: OnUnavailableCounter) {
pub fn remove_unavailable_counter_handler(&mut self, handler: Box<dyn OnUnavailableCounter>) {
let _ignored = self
.conductor
.lock()
Expand All @@ -453,7 +453,7 @@ impl Aeron {
*
* @param handler to be added to the close client handlers list.
*/
pub fn add_close_client_handler(&mut self, handler: OnCloseClient) {
pub fn add_close_client_handler(&mut self, handler: Box<dyn OnCloseClient>) {
let _ignored = self
.conductor
.lock()
Expand All @@ -466,7 +466,7 @@ impl Aeron {
*
* @param handler to be removed from the close client handlers list.
*/
pub fn remove_close_client_handler(&mut self, handler: OnCloseClient) {
pub fn remove_close_client_handler(&mut self, handler: Box<dyn OnCloseClient>) {
let _ignored = self
.conductor
.lock()
Expand Down
Loading

0 comments on commit bd8bbd5

Please sign in to comment.