-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
79 lines (67 loc) · 1.81 KB
/
Copy pathclient.cpp
File metadata and controls
79 lines (67 loc) · 1.81 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
69
70
71
72
73
74
75
76
77
78
79
#include "client.h"
#include "connect_factory.h"
#include "subscription_factory.h"
namespace Mosquittopp {
Client::Client()
{
}
Client::~Client()
{
}
bool Client::connect(std::string hostname, int port)
{
_con_helper = ConnectFactory::instance().from_identifier(hostname + ":" + std::to_string(port));
return _con_helper->connect(hostname, port);
}
void Client::disconnect()
{
if (connected()) {
_con_helper->disconnect();
}
}
bool Client::connected()
{
if (!_con_helper) {
return false;
}
return _con_helper->status() == ConnectHelper::CONNECTED;
}
LibraryVersion Client::lib_version()
{
int major, minor, revision;
mosquitto_lib_version(&major, &minor, &revision);
return LibraryVersion { major, minor, revision };
}
void Client::set_will(std::string topic, std::string payload, QOS qos, bool retain)
{
if (!connected()) {
return;
}
mosquitto_will_set(_con_helper->_token, topic.data(), static_cast<int>(payload.length()), reinterpret_cast<void*>(const_cast<char*>(payload.data())), qos, retain);
}
void Client::clear_will()
{
if (!connected()) {
return;
}
mosquitto_will_clear(_con_helper->_token);
}
void Client::set_login_info(std::string username, std::string password)
{
if (!connected()) {
return;
}
mosquitto_username_pw_set(_con_helper->_token, username.data(), password.data());
}
std::shared_ptr<Subscription> Client::subscribe(std::string pattern, QOS qos)
{
return SubscriptionFactory::get(_con_helper->_token, pattern, qos);
}
void Client::publish(std::string topic, const void* payload, std::size_t payload_len, QOS qos, bool retain)
{
if (!connected()) {
return;
}
mosquitto_publish(_con_helper->_token, nullptr, topic.c_str(), static_cast<int>(payload_len), payload, qos, retain);
}
}