Skip to content

Commit

Permalink
[eclipse-iceoryx#370] Introduce domain c publisher example
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Sep 19, 2024
1 parent 13b0316 commit 4f2749b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
5 changes: 5 additions & 0 deletions examples/c/domains/src/discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,24 @@ int main(int argc, char** argv) {
exit(-1);
}

// create a new config based on the global config
iox2_config_ptr config_ptr = iox2_config_global_config();
iox2_config_h config = NULL;
iox2_config_from_ptr(config_ptr, NULL, &config);
iox2_config_ref_h config_ref = iox2_cast_config_ref_h(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) {
iox2_config_drop(config);
printf("invalid domain name\"%s\"\n", argv[1]);
exit(-1);
}

printf("\nServices running in domain \"%s\":\n", argv[1]);

// use the custom config when listing the services
if (iox2_service_list(iox2_service_type_e_IPC, config_ptr, list_callback, NULL) != IOX2_OK) {
printf("Failed to list all services.");
}
Expand Down
37 changes: 31 additions & 6 deletions examples/c/domains/src/publisher.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,40 @@
#include <stdio.h>
#include <string.h>

int main(void) {
int main(int argc, char** argv) {
if (argc != 3) {
printf("usage: %s DOMAIN_NAME SERVICE_NAME\n", argv[0]);
exit(-1);
}

// create a new config based on the global config
iox2_config_ptr config_ptr = iox2_config_global_config();
iox2_config_h config = NULL;
iox2_config_from_ptr(config_ptr, NULL, &config);
iox2_config_ref_h config_ref = iox2_cast_config_ref_h(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) {
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_ref_h node_builder_ref = iox2_cast_node_builder_ref_h(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);
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;
goto drop_config;
}

// create service name
const char* service_name_value = "My/Funk/ServiceName";
const char* service_name_value = argv[2];
iox2_service_name_h service_name = NULL;
if (iox2_service_name_new(NULL, service_name_value, strlen(service_name_value), &service_name) != IOX2_OK) {
printf("Unable to create service name!\n");
Expand Down Expand Up @@ -103,10 +126,9 @@ int main(void) {
goto drop_publisher;
}

printf("Send sample %d ...\n", counter);
printf("[domain: \"%s\", service: \"%s\"] Send sample %d ...\n", argv[1], argv[2], counter);
}


drop_publisher:
iox2_publisher_drop(publisher);

Expand All @@ -116,6 +138,9 @@ int main(void) {
drop_node:
iox2_node_drop(node_handle);

drop_config:
iox2_config_drop(config);

end:
return 0;
}

0 comments on commit 4f2749b

Please sign in to comment.