UniSet  2.2.1
DBServer_PostgreSQL.h
00001 #ifndef DBServer_PostgreSQL_H_
00002 #define DBServer_PostgreSQL_H_
00003 // --------------------------------------------------------------------------
00004 #include <unordered_map>
00005 #include <queue>
00006 #include "UniSetTypes.h"
00007 #include "PostgreSQLInterface.h"
00008 #include "DBServer.h"
00009 //------------------------------------------------------------------------------------------
00010 class DBServer_PostgreSQL:
00011     public DBServer
00012 {
00013     public:
00014         DBServer_PostgreSQL( UniSetTypes::ObjectId id, const std::string& prefix );
00015         DBServer_PostgreSQL();
00016         virtual ~DBServer_PostgreSQL();
00017 
00019         static std::shared_ptr<DBServer_PostgreSQL> init_dbserver( int argc, const char* const* argv, const std::string& prefix = "pgsql" );
00020 
00022         static void help_print( int argc, const char* const* argv );
00023 
00024         inline std::shared_ptr<LogAgregator> logAggregator()
00025         {
00026             return loga;
00027         }
00028         inline std::shared_ptr<DebugStream> log()
00029         {
00030             return dblog;
00031         }
00032 
00033     protected:
00034         typedef std::unordered_map<int, std::string> DBTableMap;
00035 
00036         virtual void initDBServer() override;
00037         virtual void initDB( std::shared_ptr<PostgreSQLInterface>& db ) {};
00038         virtual void initDBTableMap(DBTableMap& tblMap) {};
00039 
00040         virtual void timerInfo( const UniSetTypes::TimerMessage* tm ) override;
00041         virtual void sysCommand( const UniSetTypes::SystemMessage* sm ) override;
00042         virtual void sensorInfo( const UniSetTypes::SensorMessage* sm ) override;
00043         virtual void confirmInfo( const UniSetTypes::ConfirmMessage* cmsg ) override;
00044         virtual void sigterm( int signo ) override;
00045 
00046         bool writeToBase( const string& query );
00047         void createTables( std::shared_ptr<PostgreSQLInterface>& db );
00048 
00049         inline std::string tblName(int key)
00050         {
00051             return tblMap[key];
00052         }
00053 
00054         enum Timers
00055         {
00056             PingTimer,        
00057             ReconnectTimer,   
00058             lastNumberOfTimer
00059         };
00060 
00061         std::shared_ptr<PostgreSQLInterface> db;
00062         int PingTime;
00063         int ReconnectTime;
00064         bool connect_ok;     
00066         bool activate;
00067 
00068         typedef std::queue<std::string> QueryBuffer;
00069 
00070         QueryBuffer qbuf;
00071         unsigned int qbufSize; // размер буфера сообщений.
00072         bool lastRemove;
00073 
00074         void flushBuffer();
00075         UniSetTypes::uniset_mutex mqbuf;
00076 
00077     private:
00078         DBTableMap tblMap;
00079 
00080 };
00081 //------------------------------------------------------------------------------------------
00082 #endif