src/PosixMQHandler.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 15 Mar 2022 00:28:13 +0100
branchv_0
changeset 4 8a5b86415d80
parent 3 be6f2e307a65
child 5 5a6828cfad41
permissions -rw-r--r--
send "text" and "data" attributes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
a64022f9684e new project relpipe-out-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
a64022f9684e new project relpipe-out-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
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
a64022f9684e new project relpipe-out-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,
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
a64022f9684e new project relpipe-out-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
a64022f9684e new project relpipe-out-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/>.
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <codecvt>
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/common/type/typedefs.h>
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/TypeId.h>
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/AttributeMetadata.h>
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
2
fc9911b1d295 send messages to PosixMQ: currently each attribute in one message
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    32
#include "PosixMQ.h"
3
be6f2e307a65 configuration + option: --unlink-on-close
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    33
#include "Configuration.h"
2
fc9911b1d295 send messages to PosixMQ: currently each attribute in one message
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    34
0
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
namespace relpipe {
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace out {
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace posixmq {
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
class PosixMQHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
private:
2
fc9911b1d295 send messages to PosixMQ: currently each attribute in one message
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    41
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
3
be6f2e307a65 configuration + option: --unlink-on-close
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    42
	Configuration configuration;
2
fc9911b1d295 send messages to PosixMQ: currently each attribute in one message
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    43
	shared_ptr<PosixMQ> mq;
fc9911b1d295 send messages to PosixMQ: currently each attribute in one message
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    44
4
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    45
	struct CurrentRelation {
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    46
		relpipe::common::type::StringX name;
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    47
		std::vector<relpipe::reader::handlers::AttributeMetadata> attributes;
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    48
		relpipe::common::type::Integer attributeIndex = 0;
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    49
		std::string currentValue;
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    50
	} currentRelation;
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    51
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    52
	std::string formatText(relpipe::common::type::StringX value) {
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    53
		// TODO: check valid UTF-8; if invalid, return only ASCII characters
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    54
		return convertor.to_bytes(value);
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    55
	}
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    56
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    57
	std::string formatData(relpipe::common::type::StringX value) {
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    58
		// TODO: decode HEX if type is string; return original octets if type is octet-string (not yet implemented)
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    59
		return convertor.to_bytes(value);
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    60
	}
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    61
0
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
public:
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
3
be6f2e307a65 configuration + option: --unlink-on-close
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    64
	PosixMQHandler(Configuration configuration) : configuration(configuration) {
be6f2e307a65 configuration + option: --unlink-on-close
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    65
		mq.reset(PosixMQ::open(convertor.to_bytes(configuration.queue), configuration.unlinkOnClose));
0
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	}
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
4
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    69
		currentRelation = CurrentRelation{name, attributes};
0
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	}
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	void attribute(const relpipe::common::type::StringX& value) override {
4
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    73
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    74
		auto attributeName = currentRelation.attributes[currentRelation.attributeIndex].getAttributeName();
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    75
		if (attributeName == L"text") currentRelation.currentValue = formatText(value);
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    76
		else if (attributeName == L"data") currentRelation.currentValue = formatData(value);
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    77
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    78
		currentRelation.attributeIndex++;
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    79
		if (currentRelation.attributeIndex == currentRelation.attributes.size()) {
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    80
			currentRelation.attributeIndex = 0;
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    81
			mq->send(currentRelation.currentValue);
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    82
		}
8a5b86415d80 send "text" and "data" attributes
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    83
0
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
	}
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
	void endOfPipe() {
2
fc9911b1d295 send messages to PosixMQ: currently each attribute in one message
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    87
0
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
	}
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
};
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
}
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
}
a64022f9684e new project relpipe-out-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
}