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