src/PosixMQ.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 04 Mar 2022 19:43:16 +0100
branchv_0
changeset 3 b71fc3b5e56b
parent 1 291bdd97fcff
child 6 65abb0376a0d
permissions -rw-r--r--
new option: --unlink-on-close (delete the MQ file at the end)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <mqueue.h>
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <stdexcept>
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <cstring>
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
namespace relpipe {
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
namespace in {
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
namespace posixmq {
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
class PosixMQ {
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
private:
3
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    30
	const static size_t MSG_SIZE = 8192; // TODO: configurable/dynamic
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
	std::string queueName;
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	mqd_t handle = -2;
3
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    33
	bool unlinkOnClose = false;
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
3
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    35
	PosixMQ(std::string queueName, mqd_t handle, bool unlinkOnClose) : queueName(queueName), handle(handle), unlinkOnClose(unlinkOnClose) {
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	}
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
public:
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	virtual ~PosixMQ() {
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		if (handle >= 0) mq_close(handle);
3
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    42
		if (unlinkOnClose) mq_unlink(queueName.c_str());
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	}
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
3
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    45
	static PosixMQ* open(std::string queueName, bool unlinkOnClose = false) {
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    46
		mqd_t handle = mq_open(queueName.c_str(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    47
		if (handle >= 0) return new PosixMQ(queueName, handle, unlinkOnClose);
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		else throw std::logic_error("Unable to open PosixMQ: " + queueName + " error: " + strerror(errno));
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	}
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
3
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    51
	void send(std::string message) {
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    52
		int result = mq_send(handle, message.c_str(), message.size(), 0);
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    53
		if (result) throw std::logic_error("Unable to send message to" + queueName + " error: " + strerror(errno));
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    54
	}
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    55
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	std::string receive() {
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		char buffer[MSG_SIZE + 1];
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		memset(buffer, 0, MSG_SIZE + 1);
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		ssize_t msgSize = mq_receive(handle, buffer, MSG_SIZE, nullptr);
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		if (msgSize >= 0) return std::string(buffer);
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		else throw std::logic_error("Unable to receive PosixMQ message from " + queueName + " error: " + strerror(errno));
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	}
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
};
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
}
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
}
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
}