03-publish-b2c-qos2.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <mosquittopp.h>
  5. static int run = -1;
  6. class mosquittopp_test : public mosqpp::mosquittopp
  7. {
  8. public:
  9. mosquittopp_test(const char *id);
  10. void on_connect(int rc);
  11. void on_message(const struct mosquitto_message *msg);
  12. };
  13. mosquittopp_test::mosquittopp_test(const char *id) : mosqpp::mosquittopp(id)
  14. {
  15. }
  16. void mosquittopp_test::on_connect(int rc)
  17. {
  18. if(rc){
  19. exit(1);
  20. }
  21. }
  22. void mosquittopp_test::on_message(const struct mosquitto_message *msg)
  23. {
  24. if(msg->mid != 13423){
  25. printf("Invalid mid (%d)\n", msg->mid);
  26. exit(1);
  27. }
  28. if(msg->qos != 2){
  29. printf("Invalid qos (%d)\n", msg->qos);
  30. exit(1);
  31. }
  32. if(strcmp(msg->topic, "pub/qos2/receive")){
  33. printf("Invalid topic (%s)\n", msg->topic);
  34. exit(1);
  35. }
  36. if(strcmp((char *)msg->payload, "message")){
  37. printf("Invalid payload (%s)\n", (char *)msg->payload);
  38. exit(1);
  39. }
  40. if(msg->payloadlen != 7){
  41. printf("Invalid payloadlen (%d)\n", msg->payloadlen);
  42. exit(1);
  43. }
  44. if(msg->retain != false){
  45. printf("Invalid retain (%d)\n", msg->retain);
  46. exit(1);
  47. }
  48. run = 0;
  49. }
  50. int main(int argc, char *argv[])
  51. {
  52. struct mosquittopp_test *mosq;
  53. int port = atoi(argv[1]);
  54. mosqpp::lib_init();
  55. mosq = new mosquittopp_test("publish-qos2-test");
  56. mosq->message_retry_set(3);
  57. mosq->connect("localhost", port, 60);
  58. while(run == -1){
  59. mosq->loop();
  60. }
  61. delete mosq;
  62. mosqpp::lib_cleanup();
  63. return run;
  64. }