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 |
|
|
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 "AMQP.h"
|
|
33 |
#include "Configuration.h"
|
|
34 |
#include "Hex.h"
|
|
35 |
|
|
36 |
namespace relpipe {
|
|
37 |
namespace out {
|
|
38 |
namespace amqp {
|
|
39 |
|
|
40 |
class AMQPHandler : 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<AMQP> 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 |
AMQPHandler(Configuration configuration) : configuration(configuration) {
|
|
56 |
// TODO: do not throw exception from the constructor: AMQP::open()
|
|
57 |
mq.reset(AMQP::open(convertor.to_bytes(configuration.queue), configuration.unlinkOnClose));
|
|
58 |
}
|
|
59 |
|
|
60 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
|
|
61 |
currentRelation = CurrentRelation{name, attributes};
|
|
62 |
}
|
|
63 |
|
|
64 |
void attribute(const relpipe::common::type::StringX& value) override {
|
|
65 |
|
|
66 |
auto attributeName = currentRelation.attributes[currentRelation.attributeIndex].getAttributeName();
|
|
67 |
if (attributeName == L"text" && value.size()) currentRelation.currentValue = convertor.to_bytes(value);
|
|
68 |
else if (attributeName == L"data" && value.size()) currentRelation.currentValue = Hex::fromHex(value).str();
|
|
69 |
else if (attributeName == L"text"); // keep empty or value from 'data'
|
|
70 |
else if (attributeName == L"data"); // keep empty or value from 'text'
|
|
71 |
|
|
72 |
currentRelation.attributeIndex++;
|
|
73 |
if (currentRelation.attributeIndex == currentRelation.attributes.size()) {
|
|
74 |
currentRelation.attributeIndex = 0;
|
|
75 |
mq->send(currentRelation.currentValue);
|
|
76 |
}
|
|
77 |
|
|
78 |
}
|
|
79 |
|
|
80 |
void endOfPipe() {
|
|
81 |
|
|
82 |
}
|
|
83 |
|
|
84 |
};
|
|
85 |
|
|
86 |
}
|
|
87 |
}
|
|
88 |
}
|