-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqrmosq.cpp
More file actions
68 lines (55 loc) · 1.16 KB
/
qrmosq.cpp
File metadata and controls
68 lines (55 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/** @file qrmosq.cpp
* description of qrmosq.h
* and send msg to subscriber
*/
#include <iostream>
#include <cstring>
#include <stdexcept>
#include "qrmosq.h"
#include <mosquittopp.h>
using namespace std;
qrMqtt::qrMqtt(const char *_id, const char *_topic, const char *_host, int _port) : mosquittopp(_id)
{
this->keepalive = 60;
this->id = _id;
this->port = _port;
this->host = _host;
this->topic = _topic;
/**
* error handling
* whether broker is running ?
*/
if (connect(host, port, keepalive) == MOSQ_ERR_ERRNO)
{
throw runtime_error("##ERROR##");
}
loop_start();
};
qrMqtt::~qrMqtt()
{
loop_stop();
}
void qrMqtt::on_connect(int rc)
{
if (rc == 0)
{
cout << " ##-Connected with Broker-## " << std::endl;
}
else
{
cout << "##-Unable to Connect Broker-## " << std::endl;
}
}
bool qrMqtt::send_msg(const char *message)
{
int ret = publish(NULL, this->topic, strlen(message), message, 2, false);
return (ret == MOSQ_ERR_SUCCESS);
}
void qrMqtt::on_disconnect()
{
cout << " ##-Disconnected from Broker-## " << std::endl;
}
void qrMqtt::on_publish(int mid)
{
cout << "## - Message published successfully" << endl;
}