author | František Kučera <franta-hg@frantovo.cz> |
Wed, 22 Feb 2023 20:45:40 +0100 | |
branch | v_0 |
changeset 9 | 7722d28539d5 |
parent 6 | 65abb0376a0d |
permissions | -rw-r--r-- |
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: |
9
7722d28539d5
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
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
|
31 |
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
|
32 |
std::string queueName; |
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
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
|
34 |
bool unlinkOnClose = false; |
1
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
|
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
|
36 |
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
|
37 |
} |
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 |
public: |
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
|
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
virtual ~PosixMQ() { |
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
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
|
43 |
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
|
44 |
} |
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
|
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
|
46 |
static PosixMQ* open(std::string queueName, bool unlinkOnClose = false) { |
9
7722d28539d5
default maxmsg + msgsize
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
47 |
struct mq_attr attr = (struct mq_attr){0}; |
7722d28539d5
default maxmsg + msgsize
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
48 |
attr.mq_maxmsg = MSG_COUNT; |
7722d28539d5
default maxmsg + msgsize
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
49 |
attr.mq_msgsize = MSG_SIZE; |
7722d28539d5
default maxmsg + msgsize
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
50 |
|
7722d28539d5
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_RDONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, &attr); |
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
|
52 |
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
|
53 |
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
|
54 |
} |
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
|
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
|
56 |
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
|
57 |
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
|
58 |
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
|
59 |
} |
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
|
60 |
|
1
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
std::string receive() { |
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
char buffer[MSG_SIZE + 1]; |
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
memset(buffer, 0, MSG_SIZE + 1); |
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
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
|
65 |
|
6
65abb0376a0d
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."); |
65abb0376a0d
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
291bdd97fcff
early version, support text messages only
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)); |
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
} |
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
|
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
}; |
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
|
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
} |
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
} |
291bdd97fcff
early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
75 |
} |