src/ZeroMQCommand.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 23 Jul 2022 21:36:01 +0200
branchv_0
changeset 3 3891db9e45b7
parent 2 f724d805c34a
permissions -rw-r--r--
rename --endpoint-url to --connection-string
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
e5d547ab0c51 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
e5d547ab0c51 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
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
e5d547ab0c51 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,
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
e5d547ab0c51 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
e5d547ab0c51 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/>.
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <cstdlib>
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <vector>
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <locale>
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <regex>
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <algorithm>
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <unistd.h>
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <sstream>
1
27c11cea34de first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    25
27c11cea34de first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    26
#include <zmq.hpp>
0
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/writer/RelationalWriter.h>
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/writer/RelpipeWriterException.h>
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/writer/AttributeMetadata.h>
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/writer/Factory.h>
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <relpipe/writer/TypeId.h>
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include <relpipe/cli/CLI.h>
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
#include "ZeroMQCommand.h"
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
#include "Hex.h"
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
using namespace std;
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
using namespace relpipe::cli;
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
using namespace relpipe::writer;
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
namespace relpipe {
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
namespace in {
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
namespace zeromq {
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
void ZeroMQCommand::process(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
1
27c11cea34de first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    48
	zmq::context_t zmqContext;
27c11cea34de first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
	zmq::socket_t zmqSocket(zmqContext, zmq::socket_type::pull);
3
3891db9e45b7 rename --endpoint-url to --connection-string
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    50
	zmqSocket.bind(convertor.to_bytes(configuration.connectionString));
0
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
2
f724d805c34a encryption TODO
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    52
f724d805c34a encryption TODO
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    53
	if (false) {
f724d805c34a encryption TODO
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    54
		// TODO: optionally generate and print keys and/or do encryption
f724d805c34a encryption TODO
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    55
		char server_public_key[41];
f724d805c34a encryption TODO
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    56
		char server_secret_key[41];
f724d805c34a encryption TODO
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    57
		zmq_curve_keypair(server_public_key, server_secret_key);
f724d805c34a encryption TODO
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    58
		std::cerr << "Server public key: " << std::string(server_public_key) << std::endl;
f724d805c34a encryption TODO
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    59
		std::cerr << "Server secret key: " << std::string(server_secret_key) << std::endl;
f724d805c34a encryption TODO
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    60
	}
f724d805c34a encryption TODO
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    61
0
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	writer->startRelation(configuration.relation,{
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		{L"text", TypeId::STRING},
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		{L"data", TypeId::STRING}
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	}, true);
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	for (int i = configuration.messageCount; continueProcessing && i > 0; i--) {
1
27c11cea34de first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    68
		zmq::message_t msg;
27c11cea34de first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    69
		zmqSocket.recv(&msg, 0); // FIXME: check return value
27c11cea34de first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    70
		std::string message(msg.data<char>(), msg.size());
0
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		writer->writeAttribute(Hex::toTxt(message));
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		writer->writeAttribute(Hex::toHex(message));
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	}
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
}
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
ZeroMQCommand::~ZeroMQCommand() {
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
}
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
}
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
}
e5d547ab0c51 establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
}