Skip to content

Instantly share code, notes, and snippets.

@evgeny-boger
Created June 24, 2014 21:58
Show Gist options
  • Save evgeny-boger/7418a30edbcde01835c2 to your computer and use it in GitHub Desktop.
Save evgeny-boger/7418a30edbcde01835c2 to your computer and use it in GitHub Desktop.
diff -r 4deedcb49ff5 client/sub_client.c
--- a/client/sub_client.c Tue Mar 25 10:49:28 2014 +0000
+++ b/client/sub_client.c Wed Jun 25 01:58:24 2014 +0400
@@ -57,6 +57,7 @@
bool quiet;
bool no_retain;
bool eol;
+ int remaining_messages;
};
void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
@@ -98,6 +99,13 @@
fflush(stdout);
}
}
+
+ if (ud->remaining_messages > 1) {
+ ud->remaining_messages -= 1;
+ } else if (ud->remaining_messages == 1) {
+ exit(0);
+ }
+
}
void my_connect_callback(struct mosquitto *mosq, void *obj, int result)
@@ -158,6 +166,7 @@
printf(" [--ciphers ciphers] [--insecure]]\n");
#ifdef WITH_TLS_PSK
printf(" [--psk hex-key --psk-identity identity [--ciphers ciphers]]\n");
+ printf(" [--exit-after message_count]\n");
#endif
#endif
printf(" mosquitto_sub --help\n\n");
@@ -206,6 +215,8 @@
printf(" --psk-identity : client identity string for TLS-PSK mode.\n");
#endif
#endif
+ printf(" --exit-after : exit after receiving the specified number of messages\n");
+
printf("\nSee http://mosquitto.org/ for more information.\n\n");
}
@@ -226,7 +237,7 @@
char err[1024];
struct userdata ud;
int len;
-
+
char *will_payload = NULL;
long will_payloadlen = 0;
int will_qos = 0;
@@ -249,6 +260,7 @@
memset(&ud, 0, sizeof(struct userdata));
ud.eol = true;
+ ud.remaining_messages = -1;
for(i=1; i<argc; i++){
if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--port")){
@@ -504,6 +516,19 @@
will_topic = argv[i+1];
}
i++;
+ }else if(!strcmp(argv[i], "--exit-after")){
+ if(i==argc-1){
+ fprintf(stderr, "Error: --exit-after argument given but no maximum number of messages specified.\n\n");
+ print_usage();
+ return 1;
+ }else{
+ ud.remaining_messages = atoi(argv[i+1]);
+ if(ud.remaining_messages <= 0){
+ fprintf(stderr, "Error: Invalid maximum number of messages %d.\n\n", ud.remaining_messages);
+ return 1;
+ }
+ }
+ i++;
}else{
fprintf(stderr, "Error: Unknown option '%s'.\n",argv[i]);
print_usage();
@evgeny-boger
Copy link
Author

version 1.3.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment