src/KafkaHandler.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 27 Apr 2022 21:10:10 +0200
branchv_0
changeset 0 f4d7e0965055
child 1 d7824971fd9e
permissions -rw-r--r--
establish project
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
f4d7e0965055 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
f4d7e0965055 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
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
f4d7e0965055 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,
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
f4d7e0965055 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
f4d7e0965055 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/>.
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <codecvt>
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/common/type/typedefs.h>
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/TypeId.h>
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/AttributeMetadata.h>
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include "Kafka.h"
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include "Configuration.h"
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include "Hex.h"
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace relpipe {
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace out {
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
namespace kafka {
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
class KafkaHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
private:
f4d7e0965055 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.
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	Configuration configuration;
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	shared_ptr<Kafka> mq;
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	struct CurrentRelation {
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		relpipe::common::type::StringX name;
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		std::vector<relpipe::reader::handlers::AttributeMetadata> attributes;
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		relpipe::common::type::Integer attributeIndex = 0;
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		std::string currentValue;
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	} currentRelation;
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
public:
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	KafkaHandler(Configuration configuration) : configuration(configuration) {
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		mq.reset(Kafka::open(convertor.to_bytes(configuration.queue), configuration.unlinkOnClose));
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	}
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		currentRelation = CurrentRelation{name, attributes};
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	}
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	void attribute(const relpipe::common::type::StringX& value) override {
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		auto attributeName = currentRelation.attributes[currentRelation.attributeIndex].getAttributeName();
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		if (attributeName == L"text") currentRelation.currentValue = convertor.to_bytes(value);
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		else if (attributeName == L"data") currentRelation.currentValue = Hex::fromHex(value).str();
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		currentRelation.attributeIndex++;
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		if (currentRelation.attributeIndex == currentRelation.attributes.size()) {
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
			currentRelation.attributeIndex = 0;
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
			mq->send(currentRelation.currentValue);
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		}
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	}
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	void endOfPipe() {
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
	}
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
};
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
}
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
}
f4d7e0965055 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
}