From 0e76bed50ebc8a787baea61784196eafc7881b28 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 27 Sep 2018 22:16:38 +0100 Subject: [PATCH] Add -E option to mosquitto_sub. This causes the client to exit immediately after its subscriptions are acknowledged by the broker, and can be used to create a durable client session without requiring messages to be delivered. Closes #952. --- ChangeLog.txt | 7 ++++++- client/client_shared.c | 5 +++++ client/client_shared.h | 1 + client/sub_client.c | 12 ++++++++---- man/mosquitto_sub.1.xml | 13 +++++++++++++ 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index cb028f01..c949618a 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,10 +1,15 @@ 1.6 - 2018xxxx ============== -Library features: +Client library features: - Add mosquitto_subscribe_multiple() for sending subscriptions to multiple topics in one command. +Client features: +- Add -E to mosquitto_sub, which causes it to exit immediately after having + its subscriptions acknowledged. Use with -c to create a durable client + session without requiring a message to be received. + 1.5 - 20180502 ============== diff --git a/client/client_shared.c b/client/client_shared.c index f14ebb1e..eeeee506 100644 --- a/client/client_shared.c +++ b/client/client_shared.c @@ -479,6 +479,11 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c } } i++; + }else if(!strcmp(argv[i], "-E")){ + if(pub_or_sub == CLIENT_PUB){ + goto unknown_option; + } + cfg->exit_after_sub = true; }else if(!strcmp(argv[i], "-F")){ if(pub_or_sub == CLIENT_PUB){ goto unknown_option; diff --git a/client/client_shared.h b/client/client_shared.h index f1ce6f31..06762293 100644 --- a/client/client_shared.h +++ b/client/client_shared.h @@ -74,6 +74,7 @@ struct mosq_config { bool clean_session; char **topics; /* sub */ int topic_count; /* sub */ + bool exit_after_sub; /* sub */ bool no_retain; /* sub */ bool retained_only; /* sub */ char **filter_outs; /* sub */ diff --git a/client/sub_client.c b/client/sub_client.c index 1d836846..5dce7354 100644 --- a/client/sub_client.c +++ b/client/sub_client.c @@ -96,9 +96,8 @@ void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flag cfg = (struct mosq_config *)obj; if(!result){ - for(i=0; itopic_count; i++){ - mosquitto_subscribe(mosq, NULL, cfg->topics[i], cfg->qos); - } + mosquitto_subscribe_multiple(mosq, NULL, cfg->topic_count, (const char **)cfg->topics, cfg->qos); + for(i=0; iunsub_topic_count; i++){ mosquitto_unsubscribe(mosq, NULL, cfg->unsub_topics[i]); } @@ -123,6 +122,10 @@ void my_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_c if(!cfg->quiet) printf(", %d", granted_qos[i]); } if(!cfg->quiet) printf("\n"); + + if(cfg->exit_after_sub){ + mosquitto_disconnect(mosq); + } } void my_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str) @@ -139,7 +142,7 @@ void print_usage(void) printf("mosquitto_sub version %s running on libmosquitto %d.%d.%d.\n\n", VERSION, major, minor, revision); printf("Usage: mosquitto_sub {[-h host] [-p port] [-u username [-P password]] -t topic | -L URL [-t topic]}\n"); printf(" [-c] [-k keepalive] [-q qos]\n"); - printf(" [-C msg_count] [-R] [--retained-only] [-T filter_out] [-U topic ...]\n"); + printf(" [-C msg_count] [-E] [-R] [--retained-only] [-T filter_out] [-U topic ...]\n"); printf(" [-F format]\n"); #ifndef WIN32 printf(" [-W timeout_secs]\n"); @@ -168,6 +171,7 @@ void print_usage(void) printf(" -c : disable 'clean session' (store subscription and pending messages when client disconnects).\n"); printf(" -C : disconnect and exit after receiving the 'msg_count' messages.\n"); printf(" -d : enable debug messages.\n"); + printf(" -E : Exit once all subscriptions have been acknowledged by the broker.\n"); printf(" -F : output format.\n"); printf(" -h : mqtt host to connect to. Defaults to localhost.\n"); printf(" -i : id to use for this client. Defaults to mosquitto_sub_ appended with the process id.\n"); diff --git a/man/mosquitto_sub.1.xml b/man/mosquitto_sub.1.xml index 01f9c979..b25b19a1 100644 --- a/man/mosquitto_sub.1.xml +++ b/man/mosquitto_sub.1.xml @@ -36,6 +36,7 @@ msg count + client_id client id prefix keepalive time @@ -209,6 +210,18 @@ Enable debug messages. + + + + If this option is given, + mosquitto_sub will exit immediately + that all of its subscriptions have been acknowledged by + the broker. In conjunction with + this allows a durable client session to be initialised + on the broker for future use without requiring any + messages to be received. + +