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