UniSet  2.8.0
DBServer_SQLite.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // --------------------------------------------------------------------------
20 // --------------------------------------------------------------------------
21 #ifndef DBServer_SQLite_H_
22 #define DBServer_SQLite_H_
23 // --------------------------------------------------------------------------
24 #include <unordered_map>
25 #include <queue>
26 #include "UniSetTypes.h"
27 #include "SQLiteInterface.h"
28 #include "DBServer.h"
29 // -------------------------------------------------------------------------
30 namespace uniset
31 {
32  //------------------------------------------------------------------------------------------
151  public DBServer
152  {
153  public:
154  DBServer_SQLite( uniset::ObjectId id, const std::string& prefix );
155  explicit DBServer_SQLite( const std::string& prefix );
156  virtual ~DBServer_SQLite();
157 
159  static std::shared_ptr<DBServer_SQLite> init_dbserver( int argc, const char* const* argv, const std::string& prefix = "sqlite" );
160 
162  static void help_print( int argc, const char* const* argv );
163 
164  inline std::shared_ptr<LogAgregator> logAggregator()
165  {
166  return loga;
167  }
168  inline std::shared_ptr<DebugStream> log()
169  {
170  return dblog;
171  }
172 
173  protected:
174  typedef std::unordered_map<int, std::string> DBTableMap;
175 
176  virtual void initDBServer() override;
177  virtual void initDB( const std::unique_ptr<SQLiteInterface>& db ) {};
178  virtual void initDBTableMap( DBTableMap& tblMap ) {};
179 
180  virtual void timerInfo( const uniset::TimerMessage* tm ) override;
181  virtual void sysCommand( const uniset::SystemMessage* sm ) override;
182  virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
183  virtual void confirmInfo( const uniset::ConfirmMessage* cmsg ) override;
184  virtual void onTextMessage( const uniset::TextMessage* msg ) override;
185  virtual std::string getMonitInfo( const std::string& params ) override;
186 
187  bool writeToBase( const std::string& query );
188  void createTables( SQLiteInterface* db );
189 
190  inline std::string tblName(int key)
191  {
192  return tblMap[key];
193  }
194 
195  enum Timers
196  {
199  lastNumberOfTimer
200  };
201 
202  std::unique_ptr<SQLiteInterface> db;
203  int PingTime = { 300000 };
204  int ReconnectTime = { 180000 };
205 
206  bool connect_ok = { false };
207  bool activate = { false };
208 
209  typedef std::queue<std::string> QueryBuffer;
210 
211  QueryBuffer qbuf;
212  size_t qbufSize = { 200 }; // размер буфера сообщений.
213  bool lastRemove = { false };
214 
215  void flushBuffer();
217 
218  private:
219  DBTableMap tblMap;
220 
221  };
222  // ----------------------------------------------------------------------------------
223 } // end of namespace uniset
224 //------------------------------------------------------------------------------------------
225 #endif
static std::shared_ptr< DBServer_SQLite > init_dbserver(int argc, const char *const *argv, const std::string &prefix="sqlite")
Definition: DBServer_SQLite.cc:411
Definition: CommonEventLoop.h:14
Definition: SQLiteInterface.h:85
Definition: DBServer.h:51
static void help_print(int argc, const char *const *argv)
Definition: DBServer_SQLite.cc:434
Definition: MessageType.h:170
Definition: MessageType.h:236
Definition: DBServer_SQLite.h:197
Timers
Definition: DBServer_SQLite.h:195
KeyType key(const uniset::ObjectId id, const uniset::ObjectId node)
Definition: UniSetTypes.cc:718
Definition: MessageType.h:126
Definition: MessageType.h:279
bool activate
Definition: DBServer_SQLite.h:207
Definition: DBServer_SQLite.h:198
Definition: DBServer_SQLite.h:150
Definition: Mutex.h:31
Definition: MessageType.h:213
long ObjectId
Definition: UniSetTypes_i.idl:30