/*************************************************************************** * Copyright (C) 2015 by gempa GmbH * * * * All Rights Reserved. * * * * NOTICE: All information contained herein is, and remains * * the property of gempa GmbH and its suppliers, if any. The intellectual * * and technical concepts contained herein are proprietary to gempa GmbH * * and its suppliers. * * Dissemination of this information or reproduction of this material * * is strictly forbidden unless prior written permission is obtained * * from gempa GmbH. * ***************************************************************************/ #include #include #include #include namespace { void signalHandler(int signal) { Gempa::CAPS::Application::Interrupt(signal); } void registerSignalHandler() { CAPS_DEBUG("Registering signal handler"); signal(SIGTERM, signalHandler); signal(SIGINT, signalHandler); signal(SIGHUP, SIG_IGN); signal(SIGPIPE, SIG_IGN); } } namespace Gempa { namespace CAPS { Application* Application::_app = NULL; Application::Application(int argc, char **argv) { _exitRequested = false; _argc = argc; _argv = argv; _app = this; registerSignalHandler(); } void Application::done() { _exitRequested = true; CAPS_DEBUG("leaving ::done"); } int Application::exec() { _returnCode = 1; if ( init() ) { _returnCode = 0; if ( !run() && _returnCode == 0 ) _returnCode = 1; done(); } else done(); return _returnCode; } void Application::exit(int returnCode) { _returnCode = returnCode; _exitRequested = true; } void Application::handleInterrupt(int signal) { switch ( signal ) { case SIGABRT: exit(-1); case SIGSEGV: exit(-1); default: this->exit(_returnCode); } } void Application::Interrupt(int signal) { if ( _app ) _app->handleInterrupt(signal); } bool Application::init() { setlocale(LC_ALL, "C"); if ( !initCommandLine() ) { exit(1); return false; } if ( !initConfiguration() ) { exit(1); return false; } if ( !initCommandLine() ) { exit(1); return false; } if ( !validateParameters() ) { exit(1); return false; } return true; } bool Application::initCommandLine() { return true; } bool Application::initConfiguration() { return true; } bool Application::run() { return true; } bool Application::validateParameters() { return true; } } }