deployer-corba.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Jul 3 15:30:24 CEST 2008  deployer-corba.cpp
00003 
00004                         deployer-corba.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 
00028 #include <rtt/rtt-config.h>
00029 #ifdef OS_RT_MALLOC
00030 // need access to all TLSF functions embedded in RTT
00031 #define ORO_MEMORY_POOL
00032 #include <rtt/os/tlsf/tlsf.h>
00033 #endif
00034 #include <rtt/os/main.h>
00035 #include <rtt/RTT.hpp>
00036 
00037 #include <taskbrowser/TaskBrowser.hpp>
00038 #include <deployment/CorbaDeploymentComponent.hpp>
00039 #include <rtt/transports/corba/TaskContextServer.hpp>
00040 #include <iostream>
00041 #include "deployer-funcs.hpp"
00042 
00043 #include <rtt/transports/corba/corba.h>
00044 
00045 #ifdef  ORO_BUILD_LOGGING
00046 #   ifndef OS_RT_MALLOC
00047 #   warning "Logging needs rtalloc!"
00048 #   endif
00049 #include <log4cpp/HierarchyMaintainer.hh>
00050 #include "logging/Category.hpp"
00051 #endif
00052 
00053 using namespace std;
00054 using namespace RTT;
00055 using namespace RTT::corba;
00056 namespace po = boost::program_options;
00057 
00058 int main(int argc, char** argv)
00059 {
00060         std::string                 siteFile;      // "" means use default in DeploymentComponent.cpp
00061     std::vector<std::string>    scriptFiles;
00062         std::string                 name("Deployer");
00063     bool                        requireNameService = false;
00064     bool                        deploymentOnlyChecked = false;
00065         int                                                     minNumberCPU = 0;
00066         po::variables_map           vm;
00067         po::options_description     taoOptions("Additional options can also be passed to TAO");
00068         // we don't actually list any options for TAO ...
00069 
00070         po::options_description     otherOptions;
00071 
00072 #ifdef  ORO_BUILD_RTALLOC
00073     OCL::memorySize         rtallocMemorySize   = ORO_DEFAULT_RTALLOC_SIZE;
00074         po::options_description rtallocOptions      = OCL::deployerRtallocOptions(rtallocMemorySize);
00075         otherOptions.add(rtallocOptions);
00076 #endif
00077 
00078 #if     defined(ORO_BUILD_LOGGING) && defined(OROSEM_LOG4CPP_LOGGING)
00079     // to support RTT's logging to log4cpp
00080     std::string                 rttLog4cppConfigFile;
00081     po::options_description     rttLog4cppOptions = OCL::deployerRttLog4cppOptions(rttLog4cppConfigFile);
00082     otherOptions.add(rttLog4cppOptions);
00083 #endif
00084 
00085     // as last set of options
00086     otherOptions.add(taoOptions);
00087 
00088     // were we given TAO options? ie find "--"
00089     int     taoIndex    = 0;
00090     bool    found       = false;
00091 
00092     while(!found && taoIndex<argc)
00093     {
00094         found = (0 == strcmp("--", argv[taoIndex]));
00095         if(!found) taoIndex++;
00096     }
00097 
00098     if (found) {
00099         argv[taoIndex] = argv[0];
00100     }
00101 
00102     // if TAO options not found then process all command line options,
00103     // otherwise process all options up to but not including "--"
00104         int rc = OCL::deployerParseCmdLine(!found ? argc : taoIndex, argv,
00105                                        siteFile, scriptFiles, name, requireNameService,deploymentOnlyChecked,
00106                                                                            minNumberCPU,
00107                                        vm, &otherOptions);
00108         if (0 != rc)
00109         {
00110                 return rc;
00111         }
00112 
00113 
00114         // check system capabilities
00115         rc = OCL::enforceMinNumberCPU(minNumberCPU);
00116         if (0 != rc)
00117         {
00118                 return rc;
00119         }
00120 
00121 #if     defined(ORO_BUILD_LOGGING) && defined(OROSEM_LOG4CPP_LOGGING)
00122     if (!OCL::deployerConfigureRttLog4cppCategory(rttLog4cppConfigFile))
00123     {
00124         return -1;
00125     }
00126 #endif
00127 
00128 #ifdef  ORO_BUILD_RTALLOC
00129     size_t                  memSize     = rtallocMemorySize.size;
00130     void*                   rtMem       = 0;
00131     if (0 < memSize)
00132     {
00133         // don't calloc() as is first thing TLSF does.
00134         rtMem = malloc(memSize);
00135         assert(rtMem);
00136         size_t freeMem = init_memory_pool(memSize, rtMem);
00137         if ((size_t)-1 == freeMem)
00138         {
00139             cerr << "Invalid memory pool size of " << memSize
00140                           << " bytes (TLSF has a several kilobyte overhead)." << endl;
00141             free(rtMem);
00142             return -1;
00143         }
00144         cout << "Real-time memory: " << freeMem << " bytes free of "
00145                   << memSize << " allocated." << endl;
00146     }
00147 #endif  // ORO_BUILD_RTALLOC
00148 
00149 #ifdef  ORO_BUILD_LOGGING
00150     // use our log4cpp-derived categories to do real-time logging
00151     log4cpp::HierarchyMaintainer::set_category_factory(
00152         OCL::logging::Category::createOCLCategory);
00153 #endif
00154 
00155     // start Orocos _AFTER_ setting up log4cpp
00156         if (0 == __os_init(argc - taoIndex, &argv[taoIndex]))
00157     {
00158 #ifdef  ORO_BUILD_LOGGING
00159         log(Info) << "OCL factory set for real-time logging" << endlog();
00160 #endif
00161         rc = -1;     // prove otherwise
00162         try {
00163             // if TAO options not found then have TAO process just the program name,
00164             // otherwise TAO processes the program name plus all options (potentially
00165             // none) after "--"
00166             TaskContextServer::InitOrb( argc - taoIndex, &argv[taoIndex] );
00167 
00168             OCL::CorbaDeploymentComponent dc( name, siteFile );
00169 
00170             if (0 == TaskContextServer::Create( &dc, true, requireNameService ))
00171                 {
00172                     return -1;
00173                 }
00174 
00175             // The orb thread accepts incomming CORBA calls.
00176             TaskContextServer::ThreadOrb();
00177 
00178             /* Only start the scripts after the Orb was created. Processing of
00179                scripts stops after the first failed script, and -1 is returned.
00180                Whether a script failed or all scripts succeeded, in non-daemon
00181                and non-checking mode the TaskBrowser will be run to allow
00182                inspection.
00183              */
00184             bool result = true;
00185             for (std::vector<std::string>::const_iterator iter=scriptFiles.begin();
00186                  iter!=scriptFiles.end() && result;
00187                  ++iter)
00188             {
00189                 if ( !(*iter).empty() )
00190                 {
00191                     if ( (*iter).rfind(".xml",string::npos) == (*iter).length() - 4 || (*iter).rfind(".cpf",string::npos) == (*iter).length() - 4) {
00192                         if ( deploymentOnlyChecked ) {
00193                             if (!dc.loadComponents( (*iter) )) {
00194                                 result = false;
00195                                 log(Error) << "Failed to load file: '"<< (*iter) <<"'." << endlog();
00196                             } else if (!dc.configureComponents()) {
00197                                 result = false;
00198                                 log(Error) << "Failed to configure file: '"<< (*iter) <<"'." << endlog();
00199                             }
00200                             // else leave result=true and continue
00201                         } else {
00202                             result = dc.kickStart( (*iter) );
00203                         }
00204                         continue;
00205                     }
00206 
00207                     if ( (*iter).rfind(".ops",string::npos) == (*iter).length() - 4 ||
00208                          (*iter).rfind(".osd",string::npos) == (*iter).length() - 4 ||
00209                          (*iter).rfind(".lua",string::npos) == (*iter).length() - 4) {
00210                         result = dc.runScript( (*iter) );
00211                         continue;
00212                     }
00213                     log(Error) << "Unknown extension of file: '"<< (*iter) <<"'. Must be xml, cpf for XML files or, ops, osd or lua for script files."<<endlog();
00214                 }
00215             }
00216             rc = (result ? 0 : -1);
00217 
00218             if ( !deploymentOnlyChecked && !vm.count("daemon") ) {
00219                  OCL::TaskBrowser tb( &dc );
00220                  tb.loop();
00221 
00222                  // do it while CORBA is still up in case need to do anything remote.
00223                  dc.shutdownDeployment();
00224             }
00225 
00226             TaskContextServer::ShutdownOrb();
00227 
00228             TaskContextServer::DestroyOrb();
00229 
00230 
00231         } catch( CORBA::Exception &e ) {
00232             log(Error) << argv[0] <<" ORO_main : CORBA exception raised!" << Logger::nl;
00233             log() << CORBA_EXCEPTION_INFO(e) << endlog();
00234         } catch (...) {
00235             // catch this so that we can destroy the TLSF memory correctly
00236             log(Error) << "Uncaught exception." << endlog();
00237         }
00238 
00239                 // shutdown Orocos
00240                 __os_exit();
00241         }
00242         else
00243         {
00244                 std::cerr << "Unable to start Orocos" << std::endl;
00245         rc = -1;
00246         }
00247 
00248 #ifdef  ORO_BUILD_RTALLOC
00249     if (!rtMem)
00250     {
00251         destroy_memory_pool(rtMem);
00252         free(rtMem);
00253     }
00254 #endif  // ORO_BUILD_RTALLOC
00255 
00256     return rc;
00257 }


ocl
Author(s): OCL Development Team
autogenerated on Sat Jun 8 2019 18:48:54