UniSet  2.2.1
DBServer_MySQL.h
См. документацию.
00001 /* This file is part of the UniSet project
00002  * Copyright (c) 2002 Free Software Foundation, Inc.
00003  * Copyright (c) 2002 Pavel Vainerman
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 // --------------------------------------------------------------------------
00023 // --------------------------------------------------------------------------
00024 #ifndef DBServer_MySQL_H_
00025 #define DBServer_MySQL_H_
00026 // --------------------------------------------------------------------------
00027 #include <unordered_map>
00028 #include <queue>
00029 #include "UniSetTypes.h"
00030 #include "MySQLInterface.h"
00031 #include "DBServer.h"
00032 //------------------------------------------------------------------------------------------
00134 class DBServer_MySQL:
00135     public DBServer
00136 {
00137     public:
00138         DBServer_MySQL( UniSetTypes::ObjectId id, const std::string& prefix );
00139         explicit DBServer_MySQL( const std::string& prefix );
00140         virtual ~DBServer_MySQL();
00141 
00143         static std::shared_ptr<DBServer_MySQL> init_dbserver( int argc, const char* const* argv, const std::string& prefix = "mysql" );
00144 
00146         static void help_print( int argc, const char* const* argv );
00147 
00148         inline std::shared_ptr<LogAgregator> logAggregator()
00149         {
00150             return loga;
00151         }
00152         inline std::shared_ptr<DebugStream> log()
00153         {
00154             return dblog;
00155         }
00156 
00157     protected:
00158         typedef std::unordered_map<int, std::string> DBTableMap;
00159 
00160         virtual void initDBServer() override;
00161         virtual void initDB( std::shared_ptr<MySQLInterface>& db ) {};
00162         virtual void initDBTableMap(DBTableMap& tblMap) {};
00163 
00164         virtual void timerInfo( const UniSetTypes::TimerMessage* tm ) override;
00165         virtual void sysCommand( const UniSetTypes::SystemMessage* sm ) override;
00166         virtual void sensorInfo( const UniSetTypes::SensorMessage* sm ) override;
00167         virtual void confirmInfo( const UniSetTypes::ConfirmMessage* cmsg ) override;
00168 
00169         bool writeToBase( const string& query );
00170         void createTables( MySQLInterface* db );
00171 
00172         inline std::string tblName( int key )
00173         {
00174             return tblMap[key].c_str();
00175         }
00176 
00177         enum Timers
00178         {
00179             PingTimer,        
00180             ReconnectTimer,   
00181             lastNumberOfTimer
00182         };
00183 
00184 
00185         std::shared_ptr<MySQLInterface> db;
00186         int PingTime;
00187         int ReconnectTime;
00188         bool connect_ok;     
00190         bool activate;
00191 
00192         typedef std::queue<std::string> QueryBuffer;
00193 
00194         QueryBuffer qbuf;
00195         unsigned int qbufSize; // размер буфера сообщений.
00196         bool lastRemove;
00197 
00198         void flushBuffer();
00199         UniSetTypes::uniset_rwmutex mqbuf;
00200 
00201     private:
00202         DBTableMap tblMap;
00203 
00204 };
00205 //------------------------------------------------------------------------------------------
00206 #endif