deployer.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Jul 3 15:30:14 CEST 2008  deployer.cpp
00003 
00004                         deployer.cpp -  description
00005                            -------------------
00006     begin                : Thu July 03 2008
00007     copyright            : (C) 2008 Peter Soetens
00008     email                : peter.soetens@fmtc.be
00009 
00010  ***************************************************************************
00011  *   This program is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU Lesser General Public            *
00013  *   License as published by the Free Software Foundation; either          *
00014  *   version 2.1 of the License, or (at your option) any later version.    *
00015  *                                                                         *
00016  *   This program is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00019  *   Lesser General Public License for more details.                       *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU Lesser General Public      *
00022  *   License along with this program; if not, write to the Free Software   *
00023  *   Foundation, Inc., 59 Temple Place,                                    *
00024  *   Suite 330, Boston, MA  02111-1307  USA                                *
00025  ***************************************************************************/
00026 
00027 #include <rtt/rtt-config.h>
00028 #ifdef OS_RT_MALLOC
00029 // need access to all TLSF functions embedded in RTT
00030 #define ORO_MEMORY_POOL
00031 #include <rtt/os/tlsf/tlsf.h>
00032 #endif
00033 #include <rtt/os/main.h>
00034 #include <rtt/RTT.hpp>
00035 #include <rtt/Logger.hpp>
00036 
00037 #ifndef RTT_SCRIPT_PROGRAM
00038 #define USE_TASKBROWSER
00039 #endif
00040 
00041 #ifdef USE_TASKBROWSER
00042 #include <taskbrowser/TaskBrowser.hpp>
00043 #endif
00044 #include <deployment/DeploymentComponent.hpp>
00045 #include <iostream>
00046 #include <string>
00047 #include "deployer-funcs.hpp"
00048 
00049 #ifdef  ORO_BUILD_LOGGING
00050 #   ifndef OS_RT_MALLOC
00051 #   warning "Logging needs rtalloc!"
00052 #   endif
00053 #include <log4cpp/HierarchyMaintainer.hh>
00054 #include "logging/Category.hpp"
00055 #endif
00056 
00057 using namespace RTT;
00058 namespace po = boost::program_options;
00059 using namespace std;
00060 
00061 int main(int argc, char** argv)
00062 {
00063         std::string                 siteFile;      // "" means use default in DeploymentComponent.cpp
00064     std::vector<std::string>    scriptFiles;
00065         std::string                 name("Deployer");
00066     bool                        requireNameService = false;         // not used
00067     po::variables_map           vm;
00068         po::options_description     otherOptions;
00069 
00070 #ifdef  ORO_BUILD_RTALLOC
00071     OCL::memorySize             rtallocMemorySize   = ORO_DEFAULT_RTALLOC_SIZE;
00072         po::options_description     rtallocOptions      = OCL::deployerRtallocOptions(rtallocMemorySize);
00073         otherOptions.add(rtallocOptions);
00074 #endif
00075 
00076 #if     defined(ORO_BUILD_LOGGING) && defined(OROSEM_LOG4CPP_LOGGING)
00077     // to support RTT's logging to log4cpp
00078     std::string                 rttLog4cppConfigFile;
00079     po::options_description     rttLog4cppOptions = OCL::deployerRttLog4cppOptions(rttLog4cppConfigFile);
00080     otherOptions.add(rttLog4cppOptions);
00081 #endif
00082 
00083     // were we given non-deployer options? ie find "--"
00084     int     optIndex    = 0;
00085     bool    found       = false;
00086 
00087     while(!found && optIndex<argc)
00088     {
00089         found = (0 == strcmp("--", argv[optIndex]));
00090         if(!found) optIndex++;
00091     }
00092 
00093     if (found) {
00094         argv[optIndex] = argv[0];
00095     }
00096 
00097     // if extra options not found then process all command line options,
00098     // otherwise process all options up to but not including "--"
00099     int rc = OCL::deployerParseCmdLine(!found ? argc : optIndex, argv,
00100                                        siteFile, scriptFiles, name, requireNameService,
00101                                        vm, &otherOptions);
00102 
00103     if (0 != rc)
00104         {
00105                 return rc;
00106         }
00107 
00108 #if     defined(ORO_BUILD_LOGGING) && defined(OROSEM_LOG4CPP_LOGGING)
00109     if (!OCL::deployerConfigureRttLog4cppCategory(rttLog4cppConfigFile))
00110     {
00111         return -1;
00112     }
00113 #endif
00114 
00115 #ifdef  ORO_BUILD_RTALLOC
00116     size_t                  memSize     = rtallocMemorySize.size;
00117     void*                   rtMem       = 0;
00118     size_t                  freeMem     = 0;
00119     if (0 < memSize)
00120     {
00121         // don't calloc() as is first thing TLSF does.
00122         rtMem = malloc(memSize);
00123         assert(0 != rtMem);
00124         freeMem = init_memory_pool(memSize, rtMem);
00125         if ((size_t)-1 == freeMem)
00126         {
00127             cerr << "Invalid memory pool size of " << memSize 
00128                           << " bytes (TLSF has a several kilobyte overhead)." << endl;
00129             free(rtMem);
00130             return -1;
00131         }
00132         cout << "Real-time memory: " << freeMem << " bytes free of "
00133                   << memSize << " allocated." << endl;
00134     }
00135 #endif  // ORO_BUILD_RTALLOC
00136 
00137 #ifdef  ORO_BUILD_LOGGING
00138     log4cpp::HierarchyMaintainer::set_category_factory(
00139         OCL::logging::Category::createOCLCategory);
00140 #endif
00141 
00142 
00143     /******************** WARNING ***********************
00144      *   NO log(...) statements before __os_init() !!!!! 
00145      ***************************************************/
00146 
00147     // start Orocos _AFTER_ setting up log4cpp
00148         if (0 == __os_init(argc - optIndex, &argv[optIndex]))
00149     {
00150 #ifdef  ORO_BUILD_LOGGING
00151         log(Info) << "OCL factory set for real-time logging" << endlog();
00152 #endif
00153         // scope to force dc destruction prior to memory free
00154         {
00155             OCL::DeploymentComponent dc( name, siteFile );
00156 
00157             for (std::vector<std::string>::const_iterator iter=scriptFiles.begin();
00158                  iter!=scriptFiles.end();
00159                  ++iter)
00160             {
00161                 if ( !(*iter).empty() )
00162                 {
00163                     if ( (*iter).rfind(".xml",std::string::npos) == (*iter).length() - 4 || (*iter).rfind(".cpf",std::string::npos) == (*iter).length() - 4) {
00164                         dc.kickStart( (*iter) );
00165                         continue;
00166                     } if ( (*iter).rfind(".ops",std::string::npos) == (*iter).length() - 4 || (*iter).rfind(".osd",std::string::npos) == (*iter).length() - 4) {
00167                         dc.runScript( (*iter) );
00168                         continue;
00169                     }
00170                     log(Error) << "Unknown extension of file: '"<< (*iter) <<"'. Must be xml, cpf for XML files or, ops or osd for script files."<<endlog();
00171                 }
00172             }
00173 #ifdef USE_TASKBROWSER
00174             // We don't start an interactive console when we're a daemon
00175             if ( !vm.count("daemon") ) {
00176                 OCL::TaskBrowser tb( &dc );
00177 
00178                 tb.loop();
00179 
00180                 dc.shutdownDeployment();
00181             }
00182 #endif
00183         }
00184 #ifdef  ORO_BUILD_RTALLOC
00185         if (0 != rtMem)
00186             {
00187                 // print statistics after deployment finished, but before os_exit() (needs Logger):
00188                 log(Debug) << "TLSF bytes allocated=" << memSize
00189                            << " overhead=" << (memSize - freeMem)
00190                            << " max-used=" << get_max_size(rtMem)
00191                            << " currently-used=" << get_used_size(rtMem)
00192                            << " still-allocated=" << (get_used_size(rtMem) - (memSize - freeMem))
00193                            << endlog();
00194             }
00195 #endif
00196 
00197         __os_exit();
00198         rc = 0;
00199         }
00200         else
00201         {
00202                 std::cerr << "Unable to start Orocos" << std::endl;
00203         rc = -1;
00204         }
00205 
00206 #ifdef  ORO_BUILD_LOGGING
00207     log4cpp::HierarchyMaintainer::getDefaultMaintainer().shutdown();
00208     log4cpp::HierarchyMaintainer::getDefaultMaintainer().deleteAllCategories();
00209 #endif
00210 
00211 #ifdef  ORO_BUILD_RTALLOC
00212     if (0 != rtMem)
00213     {
00214         std::cout << "TLSF bytes allocated=" << memSize
00215                   << " overhead=" << (memSize - freeMem)
00216                   << " max-used=" << get_max_size(rtMem)
00217                   << " currently-used=" << get_used_size(rtMem)
00218                   << " still-allocated=" << (get_used_size(rtMem) - (memSize - freeMem))
00219                   << "\n";
00220 
00221         destroy_memory_pool(rtMem);
00222         free(rtMem);
00223     }
00224 #endif
00225 
00226     return rc;
00227 }


ocl
Author(s): OCL Development Team
autogenerated on Mon Oct 6 2014 03:16:36