[453850] Add -C option to mosquitto_sub.

Allows the client to quit after receiving count messages.

Replaces the (unreleased) -1 option.

Bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=453850
This commit is contained in:
Roger A. Light 2015-01-30 22:23:51 +00:00
parent c201d06032
commit 15e0236d43
5 changed files with 42 additions and 27 deletions

View File

@ -70,7 +70,8 @@ Broker:
Clients:
- Both clients can now load default configuration options from a file.
- Add -1 (oneshot) option to mosquitto_sub.
- Add -C option to mosquitto_sub to allow the client to quit after receiving a
certain count of messages. Closes bug #453850.
- Add --proxy SOCKS5 support for both clients.
- Pub client supports setting its keepalive. Closes bug #454852.
- Add support for config files with default options.

View File

@ -252,12 +252,6 @@ 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");
@ -300,6 +294,22 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c
}
i++;
#endif
}else if(!strcmp(argv[i], "-C")){
if(pub_or_sub == CLIENT_PUB){
goto unknown_option;
}else{
if(i==argc-1){
fprintf(stderr, "Error: -C argument given but no count specified.\n\n");
return 1;
}else{
cfg->msg_count = atoi(argv[i+1]);
if(cfg->msg_count < 1){
fprintf(stderr, "Error: Invalid message count \"%d\".\n\n", cfg->msg_count);
return 1;
}
}
i++;
}
}else if(!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")){
cfg->debug = true;
}else if(!strcmp(argv[i], "-f") || !strcmp(argv[i], "--file")){

View File

@ -79,7 +79,7 @@ struct mosq_config {
int filter_out_count; /* sub */
bool verbose; /* sub */
bool eol; /* sub */
bool oneshot; /* sub */
int msg_count; /* sub */
#ifdef WITH_SOCKS
char *socks5_host;
int socks5_port;

View File

@ -31,6 +31,7 @@ Contributors:
#include "client_shared.h"
bool process_messages = true;
int msg_count = 0;
void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
{
@ -73,9 +74,12 @@ void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquit
fflush(stdout);
}
}
if(cfg->oneshot){
process_messages = false;
mosquitto_disconnect(mosq);
if(cfg->msg_count>0){
msg_count++;
if(cfg->msg_count == msg_count){
process_messages = false;
mosquitto_disconnect(mosq);
}
}
}
@ -126,7 +130,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(" [-1] [-T filter_out]\n");
printf(" [-C msg_count] [-T filter_out]\n");
#ifdef WITH_SRV
printf(" [-A bind_address] [-S]\n");
#else
@ -262,7 +266,7 @@ int main(int argc, char *argv[])
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
if(cfg.oneshot && rc == MOSQ_ERR_NO_CONN){
if(cfg.msg_count>0 && rc == MOSQ_ERR_NO_CONN){
rc = 0;
}
if(rc){

View File

@ -17,9 +17,9 @@
<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>-C</option> <replaceable>msg count</replaceable></arg>
<arg><option>-d</option></arg>
<arg><option>-h</option> <replaceable>hostname</replaceable></arg>
<arg><option>-i</option> <replaceable>client_id</replaceable></arg>
@ -95,19 +95,6 @@
<option>#</option> as the first character are treated as comments
and not processed any further.</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>
@ -170,6 +157,19 @@
for more information.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-C</option></term>
<listitem>
<para>Disconnect and exit the program immediately after
the given count of messages have been 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 set of fresh messages (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>-d</option></term>
<term><option>--debug</option></term>