Add --random-filter to mosquitto_sub.

This commit is contained in:
Roger A. Light 2020-03-03 14:12:30 +00:00
parent d96543c0b8
commit 0da723c1ec
7 changed files with 56 additions and 0 deletions

View File

@ -42,6 +42,8 @@ Clients:
option.
- Add `-x` to all clients to all the session-expiry-interval property to be
easily set for MQTT v5 clients.
- Add `--random-filter` to mosquitto_sub, to allow only a certain proportion
of received messages to be printed.
1.6.9 - 20200227

View File

@ -149,6 +149,7 @@ void init_config(struct mosq_config *cfg, int pub_or_sub)
cfg->repeat_count = 1;
cfg->repeat_delay.tv_sec = 0;
cfg->repeat_delay.tv_usec = 0;
cfg->random_filter = 10000;
if(pub_or_sub == CLIENT_RR){
cfg->protocol_version = MQTT_PROTOCOL_V5;
cfg->msg_count = 1;
@ -853,6 +854,21 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c
}
cfg->no_retain = true;
cfg->sub_opts |= MQTT_SUB_OPT_SEND_RETAIN_NEVER;
}else if(!strcmp(argv[i], "--random-filter")){
if(pub_or_sub != CLIENT_SUB){
goto unknown_option;
}
if(i==argc-1){
fprintf(stderr, "Error: --random-filter argument given but no chance specified.\n\n");
return 1;
}else{
cfg->random_filter = 10.0*atof(argv[i+1]);
if(cfg->random_filter > 10000 || cfg->random_filter < 1){
fprintf(stderr, "Error: --random-filter chance must be between 0.1-100.0\n\n");
return 1;
}
}
i++;
}else if(!strcmp(argv[i], "--remove-retained")){
if(pub_or_sub != CLIENT_SUB){
goto unknown_option;

View File

@ -107,6 +107,7 @@ struct mosq_config {
int timeout; /* sub */
int sub_opts; /* sub */
long session_expiry_interval;
int random_filter; /* sub */
#ifdef WITH_SOCKS
char *socks5_host;
int socks5_port;

View File

@ -256,6 +256,9 @@ void print_usage(void)
printf(" --pretty : print formatted output rather than minimised output when using the\n");
printf(" JSON output format option.\n");
printf(" --quiet : don't print error messages.\n");
printf(" --random-filter : only print a percentage of received messages. Set to 100 to have all\n");
printf(" messages printed, 50.0 to have half of the messages received on average\n");
printf(" printed, and so on.\n");
printf(" --retained-only : only handle messages with the retained flag set, and exit when the\n");
printf(" first non-retained message is received.\n");
printf(" --remove-retained : send a message to the server to clear any received retained messages\n");
@ -307,6 +310,8 @@ int main(int argc, char *argv[])
mosquitto_lib_init();
rand_init();
rc = client_config_load(&cfg, CLIENT_SUB, argc, argv);
if(rc){
if(rc == 2){

View File

@ -582,8 +582,27 @@ static void formatted_print(const struct mosq_config *lcfg, const struct mosquit
}
void rand_init(void)
{
struct tm *ti = NULL;
long ns;
if(!get_time(&ti, &ns)){
srandom(ns);
}
}
void print_message(struct mosq_config *cfg, const struct mosquitto_message *message, const mosquitto_property *properties)
{
long r;
if(cfg->random_filter < 10000){
r = random();
if((r%10000) >= cfg->random_filter){
return;
}
}
if(cfg->format){
formatted_print(cfg, message, properties);
}else if(cfg->verbose){

View File

@ -20,6 +20,7 @@ Contributors:
#include "mosquitto.h"
#include "client_shared.h"
void rand_init(void);
void print_message(struct mosq_config *cfg, const struct mosquitto_message *message, const mosquitto_property *properties);
#endif

View File

@ -44,6 +44,7 @@
<arg><option>--nodelay</option></arg>
<arg><option>--pretty</option></arg>
<arg><option>-q</option> <replaceable>message-QoS</replaceable></arg>
<arg><option>--random-filter</option> <replaceable>chance</replaceable></arg>
<arg><option>--remove-retained</option></arg>
<group choice='opt'>
<arg choice='plain'><option>-R</option></arg>
@ -508,6 +509,17 @@
their display.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--random-filter</option></term>
<listitem>
<para>This option can be used to reduce the proportion of
messages that mosquitto_sub prints. The default behaviour
is to print all incoming messages. Setting the
<replaceable>chance</replaceable> to a floating point value
between 0.1 and 100.0 will ensure that on average that
percentage of messages will be printed.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--remove-retained</option></term>
<listitem>