$treeview $search $mathjax
SEvMgr Logo  1.00.2
$projectbrief
$projectbrief
$searchbox

pysevmgr.cpp

Go to the documentation of this file.
00001 // STL
00002 #include <cassert>
00003 #include <stdexcept>
00004 #include <fstream>
00005 #include <sstream>
00006 #include <string>
00007 #include <list>
00008 #include <vector>
00009 // Boost String
00010 #include <boost/python.hpp>
00011 // StdAir
00012 #include <stdair/stdair_basic_types.hpp>
00013 #include <stdair/stdair_exceptions.hpp>
00014 #include <stdair/basic/BasFileMgr.hpp>
00015 #include <stdair/basic/BasLogParams.hpp>
00016 #include <stdair/basic/BasDBParams.hpp>
00017 // SEvMgr
00018 #include <sevmgr/SEVMGR_Service.hpp>
00019 
00020 namespace SEVMGR {
00021 
00022   struct PYEventQueueManager {
00023   public:
00025     std::string sevmgr() {
00026       std::ostringstream oStream;
00027 
00028       // Sanity check
00029       if (_logOutputStream == NULL) {
00030         oStream << "The log filepath is not valid." << std::endl;
00031         return oStream.str();
00032       }
00033       assert (_logOutputStream != NULL);
00034       
00035       try {
00036 
00037         // DEBUG
00038         *_logOutputStream << "Default service" << std::endl;
00039       
00040         if (_sevmgrService == NULL) {
00041           oStream << "The Sevmgr service has not been initialised, "
00042                   << "i.e., the init() method has not been called "
00043                   << "correctly on the PYEventQueueManager object. Please "
00044                   << "check that all the parameters are not empty and "
00045                   << "point to actual files.";
00046           *_logOutputStream << oStream.str();
00047           return oStream.str();
00048         }
00049         assert (_sevmgrService != NULL);
00050         
00051         // Do the sevmgr
00052         _sevmgrService->buildSampleBom();
00053 
00054         // DEBUG
00055         *_logOutputStream << "Default service returned" << std::endl;
00056 
00057         // DEBUG
00058         *_logOutputStream << "Sevmgr output: " << oStream.str() << std::endl;
00059 
00060       } catch (const stdair::RootException& eSevmgrError) {
00061         *_logOutputStream << "Sevmgr error: "  << eSevmgrError.what()
00062                           << std::endl;
00063         
00064       } catch (const std::exception& eStdError) {
00065         *_logOutputStream << "Error: "  << eStdError.what() << std::endl;
00066         
00067       } catch (...) {
00068         *_logOutputStream << "Unknown error" << std::endl;
00069       }
00070 
00071       return oStream.str();
00072     }
00073 
00074   public:
00076     PYEventQueueManager() : _sevmgrService (NULL), _logOutputStream (NULL) {
00077     }
00078     
00080     PYEventQueueManager (const PYEventQueueManager& iPYEventQueueManager)
00081       : _sevmgrService (iPYEventQueueManager._sevmgrService),
00082         _logOutputStream (iPYEventQueueManager._logOutputStream) {
00083     }
00084 
00086     ~PYEventQueueManager() {
00087       _sevmgrService = NULL;
00088       _logOutputStream = NULL;
00089     }
00090     
00092     bool init (const std::string& iLogFilepath,
00093                const std::string& iDBUser, const std::string& iDBPasswd,
00094                const std::string& iDBHost, const std::string& iDBPort,
00095                const std::string& iDBDBName) {
00096       bool isEverythingOK = true;
00097 
00098       try {
00099         
00100         // Check that the file path given as input corresponds to an actual file
00101         const bool isWriteable = (iLogFilepath.empty() == false);
00102         // stdair::BasFileMgr::isWriteable (iLogFilepath);
00103         if (isWriteable == false) {
00104           isEverythingOK = false;
00105           return isEverythingOK;
00106         }
00107         
00108         // Set the log parameters
00109         _logOutputStream = new std::ofstream;
00110         assert (_logOutputStream != NULL);
00111         
00112         // Open and clean the log outputfile
00113         _logOutputStream->open (iLogFilepath.c_str());
00114         _logOutputStream->clear();
00115         
00116         // DEBUG
00117         *_logOutputStream << "Python wrapper initialisation" << std::endl;
00118         const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG,
00119                                                *_logOutputStream);
00120         
00121         // Initialise the context
00122         stdair::BasDBParams lDBParams (iDBUser, iDBPasswd, iDBHost, iDBPort,
00123                                        iDBDBName);
00124         _sevmgrService = new SEVMGR_Service (lLogParams, lDBParams);
00125         
00126         // DEBUG
00127         *_logOutputStream << "Python wrapper initialised" << std::endl;
00128         
00129       } catch (const stdair::RootException& eSevmgrError) {
00130         *_logOutputStream << "Sevmgr error: "  << eSevmgrError.what()
00131                           << std::endl;
00132         
00133       } catch (const std::exception& eStdError) {
00134         *_logOutputStream << "Error: "  << eStdError.what() << std::endl;
00135         
00136       } catch (...) {
00137         *_logOutputStream << "Unknown error" << std::endl;
00138       }
00139       
00140       return isEverythingOK;
00141     }
00142 
00143   private:
00145     SEVMGR_Service* _sevmgrService;
00146     std::ofstream* _logOutputStream;
00147   };
00148 
00149 }
00150 
00151 // /////////////////////////////////////////////////////////////
00152 BOOST_PYTHON_MODULE(libpysevmgr) {
00153   boost::python::class_<SEVMGR::PYEventQueueManager> ("PYEventQueueManager")
00154     .def ("sevmgr", &SEVMGR::PYEventQueueManager::sevmgr)
00155     .def ("init", &SEVMGR::PYEventQueueManager::init);
00156 }