Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature request] v3.1 request response client #2503

Closed
jsaak opened this issue Apr 4, 2022 · 4 comments
Closed

[feature request] v3.1 request response client #2503

jsaak opened this issue Apr 4, 2022 · 4 comments

Comments

@jsaak
Copy link

jsaak commented Apr 4, 2022

mosquitto_rr only works with MQTT version 5 and up
Due to bugs we still use mosquitto version 1.5.8, which does not support v5.

So i implemented a publish feature to mosquitto_sub client:
./mosquitto_sub -t sub_topic -p 1884 --publish-topic pub_topic --publish-message pub_message --publish-qos 1 -C 1

Known limitation, is that you can only subscribe to one topic.

This is a diff for 1.5.8, but should be pretty straight-forward to copy it to current development, I guess.

Anyway here it is, if someone needs it:

diff --git a/client/client_shared.c b/client/client_shared.c
index 997b87b..6dc1c9c 100644
--- a/client/client_shared.c
+++ b/client/client_shared.c
@@ -740,6 +740,31 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c
                                        return 1;
                                i++;
                        }
+               }else if(!strcmp(argv[i], "--publish-topic")){
+                       if(pub_or_sub == CLIENT_PUB){
+                               goto unknown_option;
+                       }
+      cfg->topic = strdup(argv[i+1]);
+      cfg->publish_and_subscribe = true;
+      cfg->publish_and_subscribe_qos = 0;
+                       i++;
+               }else if(!strcmp(argv[i], "--publish-message")){
+                       if(pub_or_sub == CLIENT_PUB){
+                               goto unknown_option;
+                       }
+      cfg->message = strdup(argv[i+1]);
+      cfg->msglen = strlen(cfg->message);
+                       i++;
+               }else if(!strcmp(argv[i], "--publish-qos")){
+                       if(pub_or_sub == CLIENT_PUB){
+                               goto unknown_option;
+                       }
+      cfg->publish_and_subscribe_qos = atoi(argv[i+1]);
+      if(cfg->publish_and_subscribe_qos < 0 || cfg->publish_and_subscribe_qos > 2){
+        fprintf(stderr, "Error: Invalid publish_and_subscribe QoS %d.\n\n", cfg->publish_and_subscribe_qos);
+        return 1;
+                       }
+                       i++;
                }else if(!strcmp(argv[i], "-T") || !strcmp(argv[i], "--filter-out")){
                        if(pub_or_sub == CLIENT_PUB){
                                goto unknown_option;
diff --git a/client/client_shared.h b/client/client_shared.h
index 5dd9fad..3707785 100644
--- a/client/client_shared.h
+++ b/client/client_shared.h
@@ -91,6 +91,8 @@ struct mosq_config {
        char *socks5_username;
        char *socks5_password;
 #endif
+  bool publish_and_subscribe;
+       int publish_and_subscribe_qos;
 };
 
 int client_config_load(struct mosq_config *config, int pub_or_sub, int argc, char *argv[]);
diff --git a/client/sub_client.c b/client/sub_client.c
index 9ff2867..215ffe6 100644
--- a/client/sub_client.c
+++ b/client/sub_client.c
@@ -118,11 +118,18 @@ void my_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_c
        assert(obj);
        cfg = (struct mosq_config *)obj;
 
-       if(!cfg->quiet) printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
-       for(i=1; i<qos_count; i++){
-               if(!cfg->quiet) printf(", %d", granted_qos[i]);
-       }
-       if(!cfg->quiet) printf("\n");
+  if(cfg->debug) {
+    if(!cfg->quiet) printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
+    for(i=1; i<qos_count; i++){
+      if(!cfg->quiet) printf(", %d", granted_qos[i]);
+    }
+    if(!cfg->quiet) printf("\n");
+  }
+
+  int mid_sent;
+  if(cfg->publish_and_subscribe) {
+    mosquitto_publish(mosq, &mid_sent, cfg->topic, cfg->msglen, cfg->message, cfg->publish_and_subscribe_qos, 0);
+  }
 }
 
 void my_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str)
@@ -282,8 +289,11 @@ int main(int argc, char *argv[])
        }
        if(cfg.debug){
                mosquitto_log_callback_set(mosq, my_log_callback);
+       }
+  if(cfg.debug || cfg.publish_and_subscribe){
                mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);
        }
+
        mosquitto_connect_with_flags_callback_set(mosq, my_connect_callback);
        mosquitto_message_callback_set(mosq, my_message_callback);
@ralight
Copy link
Contributor

ralight commented May 23, 2022

mosquitto_rr works fine with v3.1 and v3.1.1: mosquitto_rr -V 31 -t request-topic -e response-topic -m hello.

We should have a chat about what bugs are stopping you moving from 1.5.8.

@jsaak
Copy link
Author

jsaak commented May 24, 2022

Good to know, thanks. Somehow I was never able to use it like this, but it does really work.

My showstopper bug is this currently:
#2467

@ralight
Copy link
Contributor

ralight commented May 24, 2022

Thank you, I'll take a look.

@ralight
Copy link
Contributor

ralight commented May 25, 2022

I'm going to close this then, as mosquitto_rr does what you want already.

@ralight ralight closed this as completed May 25, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants