link to mosquittopp library v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 06 May 2022 23:06:44 +0200
branchv_0
changeset 1 cb9577442d3b
parent 0 7ef5ce9477c8
child 2 1a0fbd17db13
link to mosquittopp library
src/CMakeLists.txt
src/MQTT.h
src/MQTTHandler.h
--- a/src/CMakeLists.txt	Fri May 06 21:54:32 2022 +0200
+++ b/src/CMakeLists.txt	Fri May 06 23:06:44 2022 +0200
@@ -34,7 +34,7 @@
 )
 
 # Link libraries:
-target_link_libraries(${EXECUTABLE_FILE} ${RELPIPE_LIBS_LIBRARIES} rt)
+target_link_libraries(${EXECUTABLE_FILE} ${RELPIPE_LIBS_LIBRARIES} mosquittopp)
 set_property(TARGET ${EXECUTABLE_FILE} PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE)
 
 install(TARGETS ${EXECUTABLE_FILE} DESTINATION bin)
--- a/src/MQTT.h	Fri May 06 21:54:32 2022 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,70 +0,0 @@
-/**
- * Relational pipes
- * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, version 3 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-#pragma once
-
-#include <mqueue.h>
-#include <string>
-#include <stdexcept>
-#include <cstring>
-
-namespace relpipe {
-namespace out {
-namespace mqtt {
-
-class MQTT {
-private:
-	const static size_t MSG_SIZE = 8192; // TODO: configurable/dynamic
-	std::string queueName;
-	mqd_t handle = -2;
-	bool unlinkOnClose = false;
-
-	MQTT(std::string queueName, mqd_t handle, bool unlinkOnClose) : queueName(queueName), handle(handle), unlinkOnClose(unlinkOnClose) {
-	}
-
-public:
-
-	virtual ~MQTT() {
-		if (handle >= 0) mq_close(handle);
-		if (unlinkOnClose) mq_unlink(queueName.c_str());
-	}
-
-	static MQTT* open(std::string queueName, bool unlinkOnClose = false) {
-		mqd_t handle = mq_open(queueName.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
-		if (handle >= 0) return new MQTT(queueName, handle, unlinkOnClose);
-		else throw std::logic_error("Unable to open MQTT: " + queueName + " error: " + strerror(errno));
-	}
-
-	void send(std::string message) {
-		int result = mq_send(handle, message.c_str(), message.size(), 0);
-		if (result) throw std::logic_error("Unable to send message to" + queueName + " error: " + strerror(errno));
-	}
-
-	std::string receive() {
-		char buffer[MSG_SIZE + 1];
-		memset(buffer, 0, MSG_SIZE + 1);
-		ssize_t msgSize = mq_receive(handle, buffer, MSG_SIZE, nullptr);
-
-		if (msgSize > sizeof (buffer))throw std::logic_error("Invalid MQTT message size.");
-		else if (msgSize >= 0) return std::string(buffer, msgSize);
-		else throw std::logic_error("Unable to receive MQTT message from " + queueName + " error: " + strerror(errno));
-	}
-
-};
-
-}
-}
-}
--- a/src/MQTTHandler.h	Fri May 06 21:54:32 2022 +0200
+++ b/src/MQTTHandler.h	Fri May 06 23:06:44 2022 +0200
@@ -24,12 +24,13 @@
 #include <locale>
 #include <codecvt>
 
+#include <mosquittopp.h>
+
 #include <relpipe/common/type/typedefs.h>
 #include <relpipe/reader/TypeId.h>
 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
 #include <relpipe/reader/handlers/AttributeMetadata.h>
 
-#include "MQTT.h"
 #include "Configuration.h"
 #include "Hex.h"
 
@@ -41,7 +42,6 @@
 private:
 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
 	Configuration configuration;
-	shared_ptr<MQTT> mq;
 
 	struct CurrentRelation {
 		relpipe::common::type::StringX name;
@@ -53,8 +53,13 @@
 public:
 
 	MQTTHandler(Configuration configuration) : configuration(configuration) {
-		// TODO: do not throw exception from the constructor: MQTT::open()
-		mq.reset(MQTT::open(convertor.to_bytes(configuration.queue), configuration.unlinkOnClose));
+
+		{
+			// TODO: remove
+			int major, minor, patch;
+			mosqpp::lib_version(&major, &minor, &patch);
+			std::cerr << "mosquitto version: " << major << "." << minor << "." << patch << std::endl;
+		}
 	}
 
 	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
@@ -72,7 +77,7 @@
 		currentRelation.attributeIndex++;
 		if (currentRelation.attributeIndex == currentRelation.attributes.size()) {
 			currentRelation.attributeIndex = 0;
-			mq->send(currentRelation.currentValue);
+			// FIXME: send the message
 		}
 
 	}