author | František Kučera <franta-hg@frantovo.cz> |
Sat, 20 Aug 2022 22:12:25 +0200 | |
branch | v_0 |
changeset 29 | 97fe991ad2cb |
parent 8 | 3e17086fffea |
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> |
|
8
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
26 |
#include <regex> |
0 | 27 |
|
28 |
#include <relpipe/common/type/typedefs.h> |
|
29 |
#include <relpipe/reader/TypeId.h> |
|
30 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
31 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
32 |
||
33 |
#include "Socket.h" |
|
34 |
#include "Configuration.h" |
|
35 |
#include "Hex.h" |
|
36 |
||
37 |
namespace relpipe { |
|
38 |
namespace out { |
|
39 |
namespace socket { |
|
40 |
||
41 |
class SocketHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
|
42 |
private: |
|
43 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
|
44 |
Configuration configuration; |
|
3
e701e06ff561
add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
45 |
std::shared_ptr<Socket> socket; |
8
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
46 |
std::wregex relationPattern; |
0 | 47 |
|
48 |
struct CurrentRelation { |
|
49 |
relpipe::common::type::StringX name; |
|
50 |
std::vector<relpipe::reader::handlers::AttributeMetadata> attributes; |
|
8
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
51 |
bool valid; |
0 | 52 |
relpipe::common::type::Integer attributeIndex = 0; |
53 |
std::string currentValue; |
|
54 |
} currentRelation; |
|
55 |
||
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
56 |
void configureSocket() { |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
57 |
if (socket) { |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
58 |
// already configured, reuse |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
59 |
} else { |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
60 |
std::vector<SocketOption> options; |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
61 |
for (auto o : configuration.options) options.push_back({convertor.to_bytes(o.name), convertor.to_bytes(o.value)}); |
5
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
62 |
if (auto f = SocketFactory::find(options)) socket = f->open(options); |
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
63 |
} |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
64 |
} |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
65 |
|
0 | 66 |
public: |
67 |
||
8
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
68 |
SocketHandler(Configuration configuration) : configuration(configuration), relationPattern(std::wregex(configuration.relation)) { |
0 | 69 |
} |
70 |
||
71 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
8
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
72 |
currentRelation = CurrentRelation{name, attributes, std::regex_match(name, relationPattern)}; |
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
73 |
if (currentRelation.valid) configureSocket(); |
0 | 74 |
} |
75 |
||
76 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
8
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
77 |
if (currentRelation.valid) { |
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
78 |
auto attributeName = currentRelation.attributes[currentRelation.attributeIndex].getAttributeName(); |
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
79 |
if (attributeName == L"text" && value.size()) currentRelation.currentValue = convertor.to_bytes(value); |
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
80 |
else if (attributeName == L"data" && value.size()) currentRelation.currentValue = Hex::fromHex(value).str(); |
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
81 |
else if (attributeName == L"text"); // keep empty or value from 'data' |
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
82 |
else if (attributeName == L"data"); // keep empty or value from 'text' |
0 | 83 |
|
8
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
84 |
currentRelation.attributeIndex++; |
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
85 |
if (currentRelation.attributeIndex == currentRelation.attributes.size()) { |
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
86 |
currentRelation.attributeIndex = 0; |
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
87 |
socket->send(currentRelation.currentValue); |
3e17086fffea
process only relations named 'message' or matching the --relation pattern
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
88 |
} |
0 | 89 |
} |
90 |
} |
|
91 |
||
92 |
void endOfPipe() { |
|
93 |
||
94 |
} |
|
95 |
||
96 |
}; |
|
97 |
||
98 |
} |
|
99 |
} |
|
100 |
} |