Skip to content

Commit

Permalink
[eclipse-iceoryx#210] Rework _h_ref handling
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Sep 28, 2024
1 parent 58b90db commit f230a93
Show file tree
Hide file tree
Showing 60 changed files with 928 additions and 1,252 deletions.
3 changes: 1 addition & 2 deletions examples/c/domains/src/discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ int main(int argc, char** argv) {
iox2_config_ptr config_ptr = iox2_config_global_config();
iox2_config_h config = NULL;
iox2_config_from_ptr(config_ptr, NULL, &config);
iox2_config_h_ref config_ref = iox2_cast_config_h_ref(config);
config_ptr = iox2_cast_config_ptr(config);

// The domain name becomes the prefix for all resources.
// Therefore, different domain names never share the same resources.
if (iox2_config_global_set_prefix(config_ref, argv[1]) != IOX2_OK) {
if (iox2_config_global_set_prefix(&config, argv[1]) != IOX2_OK) {
iox2_config_drop(config);
printf("invalid domain name\"%s\"\n", argv[1]);
exit(-1);
Expand Down
24 changes: 8 additions & 16 deletions examples/c/domains/src/publisher.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,21 @@ int main(int argc, char** argv) {
iox2_config_ptr config_ptr = iox2_config_global_config();
iox2_config_h config = NULL;
iox2_config_from_ptr(config_ptr, NULL, &config);
iox2_config_h_ref config_ref = iox2_cast_config_h_ref(config);

// The domain name becomes the prefix for all resources.
// Therefore, different domain names never share the same resources.
if (iox2_config_global_set_prefix(config_ref, argv[1]) != IOX2_OK) {
if (iox2_config_global_set_prefix(&config, argv[1]) != IOX2_OK) {
printf("invalid domain name\"%s\"\n", argv[1]);
goto drop_config;
}

// create new node
iox2_node_builder_h node_builder_handle = iox2_node_builder_new(NULL);
iox2_node_h node_handle = NULL;
iox2_node_builder_h_ref node_builder_ref = iox2_cast_node_builder_h_ref(node_builder_handle);

// use the custom config when creating the custom node
// every service constructed by the node will use this config
iox2_node_builder_set_config(node_builder_ref, config_ref);
iox2_node_builder_set_config(&node_builder_handle, &config);
if (iox2_node_builder_create(node_builder_handle, NULL, iox2_service_type_e_IPC, &node_handle) != IOX2_OK) {
goto drop_config;
}
Expand All @@ -63,15 +61,12 @@ int main(int argc, char** argv) {

// create service builder
iox2_service_name_ptr service_name_ptr = iox2_cast_service_name_ptr(service_name);
iox2_node_h_ref node_h_refandle = iox2_cast_node_h_ref(node_handle);
iox2_service_builder_h service_builder = iox2_node_service_builder(node_h_refandle, NULL, service_name_ptr);
iox2_service_builder_h service_builder = iox2_node_service_builder(&node_handle, NULL, service_name_ptr);
iox2_service_builder_pub_sub_h service_builder_pub_sub = iox2_service_builder_pub_sub(service_builder);
iox2_service_builder_pub_sub_h_ref service_builder_pub_sub_ref =
iox2_cast_service_builder_pub_sub_h_ref(service_builder_pub_sub);

// set pub sub payload type
const char* payload_type_name = "16TransmissionData";
if (iox2_service_builder_pub_sub_set_payload_type_details(service_builder_pub_sub_ref,
if (iox2_service_builder_pub_sub_set_payload_type_details(&service_builder_pub_sub,
iox2_type_variant_e_FIXED_SIZE,
payload_type_name,
strlen(payload_type_name),
Expand All @@ -90,31 +85,28 @@ int main(int argc, char** argv) {
}

// create publisher
iox2_port_factory_pub_sub_h_ref ref_service = iox2_cast_port_factory_pub_sub_h_ref(service);
iox2_port_factory_publisher_builder_h publisher_builder =
iox2_port_factory_pub_sub_publisher_builder(ref_service, NULL);
iox2_port_factory_pub_sub_publisher_builder(&service, NULL);
iox2_publisher_h publisher = NULL;
if (iox2_port_factory_publisher_builder_create(publisher_builder, NULL, &publisher) != IOX2_OK) {
printf("Unable to create publisher!\n");
goto drop_service;
}
iox2_publisher_h_ref publisher_ref = iox2_cast_publisher_h_ref(publisher);

int32_t counter = 0;
while (iox2_node_wait(node_h_refandle, 1, 0) == iox2_node_event_e_TICK) {
while (iox2_node_wait(&node_handle, 1, 0) == iox2_node_event_e_TICK) {
counter += 1;

// loan sample
iox2_sample_mut_h sample = NULL;
if (iox2_publisher_loan(publisher_ref, NULL, &sample) != IOX2_OK) {
if (iox2_publisher_loan(&publisher, NULL, &sample) != IOX2_OK) {
printf("Failed to loan sample\n");
goto drop_publisher;
}
iox2_sample_mut_h_ref sample_ref = iox2_cast_sample_mut_h_ref(sample);

// write payload
struct TransmissionData* payload = NULL;
iox2_sample_mut_payload_mut(sample_ref, (void**) &payload, NULL);
iox2_sample_mut_payload_mut(&sample, (void**) &payload, NULL);
payload->x = counter;
payload->y = counter * 3;
payload->funky = counter * 812.12; // NOLINT
Expand Down
24 changes: 8 additions & 16 deletions examples/c/domains/src/subscriber.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,21 @@ int main(int argc, char** argv) {
iox2_config_ptr config_ptr = iox2_config_global_config();
iox2_config_h config = NULL;
iox2_config_from_ptr(config_ptr, NULL, &config);
iox2_config_h_ref config_ref = iox2_cast_config_h_ref(config);

// The domain name becomes the prefix for all resources.
// Therefore, different domain names never share the same resources.
if (iox2_config_global_set_prefix(config_ref, argv[1]) != IOX2_OK) {
if (iox2_config_global_set_prefix(&config, argv[1]) != IOX2_OK) {
printf("invalid domain name\"%s\"\n", argv[1]);
goto drop_config;
}

// create new node
iox2_node_builder_h node_builder_handle = iox2_node_builder_new(NULL);
iox2_node_h node_handle = NULL;
iox2_node_builder_h_ref node_builder_ref = iox2_cast_node_builder_h_ref(node_builder_handle);

// use the custom config when creating the custom node
// every service constructed by the node will use this config
iox2_node_builder_set_config(node_builder_ref, config_ref);
iox2_node_builder_set_config(&node_builder_handle, &config);
if (iox2_node_builder_create(node_builder_handle, NULL, iox2_service_type_e_IPC, &node_handle) != IOX2_OK) {
printf("Could not create node!\n");
goto end;
Expand All @@ -64,15 +62,12 @@ int main(int argc, char** argv) {

// create service builder
iox2_service_name_ptr service_name_ptr = iox2_cast_service_name_ptr(service_name);
iox2_node_h_ref node_h_refandle = iox2_cast_node_h_ref(node_handle);
iox2_service_builder_h service_builder = iox2_node_service_builder(node_h_refandle, NULL, service_name_ptr);
iox2_service_builder_h service_builder = iox2_node_service_builder(&node_handle, NULL, service_name_ptr);
iox2_service_builder_pub_sub_h service_builder_pub_sub = iox2_service_builder_pub_sub(service_builder);
iox2_service_builder_pub_sub_h_ref service_builder_pub_sub_ref =
iox2_cast_service_builder_pub_sub_h_ref(service_builder_pub_sub);

// set pub sub payload type
const char* payload_type_name = "16TransmissionData";
if (iox2_service_builder_pub_sub_set_payload_type_details(service_builder_pub_sub_ref,
if (iox2_service_builder_pub_sub_set_payload_type_details(&service_builder_pub_sub,
iox2_type_variant_e_FIXED_SIZE,
payload_type_name,
strlen(payload_type_name),
Expand All @@ -91,32 +86,29 @@ int main(int argc, char** argv) {
}

// create subscriber
iox2_port_factory_pub_sub_h_ref ref_service = iox2_cast_port_factory_pub_sub_h_ref(service);
iox2_port_factory_subscriber_builder_h subscriber_builder =
iox2_port_factory_pub_sub_subscriber_builder(ref_service, NULL);
iox2_port_factory_pub_sub_subscriber_builder(&service, NULL);
iox2_subscriber_h subscriber = NULL;
if (iox2_port_factory_subscriber_builder_create(subscriber_builder, NULL, &subscriber) != IOX2_OK) {
printf("Unable to create subscriber!\n");
goto drop_service;
}
iox2_subscriber_h_ref subscriber_ref = iox2_cast_subscriber_h_ref(subscriber);

uint64_t counter = 0;
printf("subscribed to: [domain: \"%s\", service: \"%s\"]\n", argv[1], argv[2]);
while (iox2_node_wait(node_h_refandle, 1, 0) == iox2_node_event_e_TICK) {
while (iox2_node_wait(&node_handle, 1, 0) == iox2_node_event_e_TICK) {
counter += 1;

// receive sample
iox2_sample_h sample = NULL;
if (iox2_subscriber_receive(subscriber_ref, NULL, &sample) != IOX2_OK) {
if (iox2_subscriber_receive(&subscriber, NULL, &sample) != IOX2_OK) {
printf("Failed to receive sample\n");
goto drop_subscriber;
}

if (sample != NULL) {
iox2_sample_h_ref sample_ref = iox2_cast_sample_h_ref(sample);
struct TransmissionData* payload = NULL;
iox2_sample_payload(sample_ref, (const void**) &payload, NULL);
iox2_sample_payload(&sample, (const void**) &payload, NULL);

printf(
"received: TransmissionData { .x: %d, .y: %d, .funky: %lf}\n", payload->x, payload->y, payload->funky);
Expand Down
11 changes: 4 additions & 7 deletions examples/c/event/src/listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ int main(void) {

// create service
iox2_service_name_ptr service_name_ptr = iox2_cast_service_name_ptr(service_name);
iox2_node_h_ref node_h_refandle = iox2_cast_node_h_ref(node_handle);
iox2_service_builder_h service_builder = iox2_node_service_builder(node_h_refandle, NULL, service_name_ptr);
iox2_service_builder_h service_builder = iox2_node_service_builder(&node_handle, NULL, service_name_ptr);
iox2_service_builder_event_h service_builder_event = iox2_service_builder_event(service_builder);
iox2_port_factory_event_h service = NULL;
if (iox2_service_builder_event_open_or_create(service_builder_event, NULL, &service) != IOX2_OK) {
Expand All @@ -45,19 +44,17 @@ int main(void) {
}

// create listener
iox2_port_factory_event_h_ref ref_service = iox2_cast_port_factory_event_h_ref(service);
iox2_port_factory_listener_builder_h listener_builder = iox2_port_factory_event_listener_builder(ref_service, NULL);
iox2_port_factory_listener_builder_h listener_builder = iox2_port_factory_event_listener_builder(&service, NULL);
iox2_listener_h listener = NULL;
if (iox2_port_factory_listener_builder_create(listener_builder, NULL, &listener) != IOX2_OK) {
printf("Unable to create listener!\n");
goto drop_service;
}
iox2_listener_h_ref listener_ref = iox2_cast_listener_h_ref(listener);
iox2_event_id_t event_id;

while (iox2_node_wait(node_h_refandle, 0, 0) == iox2_node_event_e_TICK) {
while (iox2_node_wait(&node_handle, 0, 0) == iox2_node_event_e_TICK) {
bool has_received_one = false;
if (iox2_listener_timed_wait_one(listener_ref, &event_id, &has_received_one, 1, 0) != IOX2_OK) {
if (iox2_listener_timed_wait_one(&listener, &event_id, &has_received_one, 1, 0) != IOX2_OK) {
printf("Unable to wait for notification!\n");
goto drop_listener;
}
Expand Down
11 changes: 4 additions & 7 deletions examples/c/event/src/notifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ int main(void) {

// create service
iox2_service_name_ptr service_name_ptr = iox2_cast_service_name_ptr(service_name);
iox2_node_h_ref node_h_refandle = iox2_cast_node_h_ref(node_handle);
iox2_service_builder_h service_builder = iox2_node_service_builder(node_h_refandle, NULL, service_name_ptr);
iox2_service_builder_h service_builder = iox2_node_service_builder(&node_handle, NULL, service_name_ptr);
iox2_service_builder_event_h service_builder_event = iox2_service_builder_event(service_builder);
iox2_port_factory_event_h service = NULL;
if (iox2_service_builder_event_open_or_create(service_builder_event, NULL, &service) != IOX2_OK) {
Expand All @@ -52,20 +51,18 @@ int main(void) {
}

// create notifier
iox2_port_factory_event_h_ref ref_service = iox2_cast_port_factory_event_h_ref(service);
iox2_port_factory_notifier_builder_h notifier_builder = iox2_port_factory_event_notifier_builder(ref_service, NULL);
iox2_port_factory_notifier_builder_h notifier_builder = iox2_port_factory_event_notifier_builder(&service, NULL);
iox2_notifier_h notifier = NULL;
if (iox2_port_factory_notifier_builder_create(notifier_builder, NULL, &notifier) != IOX2_OK) {
printf("Unable to create notifier!\n");
goto drop_service;
}
iox2_notifier_h_ref notifier_ref = iox2_cast_notifier_h_ref(notifier);

uint64_t counter = 0;
while (iox2_node_wait(node_h_refandle, 0, 0) == iox2_node_event_e_TICK) {
while (iox2_node_wait(&node_handle, 0, 0) == iox2_node_event_e_TICK) {
counter += 1;
iox2_event_id_t event_id = { .value = counter % 12 }; // NOLINT
if (iox2_notifier_notify_with_custom_event_id(notifier_ref, &event_id, NULL) != IOX2_OK) {
if (iox2_notifier_notify_with_custom_event_id(&notifier, &event_id, NULL) != IOX2_OK) {
printf("Failed to notify listener!\n");
goto drop_notifier;
}
Expand Down
18 changes: 6 additions & 12 deletions examples/c/publish_subscribe/src/publisher.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,12 @@ int main(void) {

// create service builder
iox2_service_name_ptr service_name_ptr = iox2_cast_service_name_ptr(service_name);
iox2_node_h_ref node_h_refandle = iox2_cast_node_h_ref(node_handle);
iox2_service_builder_h service_builder = iox2_node_service_builder(node_h_refandle, NULL, service_name_ptr);
iox2_service_builder_h service_builder = iox2_node_service_builder(&node_handle, NULL, service_name_ptr);
iox2_service_builder_pub_sub_h service_builder_pub_sub = iox2_service_builder_pub_sub(service_builder);
iox2_service_builder_pub_sub_h_ref service_builder_pub_sub_ref =
iox2_cast_service_builder_pub_sub_h_ref(service_builder_pub_sub);

// set pub sub payload type
const char* payload_type_name = "16TransmissionData";
if (iox2_service_builder_pub_sub_set_payload_type_details(service_builder_pub_sub_ref,
if (iox2_service_builder_pub_sub_set_payload_type_details(&service_builder_pub_sub,
iox2_type_variant_e_FIXED_SIZE,
payload_type_name,
strlen(payload_type_name),
Expand All @@ -68,31 +65,28 @@ int main(void) {
}

// create publisher
iox2_port_factory_pub_sub_h_ref ref_service = iox2_cast_port_factory_pub_sub_h_ref(service);
iox2_port_factory_publisher_builder_h publisher_builder =
iox2_port_factory_pub_sub_publisher_builder(ref_service, NULL);
iox2_port_factory_pub_sub_publisher_builder(&service, NULL);
iox2_publisher_h publisher = NULL;
if (iox2_port_factory_publisher_builder_create(publisher_builder, NULL, &publisher) != IOX2_OK) {
printf("Unable to create publisher!\n");
goto drop_service;
}
iox2_publisher_h_ref publisher_ref = iox2_cast_publisher_h_ref(publisher);

int32_t counter = 0;
while (iox2_node_wait(node_h_refandle, 1, 0) == iox2_node_event_e_TICK) {
while (iox2_node_wait(&node_handle, 1, 0) == iox2_node_event_e_TICK) {
counter += 1;

// loan sample
iox2_sample_mut_h sample = NULL;
if (iox2_publisher_loan(publisher_ref, NULL, &sample) != IOX2_OK) {
if (iox2_publisher_loan(&publisher, NULL, &sample) != IOX2_OK) {
printf("Failed to loan sample\n");
goto drop_publisher;
}
iox2_sample_mut_h_ref sample_ref = iox2_cast_sample_mut_h_ref(sample);

// write payload
struct TransmissionData* payload = NULL;
iox2_sample_mut_payload_mut(sample_ref, (void**) &payload, NULL);
iox2_sample_mut_payload_mut(&sample, (void**) &payload, NULL);
payload->x = counter;
payload->y = counter * 3;
payload->funky = counter * 812.12; // NOLINT
Expand Down
18 changes: 6 additions & 12 deletions examples/c/publish_subscribe/src/subscriber.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,12 @@ int main(void) {

// create service builder
iox2_service_name_ptr service_name_ptr = iox2_cast_service_name_ptr(service_name);
iox2_node_h_ref node_h_refandle = iox2_cast_node_h_ref(node_handle);
iox2_service_builder_h service_builder = iox2_node_service_builder(node_h_refandle, NULL, service_name_ptr);
iox2_service_builder_h service_builder = iox2_node_service_builder(&node_handle, NULL, service_name_ptr);
iox2_service_builder_pub_sub_h service_builder_pub_sub = iox2_service_builder_pub_sub(service_builder);
iox2_service_builder_pub_sub_h_ref service_builder_pub_sub_ref =
iox2_cast_service_builder_pub_sub_h_ref(service_builder_pub_sub);

// set pub sub payload type
const char* payload_type_name = "16TransmissionData";
if (iox2_service_builder_pub_sub_set_payload_type_details(service_builder_pub_sub_ref,
if (iox2_service_builder_pub_sub_set_payload_type_details(&service_builder_pub_sub,
iox2_type_variant_e_FIXED_SIZE,
payload_type_name,
strlen(payload_type_name),
Expand All @@ -68,31 +65,28 @@ int main(void) {
}

// create subscriber
iox2_port_factory_pub_sub_h_ref ref_service = iox2_cast_port_factory_pub_sub_h_ref(service);
iox2_port_factory_subscriber_builder_h subscriber_builder =
iox2_port_factory_pub_sub_subscriber_builder(ref_service, NULL);
iox2_port_factory_pub_sub_subscriber_builder(&service, NULL);
iox2_subscriber_h subscriber = NULL;
if (iox2_port_factory_subscriber_builder_create(subscriber_builder, NULL, &subscriber) != IOX2_OK) {
printf("Unable to create subscriber!\n");
goto drop_service;
}
iox2_subscriber_h_ref subscriber_ref = iox2_cast_subscriber_h_ref(subscriber);

uint64_t counter = 0;
while (iox2_node_wait(node_h_refandle, 1, 0) == iox2_node_event_e_TICK) {
while (iox2_node_wait(&node_handle, 1, 0) == iox2_node_event_e_TICK) {
counter += 1;

// receive sample
iox2_sample_h sample = NULL;
if (iox2_subscriber_receive(subscriber_ref, NULL, &sample) != IOX2_OK) {
if (iox2_subscriber_receive(&subscriber, NULL, &sample) != IOX2_OK) {
printf("Failed to receive sample\n");
goto drop_subscriber;
}

if (sample != NULL) {
iox2_sample_h_ref sample_ref = iox2_cast_sample_h_ref(sample);
struct TransmissionData* payload = NULL;
iox2_sample_payload(sample_ref, (const void**) &payload, NULL);
iox2_sample_payload(&sample, (const void**) &payload, NULL);

printf(
"received: TransmissionData { .x: %d, .y: %d, .funky: %lf}\n", payload->x, payload->y, payload->funky);
Expand Down
Loading

0 comments on commit f230a93

Please sign in to comment.