From a285c6ce5cf9f264a14e16287237a93a3ffea1cb Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Sun, 25 May 2014 00:49:08 +0100 Subject: [PATCH] Add -1 (oneshot) option to mosquitto_sub. Thanks to JP Mens. --- ChangeLog.txt | 1 + client/client_shared.c | 6 ++++++ client/client_shared.h | 1 + client/sub_client.c | 14 +++++++++++++- man/mosquitto_sub.1.xml | 14 ++++++++++++++ 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 8c9117f2..640a7138 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -2,6 +2,7 @@ Clients: - Both clients can now load default configuration options from a file. +- Add -1 (oneshot) option to mosquitto_sub. 1.3.2 - 2014xxxx ================ diff --git a/client/client_shared.c b/client/client_shared.c index 81388ca5..8ebb2561 100644 --- a/client/client_shared.c +++ b/client/client_shared.c @@ -184,6 +184,12 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c } } i++; + }else if(!strcmp(argv[i], "-1") || !strcmp(argv[i], "--oneshot")){ + if(pub_or_sub == CLIENT_PUB){ + goto unknown_option; + }else{ + cfg->oneshot = true; + } }else if(!strcmp(argv[i], "-A")){ if(i==argc-1){ fprintf(stderr, "Error: -A argument given but no address specified.\n\n"); diff --git a/client/client_shared.h b/client/client_shared.h index 5d2b6fed..c55f5bb1 100644 --- a/client/client_shared.h +++ b/client/client_shared.h @@ -76,6 +76,7 @@ struct mosq_config { int filter_out_count; /* sub */ bool verbose; /* sub */ bool eol; /* sub */ + bool oneshot; /* sub */ #endif }; diff --git a/client/sub_client.c b/client/sub_client.c index afdfe43b..58855eb9 100644 --- a/client/sub_client.c +++ b/client/sub_client.c @@ -30,12 +30,16 @@ Contributors: #include #include "client_shared.h" +bool process_messages = true; + void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) { struct mosq_config *cfg; int i; bool res; + if(process_messages == false) return; + assert(obj); cfg = (struct mosq_config *)obj; @@ -69,6 +73,10 @@ void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquit fflush(stdout); } } + if(cfg->oneshot){ + process_messages = false; + mosquitto_disconnect(mosq); + } } void my_connect_callback(struct mosquitto *mosq, void *obj, int result) @@ -118,7 +126,7 @@ void print_usage(void) printf("mosquitto_sub is a simple mqtt client that will subscribe to a single topic and print all messages it receives.\n"); printf("mosquitto_sub version %s running on libmosquitto %d.%d.%d.\n\n", VERSION, major, minor, revision); printf("Usage: mosquitto_sub [-c] [-h host] [-k keepalive] [-p port] [-q qos] [-R] -t topic ...\n"); - printf(" [-T filter_out]\n"); + printf(" [-1] [-T filter_out]\n"); printf(" [-A bind_address] [-S]\n"); printf(" [-i id] [-I id_prefix]\n"); printf(" [-d] [-N] [--quiet] [-v]\n"); @@ -132,6 +140,7 @@ void print_usage(void) #endif #endif printf(" mosquitto_sub --help\n\n"); + printf(" -1 : disconnect and exit after receiving the first message.\n"); printf(" -A : bind the outgoing socket to this host/ip address. Use to control which interface\n"); printf(" the client communicates over.\n"); printf(" -c : disable 'clean session' (store subscription and pending messages when client disconnects).\n"); @@ -229,6 +238,9 @@ int main(int argc, char *argv[]) mosquitto_destroy(mosq); mosquitto_lib_cleanup(); + if(cfg.oneshot && rc == MOSQ_ERR_NO_CONN){ + rc = 0; + } if(rc){ fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc)); } diff --git a/man/mosquitto_sub.1.xml b/man/mosquitto_sub.1.xml index e729b4c5..7541d1ba 100644 --- a/man/mosquitto_sub.1.xml +++ b/man/mosquitto_sub.1.xml @@ -17,6 +17,7 @@ mosquitto_sub + bind_address @@ -90,6 +91,19 @@ currently some options cannot be negated, e.g. . + + + + Disconnect and exit the program immediately after the + first message is received. This may be useful in shell + scripts where on a single status value is required, for + example. + Combine with to print only the + first fresh message (i.e. that does not have the + retained flag set), or with to + filter which topics are processed. + +