Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save evgeny-boger/d2b2e3b3c0aec9d8e614 to your computer and use it in GitHub Desktop.
Save evgeny-boger/d2b2e3b3c0aec9d8e614 to your computer and use it in GitHub Desktop.
--- /home/boger/Projects/wheezy-armel/mosquitto-1.2.1/client/sub_client.c 2013-09-19 00:28:27.000000000 +0400
+++ sub_client.c 2014-06-25 02:06:39.189963840 +0400
@@ -54,6 +54,7 @@
int verbose;
bool quiet;
bool no_retain;
+ int remaining_messages;
};
void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
@@ -78,6 +79,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)
@@ -136,6 +144,7 @@
printf(" [{--cafile file | --capath dir} [--cert file] [--key file] [--insecure]]\n");
#ifdef WITH_TLS_PSK
printf(" [--psk hex-key --psk-identity identity]\n");
+ printf(" [--exit-after message_count]\n");
#endif
#endif
printf(" mosquitto_sub --help\n\n");
@@ -181,6 +190,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");
}
@@ -201,7 +212,7 @@
char err[1024];
struct userdata ud;
int len;
-
+
char *will_payload = NULL;
long will_payloadlen = 0;
int will_qos = 0;
@@ -220,6 +231,8 @@
memset(&ud, 0, sizeof(struct userdata));
+ ud.remaining_messages = -1;
+
for(i=1; i<argc; i++){
if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--port")){
if(i==argc-1){
@@ -450,6 +463,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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment