author | František Kučera <franta-hg@frantovo.cz> |
Sat, 23 Jul 2022 21:35:51 +0200 | |
branch | v_0 |
changeset 2 | e6294ba5017f |
parent 1 | ccaed729f8d3 |
permissions | -rw-r--r-- |
0 | 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 |
||
1 | 27 |
#include <zmq.hpp> |
28 |
||
0 | 29 |
#include <relpipe/common/type/typedefs.h> |
30 |
#include <relpipe/reader/TypeId.h> |
|
31 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
32 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
33 |
||
34 |
#include "Configuration.h" |
|
35 |
#include "Hex.h" |
|
36 |
||
37 |
namespace relpipe { |
|
38 |
namespace out { |
|
39 |
namespace zeromq { |
|
40 |
||
41 |
class ZeroMQHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
|
42 |
private: |
|
43 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
|
44 |
Configuration configuration; |
|
1 | 45 |
zmq::context_t zmqContext; |
46 |
zmq::socket_t zmqSocket; |
|
0 | 47 |
|
48 |
struct CurrentRelation { |
|
49 |
relpipe::common::type::StringX name; |
|
50 |
std::vector<relpipe::reader::handlers::AttributeMetadata> attributes; |
|
51 |
relpipe::common::type::Integer attributeIndex = 0; |
|
52 |
std::string currentValue; |
|
53 |
} currentRelation; |
|
54 |
||
55 |
public: |
|
56 |
||
1 | 57 |
ZeroMQHandler(Configuration configuration) : configuration(configuration), zmqSocket(zmqContext, zmq::socket_type::push) { |
2
e6294ba5017f
rename --endpoint-url to --connection-string
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
58 |
zmqSocket.connect(convertor.to_bytes(configuration.connectionString)); |
0 | 59 |
} |
60 |
||
61 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
62 |
currentRelation = CurrentRelation{name, attributes}; |
|
63 |
} |
|
64 |
||
65 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
66 |
||
67 |
auto attributeName = currentRelation.attributes[currentRelation.attributeIndex].getAttributeName(); |
|
68 |
if (attributeName == L"text" && value.size()) currentRelation.currentValue = convertor.to_bytes(value); |
|
69 |
else if (attributeName == L"data" && value.size()) currentRelation.currentValue = Hex::fromHex(value).str(); |
|
70 |
else if (attributeName == L"text"); // keep empty or value from 'data' |
|
71 |
else if (attributeName == L"data"); // keep empty or value from 'text' |
|
72 |
||
73 |
currentRelation.attributeIndex++; |
|
74 |
if (currentRelation.attributeIndex == currentRelation.attributes.size()) { |
|
75 |
currentRelation.attributeIndex = 0; |
|
1 | 76 |
zmqSocket.send(currentRelation.currentValue.c_str(), currentRelation.currentValue.size(), 0); // FIXME: check return value |
0 | 77 |
} |
78 |
||
79 |
} |
|
80 |
||
81 |
void endOfPipe() { |
|
82 |
||
83 |
} |
|
84 |
||
85 |
}; |
|
86 |
||
87 |
} |
|
88 |
} |
|
89 |
} |