diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-10-25 13:57:59 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-10-25 13:57:59 +0200 |
| commit | f2f0a815ccdecb9c65c6d661f9b57cc8ba0ab81e (patch) | |
| tree | b9657517d0f478a84ed0d16a887416b6557c987c | |
| parent | d2a6897f05a07a058b2185c953b30d26fded081b (diff) | |
| download | use-mosquitto-f2f0a815ccdecb9c65c6d661f9b57cc8ba0ab81e.tar.gz use-mosquitto-f2f0a815ccdecb9c65c6d661f9b57cc8ba0ab81e.tar.bz2 use-mosquitto-f2f0a815ccdecb9c65c6d661f9b57cc8ba0ab81e.zip | |
benutze atexit() zum Aufräumen der Bibliothek
| -rw-r--r-- | src/main.c | 25 |
1 files changed, 18 insertions, 7 deletions
| @@ -10,6 +10,10 @@ | |||
| 10 | // Project | 10 | // Project |
| 11 | #include <mosquitto.h> | 11 | #include <mosquitto.h> |
| 12 | 12 | ||
| 13 | #ifndef UNUSED | ||
| 14 | # define UNUSED(x) (void) (x) | ||
| 15 | #endif | ||
| 16 | |||
| 13 | #define CLIENT_ID "my-client-id" | 17 | #define CLIENT_ID "my-client-id" |
| 14 | #define HOST "192.168.77.2" | 18 | #define HOST "192.168.77.2" |
| 15 | #define PORT 1883 | 19 | #define PORT 1883 |
| @@ -21,7 +25,7 @@ | |||
| 21 | 25 | ||
| 22 | static volatile bool terminate = false; // NOLINT | 26 | static volatile bool terminate = false; // NOLINT |
| 23 | 27 | ||
| 24 | void | 28 | static void |
| 25 | handler(int signal) | 29 | handler(int signal) |
| 26 | { | 30 | { |
| 27 | if ( signal == SIGINT ) { | 31 | if ( signal == SIGINT ) { |
| @@ -32,19 +36,26 @@ handler(int signal) | |||
| 32 | static void | 36 | static void |
| 33 | message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) | 37 | message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) |
| 34 | { | 38 | { |
| 35 | (void) obj; | 39 | UNUSED(obj); |
| 36 | (void) mosq; | 40 | UNUSED(mosq); |
| 37 | printf("message '%.*s' for topic '%s'\n", | 41 | printf("message '%.*s' for topic '%s'\n", |
| 38 | message->payloadlen, (const char *) message->payload, | 42 | message->payloadlen, (const char *) message->payload, |
| 39 | message->topic); | 43 | message->topic); |
| 40 | } | 44 | } |
| 41 | 45 | ||
| 46 | static void | ||
| 47 | cleanup(void) | ||
| 48 | { | ||
| 49 | mosquitto_lib_cleanup(); | ||
| 50 | } | ||
| 51 | |||
| 42 | int | 52 | int |
| 43 | main(void) | 53 | main(void) |
| 44 | { | 54 | { |
| 45 | (void) signal(SIGINT, handler); | 55 | (void) signal(SIGINT, handler); |
| 46 | 56 | ||
| 47 | mosquitto_lib_init(); | 57 | mosquitto_lib_init(); |
| 58 | (void) atexit(cleanup); | ||
| 48 | 59 | ||
| 49 | struct mosquitto *client = mosquitto_new(CLIENT_ID, true, NULL); | 60 | struct mosquitto *client = mosquitto_new(CLIENT_ID, true, NULL); |
| 50 | if ( client == NULL ) { | 61 | if ( client == NULL ) { |
| @@ -71,7 +82,9 @@ main(void) | |||
| 71 | mosquitto_subscribe(client, NULL, TOPIC, 0); | 82 | mosquitto_subscribe(client, NULL, TOPIC, 0); |
| 72 | 83 | ||
| 73 | while ( !terminate ) { | 84 | while ( !terminate ) { |
| 74 | err = mosquitto_loop(client, -1, 1); | 85 | static const int defaultTimeout = -1; // 1000ms |
| 86 | |||
| 87 | err = mosquitto_loop(client, defaultTimeout, 1); | ||
| 75 | if ( !terminate && err ) { | 88 | if ( !terminate && err ) { |
| 76 | (void) fprintf(stderr, "connection error: %s\n", mosquitto_strerror(err)); | 89 | (void) fprintf(stderr, "connection error: %s\n", mosquitto_strerror(err)); |
| 77 | sleep(WAIT_UNTIL_RECONNECT); // NOLINT | 90 | sleep(WAIT_UNTIL_RECONNECT); // NOLINT |
| @@ -79,11 +92,9 @@ main(void) | |||
| 79 | } | 92 | } |
| 80 | } | 93 | } |
| 81 | 94 | ||
| 82 | printf("cleanup...\n"); | 95 | puts("shutdown..."); |
| 83 | mosquitto_disconnect(client); | 96 | mosquitto_disconnect(client); |
| 84 | mosquitto_destroy(client); | 97 | mosquitto_destroy(client); |
| 85 | 98 | ||
| 86 | mosquitto_lib_cleanup(); | ||
| 87 | |||
| 88 | return EXIT_SUCCESS; | 99 | return EXIT_SUCCESS; |
| 89 | } | 100 | } |
