author | František Kučera <franta-hg@frantovo.cz> |
Sat, 30 Jul 2022 12:58:14 +0200 | |
branch | v_0 |
changeset 8 | 3e17086fffea |
parent 6 | f0c51bc4d164 |
child 17 | b9dcb7aa75e1 |
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); |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
88 |
std::regex pattern("(tcp|udp|sctp)://([^:]+):([0-9]+)|(uds)://(.*)"); |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
89 |
std::smatch match; |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
90 |
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
|
91 |
setIfMissing(c.options, OPTION_PROTOCOL, match[1], false); |
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[4], false); |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
93 |
setIfMissing(c.options, OPTION_HOST, match[2], false); |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
94 |
setIfMissing(c.options, OPTION_PORT, match[3], false); |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
95 |
setIfMissing(c.options, OPTION_PATH, match[5], false); |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
96 |
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
|
97 |
} else { |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
98 |
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
|
99 |
} |
f0c51bc4d164
configuration: parse the connection string
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
100 |
} |
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 |
// 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
|
103 |
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
|
104 |
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
|
105 |
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
|
106 |
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
|
107 |
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
|
108 |
|
0 | 109 |
return c; |
110 |
} |
|
111 |
||
112 |
virtual ~CLIParser() { |
|
113 |
} |
|
114 |
}; |
|
115 |
||
116 |
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
|
117 |
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
|
118 |
const relpipe::common::type::StringX CLIParser::OPTION_CONNECTION_OPTION = L"--connection-option"; |
0 | 119 |
|
120 |
} |
|
121 |
} |
|
122 |
} |