From f4a2905d40a3592fdd3c784091c9b2e5efef58bf Mon Sep 17 00:00:00 2001 From: Martin Vajnar Date: Sat, 13 Jul 2013 19:11:51 +0200 Subject: [PATCH] ROBOT_DEMO: add proper destructor in C version --- orte/examples/robot/publisher.c | 24 ++++++++++++++++++++---- orte/examples/robot/subscriber.c | 16 +++++++++++----- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/orte/examples/robot/publisher.c b/orte/examples/robot/publisher.c index db4c6c2..ac30b9c 100644 --- a/orte/examples/robot/publisher.c +++ b/orte/examples/robot/publisher.c @@ -1,5 +1,6 @@ #include #include +#include #include "orte.h" typedef struct motion_speed_type motion_speed; @@ -20,7 +21,16 @@ struct robottype_orte_data orte; void send_dummy_cb(const ORTESendInfo *info, void *vinstance, void *sendCallBackParam) { -printf("sent1\n"); + struct motion_speed_type *m = (struct motion_speed_type *)vinstance; + + switch(info->status) { + case NEED_DATA: + printf("Odeslana data: left: %d, right: %d\n", m->left, m->right); + break; + case CQL: + printf("Kriticka uroven fronty zprav na odeslani.\n"); + break; + } } int robottype_roboorte_init(struct robottype_orte_data *data) { @@ -96,6 +106,12 @@ void robottype_publisher_motion_speed_create(struct robottype_orte_data *data, O data->publication_motion_speed = ORTEPublicationCreate(data->orte_domain, "motion_speed", "motion_speed", &data->motion_speed, &persistance, data->strength, callback, arg, &delay); } +void destroy(int sig) { + ORTEPublicationDestroy(orte.publication_motion_speed); + ORTETypeRegisterDestroyAll(orte.orte_domain); + ORTEDomainAppDestroy(orte.orte_domain); +} + int main(void) { orte.strength = 30; robottype_roboorte_init(&orte); @@ -105,9 +121,9 @@ int main(void) { orte.motion_speed.left = 20000; ORTEPublicationSend(orte.publication_motion_speed); - while (1) { - ORTESleepMs(1000); - } + signal(SIGINT, &destroy); + signal(SIGTERM, &destroy); + pause(); return 0; } diff --git a/orte/examples/robot/subscriber.c b/orte/examples/robot/subscriber.c index aa1eaff..c396c04 100644 --- a/orte/examples/robot/subscriber.c +++ b/orte/examples/robot/subscriber.c @@ -1,5 +1,6 @@ #include #include +#include #include "orte.h" typedef struct motion_speed_type motion_speed; @@ -17,7 +18,7 @@ struct robottype_orte_data { }; struct robottype_orte_data orte; - +ORTESubscription *s; int robottype_roboorte_init(struct robottype_orte_data *data) { int rv = 0; @@ -83,7 +84,6 @@ motion_speed_type_register(ORTEDomain *d) { } void robottype_subscriber_motion_speed_create(struct robottype_orte_data *data, ORTERecvCallBack callback, void *arg) { - ORTESubscription *s; NtpTime deadline, minimumSeparation; motion_speed_type_register(data->orte_domain); @@ -112,14 +112,20 @@ void rcv_motion_speed_cb(const ORTERecvInfo *info, void *vinstance, } } +void destroy(int sig) { + ORTESubscriptionDestroy(s); + ORTETypeRegisterDestroyAll(orte.orte_domain); + ORTEDomainAppDestroy(orte.orte_domain); +} + int main(void) { orte.strength = 30; robottype_roboorte_init(&orte); robottype_subscriber_motion_speed_create(&orte, rcv_motion_speed_cb, &orte); - while (1) { - ORTESleepMs(1000); - } + signal(SIGINT, &destroy); + signal(SIGTERM, &destroy); + pause(); return 0; } -- 2.39.2