diff --git a/ChangeLog.txt b/ChangeLog.txt
index 96ed5689..103eb35f 100644
--- a/ChangeLog.txt
+++ b/ChangeLog.txt
@@ -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
diff --git a/client/client_shared.c b/client/client_shared.c
index 79e08a22..8f78bfb7 100644
--- a/client/client_shared.c
+++ b/client/client_shared.c
@@ -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;
diff --git a/client/client_shared.h b/client/client_shared.h
index 1a00572c..1712ea60 100644
--- a/client/client_shared.h
+++ b/client/client_shared.h
@@ -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;
diff --git a/client/sub_client.c b/client/sub_client.c
index 3a4b87a6..513bfb4b 100644
--- a/client/sub_client.c
+++ b/client/sub_client.c
@@ -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){
diff --git a/client/sub_client_output.c b/client/sub_client_output.c
index 2332e5de..e764ef21 100644
--- a/client/sub_client_output.c
+++ b/client/sub_client_output.c
@@ -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){
diff --git a/client/sub_client_output.h b/client/sub_client_output.h
index cd8c75fe..358cada9 100644
--- a/client/sub_client_output.h
+++ b/client/sub_client_output.h
@@ -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
diff --git a/man/mosquitto_sub.1.xml b/man/mosquitto_sub.1.xml
index a899a841..841851cc 100644
--- a/man/mosquitto_sub.1.xml
+++ b/man/mosquitto_sub.1.xml
@@ -44,6 +44,7 @@
message-QoS
+ chance
@@ -508,6 +509,17 @@
their display.
+
+
+
+ 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
+ chance to a floating point value
+ between 0.1 and 100.0 will ensure that on average that
+ percentage of messages will be printed.
+
+