src/SocketCommand.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 21 Aug 2022 00:16:50 +0200
branchv_0
changeset 3 2b57c8683ffe
parent 1 d93ea7346b66
permissions -rw-r--r--
synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
e8f15f432efc 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
e8f15f432efc 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
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
e8f15f432efc 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,
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
e8f15f432efc 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
e8f15f432efc 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/>.
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <cstdlib>
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <vector>
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <locale>
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <regex>
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <algorithm>
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <unistd.h>
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <sstream>
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <iomanip>
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/writer/RelationalWriter.h>
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/writer/RelpipeWriterException.h>
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/writer/AttributeMetadata.h>
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/writer/Factory.h>
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/writer/TypeId.h>
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include <relpipe/cli/CLI.h>
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include "SocketCommand.h"
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
#include "Socket.h"
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
#include "Hex.h"
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
using namespace std;
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
using namespace relpipe::cli;
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
using namespace relpipe::writer;
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
namespace relpipe {
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
namespace in {
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
namespace socket {
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
void SocketCommand::process(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	vector<AttributeMetadata> metadata;
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
3
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    50
	std::vector<SocketOption> options;
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    51
	for (auto o : configuration.options) options.push_back({convertor.to_bytes(o.name), convertor.to_bytes(o.value)});
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    52
	std::shared_ptr<Socket> socket = SocketFactory::find(options)->open(options);
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	writer->startRelation(configuration.relation,{
3
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    55
		{L"host", TypeId::STRING},
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    56
		{L"port", TypeId::INTEGER},
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    57
		{L"pid", TypeId::INTEGER},
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    58
		{L"gid", TypeId::INTEGER},
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    59
		{L"uid", TypeId::INTEGER},
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		{L"text", TypeId::STRING},
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		{L"data", TypeId::STRING}
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	}, true);
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	for (int i = configuration.messageCount; continueProcessing && i > 0; i--) {
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		// TODO: maybe rather call mq_timedreceive() inside and check continueProcessing (to be able to stop even when no messages are comming)
3
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    66
		IncomingMessage message = socket->receive();
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
3
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    68
		writer->writeAttribute(convertor.from_bytes(message.remoteHost));
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    69
		writer->writeAttribute(std::to_wstring(message.remotePort));
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    70
		writer->writeAttribute(std::to_wstring(message.remotePID));
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    71
		writer->writeAttribute(std::to_wstring(message.remoteGID));
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    72
		writer->writeAttribute(std::to_wstring(message.remoteUID));
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    73
		writer->writeAttribute(Hex::toTxt(message.data));
2b57c8683ffe synchronize interface and common classes with relpipe-out-socket + implement basic UDPServerSocket::receive()
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    74
		writer->writeAttribute(Hex::toHex(message.data));
0
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	}
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
}
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
SocketCommand::~SocketCommand() {
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
}
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
}
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
}
e8f15f432efc establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
}