src/relpipe-in-amqp.cpp
branchv_0
changeset 0 08cb319d7c3a
equal deleted inserted replaced
-1:000000000000 0:08cb319d7c3a
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 #include <cstdlib>
       
    18 #include <csignal>
       
    19 #include <vector>
       
    20 #include <memory>
       
    21 #include <regex>
       
    22 #include <algorithm>
       
    23 #include <unistd.h>
       
    24 
       
    25 #include <relpipe/writer/RelationalWriter.h>
       
    26 #include <relpipe/writer/RelpipeWriterException.h>
       
    27 #include <relpipe/writer/AttributeMetadata.h>
       
    28 #include <relpipe/writer/Factory.h>
       
    29 #include <relpipe/writer/TypeId.h>
       
    30 
       
    31 #include <relpipe/cli/CLI.h>
       
    32 
       
    33 #include "AMQPCommand.h"
       
    34 #include "CLIParser.h"
       
    35 #include "Configuration.h"
       
    36 
       
    37 using namespace relpipe::cli;
       
    38 using namespace relpipe::writer;
       
    39 using namespace relpipe::in::amqp;
       
    40 
       
    41 static std::shared_ptr<AMQPCommand> command = nullptr;
       
    42 
       
    43 void finish(int sig) {
       
    44 	if (command) command->finish(sig);
       
    45 }
       
    46 
       
    47 int main(int argc, char** argv) {
       
    48 	setlocale(LC_ALL, "");
       
    49 	CLI::untieStdIO();
       
    50 	CLI cli(argc, argv);
       
    51 
       
    52 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
       
    53 
       
    54 	try {
       
    55 		signal(SIGHUP, finish);
       
    56 		signal(SIGINT, finish);
       
    57 		CLIParser cliParser;
       
    58 		Configuration configuration = cliParser.parse(cli.arguments());
       
    59 		command.reset(new AMQPCommand());
       
    60 		std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout));
       
    61 		writer->setBufferingMode(BufferingMode::ENVIRONMENT, BufferingMode::RECORD);
       
    62 		command->process(writer, configuration);
       
    63 		resultCode = CLI::EXIT_CODE_SUCCESS;
       
    64 	} catch (RelpipeCLIException e) {
       
    65 		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessage().c_str());
       
    66 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    67 		resultCode = e.getExitCode();
       
    68 	} catch (RelpipeWriterException e) {
       
    69 		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessage().c_str());
       
    70 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    71 		resultCode = CLI::EXIT_CODE_DATA_ERROR;
       
    72 	}
       
    73 
       
    74 	return resultCode;
       
    75 }