UniSet  2.24.2
RRDServer.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 // -----------------------------------------------------------------------------
17 #ifndef _RRDServer_H_
18 #define _RRDServer_H_
19 // -----------------------------------------------------------------------------
20 #include <unordered_map>
21 #include <list>
22 #include <memory>
23 #include "UObject_SK.h"
24 #include "SMInterface.h"
25 #include "SharedMemory.h"
26 #include "extensions/Extensions.h"
27 // --------------------------------------------------------------------------
28 namespace uniset
29 {
30  // -----------------------------------------------------------------------------
83  // -----------------------------------------------------------------------------
85  class RRDServer:
86  public UObject_SK
87  {
88  public:
89  RRDServer( uniset::ObjectId objId, xmlNode* cnode, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
90  const std::string& prefix = "rrd" );
91  virtual ~RRDServer();
92 
94  static std::shared_ptr<RRDServer> init_rrdstorage( int argc, const char* const* argv,
95  uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
96  const std::string& prefix = "rrd" );
97 
99  static void help_print( int argc, const char* const* argv );
100 
101  inline std::shared_ptr<LogAgregator> getLogAggregator()
102  {
103  return loga;
104  }
105  inline std::shared_ptr<DebugStream> log()
106  {
107  return mylog;
108  }
109 
110  const size_t RRD_MAX_DSNAME_LEN = 19;
112  protected:
113  RRDServer();
114 
115  virtual void askSensors( UniversalIO::UIOCommand cmd ) override;
116  virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
117  virtual void timerInfo( const uniset::TimerMessage* tm ) override;
118  virtual void sysCommand( const uniset::SystemMessage* sm ) override;
119 
120  void initRRD( xmlNode* cnode, int tmID );
121  virtual void step() override;
122 
123  std::shared_ptr<SMInterface> shm;
124 
125  struct DSInfo
126  {
127  uniset::ObjectId sid;
128  std::string dsname;
129  long value;
130 
131  DSInfo( uniset::ObjectId id, const std::string& dsname, long defval ):
132  sid(id), dsname(dsname), value(defval) {}
133  };
134 
135  // Т.к. RRD требует чтобы данные записывались именно в том порядке в котором они были добавлены
136  // при инициализации и при этом, нам нужен быстрый доступ в обработчике sensorInfo.
137  // То создаём vector<> для последовательного прохода по элементам в нужном порядке
138  // и unordered_map<> для быстрого доступа к элементам в sensorInfo
139  // При этом используем shared_ptr чтобы элементы указывали на один и тот же DSInfo
140  typedef std::unordered_map<uniset::ObjectId, std::shared_ptr<DSInfo>> DSMap;
141  typedef std::vector<std::shared_ptr<DSInfo>> DSList;
142 
143  struct RRDInfo
144  {
145  std::string filename;
146  long tid;
147  long sec;
148  DSMap dsmap;
149  DSList dslist;
150 
151  RRDInfo( const std::string& fname, long tmID, long sec, const DSList& lst );
152  };
153 
154  typedef std::vector<RRDInfo> RRDList;
155 
156  RRDList rrdlist;
157 
158  private:
159 
160  std::string prefix;
161  };
162  // --------------------------------------------------------------------------
163 } // end of namespace uniset
164 // -----------------------------------------------------------------------------
165 #endif // _RRDServer_H_
166 // -----------------------------------------------------------------------------
Definition: UObject_SK.h:30
Definition: RRDServer.h:87
static std::shared_ptr< RRDServer > init_rrdstorage(int argc, const char *const *argv, uniset::ObjectId shmID, const std::shared_ptr< SharedMemory > &ic=nullptr, const std::string &prefix="rrd")
Definition: RRDServer.cc:292
const size_t RRD_MAX_DSNAME_LEN
Definition: RRDServer.h:110
static void help_print(int argc, const char *const *argv)
Definition: RRDServer.cc:268
Definition: MessageType.h:127
Definition: MessageType.h:171
Definition: MessageType.h:214
Definition: CommonEventLoop.h:15
long ObjectId
Definition: UniSetTypes_i.idl:30
Definition: RRDServer.h:126
Definition: RRDServer.h:144