03-publish-b2c-qos1.cpp 1.4 KB

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