src/PosixMQCommand.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 05 Mar 2022 19:41:05 +0100
branchv_0
changeset 4 f9d43c5adae9
parent 3 b71fc3b5e56b
child 5 c6ef2463e100
permissions -rw-r--r--
return also raw (hex) message data
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
2
1eef3d465863 update copyright year
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
e8205d9206fb new project relpipe-in-posixmq
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
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <cstdlib>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <vector>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <locale>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <regex>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <algorithm>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <unistd.h>
4
f9d43c5adae9 return also raw (hex) message data
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    24
#include <sstream>
f9d43c5adae9 return also raw (hex) message data
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    25
#include <iomanip>
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/writer/RelationalWriter.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/writer/RelpipeWriterException.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/writer/AttributeMetadata.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/writer/Factory.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/writer/TypeId.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include <relpipe/cli/CLI.h>
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include "PosixMQCommand.h"
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    36
#include "PosixMQ.h"
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
using namespace std;
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
using namespace relpipe::cli;
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
using namespace relpipe::writer;
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
namespace relpipe {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
namespace in {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
namespace posixmq {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
void PosixMQCommand::process(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	vector<AttributeMetadata> metadata;
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
3
b71fc3b5e56b new option: --unlink-on-close (delete the MQ file at the end)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    49
	std::shared_ptr<PosixMQ> mq(PosixMQ::open(convertor.to_bytes(configuration.queue), configuration.unlinkOnClose));
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
	writer->startRelation(configuration.relation,{
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    52
		{L"queue", TypeId::STRING},
4
f9d43c5adae9 return also raw (hex) message data
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    53
		{L"text", TypeId::STRING},
f9d43c5adae9 return also raw (hex) message data
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    54
		{L"data", TypeId::STRING}
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	}, true);
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    57
	for (int i = configuration.messageCount; i > 0; i--) {
4
f9d43c5adae9 return also raw (hex) message data
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    58
		std::string message = mq->receive();
f9d43c5adae9 return also raw (hex) message data
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    59
f9d43c5adae9 return also raw (hex) message data
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    60
		std::stringstream hex;
f9d43c5adae9 return also raw (hex) message data
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    61
		hex << std::hex << std::setfill('0');
f9d43c5adae9 return also raw (hex) message data
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    62
		for (uint8_t b : message) hex << std::setw(2) << (int) b;
f9d43c5adae9 return also raw (hex) message data
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    63
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    64
		writer->writeAttribute(configuration.queue);
4
f9d43c5adae9 return also raw (hex) message data
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    65
		writer->writeAttribute(convertor.from_bytes(message)); // Show at least ASCII characters + ... if UTF-8 decoding fails
f9d43c5adae9 return also raw (hex) message data
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    66
		writer->writeAttribute(convertor.from_bytes(hex.str()));
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    67
	}
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
}
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
1
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    71
PosixMQCommand::~PosixMQCommand() {
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    72
}
291bdd97fcff early version, support text messages only
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    73
0
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
}
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
}
e8205d9206fb new project relpipe-in-posixmq
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
}