src/AMQPHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 01 May 2022 18:30:06 +0200
branchv_0
changeset 0 349627c91695
permissions -rw-r--r--
establish project
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
349627c91695 establish project
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
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <codecvt>
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/common/type/typedefs.h>
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/TypeId.h>
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/AttributeMetadata.h>
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include "AMQP.h"
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include "Configuration.h"
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include "Hex.h"
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace relpipe {
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace out {
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
namespace amqp {
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
class AMQPHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
private:
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	Configuration configuration;
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	shared_ptr<AMQP> mq;
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	struct CurrentRelation {
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		relpipe::common::type::StringX name;
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		std::vector<relpipe::reader::handlers::AttributeMetadata> attributes;
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		relpipe::common::type::Integer attributeIndex = 0;
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		std::string currentValue;
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	} currentRelation;
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
public:
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	AMQPHandler(Configuration configuration) : configuration(configuration) {
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		// TODO: do not throw exception from the constructor: AMQP::open()
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		mq.reset(AMQP::open(convertor.to_bytes(configuration.queue), configuration.unlinkOnClose));
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	}
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		currentRelation = CurrentRelation{name, attributes};
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	}
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	void attribute(const relpipe::common::type::StringX& value) override {
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		auto attributeName = currentRelation.attributes[currentRelation.attributeIndex].getAttributeName();
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		if (attributeName == L"text" && value.size()) currentRelation.currentValue = convertor.to_bytes(value);
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		else if (attributeName == L"data" && value.size()) currentRelation.currentValue = Hex::fromHex(value).str();
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		else if (attributeName == L"text"); // keep empty or value from 'data'
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		else if (attributeName == L"data"); // keep empty or value from 'text'
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		currentRelation.attributeIndex++;
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		if (currentRelation.attributeIndex == currentRelation.attributes.size()) {
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
			currentRelation.attributeIndex = 0;
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
			mq->send(currentRelation.currentValue);
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
		}
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	}
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	void endOfPipe() {
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
	}
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
};
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
}
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
}
349627c91695 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
}