UniSet  2.6.0
RRDServer.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 // -----------------------------------------------------------------------------
00017 #ifndef _RRDServer_H_
00018 #define _RRDServer_H_
00019 // -----------------------------------------------------------------------------
00020 #include <unordered_map>
00021 #include <list>
00022 #include <memory>
00023 #include "UObject_SK.h"
00024 #include "SMInterface.h"
00025 #include "SharedMemory.h"
00026 #include "extensions/Extensions.h"
00027 // --------------------------------------------------------------------------
00028 namespace uniset
00029 {
00030 // -----------------------------------------------------------------------------
00084 class RRDServer:
00085     public UObject_SK
00086 {
00087     public:
00088         RRDServer( uniset::ObjectId objId, xmlNode* cnode, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
00089                    const std::string& prefix = "rrd" );
00090         virtual ~RRDServer();
00091 
00093         static std::shared_ptr<RRDServer> init_rrdstorage( int argc, const char* const* argv,
00094                 uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
00095                 const std::string& prefix = "rrd" );
00096 
00098         static void help_print( int argc, const char* const* argv );
00099 
00100         inline std::shared_ptr<LogAgregator> getLogAggregator()
00101         {
00102             return loga;
00103         }
00104         inline std::shared_ptr<DebugStream> log()
00105         {
00106             return mylog;
00107         }
00108 
00109         const size_t RRD_MAX_DSNAME_LEN = 19; 
00111     protected:
00112         RRDServer();
00113 
00114         virtual void askSensors( UniversalIO::UIOCommand cmd ) override;
00115         virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
00116         virtual void timerInfo( const uniset::TimerMessage* tm ) override;
00117         virtual void sysCommand( const uniset::SystemMessage* sm ) override;
00118 
00119         void initRRD( xmlNode* cnode, int tmID );
00120         virtual void step() override;
00121 
00122         std::shared_ptr<SMInterface> shm;
00123 
00124         struct DSInfo
00125         {
00126             uniset::ObjectId sid;
00127             std::string dsname;
00128             long value;
00129 
00130             DSInfo( uniset::ObjectId id, const std::string& dsname, long defval ):
00131                 sid(id), dsname(dsname), value(defval) {}
00132         };
00133 
00134         // Т.к. RRD требует чтобы данные записывались именно в том порядке в котором они были добавлены
00135         // при инициализации и при этом, нам нужен быстрый доступ в обработчике sensorInfo.
00136         // То создаём list<> для последовательного прохода по элементам в нужном порядке
00137         // и unordered_map<> для быстрого доступа к элементам в sensorInfo
00138         // При этом используем shared_ptr чтобы элементы указывали на один и тот же DSInfo
00139         typedef std::unordered_map<uniset::ObjectId, std::shared_ptr<DSInfo>> DSMap;
00140         typedef std::list<std::shared_ptr<DSInfo>> DSList;
00141 
00142         struct RRDInfo
00143         {
00144             std::string filename;
00145             long tid;
00146             long sec;
00147             DSMap dsmap;
00148             DSList dslist;
00149 
00150             RRDInfo( const std::string& fname, long tmID, long sec, const DSList& lst );
00151         };
00152 
00153         typedef std::list<RRDInfo> RRDList;
00154 
00155         RRDList rrdlist;
00156 
00157     private:
00158 
00159         std::string prefix;
00160 };
00161 // --------------------------------------------------------------------------
00162 } // end of namespace uniset
00163 // -----------------------------------------------------------------------------
00164 #endif // _RRDServer_H_
00165 // -----------------------------------------------------------------------------