如何在c ++中设置Mosquitto的配置属性?

时间:2016-08-02 09:36:36

标签: c++ mosquitto

我正在编写mosquitto代码,用于在订阅特定主题后使用该消息。现在我想为mosquitto设置不同的配置

autosave_interval 100
persistence true
persistence_location /var/lib/mosquitto/
persistence_file mosquitto.db

但我不知道如何在c ++中设置此配置。我试图谷歌它但没有找到任何结果。 Plz,帮忙。下面是蚊子的c ++代码

myMosq.h

/*
 * myMosq.h
 *
 *  Created on: Jul 28, 2016
 *      Author: nilav
 */
#include <iostream>
#ifndef MYMOSQ_H_
#define MYMOSQ_H_

#include <mosquittopp.h>
#include <mosquitto.h>
using namespace std;

class myMosq : public mosqpp::mosquittopp
{
private:
 const char     *     host;
 const char    *     id;
 const char    *     topic;
 int                port;
 int                keepalive;

 void on_connect(int rc);
 void on_message(const struct mosquitto_message *message);
 void on_disconnect(int rc);
 void on_subscribe(int mid, int qos_count, const int *granted_qos);
 void on_publish(int mid);
 void on_unsubscribe(int mid);

public:
 myMosq(const char *id, const char * _topic, const char *host, int port);
 ~myMosq();
 bool send_message(string responseMessage);
 bool receive_message();
 void writeToDatabase(string query);
};

#endif

myMosq.cpp

#include <cstdio>
#include <cstring>
#include <iostream>

#include "myMosq.h"
#include <mosquittopp.h>
#include "Configuration.h"
#include "Databases.h"

using namespace std;

Configuration configuration;

myMosq::myMosq(const char * _id,const char * _topic, const char * _host, int _port) : mosquittopp(_id)
 {
 mosqpp::lib_init();        // Mandatory initialization for mosquitto library
 this->keepalive = 60;    // Basic configuration setup for myMosq class
 this->id = _id;
 this->port = _port;
 this->host = _host;
 this->topic = _topic;
 connect_async(host,     // non blocking connection to broker request
 port,
 keepalive);
 loop_start();            // Start thread managing connection / publish / subscribe
 };

myMosq::~myMosq() {
 loop_stop();            // Kill the thread
 mosqpp::lib_cleanup();    // Mosquitto library cleanup
 }


bool myMosq::receive_message()
 {
    int set = subscribe(NULL, configuration.subscriptionTopic.c_str(),2);
    return set;
 }

bool myMosq::send_message(string responseMessage) {
    int ret = publish(NULL,configuration.producerTopic.c_str(),strlen(responseMessage.c_str()),responseMessage.c_str(),1,false);
    return (ret = MOSQ_ERR_SUCCESS);
}

void myMosq::on_disconnect(int rc) {
 std::cout << ">> myMosq - disconnection(" << rc << ")" << std::endl;
 }


void myMosq::on_connect(int rc)
 {
 if ( rc == 0 ) {
 std::cout << ">> myMosq - connected with server" << std::endl;
 } else {
 std::cout << ">> myMosq - Impossible to connect with server(" << rc << ")" << std::endl;
 }
 }

void myMosq::on_message(const struct mosquitto_message *message) {

    char * pchar = (char*)(message->payload);
    string str(pchar);
    writeToDatabase(str);
}

void myMosq::on_subscribe(int mid, int qos_count, const int *granted_qos)
{
    std::cout << ">> subscription succeeded (" << mid << ") " << std::endl;
}

void myMosq::on_publish(int mid) {
    std::cout << ">> myMosq - Message (" << mid << ") succeed to be published " << std::endl;
}

void myMosq::writeToDatabase(string query) {

    Databases* database = new Databases(configuration.db,
            configuration.dbPort, configuration.username, configuration.password,
            configuration.schema);
    database->writeDatabase(query);

    if(database->responseMessage == "") {
        database->responseMessage = "SUCCESS";
    }

    this->send_message(database->responseMessage);
}

void myMosq::on_unsubscribe(int mid) {
    cout<<"unscubscribed";
};

1 个答案:

答案 0 :(得分:0)

您看到的选项是mosquitto broker,它几乎就像服务器一样。

Mosquitto C ++库是一个客户端库,这些选项(例如autosave_interval)对客户端无效。关于持久性,mosquitto C / C ++客户端库目前还没有提供文件持久性。