main.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2013, Institute for Artificial Intelligence,
00005  *  Universität Bremen.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Institute for Artificial Intelligence,
00019  *     Universität Bremen, nor the names of its contributors may be
00020  *     used to endorse or promote products derived from this software
00021  *     without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *********************************************************************/
00036 
00040 // System
00041 #include <iostream>
00042 #include <cstdlib>
00043 #include <signal.h>
00044 #include <stdio.h>
00045 #include <string.h>
00046 #include <unistd.h>
00047 #include <getopt.h>
00048 
00049 // Private
00050 #include <BeliefstateROS.h>
00051 
00052 
00053 // Storage of former signal handlers
00054 typedef void (*Handler)(int signum);
00055 Handler hdlrOldSIGWINCH = SIG_IGN;
00056 
00057 
00058 // Global variable for shutdown triggering
00059 beliefstate::BeliefstateROS* g_bsBeliefstate;
00060 
00061 
00062 void printHelp(string strExecutableName) {
00063   std::cout << "Beliefstate System (version " + g_bsBeliefstate->version() + ") by Jan Winkler <winkler@cs.uni-bremen.de>" << std::endl;
00064   std::cout << "Licensed under BSD. https://www.github.com/fairlight1337/beliefstate" << std::endl << std::endl;
00065   std::cout << "Usage: " << strExecutableName << " [options]" << std::endl << std::endl;
00066   
00067   std::cout << "Available options are:" << std::endl;
00068   std::cout << "  -h, --help\t\tPrint this help" << std::endl;
00069   std::cout << "  -c, --config <file>\tLoad config file <file> instead of the default one" << std::endl;
00070   std::cout << std::endl;
00071   
00072   std::cout << "Should any questions arise, feel free to send an email to winkler@cs.uni-bremen.de" << std::endl;
00073 }
00074 
00075 void catchHandler(int nSignum) {
00076   switch(nSignum) {
00077   case SIGTERM:
00078   case SIGINT: {
00079     g_bsBeliefstate->triggerShutdown();
00080   } break;
00081     
00082   case SIGWINCH: {
00083     if(hdlrOldSIGWINCH != SIG_IGN && hdlrOldSIGWINCH != SIG_DFL) {
00084       (*hdlrOldSIGWINCH)(SIGWINCH);
00085     }
00086     
00087     g_bsBeliefstate->triggerTerminalResize();
00088   } break;
00089     
00090   default:
00091     break;
00092   }
00093 }
00094 
00095 int main(int argc, char** argv) {
00096   g_bsBeliefstate = new beliefstate::BeliefstateROS(argc, argv);
00097   
00098   // Read command line parameters
00099   int nC, option_index = 0;
00100   static struct option long_options[] = {{"config", required_argument, 0, 'c'},
00101                                          {"help",   no_argument,       0, 'h'},
00102                                          {0,        0,                 0, 0}};
00103   
00104   std::string strConfigFile = "";
00105   bool bQuit = false;
00106   
00107   while((nC = getopt_long(argc, argv, "c:h", long_options, &option_index)) != -1) {
00108     switch(nC) {
00109     case 'c': {
00110       strConfigFile = string(optarg);
00111     } break;
00112       
00113     case 'h': {
00114       printHelp(string(argv[0]));
00115       bQuit = true;
00116     } break;
00117       
00118     default: {
00119     } break;
00120     }
00121   }
00122   
00123   if(bQuit == false) {
00124     g_bsBeliefstate->info("Starting beliefstate system (version " + g_bsBeliefstate->version() + ").");
00125     
00126     beliefstate::Result resInit = g_bsBeliefstate->init(strConfigFile);
00127     
00128     if(resInit.bSuccess) {
00129       // Catch SIGTERM and SIGINT and bind them to the callback function
00130       // catchSIGTERMandSIGINT. This will trigger the shutdown mechanism
00131       // in the Beliefstate instance.
00132       struct sigaction action;
00133       memset(&action, 0, sizeof(struct sigaction));
00134       action.sa_handler = catchHandler;
00135       sigaction(SIGTERM, &action, NULL);
00136       sigaction(SIGINT, &action, NULL);
00137       
00138       hdlrOldSIGWINCH = signal(SIGWINCH, SIG_IGN);
00139       sigaction(SIGWINCH, &action, NULL);
00140       
00141       g_bsBeliefstate->info("Initialization complete, ready for action.", true);
00142       while(g_bsBeliefstate->cycle()) {
00143         // Idle here at will.
00144         usleep(10);
00145       }
00146     } else {
00147       g_bsBeliefstate->fail("Initialization of the beliefstate system failed. Being a quitter.");
00148     }
00149     
00150     std::cout << "\r";
00151     g_bsBeliefstate->info("Exiting gracefully.");
00152     g_bsBeliefstate->cycle();
00153     
00154     g_bsBeliefstate->deinit();
00155     g_bsBeliefstate->cycle();
00156     
00157     delete g_bsBeliefstate;
00158   }
00159   
00160   return EXIT_SUCCESS;
00161 }


beliefstate
Author(s): Jan Winkler
autogenerated on Sun Oct 5 2014 22:30:15