UniSet  2.6.0
Configuration.h
00001 /*
00002  * Copyright (c) 2015 Pavel Vainerman.
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, version 2.1.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00011  * Lesser General Lesser Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 // --------------------------------------------------------------------------
00021 // --------------------------------------------------------------------------
00022 #ifndef Configuration_H_
00023 #define Configuration_H_
00024 // --------------------------------------------------------------------------
00025 #include <memory>
00026 #include <string>
00027 #include <ostream>
00028 #include "UniXML.h"
00029 #include "UniSetTypes.h"
00030 #include "ObjectIndex.h"
00031 #include "PassiveTimer.h" // for timeout_t
00032 #include "IORFile.h"
00033 #include "Debug.h"
00034 // --------------------------------------------------------------------------
00035 /*
00036     В функции main нужно обязательно вызывать uniset::uniset_init(argc,argv);
00037 */
00038 namespace uniset
00039 {
00045 class Configuration
00046 {
00047     public:
00048         virtual ~Configuration();
00049 
00050         static std::ostream& help(std::ostream& os);
00051 
00053         Configuration( int argc, const char* const* argv, const std::string& xmlfile = "" );
00054 
00056         Configuration( int argc, const char* const* argv, std::shared_ptr<ObjectIndex> oind, const std::string& xmlfile = "" );
00057 
00059         Configuration( int argc, const char* const* argv,
00060                        const std::string& fileConf, uniset::ObjectInfo* objectsMap );
00061 
00063         std::string getField(const std::string& path) const noexcept;
00065         int getIntField(const std::string& path) const noexcept;
00067         int getPIntField(const std::string& path, int def) const noexcept;
00068 
00069         xmlNode* findNode(xmlNode* node, const std::string& searchnode, const std::string& name = "" ) const noexcept;
00070 
00071         // Получить узел
00072         xmlNode* getNode(const std::string& path) const noexcept;
00073 
00074         // Получить указанное свойство пути
00075         std::string getProp(xmlNode*, const std::string& name) const noexcept;
00076         int getIntProp(xmlNode*, const std::string& name) const noexcept;
00077         int getPIntProp(xmlNode*, const std::string& name, int def) const noexcept;
00078 
00079         // Получить указанное свойство по имени узла
00080         std::string getPropByNodeName(const std::string& nodename, const std::string& prop) const noexcept;
00081 
00082         std::string getRootDir() const noexcept; 
00083         int getArgc() const noexcept;
00084         const char* const* getArgv() const noexcept;
00085         ObjectId getDBServer() const noexcept;
00086         ObjectId getLocalNode() const noexcept;
00087         std::string getLocalNodeName() const noexcept;
00088         const std::string getNSName() const noexcept;
00089 
00090         // repository
00091         std::string getRootSection() const noexcept;
00092         std::string getSensorsSection() const noexcept;
00093         std::string getObjectsSection() const noexcept;
00094         std::string getControllersSection() const noexcept;
00095         std::string getServicesSection() const noexcept;
00096         // xml
00097         xmlNode* getXMLSensorsSection() noexcept;
00098         xmlNode* getXMLObjectsSection() noexcept;
00099         xmlNode* getXMLControllersSection() noexcept;
00100         xmlNode* getXMLServicesSection()  noexcept;
00101         xmlNode* getXMLNodesSection() noexcept;
00102         xmlNode* getXMLObjectNode( uniset::ObjectId ) const noexcept;
00103 
00104         UniversalIO::IOType getIOType( uniset::ObjectId ) const noexcept;
00105         UniversalIO::IOType getIOType( const std::string& name ) const noexcept;
00106 
00107         // net
00108         size_t getCountOfNet() const noexcept;
00109         size_t getRepeatTimeout() const noexcept;
00110         size_t getRepeatCount() const noexcept;
00111 
00112         uniset::ObjectId getSensorID( const std::string& name ) const noexcept;
00113         uniset::ObjectId getControllerID( const std::string& name ) const noexcept;
00114         uniset::ObjectId getObjectID( const std::string& name ) const noexcept;
00115         uniset::ObjectId getServiceID( const std::string& name ) const noexcept;
00116         uniset::ObjectId getNodeID( const std::string& name ) const noexcept;
00117 
00118         const std::string getConfFileName() const noexcept;
00119         std::string getImagesDir() const noexcept;
00120 
00121         timeout_t getHeartBeatTime() const noexcept;
00122 
00123         // dirs
00124         const std::string getConfDir() const noexcept;
00125         const std::string getDataDir() const noexcept;
00126         const std::string getBinDir() const noexcept;
00127         const std::string getLogDir() const noexcept;
00128         const std::string getLockDir() const noexcept;
00129         const std::string getDocDir() const noexcept;
00130 
00131         bool isLocalIOR() const noexcept;
00132         bool isTransientIOR() const noexcept;
00133 
00135         std::string getArgParam(const std::string& name, const std::string& defval = "") const noexcept;
00136 
00138         std::string getArg2Param(const std::string& name, const std::string& defval, const std::string& defval2 = "") const noexcept;
00139 
00141         int getArgInt(const std::string& name, const std::string& defval = "") const noexcept;
00142 
00144         int getArgPInt(const std::string& name, int defval) const noexcept;
00145         int getArgPInt(const std::string& name, const std::string& strdefval, int defval) const noexcept;
00146 
00147         xmlNode* initLogStream( DebugStream& deb, const std::string& nodename ) noexcept;
00148         xmlNode* initLogStream( std::shared_ptr<DebugStream> deb, const std::string& nodename ) noexcept;
00149         xmlNode* initLogStream( DebugStream* deb, const std::string& nodename ) noexcept;
00150 
00151         uniset::ListOfNode::const_iterator listNodesBegin() const noexcept;
00152         uniset::ListOfNode::const_iterator listNodesEnd() const noexcept;
00153 
00155         std::shared_ptr<ObjectIndex> oind;
00156 
00158         std::shared_ptr<IORFile> iorfile;
00159 
00161         const std::shared_ptr<UniXML> getConfXML() const noexcept;
00162 
00163         CORBA::ORB_ptr getORB() const;
00164         const CORBA::PolicyList getPolicy() const noexcept;
00165 
00166     protected:
00167         Configuration();
00168 
00169         virtual void initConfiguration(int argc, const char* const* argv);
00170 
00171         void createNodesList();
00172         virtual void initNode( uniset::NodeInfo& ninfo, UniXML::iterator& it) noexcept;
00173 
00174         void initRepSections();
00175         std::string getRepSectionName(const std::string& sec, xmlNode* secnode = 0 );
00176         void setConfFileName( const std::string& fn = "" );
00177         void initParameters();
00178         void setLocalNode( const std::string& nodename );
00179 
00180         std::string getPort( const std::string& port = "" ) const noexcept;
00181 
00182         std::string rootDir = { "" };
00183         std::shared_ptr<UniXML> unixml;
00184 
00185         int _argc = { 0 };
00186         const char** _argv = { nullptr };
00187         CORBA::ORB_var orb;
00188         CORBA::PolicyList policyList;
00189 
00190         std::string NSName = { "" };  
00191         size_t countOfNet = { 1 };    
00192         size_t repeatCount = { 3 };   
00195         timeout_t repeatTimeout = { 50 };    
00197         uniset::ListOfNode lnodes;
00198 
00199         // repository
00200         std::string secRoot = { "" };
00201         std::string secSensors = { "" };
00202         std::string secObjects = { "" };
00203         std::string secControlles = { "" };
00204         std::string secServices = { "" };
00205 
00206         // xml
00207         xmlNode* xmlSensorsSec = { 0 };
00208         xmlNode* xmlObjectsSec = { 0 };
00209         xmlNode* xmlControllersSec = { 0 };
00210         xmlNode* xmlServicesSec = { 0 };
00211         xmlNode* xmlNodesSec = { 0 };
00212 
00213         ObjectId localDBServer = { uniset::DefaultObjectId };
00214         ObjectId localNode = { uniset::DefaultObjectId };
00215 
00216         std::string localNodeName = { "" };
00217         std::string fileConfName = { "" };
00218         std::string imagesDir = { "" };
00219 
00220         std::string confDir = { "" };
00221         std::string dataDir = { "" };
00222         std::string binDir = { "" };
00223         std::string logDir = { "" };
00224         std::string docDir = { "" };
00225         std::string lockDir = { "" };
00226         bool localIOR = { false };
00227         bool transientIOR = { false };
00228 
00229         timeout_t heartbeat_msec = { 3000 };
00230 };
00231 
00233 std::shared_ptr<Configuration> uniset_conf() noexcept;
00234 
00236 std::shared_ptr<DebugStream> ulog() noexcept;
00237 
00239 std::shared_ptr<Configuration> uniset_init( int argc, const char* const* argv, const std::string& xmlfile = "configure.xml" );
00240 // --------------------------------------------------------------------------
00241 }    // end of uniset namespace
00242 // --------------------------------------------------------------------------
00243 // "синтаксический сахар"..для логов
00244 #define uinfo if( uniset::ulog()->debugging(Debug::INFO) ) uniset::ulog()->info()
00245 #define uwarn if( uniset::ulog()->debugging(Debug::WARN) ) uniset::ulog()->warn()
00246 #define ucrit if( uniset::ulog()->debugging(Debug::CRIT) ) uniset::ulog()->crit()
00247 #define ulog1 if( uniset::ulog()->debugging(Debug::LEVEL1) ) uniset::ulog()->level1()
00248 #define ulog2 if( uniset::ulog()->debugging(Debug::LEVEL2) ) uniset::ulog()->level2()
00249 #define ulog3 if( uniset::ulog()->debugging(Debug::LEVEL3) ) uniset::ulog()->level3()
00250 #define ulog4 if( uniset::ulog()->debugging(Debug::LEVEL4) ) uniset::ulog()->level4()
00251 #define ulog5 if( uniset::ulog()->debugging(Debug::LEVEL5) ) uniset::ulog()->level5()
00252 #define ulog6 if( uniset::ulog()->debugging(Debug::LEVEL6) ) uniset::ulog()->level6()
00253 #define ulog7 if( uniset::ulog()->debugging(Debug::LEVEL7) ) uniset::ulog()->level7()
00254 #define ulog8 if( uniset::ulog()->debugging(Debug::LEVEL8) ) uniset::ulog()->level8()
00255 #define ulog9 if( uniset::ulog()->debugging(Debug::LEVEL9) ) uniset::ulog()->level9()
00256 #define ulogsys if( uniset::ulog()->debugging(Debug::SYSTEM) ) uniset::ulog()->system()
00257 #define ulogrep if( uniset::ulog()->debugging(Debug::REPOSITORY) ) uniset::ulog()->repository()
00258 #define ulogany uniset::ulog()->any()
00259 // --------------------------------------------------------------------------
00260 #endif // Configuration_H_