icedelivery in C🔗
You can find a more detailed description of the C API in the iceoryx_binding_c README.md.
Introduction🔗
The behavior and structure is identical to the icedelivery C++ example so that we explain here only the C API differences and not the underlying mechanisms.
Expected Output🔗
Code walkthrough🔗
Subscriber🔗
Like in the icedelivery C++ example we again follow the steps like:
- Create runtime instance.
- Create subscriber port and subscribe to the offered service.
- Receive data.
- C API: Additionally, we have to remove the previously allocated Subscriber port!
Let's take a look at the receiving
function that comes with the
ice_c_subscriber.c
example.
-
We register our process at roudi with the name
iox-c-subscriber
const char APP_NAME[] = "iox-c-subscriber"; iox_runtime_init("APP_NAME");
-
We create a subscriber port and subscribe to the service {"Radar", "FrontLeft", "Counter" }. Hereby the
historyRequest
tells the subscriber how many previously sent samples it should receive right after the connection is established and thequeueCapacity
how many samples the subscriber can hold. These are samples which the publisher has sent before the subscriber was connected. ThenodeName
is the name of the node, where the subscriber belongs. ThesubscriberStorage
is the place where the subscriber is stored in memory andsubscriber
is actually a pointer to that location.iox_sub_options_t options; iox_sub_options_init(&options); options.historyRequest = 10U; options.queueCapacity = 5U; options.nodeName = "iox-c-subscriber-node"; iox_sub_storage_t subscriberStorage; iox_sub_t subscriber = iox_sub_init(&subscriberStorage, "Radar", "FrontLeft", "Object", &options);
-
In this loop we receive samples as long the
killswitch
is not set totrue
by an external signal and then print the counter value to the console.while (!killswitch) { if (SubscribeState_SUBSCRIBED == iox_sub_get_subscription_state(subscriber)) { const void* userPayload = NULL; while (ChunkReceiveResult_SUCCESS == iox_sub_take_chunk(subscriber, &userPayload)) { const struct RadarObject* sample = (const struct RadarObject*)(userPayload); printf("%s got value: %.0f\n", APP_NAME, sample->x); iox_sub_release_chunk(subscriber, userPayload); } } else { printf("Not subscribed!\n"); } sleep_for(1000); }
-
When using the C API we have to clean up the subscriber after its usage.
iox_sub_deinit(subscriber);
Publisher🔗
The publisher is implemented in a similar way like in the icedelivery C++ example:
- Create runtime instance.
- Create publisher port and offer the service.
- Send data.
- C API: Additionally, we have to remove the previously allocated Publisher port!
Let's take a look at the sending
function that comes with the
ice_c_publisher.c
example.
-
We register our process at roudi with the name
iox-c-subscriber
const char APP_NAME[] = "iox-c-publisher"; iox_runtime_init("APP_NAME");
-
We create a publisher with the service
iox_pub_options_t options; iox_pub_options_init(&options); options.historyCapacity = 10U; options.nodeName = "iox-c-publisher-node"; iox_pub_storage_t publisherStorage; iox_pub_t publisher = iox_pub_init(&publisherStorage, "Radar", "FrontLeft", "Object", &options);
-
Till an external signal sets
killswitch
totrue
we will send an incrementing number to all subscribers in every iteration and print the value of this number to the console.double ct = 0.0; while (!killswitch) { void* userPayload = NULL; if (AllocationResult_SUCCESS == iox_pub_loan_chunk(publisher, &userPayload, sizeof(struct RadarObject))) { struct RadarObject* sample = (struct RadarObject*)userPayload; sample->x = ct; sample->y = ct; sample->z = ct; printf("%s sent value: %.0f\n", APP_NAME, ct); iox_pub_publish_chunk(publisher, userPayload); ++ct; sleep_for(400); } else { printf("Failed to allocate chunk!"); } }
-
And we cleanup our publisher port.
iox_pub_destroy(publisher);