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