src/PosixMQ.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 01 Mar 2022 00:47:49 +0100
branchv_0
changeset 1 291bdd97fcff
child 3 b71fc3b5e56b
permissions -rw-r--r--
early version, support text messages only
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:
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
	size_t MSG_SIZE = 8192; // TODO: configurable/dynamic
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;
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	PosixMQ(std::string queueName, mqd_t handle) : queueName(queueName), handle(handle) {
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	}
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
public:
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	virtual ~PosixMQ() {
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		if (handle >= 0) mq_close(handle);
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	}
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	static PosixMQ* open(std::string queueName) {
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		mqd_t handle = mq_open(queueName.c_str(), O_RDONLY | O_CREAT);
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		if (handle >= 0) return new PosixMQ(queueName, handle);
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		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
    47
	}
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	std::string receive() {
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		char buffer[MSG_SIZE + 1];
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		memset(buffer, 0, MSG_SIZE + 1);
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		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
    53
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		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
    55
		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
    56
	}
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	void unlink() {
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		mq_unlink(queueName.c_str());
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
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
};
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
}