UniSet  2.7.0
SharedMemory.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 SharedMemory_H_
18 #define SharedMemory_H_
19 // -----------------------------------------------------------------------------
20 #include <unordered_map>
21 #include <string>
22 #include <memory>
23 #include <deque>
24 #include <time.h>
25 #include "IONotifyController.h"
26 #include "Mutex.h"
27 #include "PassiveTimer.h"
28 #include "WDTInterface.h"
29 #include "LogServer.h"
30 #include "DebugStream.h"
31 #include "LogAgregator.h"
32 #include "VMonitor.h"
33 #include "IOConfig_XML.h"
34 // -----------------------------------------------------------------------------
35 #ifndef vmonit
36 #define vmonit( var ) vmon.add( #var, var )
37 #endif
38 // --------------------------------------------------------------------------
39 namespace uniset
40 {
41  // -----------------------------------------------------------------------------
42 
326  public IONotifyController
327  {
328  public:
329 
330  // конструктор с конфигурированием через xml
332  const std::shared_ptr<IOConfig_XML>& ioconf,
333  const std::string& confname = "" );
334 
335  virtual ~SharedMemory();
336 
338  static std::shared_ptr<SharedMemory> init_smemory( int argc, const char* const* argv );
339 
341  static void help_print( int argc, const char* const* argv );
342 
343  // функция определяет "готовность" SM к работе.
344  // должна использоваться другими процессами, для того,
345  // чтобы понять, когда можно получать от SM данные.
346  virtual CORBA::Boolean exist() override;
347 
348  virtual uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override;
349 
350  void addReadItem( IOConfig_XML::ReaderSlot sl );
351 
352  // ------------ HISTORY --------------------
353  typedef std::deque<long> HBuffer;
354 
355  struct HistoryItem
356  {
357  explicit HistoryItem( size_t bufsize = 0 ): id(uniset::DefaultObjectId), buf(bufsize) {}
358  HistoryItem( const uniset::ObjectId _id, const size_t bufsize, const long val ): id(_id), buf(bufsize, val) {}
359 
360  inline void init( size_t size, long val )
361  {
362  if( size > 0 )
363  buf.assign(size, val);
364  }
365 
366  uniset::ObjectId id;
367  HBuffer buf;
368 
369  IOStateList::iterator ioit;
370 
371  void add( long val, size_t size )
372  {
373  // т.к. буфер у нас уже заданного размера
374  // то просто удаляем очередную точку в начале
375  // и добавляем в конце
376  buf.pop_front();
377  buf.push_back(val);
378  }
379  };
380 
381  typedef std::list<HistoryItem> HistoryList;
382 
383  struct HistoryInfo
384  {
385  HistoryInfo()
386  {
387  ::clock_gettime(CLOCK_REALTIME, &fuse_tm);
388  }
389 
390  long id = { 0 }; // ID
391  HistoryList hlst; // history list
392  size_t size = { 0 };
393  std::string filter = { "" }; // filter field
394  uniset::ObjectId fuse_id = { uniset::DefaultObjectId }; // fuse sesnsor
395  bool fuse_invert = { false };
396  bool fuse_use_val = { false };
397  long fuse_val = { 0 };
398  timespec fuse_tm = { 0, 0 }; // timestamp
399  };
400 
401  friend std::ostream& operator<<( std::ostream& os, const HistoryInfo& h );
402 
403  typedef std::list<HistoryInfo> History;
404 
405  // т.к. могуть быть одинаковые "детонаторы" для разных "историй" то,
406  // вводим не просто map, а "map списка историй".
407  // точнее итераторов-историй.
408  typedef std::list<History::iterator> HistoryItList;
409  typedef std::unordered_map<uniset::ObjectId, HistoryItList> HistoryFuseMap;
410 
411  typedef sigc::signal<void, const HistoryInfo&> HistorySlot;
412  HistorySlot signal_history();
414  inline int getHistoryStep() const
415  {
416  return histSaveTime;
417  }
418 
419  // -------------------------------------------------------------------------------
420 
421  inline std::shared_ptr<LogAgregator> logAgregator()
422  {
423  return loga;
424  }
425  inline std::shared_ptr<DebugStream> log()
426  {
427  return smlog;
428  }
429 
430  protected:
431  typedef std::list<IOConfig_XML::ReaderSlot> ReadSlotList;
432  ReadSlotList lstRSlot;
433 
434  virtual void sysCommand( const uniset::SystemMessage* sm ) override;
435  virtual void timerInfo( const uniset::TimerMessage* tm ) override;
436  virtual std::string getTimerName(int id) const override;
437 
438  void sendEvent( uniset::SystemMessage& sm );
439  void initFromReserv();
440  bool initFromSM( uniset::ObjectId sm_id, uniset::ObjectId sm_node );
441 
442  virtual bool activateObject() override;
443  virtual bool deactivateObject() override;
444  bool readItem( const std::shared_ptr<UniXML>& xml, UniXML::iterator& it, xmlNode* sec );
445 
446  void buildEventList( xmlNode* cnode );
447  void readEventList( const std::string& oname );
448 
449  std::mutex mutexStart;
450 
452  {
453  public:
454  HeartBeatInfo():
457  reboot_msec(UniSetTimer::WaitUpTime),
458  timer_running(false),
459  ptReboot(UniSetTimer::WaitUpTime)
460  {}
461 
462  uniset::ObjectId a_sid; // аналоговый счётчик
463  uniset::ObjectId d_sid; // дискретный датчик состояния процесса
464  IOStateList::iterator a_it;
465  IOStateList::iterator d_it;
466 
467  timeout_t reboot_msec;
472  bool timer_running;
473  PassiveTimer ptReboot;
474  };
475 
476  enum Timers
477  {
478  tmHeartBeatCheck,
479  tmEvent,
480  tmHistory,
481  tmPulsar,
482  tmLastOfTimerID
483  };
484 
485  int heartbeatCheckTime;
486  std::string heartbeat_node;
487  int histSaveTime;
488 
489  void checkHeartBeat();
490 
491  typedef std::list<HeartBeatInfo> HeartBeatList;
492  HeartBeatList hblist; // список датчиков "сердцебиения"
493  std::shared_ptr<WDTInterface> wdt;
494  std::atomic_bool activated = { false };
495  std::atomic_bool workready = { false };
496  std::atomic_bool cancelled = { false };
497 
498  typedef std::list<uniset::ObjectId> EventList;
499  EventList elst;
500  std::string e_filter;
501  int evntPause;
502  int activateTimeout;
503 
504  virtual void logging( uniset::SensorMessage& sm ) override;
505 
506  bool dblogging = { false };
507 
510  // оптимизация с использованием userdata (IOController::USensorInfo::userdata) нужна
511  // чтобы не использовать поиск в HistoryFuseMap (см. checkFuse)
512  // т.к. 0,1 - использует IONotifyController (см. IONotifyController::UserDataID)
513  // то используем не занятый "2" - в качестве элемента userdata
514  static const size_t udataHistory = 2;
515 
516  History hist;
517  HistoryFuseMap histmap;
519  virtual void checkFuse( std::shared_ptr<IOController::USensorInfo>& usi, IOController* );
520  virtual void saveToHistory();
521 
522  void buildHistoryList( xmlNode* cnode );
523  void checkHistoryFilter( UniXML::iterator& it );
524 
525  IOStateList::iterator itPulsar;
526  uniset::ObjectId sidPulsar;
527  int msecPulsar;
528 
529  xmlNode* confnode;
530 
531  std::shared_ptr<LogAgregator> loga;
532  std::shared_ptr<DebugStream> smlog;
533  std::shared_ptr<LogServer> logserv;
534  std::string logserv_host = {""};
535  int logserv_port = {0};
536 
537  VMonitor vmon;
538 
539  private:
540  HistorySlot m_historySignal;
541  };
542  // --------------------------------------------------------------------------
543 } // end of namespace uniset
544 // -----------------------------------------------------------------------------
545 #endif // SharedMemory_H_
546 // -----------------------------------------------------------------------------
Пассивный таймер
Definition: PassiveTimer.h:92
HistoryFuseMap histmap
Definition: SharedMemory.h:517
Definition: CallbackTimer.h:29
Definition: SharedMemory.h:355
Definition: UniXML.h:43
Definition: SharedMemory.h:451
virtual std::string getTimerName(int id) const override
Definition: SharedMemory.cc:227
virtual bool activateObject() override
Definition: SharedMemory.cc:323
timeout_t reboot_msec
Definition: SharedMemory.h:467
Definition: MessageType.h:168
Definition: VMonitor.h:116
Definition: SharedMemory.h:325
const ObjectId DefaultObjectId
Definition: UniSetTypes.h:69
sigc::slot< bool, const std::shared_ptr< UniXML > &, UniXML::iterator &, xmlNode * > ReaderSlot
Definition: IOConfig_XML.h:65
int getHistoryStep() const
Definition: SharedMemory.h:414
Definition: IONotifyController.h:132
Definition: MessageType.h:124
virtual void logging(uniset::SensorMessage &sm) override
сохранение информации об изменении состояния датчика
Definition: SharedMemory.cc:630
Definition: SharedMemory.h:383
Definition: MessageType.h:211
HistorySlot signal_history()
Definition: SharedMemory.cc:752
static const size_t udataHistory
Definition: SharedMemory.h:514
virtual bool deactivateObject() override
Definition: SharedMemory.cc:309
Definition: IOController.h:45
static std::shared_ptr< SharedMemory > init_smemory(int argc, const char *const *argv)
Definition: SharedMemory.cc:522
Definition: UniSetTypes_i.idl:64
long ObjectId
Definition: UniSetTypes_i.idl:30
static void help_print(int argc, const char *const *argv)
Definition: SharedMemory.cc:32
static const timeout_t WaitUpTime
Definition: PassiveTimer.h:69