author | František Kučera <franta-hg@frantovo.cz> |
Thu, 18 Aug 2022 22:42:42 +0200 | |
branch | v_0 |
changeset 26 | 07949ba141b7 |
parent 20 | ad21bff45051 |
child 29 | 97fe991ad2cb |
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 <vector> |
|
20 |
#include <iostream> |
|
5
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
21 |
#include <codecvt> |
6
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
22 |
#include <regex> |
0 | 23 |
|
24 |
#include <relpipe/common/type/typedefs.h> |
|
25 |
#include <relpipe/cli/CLI.h> |
|
26 |
#include <relpipe/cli/RelpipeCLIException.h> |
|
27 |
||
28 |
#include "Configuration.h" |
|
29 |
||
30 |
namespace relpipe { |
|
31 |
namespace out { |
|
32 |
namespace socket { |
|
33 |
||
34 |
class CLIParser { |
|
35 |
private: |
|
5
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
36 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
0 | 37 |
|
38 |
relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& arguments, int& i) { |
|
39 |
if (i < arguments.size()) return arguments[i++]; |
|
40 |
else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
41 |
} |
|
42 |
||
43 |
/** |
|
44 |
* TODO: use a common method |
|
45 |
*/ |
|
46 |
bool parseBoolean(const relpipe::common::type::StringX& value) { |
|
47 |
if (value == L"true") return true; |
|
48 |
else if (value == L"false") return false; |
|
49 |
else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
50 |
} |
|
51 |
||
6
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
52 |
void setIfMissing(std::vector<Configuration::SocketOption>& options, const std::string& name, const std::string& value, bool includeEmptyValues = true) { |
5
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
53 |
auto n = convertor.from_bytes(name); |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
54 |
auto v = convertor.from_bytes(value); |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
55 |
for (auto o : options) if (o.name == n) return; |
6
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
56 |
if (value.size() || includeEmptyValues) options.push_back({n, v}); |
5
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
57 |
} |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
58 |
|
0 | 59 |
public: |
60 |
||
61 |
static const relpipe::common::type::StringX OPTION_RELATION; |
|
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
62 |
static const relpipe::common::type::StringX OPTION_CONNECTION_STRING; |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
63 |
static const relpipe::common::type::StringX OPTION_CONNECTION_OPTION; |
0 | 64 |
|
65 |
Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) { |
|
66 |
Configuration c; |
|
6
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
67 |
relpipe::common::type::StringX connectionString; |
0 | 68 |
|
69 |
for (int i = 0; i < arguments.size();) { |
|
70 |
relpipe::common::type::StringX option = readNext(arguments, i); |
|
71 |
||
72 |
if (option == OPTION_RELATION) { |
|
73 |
c.relation = readNext(arguments, i); |
|
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
74 |
} else if (option == OPTION_CONNECTION_STRING) { |
6
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
75 |
connectionString = readNext(arguments, i); |
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
76 |
} else if (option == OPTION_CONNECTION_OPTION) { |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
auto name = readNext(arguments, i); |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
auto value = readNext(arguments, i); |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
79 |
c.options.push_back({name, value}); |
0 | 80 |
} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
81 |
} |
|
82 |
||
5
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
83 |
using namespace options; |
6
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
84 |
|
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
85 |
// Parse the connection string and convert it to options: |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
86 |
if (connectionString.size()) { |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
87 |
std::string connectionStringBytes = convertor.to_bytes(connectionString); |
20
ad21bff45051
support IPv6 link local addresses not only in the „host“ option but also in the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
88 |
std::regex pattern("(tcp|udp|sctp)://(([^:]+)|\\[([0-9a-fA-F:]+(%[a-zA-Z0-9]+))\\]):([0-9]+)|(uds)://(.*)"); |
ad21bff45051
support IPv6 link local addresses not only in the „host“ option but also in the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
89 |
// 1 23 4 5 6 7 8 |
6
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
90 |
std::smatch match; |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
91 |
if (std::regex_match(connectionStringBytes, match, pattern)) { |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
92 |
setIfMissing(c.options, OPTION_PROTOCOL, match[1], false); |
20
ad21bff45051
support IPv6 link local addresses not only in the „host“ option but also in the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
93 |
setIfMissing(c.options, OPTION_PROTOCOL, match[7], false); |
17
b9dcb7aa75e1
support literal IPv6 addresses in connection strings
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
94 |
setIfMissing(c.options, OPTION_HOST, match[3], false); |
b9dcb7aa75e1
support literal IPv6 addresses in connection strings
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
95 |
setIfMissing(c.options, OPTION_HOST, match[4], false); |
20
ad21bff45051
support IPv6 link local addresses not only in the „host“ option but also in the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
96 |
setIfMissing(c.options, OPTION_PORT, match[6], false); |
ad21bff45051
support IPv6 link local addresses not only in the „host“ option but also in the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
97 |
setIfMissing(c.options, OPTION_PATH, match[8], false); |
6
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
98 |
if (match[1] == PROTOCOL_TCP) setIfMissing(c.options, OPTION_MODE, MODE_STREAM); |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
99 |
} else { |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
100 |
throw relpipe::cli::RelpipeCLIException(L"Invalid connection string: " + connectionString, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
101 |
} |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
102 |
} |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
103 |
|
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
104 |
// Set defaults when values are missing: |
5
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
105 |
setIfMissing(c.options, OPTION_PROTOCOL, PROTOCOL_UDP); |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
106 |
setIfMissing(c.options, OPTION_ROLE, ROLE_CLIENT); |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
107 |
setIfMissing(c.options, OPTION_MODE, MODE_DATAGRAM); |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
108 |
setIfMissing(c.options, OPTION_HOST, "127.0.0.1"); |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
109 |
setIfMissing(c.options, OPTION_PORT, "64000"); |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
110 |
|
0 | 111 |
return c; |
112 |
} |
|
113 |
||
114 |
virtual ~CLIParser() { |
|
115 |
} |
|
116 |
}; |
|
117 |
||
118 |
const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation"; |
|
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
119 |
const relpipe::common::type::StringX CLIParser::OPTION_CONNECTION_STRING = L"--connection-string"; |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
120 |
const relpipe::common::type::StringX CLIParser::OPTION_CONNECTION_OPTION = L"--connection-option"; |
0 | 121 |
|
122 |
} |
|
123 |
} |
|
124 |
} |