src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 21 Aug 2022 00:16:50 +0200
branchv_0
changeset 3 2b57c8683ffe
parent 0 e8f15f432efc
permissions -rw-r--r--
synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()

/**
 * Relational pipes
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <vector>
#include <iostream>
#include <codecvt>
#include <regex>

#include <relpipe/common/type/typedefs.h>
#include <relpipe/cli/CLI.h>
#include <relpipe/cli/RelpipeCLIException.h>

#include "Configuration.h"

namespace relpipe {
namespace in {
namespace socket {

class CLIParser {
private:
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.

	relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& arguments, int& i) {
		if (i < arguments.size()) return arguments[i++];
		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);
	}

	/**
	 * TODO: use a common method
	 */
	bool parseBoolean(const relpipe::common::type::StringX& value) {
		if (value == L"true") return true;
		else if (value == L"false") return false;
		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);
	}

	void setIfMissing(std::vector<Configuration::SocketOption>& options, const std::string& name, const std::string& value, bool includeEmptyValues = true) {
		auto n = convertor.from_bytes(name);
		auto v = convertor.from_bytes(value);
		for (auto o : options) if (o.name == n) return;
		if (value.size() || includeEmptyValues) options.push_back({n, v});
	}

public:

	static const relpipe::common::type::StringX OPTION_RELATION;
	static const relpipe::common::type::StringX OPTION_CONNECTION_STRING;
	static const relpipe::common::type::StringX OPTION_CONNECTION_OPTION;
	static const relpipe::writer::string_t OPTION_MESSAGE_COUNT;

	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
		Configuration c;
		relpipe::common::type::StringX connectionString;

		for (int i = 0; i < arguments.size();) {
			relpipe::common::type::StringX option = readNext(arguments, i);

			if (option == OPTION_RELATION) {
				c.relation = readNext(arguments, i);
			} else if (option == OPTION_MESSAGE_COUNT) {
				c.messageCount = std::stoull(readNext(arguments, i));
			} else if (option == OPTION_CONNECTION_STRING) {
				connectionString = readNext(arguments, i);
			} else if (option == OPTION_CONNECTION_OPTION) {
				auto name = readNext(arguments, i);
				auto value = readNext(arguments, i);
				c.options.push_back({name, value});
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
		}

		using namespace options;

		// Parse the connection string and convert it to options:
		if (connectionString.size()) {
			std::string connectionStringBytes = convertor.to_bytes(connectionString);
			std::regex pattern("(tcp|udp|sctp)://(([^:]+)|\\[([0-9a-fA-F:]+(%[a-zA-Z0-9]+)?)\\]):([0-9]+)|(uds)://(.*)");
			//                  1                23          4             5                    6        7       8
			std::smatch match;
			if (std::regex_match(connectionStringBytes, match, pattern)) {
				setIfMissing(c.options, OPTION_PROTOCOL, match[1], false);
				setIfMissing(c.options, OPTION_PROTOCOL, match[7], false);
				setIfMissing(c.options, OPTION_HOST, match[3], false);
				setIfMissing(c.options, OPTION_HOST, match[4], false);
				setIfMissing(c.options, OPTION_PORT, match[6], false);
				setIfMissing(c.options, OPTION_PATH, match[8], false);
				if (match[1] == PROTOCOL_TCP) setIfMissing(c.options, OPTION_MODE, MODE_STREAM);
			} else {
				throw relpipe::cli::RelpipeCLIException(L"Invalid connection string: " + connectionString, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
			}
		}

		// Set defaults when values are missing:
		setIfMissing(c.options, OPTION_PROTOCOL, PROTOCOL_UDP);
		setIfMissing(c.options, OPTION_ROLE, ROLE_SERVER);
		setIfMissing(c.options, OPTION_MODE, MODE_DATAGRAM);
		setIfMissing(c.options, OPTION_HOST, "127.0.0.1");
		setIfMissing(c.options, OPTION_PORT, "64000");

		return c;
	}

	virtual ~CLIParser() {
	}
};

const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
const relpipe::common::type::StringX CLIParser::OPTION_CONNECTION_STRING = L"--connection-string";
const relpipe::common::type::StringX CLIParser::OPTION_CONNECTION_OPTION = L"--connection-option";
const relpipe::writer::string_t CLIParser::OPTION_MESSAGE_COUNT = L"--message-count";

}
}
}