src/PosixMQ.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 04 Mar 2022 01:11:27 +0100
branchv_0
changeset 1 67898f122f53
child 2 fc9911b1d295
permissions -rw-r--r--
import PosixMQ.h from relpipe-in-posixmq
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 {
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
namespace in {
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) {
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		mqd_t handle = mq_open(queueName.c_str(), O_RDONLY | O_CREAT);
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
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	std::string receive() {
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		char buffer[MSG_SIZE + 1];
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		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
    52
		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
    53
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		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
    55
		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
    56
	}
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	void unlink() {
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		mq_unlink(queueName.c_str());
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	}
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
67898f122f53 import PosixMQ.h from relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
}
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
}