UniSet  2.6.0
DBServer_SQLite.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 // --------------------------------------------------------------------------
00020 // --------------------------------------------------------------------------
00021 #ifndef DBServer_SQLite_H_
00022 #define DBServer_SQLite_H_
00023 // --------------------------------------------------------------------------
00024 #include <unordered_map>
00025 #include <queue>
00026 #include "UniSetTypes.h"
00027 #include "SQLiteInterface.h"
00028 #include "DBServer.h"
00029 // -------------------------------------------------------------------------
00030 namespace uniset
00031 {
00032 //------------------------------------------------------------------------------------------
00137 class DBServer_SQLite:
00138     public DBServer
00139 {
00140     public:
00141         DBServer_SQLite( uniset::ObjectId id, const std::string& prefix );
00142         explicit DBServer_SQLite( const std::string& prefix );
00143         virtual ~DBServer_SQLite();
00144 
00146         static std::shared_ptr<DBServer_SQLite> init_dbserver( int argc, const char* const* argv, const std::string& prefix = "sqlite" );
00147 
00149         static void help_print( int argc, const char* const* argv );
00150 
00151         inline std::shared_ptr<LogAgregator> logAggregator()
00152         {
00153             return loga;
00154         }
00155         inline std::shared_ptr<DebugStream> log()
00156         {
00157             return dblog;
00158         }
00159 
00160     protected:
00161         typedef std::unordered_map<int, std::string> DBTableMap;
00162 
00163         virtual void initDBServer() override;
00164         virtual void initDB( std::shared_ptr<SQLiteInterface>& db ) {};
00165         virtual void initDBTableMap(DBTableMap& tblMap) {};
00166 
00167         virtual void timerInfo( const uniset::TimerMessage* tm ) override;
00168         virtual void sysCommand( const uniset::SystemMessage* sm ) override;
00169         virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
00170         virtual void confirmInfo( const uniset::ConfirmMessage* cmsg ) override;
00171 
00172         bool writeToBase( const string& query );
00173         void createTables( SQLiteInterface* db );
00174 
00175         inline std::string tblName(int key)
00176         {
00177             return tblMap[key];
00178         }
00179 
00180         enum Timers
00181         {
00182             PingTimer,        
00183             ReconnectTimer,   
00184             lastNumberOfTimer
00185         };
00186 
00187 
00188         std::shared_ptr<SQLiteInterface> db;
00189         int PingTime;
00190         int ReconnectTime;
00191         bool connect_ok;     
00193         bool activate;
00194 
00195         typedef std::queue<std::string> QueryBuffer;
00196 
00197         QueryBuffer qbuf;
00198         unsigned int qbufSize; // размер буфера сообщений.
00199         bool lastRemove;
00200 
00201         void flushBuffer();
00202         uniset::uniset_rwmutex mqbuf;
00203 
00204     private:
00205         DBTableMap tblMap;
00206 
00207 };
00208 // ----------------------------------------------------------------------------------
00209 } // end of namespace uniset
00210 //------------------------------------------------------------------------------------------
00211 #endif