forked from eclipse-iceoryx/iceoryx2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[eclipse-iceoryx#210] Add minimal publish-subscribe example
- Loading branch information
1 parent
4452dd3
commit 7539424
Showing
9 changed files
with
157 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
cmake_minimum_required(VERSION 3.28) | ||
project(example_c_publish_subscribe) | ||
|
||
find_package(iceoryx2_lang_c REQUIRED) | ||
|
||
add_executable(example_c_publisher src/publisher.c) | ||
target_link_libraries(example_c_publisher iceoryx2_lang_c) | ||
|
||
add_executable(example_c_subscriber src/subscriber.c) | ||
target_link_libraries(example_c_subscriber iceoryx2_lang_c) |
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,8 @@ | ||
#include "iox2/iceoryx2.h" | ||
|
||
#include <stdint.h> | ||
|
||
int main(void) { | ||
const uint32_t NUMBER_OF_SECONDS_TO_RUN = 10; | ||
return run_publisher(NUMBER_OF_SECONDS_TO_RUN); | ||
} |
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,8 @@ | ||
#include "iox2/iceoryx2.h" | ||
|
||
#include <stdint.h> | ||
|
||
int main(void) { | ||
const uint32_t NUMBER_OF_SECONDS_TO_RUN = 10; | ||
return run_subscriber(NUMBER_OF_SECONDS_TO_RUN); | ||
} |
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
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,65 @@ | ||
use core::time::Duration; | ||
use iceoryx2::prelude::*; | ||
|
||
const CYCLE_TIME: Duration = Duration::from_secs(1); | ||
|
||
#[no_mangle] | ||
pub extern "C" fn run_publisher(seconds: u32) -> i32 { | ||
let service_name = ServiceName::new("Hello/from/C"); | ||
|
||
if service_name.is_err() { | ||
return -1; | ||
} | ||
|
||
let service_name = service_name.unwrap(); | ||
|
||
let service = zero_copy::Service::new(&service_name) | ||
.publish_subscribe::<u64>() | ||
.open_or_create(); | ||
|
||
if service.is_err() { | ||
return -1; | ||
} | ||
|
||
let service = service.unwrap(); | ||
|
||
let publisher = service.publisher().create(); | ||
|
||
if publisher.is_err() { | ||
return -1; | ||
} | ||
|
||
let publisher = publisher.unwrap(); | ||
|
||
let mut counter: u64 = 0; | ||
|
||
let mut remaining_seconds = seconds; | ||
|
||
while let Iox2Event::Tick = Iox2::wait(CYCLE_TIME) { | ||
counter += 1; | ||
let sample = publisher.loan_uninit(); | ||
|
||
if sample.is_err() { | ||
return -1; | ||
} | ||
|
||
let sample = sample.unwrap(); | ||
|
||
let sample = sample.write_payload(counter); | ||
|
||
if sample.send().is_err() { | ||
return -1; | ||
} | ||
|
||
println!("Send sample {} ...", counter); | ||
|
||
remaining_seconds = remaining_seconds.saturating_sub(1); | ||
if remaining_seconds == 0 { | ||
break; | ||
} | ||
} | ||
|
||
println!("exit"); | ||
|
||
return 0; | ||
} |
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,54 @@ | ||
use core::time::Duration; | ||
use iceoryx2::prelude::*; | ||
|
||
const CYCLE_TIME: Duration = Duration::from_secs(1); | ||
|
||
#[no_mangle] | ||
pub extern "C" fn run_subscriber(seconds: u32) -> i32 { | ||
let service_name = ServiceName::new("Hello/from/C"); | ||
|
||
if service_name.is_err() { | ||
return -1; | ||
} | ||
|
||
let service_name = service_name.unwrap(); | ||
|
||
let service = zero_copy::Service::new(&service_name) | ||
.publish_subscribe::<u64>() | ||
.open_or_create(); | ||
|
||
if service.is_err() { | ||
return -1; | ||
} | ||
|
||
let service = service.unwrap(); | ||
|
||
let subscriber = service.subscriber().create(); | ||
|
||
if subscriber.is_err() { | ||
return -1; | ||
} | ||
|
||
let subscriber = subscriber.unwrap(); | ||
|
||
let mut remaining_seconds = seconds; | ||
|
||
while let Iox2Event::Tick = Iox2::wait(CYCLE_TIME) { | ||
loop { | ||
match subscriber.receive() { | ||
Ok(Some(sample)) => println!("received: {:?}", *sample), | ||
Ok(None) => break, | ||
Err(_) => return -1, | ||
} | ||
} | ||
|
||
remaining_seconds = remaining_seconds.saturating_sub(1); | ||
if remaining_seconds == 0 { | ||
break; | ||
} | ||
} | ||
|
||
println!("exit"); | ||
|
||
return 0; | ||
} |