src/PosixMQ.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 04 Mar 2022 01:40:50 +0100
branchv_0
changeset 2 fc9911b1d295
parent 1 67898f122f53
child 3 be6f2e307a65
permissions -rw-r--r--
send messages to PosixMQ: currently each attribute in one message
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:
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
	size_t MSG_SIZE = 8192; // TODO: configurable/dynamic
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
	std::string queueName;
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	mqd_t handle = -2;
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	PosixMQ(std::string queueName, mqd_t handle) : queueName(queueName), handle(handle) {
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	}
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
public:
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
	virtual ~PosixMQ() {
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		if (handle >= 0) mq_close(handle);
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	}
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	static PosixMQ* open(std::string queueName) {
2
fc9911b1d295 send messages to PosixMQ: currently each attribute in one message
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    44
		mqd_t handle = mq_open(queueName.c_str(), O_RDWR | O_CREAT);
1
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		if (handle >= 0) return new PosixMQ(queueName, handle);
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
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));
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	}
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
2
fc9911b1d295 send messages to PosixMQ: currently each attribute in one message
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    49
	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
    50
		int result = mq_send(handle, message.c_str(), message.size(), 0);
fc9911b1d295 send messages to PosixMQ: currently each attribute in one message
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    51
		if (result) throw std::logic_error("mq_send() = " + std::to_string(result) + " error: " + strerror(errno));
fc9911b1d295 send messages to PosixMQ: currently each attribute in one message
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    52
	}
fc9911b1d295 send messages to PosixMQ: currently each attribute in one message
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    53
1
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	std::string receive() {
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		char buffer[MSG_SIZE + 1];
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		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
    57
		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
    58
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		if (msgSize >= 0) return std::string(buffer);
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		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
    61
	}
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	void unlink() {
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		mq_unlink(queueName.c_str());
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	}
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
};
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
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
}