UniSet  2.2.1
SharedMemory.h
00001 // -----------------------------------------------------------------------------
00002 #ifndef SharedMemory_H_
00003 #define SharedMemory_H_
00004 // -----------------------------------------------------------------------------
00005 #include <unordered_map>
00006 #include <string>
00007 #include <memory>
00008 #include <deque>
00009 #include "IONotifyController.h"
00010 #include "Mutex.h"
00011 #include "PassiveTimer.h"
00012 #include "NCRestorer.h"
00013 #include "WDTInterface.h"
00014 #include "LogServer.h"
00015 #include "DebugStream.h"
00016 #include "LogAgregator.h"
00017 #include "VMonitor.h"
00018 // -----------------------------------------------------------------------------
00019 #ifndef vmonit
00020 #define vmonit( var ) vmon.add( #var, var )
00021 #endif
00022 // -----------------------------------------------------------------------------
00023 
00284 class SharedMemory:
00285     public IONotifyController
00286 {
00287     public:
00288         SharedMemory( UniSetTypes::ObjectId id, const std::string& datafile, const std::string& confname = "" );
00289         virtual ~SharedMemory();
00290 
00292         static std::shared_ptr<SharedMemory> init_smemory( int argc, const char* const* argv );
00294         static void help_print( int argc, const char* const* argv );
00295 
00296         // функция определяет "готовность" SM к работе.
00297         // должна использоваться другими процессами, для того,
00298         // чтобы понять, когда можно получать от SM данные.
00299         virtual CORBA::Boolean exist() override;
00300 
00301         virtual UniSetTypes::SimpleInfo* getInfo( CORBA::Long userparam = 0 ) override;
00302 
00303         void addReadItem( Restorer_XML::ReaderSlot sl );
00304 
00305 
00306         // ------------  HISTORY  --------------------
00307         typedef std::deque<long> HBuffer;
00308 
00309         struct HistoryItem
00310         {
00311             explicit HistoryItem( size_t bufsize = 0 ): id(UniSetTypes::DefaultObjectId), buf(bufsize) {}
00312             HistoryItem( const UniSetTypes::ObjectId _id, const size_t bufsize, const long val ): id(_id), buf(bufsize, val) {}
00313 
00314             inline void init( unsigned int size, long val )
00315             {
00316                 if( size > 0 )
00317                     buf.assign(size, val);
00318             }
00319 
00320             UniSetTypes::ObjectId id;
00321             HBuffer buf;
00322 
00323             IOStateList::iterator ioit;
00324 
00325             void add( long val, size_t size )
00326             {
00327                 // т.е. буфер у нас уже заданного размера
00328                 // то просто удаляем очередную точку в начале
00329                 // и добавляем в конце
00330                 buf.pop_front();
00331                 buf.push_back(val);
00332             }
00333         };
00334 
00335         typedef std::list<HistoryItem> HistoryList;
00336 
00337         struct HistoryInfo
00338         {
00339             HistoryInfo():
00340                 id(0),
00341                 size(0), filter(""),
00342                 fuse_id(UniSetTypes::DefaultObjectId),
00343                 fuse_invert(false), fuse_use_val(false), fuse_val(0),
00344                 fuse_sec(0), fuse_usec(0)
00345             {
00346                 struct timeval tv;
00347                 struct timezone tz;
00348                 gettimeofday(&tv, &tz);
00349                 fuse_sec = tv.tv_sec;
00350                 fuse_usec = tv.tv_usec;
00351             }
00352 
00353             long id;                        // ID
00354             HistoryList hlst;               // history list
00355             int size;
00356             std::string filter;             // filter field
00357             UniSetTypes::ObjectId fuse_id;  // fuse sesnsor
00358             bool fuse_invert;
00359             bool fuse_use_val;
00360             long fuse_val;
00361             // timestamp
00362             long fuse_sec;
00363             long fuse_usec;
00364         };
00365 
00366         friend std::ostream& operator<<( std::ostream& os, const HistoryInfo& h );
00367 
00368         typedef std::list<HistoryInfo> History;
00369 
00370         // т.к. могуть быть одинаковые "детонаторы" для разных "историй" то,
00371         // вводим не просто map, а "map списка историй".
00372         // точнее итераторов-историй.
00373         typedef std::list<History::iterator> HistoryItList;
00374         typedef std::unordered_map<UniSetTypes::ObjectId, HistoryItList> HistoryFuseMap;
00375 
00376         typedef sigc::signal<void, const HistoryInfo&> HistorySlot;
00377         HistorySlot signal_history(); 
00379         inline int getHistoryStep()
00380         {
00381             return histSaveTime;    
00382         }
00383 
00384         inline std::shared_ptr<LogAgregator> logAgregator()
00385         {
00386             return loga;
00387         }
00388         inline std::shared_ptr<DebugStream> log()
00389         {
00390             return smlog;
00391         }
00392 
00393     protected:
00394         typedef std::list<Restorer_XML::ReaderSlot> ReadSlotList;
00395         ReadSlotList lstRSlot;
00396 
00397         virtual void sysCommand( const UniSetTypes::SystemMessage* sm ) override;
00398         virtual void timerInfo( const UniSetTypes::TimerMessage* tm ) override;
00399         virtual void askSensors( UniversalIO::UIOCommand cmd ) {};
00400         void sendEvent( UniSetTypes::SystemMessage& sm );
00401         void initFromReserv();
00402         bool initFromSM( UniSetTypes::ObjectId sm_id, UniSetTypes::ObjectId sm_node );
00403 
00404         // действия при завершении работы
00405         virtual void sigterm( int signo ) override;
00406         virtual bool activateObject() override;
00407         virtual bool deactivateObject() override;
00408         bool readItem( const std::shared_ptr<UniXML>& xml, UniXML::iterator& it, xmlNode* sec );
00409 
00410         void buildEventList( xmlNode* cnode );
00411         void readEventList( const std::string& oname );
00412 
00413         UniSetTypes::uniset_rwmutex mutex_start;
00414 
00415         class HeartBeatInfo
00416         {
00417             public:
00418                 HeartBeatInfo():
00419                     a_sid(UniSetTypes::DefaultObjectId),
00420                     d_sid(UniSetTypes::DefaultObjectId),
00421                     reboot_msec(UniSetTimer::WaitUpTime),
00422                     timer_running(false),
00423                     ptReboot(UniSetTimer::WaitUpTime)
00424                 {}
00425 
00426                 UniSetTypes::ObjectId a_sid; // аналоговый счётчик
00427                 UniSetTypes::ObjectId d_sid; // дискретный датчик состояния процесса
00428                 IOStateList::iterator a_it;
00429                 IOStateList::iterator d_it;
00430 
00431                 timeout_t reboot_msec; 
00436                 bool timer_running;
00437                 PassiveTimer ptReboot;
00438         };
00439 
00440         enum Timers
00441         {
00442             tmHeartBeatCheck,
00443             tmEvent,
00444             tmHistory,
00445             tmPulsar,
00446             tmLastOfTimerID
00447         };
00448 
00449         int heartbeatCheckTime;
00450         std::string heartbeat_node;
00451         int histSaveTime;
00452 
00453         void checkHeartBeat();
00454 
00455         typedef std::list<HeartBeatInfo> HeartBeatList;
00456         HeartBeatList hlist; // список датчиков "сердцебиения"
00457         std::shared_ptr<WDTInterface> wdt;
00458         std::atomic_bool activated;
00459         std::atomic_bool workready;
00460 
00461         typedef std::list<UniSetTypes::ObjectId> EventList;
00462         EventList elst;
00463         std::string e_filter;
00464         int evntPause;
00465         int activateTimeout;
00466 
00467         virtual void loggingInfo( UniSetTypes::SensorMessage& sm ) override;
00468         virtual void dumpOrdersList( const UniSetTypes::ObjectId sid, const IONotifyController::ConsumerListInfo& lst ) override {};
00469         virtual void dumpThresholdList( const UniSetTypes::ObjectId sid, const IONotifyController::ThresholdExtList& lst ) override {}
00470 
00471         bool dblogging;
00472 
00473         History hist;
00474         HistoryFuseMap histmap;  
00476         virtual void updateHistory( std::shared_ptr<IOController::USensorInfo>& it, IOController* );
00477         virtual void saveHistory();
00478 
00479         void buildHistoryList( xmlNode* cnode );
00480         void checkHistoryFilter( UniXML::iterator& it );
00481 
00482         IOStateList::iterator itPulsar;
00483         UniSetTypes::ObjectId sidPulsar;
00484         int msecPulsar;
00485 
00486         xmlNode* confnode;
00487 
00488         std::shared_ptr<LogAgregator> loga;
00489         std::shared_ptr<DebugStream> smlog;
00490         std::shared_ptr<LogServer> logserv;
00491         std::string logserv_host = {""};
00492         int logserv_port = {0};
00493 
00494         VMonitor vmon;
00495 
00496     private:
00497         HistorySlot m_historySignal;
00498 };
00499 // -----------------------------------------------------------------------------
00500 #endif // SharedMemory_H_
00501 // -----------------------------------------------------------------------------