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.
pull/937/merge
Roger A. Light 7 years ago
parent 8b66a323cd
commit 0e76bed50e

@ -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
==============

@ -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;

@ -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 */

@ -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; i<cfg->topic_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; i<cfg->unsub_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");

@ -36,6 +36,7 @@
<arg><option>-c</option></arg>
<arg><option>-C</option> <replaceable>msg count</replaceable></arg>
<arg><option>-d</option></arg>
<arg><option>-E</option></arg>
<arg><option>-i</option> <replaceable>client_id</replaceable></arg>
<arg><option>-I</option> <replaceable>client id prefix</replaceable></arg>
<arg><option>-k</option> <replaceable>keepalive time</replaceable></arg>
@ -209,6 +210,18 @@
<para>Enable debug messages.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-E</option></term>
<listitem>
<para>If this option is given,
<command>mosquitto_sub</command> will exit immediately
that all of its subscriptions have been acknowledged by
the broker. In conjunction with <option>-c</option>
this allows a durable client session to be initialised
on the broker for future use without requiring any
messages to be received.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-F</option></term>
<listitem>

Loading…
Cancel
Save