2016-08-02 24 views
1

Ich schreibe Mosquitto-Code für den Verzehr der Nachricht nach dem Abonnieren eines bestimmten Themas. Jetzt möchte ich andere Konfiguration für mosquitto einstellen wieWie legen Sie die Konfigurationseigenschaften für Mosquitto in C++ fest?

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

Aber ich habe keine Ahnung, wie diese Konfiguration in c einzustellen ++. Ich habe versucht, es zu googeln, konnte aber kein Ergebnis finden. Plz, Hilfe. Im Folgenden finden Sie C++ Code für Moskito

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"; 
}; 

Antwort

0

Die Optionen sind für mosquitto Broker sehen, die fast wirkt wie ein Server.

Die Mosquitto C++ - Bibliothek ist eine Clientbibliothek, und diese Optionen (z. B. autosave_interval) sind für einen Client nicht gültig. Im Hinblick auf die Persistenz bietet die mosquitto C/C++ - Client-Bibliothek derzeit keine Dateipersistenz.