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