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

/**
 * 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/>.
 */
#include <cstdlib>
#include <vector>
#include <memory>
#include <locale>
#include <regex>
#include <algorithm>
#include <unistd.h>
#include <sstream>

#include <zmq.hpp>

#include <relpipe/writer/RelationalWriter.h>
#include <relpipe/writer/RelpipeWriterException.h>
#include <relpipe/writer/AttributeMetadata.h>
#include <relpipe/writer/Factory.h>
#include <relpipe/writer/TypeId.h>

#include <relpipe/cli/CLI.h>

#include "ZeroMQCommand.h"
#include "Hex.h"

using namespace std;
using namespace relpipe::cli;
using namespace relpipe::writer;

namespace relpipe {
namespace in {
namespace zeromq {

void ZeroMQCommand::process(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
	zmq::context_t zmqContext;
	zmq::socket_t zmqSocket(zmqContext, zmq::socket_type::pull);
	zmqSocket.bind(convertor.to_bytes(configuration.connectionString));


	if (false) {
		// TODO: optionally generate and print keys and/or do encryption
		char server_public_key[41];
		char server_secret_key[41];
		zmq_curve_keypair(server_public_key, server_secret_key);
		std::cerr << "Server public key: " << std::string(server_public_key) << std::endl;
		std::cerr << "Server secret key: " << std::string(server_secret_key) << std::endl;
	}

	writer->startRelation(configuration.relation,{
		{L"text", TypeId::STRING},
		{L"data", TypeId::STRING}
	}, true);

	for (int i = configuration.messageCount; continueProcessing && i > 0; i--) {
		zmq::message_t msg;
		zmqSocket.recv(&msg, 0); // FIXME: check return value
		std::string message(msg.data<char>(), msg.size());

		writer->writeAttribute(Hex::toTxt(message));
		writer->writeAttribute(Hex::toHex(message));
	}

}

ZeroMQCommand::~ZeroMQCommand() {
}

}
}
}