src/SocketHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 20 Aug 2022 00:20:03 +0200
branchv_0
changeset 27 e6e5780339bd
parent 8 3e17086fffea
permissions -rw-r--r--
introduce IncomingMessage and OutgoingMessage types
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/common/type/typedefs.h>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/TypeId.h>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/reader/handlers/AttributeMetadata.h>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include "Socket.h"
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include "Configuration.h"
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include "Hex.h"
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace relpipe {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
namespace out {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
namespace socket {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
class SocketHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
private:
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	struct CurrentRelation {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		relpipe::common::type::StringX name;
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		relpipe::common::type::Integer attributeIndex = 0;
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		std::string currentValue;
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	} currentRelation;
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
public:
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
		}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
	void endOfPipe() {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
};
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
}