src/SocketHandler.h
author František Kučera <franta-hg@frantovo.cz>
Thu, 28 Jul 2022 02:45:11 +0200
branchv_0
changeset 3 e701e06ff561
parent 2 fb399fd4f053
child 4 8d036e5e5fcc
permissions -rw-r--r--
add some factories: generic/template version
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>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/common/type/typedefs.h>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/TypeId.h>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/AttributeMetadata.h>
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include "Socket.h"
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include "Configuration.h"
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include "Hex.h"
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace relpipe {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace out {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
namespace socket {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
class SocketHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
private:
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	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
    43
	Configuration configuration;
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    44
	std::shared_ptr<Socket> socket;
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	struct CurrentRelation {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		relpipe::common::type::StringX name;
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		std::vector<relpipe::reader::handlers::AttributeMetadata> attributes;
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		relpipe::common::type::Integer attributeIndex = 0;
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		std::string currentValue;
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	} currentRelation;
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
public:
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	SocketHandler(Configuration configuration) : configuration(configuration) {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		// TODO: do not throw exception from the constructor: Socket::open()
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    57
		// socket = std::make_shared<TCPSocket>(); // TODO: create a TCP, UDP… socket
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    58
		socket.reset(SocketFactory::find("tcp://TODO:connectionString")->open("tcp://TODO:connectionString2")); // TODO: connection string + check nullptr
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		currentRelation = CurrentRelation{name, attributes};
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	void attribute(const relpipe::common::type::StringX& value) override {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		auto attributeName = currentRelation.attributes[currentRelation.attributeIndex].getAttributeName();
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		if (attributeName == L"text" && value.size()) currentRelation.currentValue = convertor.to_bytes(value);
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		else if (attributeName == L"data" && value.size()) currentRelation.currentValue = Hex::fromHex(value).str();
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		else if (attributeName == L"text"); // keep empty or value from 'data'
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
		else if (attributeName == L"data"); // keep empty or value from 'text'
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		currentRelation.attributeIndex++;
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		if (currentRelation.attributeIndex == currentRelation.attributes.size()) {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
			currentRelation.attributeIndex = 0;
1
e3265afd1111 first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    76
			socket->send(currentRelation.currentValue);
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
		}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
	void endOfPipe() {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
};
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
}