Skip to content

Commit

Permalink
Add examples for subscribe_simple.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Jan 26, 2016
1 parent 896b456 commit d157e8c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
6 changes: 3 additions & 3 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ Client library:
- Outgoing messages with QoS>1 are no longer retried after a timeout period.
Messages will be retried when a client reconnects.
- DNS-SRV support is now disabled by default.
- Add mosquitto_subscribe_single() and mosquitto_subscribe_multiple(). These
are two helper functions to make retrieving messages from a broker very
straightforward.
- Add mosquitto_subscribe_simple() This is a helper function to make
retrieving messages from a broker very straightforward. Examples of its user
are in examples/subscribe_simple.

Client:
- Add -x to mosquitto_sub for printing the payload in hexadecimal format.
Expand Down
23 changes: 23 additions & 0 deletions examples/subscribe_simple/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
include ../../config.mk

.PHONY: all

all : sub_single sub_multiple

sub_single : single.o
${CROSS_COMPILE}${CC} $^ -o $@ ../../lib/libmosquitto.so.${SOVERSION}

sub_multiple : multiple.o
${CROSS_COMPILE}${CC} $^ -o $@ ../../lib/libmosquitto.so.${SOVERSION}

single.o : single.c ../../lib/libmosquitto.so.${SOVERSION}
${CROSS_COMPILE}${CC} -c $< -o $@ -I../../lib ${CFLAGS}

multiple.o : multiple.c ../../lib/libmosquitto.so.${SOVERSION}
${CROSS_COMPILE}${CC} -c $< -o $@ -I../../lib ${CFLAGS}

../../lib/libmosquitto.so.${SOVERSION} :
$(MAKE) -C ../../lib

clean :
-rm -f *.o sub_single sub_multiple
39 changes: 39 additions & 0 deletions examples/subscribe_simple/multiple.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdlib.h>
#include <stdio.h>
#include "mosquitto.h"

#define COUNT 3

int main(int argc, char *argv[])
{
int rc;
int i;
struct mosquitto_message *msg;

mosquitto_lib_init();

rc = mosquitto_subscribe_simple(
&msg, COUNT,
"irc/#", 0, true,
"test.mosquitto.org", 1883,
NULL, 60, true,
NULL, NULL,
NULL, NULL);

if(rc){
printf("Error: %s\n", mosquitto_strerror(rc));
mosquitto_lib_cleanup();
return rc;
}

for(i=0; i<COUNT; i++){
printf("%s %s\n", msg[i].topic, (char *)msg[i].payload);
mosquitto_message_free_contents(&msg[i]);
}
free(msg);

mosquitto_lib_cleanup();

return 0;
}

33 changes: 33 additions & 0 deletions examples/subscribe_simple/single.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdlib.h>
#include <stdio.h>
#include "mosquitto.h"

int main(int argc, char *argv[])
{
int rc;
struct mosquitto_message *msg;

mosquitto_lib_init();

rc = mosquitto_subscribe_simple(
&msg, 1,
"irc/#", 0, true,
"test.mosquitto.org", 1883,
NULL, 60, true,
NULL, NULL,
NULL, NULL);

if(rc){
printf("Error: %s\n", mosquitto_strerror(rc));
mosquitto_lib_cleanup();
return rc;
}

printf("%s %s\n", msg->topic, (char *)msg->payload);
mosquitto_message_free(&msg);

mosquitto_lib_cleanup();

return 0;
}

0 comments on commit d157e8c

Please sign in to comment.