mirror of
https://github.com/eclipse/mosquitto.git
synced 2025-05-09 01:01:11 +08:00
Add -1 (oneshot) option to mosquitto_sub.
Thanks to JP Mens.
This commit is contained in:
parent
8cb0eab353
commit
a285c6ce5c
@ -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
|
||||
================
|
||||
|
@ -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");
|
||||
|
@ -76,6 +76,7 @@ struct mosq_config {
|
||||
int filter_out_count; /* sub */
|
||||
bool verbose; /* sub */
|
||||
bool eol; /* sub */
|
||||
bool oneshot; /* sub */
|
||||
#endif
|
||||
};
|
||||
|
||||
|
@ -30,12 +30,16 @@ Contributors:
|
||||
#include <mosquitto.h>
|
||||
#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));
|
||||
}
|
||||
|
@ -17,6 +17,7 @@
|
||||
<refsynopsisdiv>
|
||||
<cmdsynopsis>
|
||||
<command>mosquitto_sub</command>
|
||||
<arg><option>-1</option></arg>
|
||||
<arg><option>-A</option> <replaceable>bind_address</replaceable></arg>
|
||||
<arg><option>-c</option></arg>
|
||||
<arg><option>-d</option></arg>
|
||||
@ -90,6 +91,19 @@
|
||||
currently some options cannot be negated, e.g.
|
||||
<option>-S</option>.</para>
|
||||
<variablelist>
|
||||
<varlistentry>
|
||||
<term><option>-1</option></term>
|
||||
<listitem>
|
||||
<para>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.</para>
|
||||
<para>Combine with <option>-R</option> to print only the
|
||||
first fresh message (i.e. that does not have the
|
||||
retained flag set), or with <option>-T</option> to
|
||||
filter which topics are processed.</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
<varlistentry>
|
||||
<term><option>-A</option></term>
|
||||
<listitem>
|
||||
|
Loading…
x
Reference in New Issue
Block a user