AlarmNotifications
PANDA Slow Control Alarm Daemon
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
daemon.cpp
Go to the documentation of this file.
1 
34 #include "daemon.h"
35 
36 
37 #include <iostream>
38 #include <QtCore/QDateTime>
39 
40 #include "exceptionhandler.h"
41 
42 using namespace AlarmNotifications;
43 
45 {
46  try
47  {
48  static Daemon global_instance;
49  return global_instance;
50  }
51  catch ( std::exception& e )
52  {
53  ExceptionHandler ( e, "while starting the AlarmNotifications daemon.", true );
54  exit ( 1 ); // ExceptionHandler will also call exit(), but g++ does not know when compiling this
55  // so it will complain about a missing return value
56  }
57  catch ( ... )
58  {
59  ExceptionHandler ( "while starting the AlarmNotifications daemon.", true );
60  exit ( 1 ); // ExceptionHandler will also call exit(), but g++ does not know when compiling this
61  // so it will complain about a missing return value
62  }
63 }
64 
66  : _run ( true )
67 {
68  hsigint = signal ( SIGINT, &signalReceiver );
69  hsighup = signal ( SIGHUP, &signalReceiver );
70  hsigquit = signal ( SIGQUIT, &signalReceiver );
71  hsigusr1 = signal ( SIGUSR1, &signalReceiver );
72  hsigusr2 = signal ( SIGUSR2, &signalReceiver );
73  hsigterm = signal ( SIGTERM, &signalReceiver );
74  std::cout << QDateTime::currentDateTime().toString ( QString::fromUtf8 ( "dd. MMM yyyy hh:mm:ss" ) ).toStdString() << ": Starting AlarmNotifications daemon..." << std::endl;
75 }
76 
78 {
79  std::cout << QDateTime::currentDateTime().toString ( QString::fromUtf8 ( "dd. MMM yyyy hh:mm:ss" ) ).toStdString() << ": Stopping AlarmNotifications daemon..." << std::endl;
80 }
81 
83 {
84  try
85  {
86  while ( _run )
87  {
88  sleep ( DaemonSleepTimeout );
89  std::cout << QDateTime::currentDateTime().toString ( QString::fromUtf8 ( "dd. MMM yyyy hh:mm:ss" ) ).toStdString() << ": ";
90  if ( _asc.getNumberOfAlarms() == 0 )
91  std::cout << "No alarms active.";
92  else
93  std::cout << "Number of active alarms: " << _asc.getNumberOfAlarms();
94  std::cout << std::endl;
95  }
96  }
97  catch ( std::exception& e )
98  {
99  ExceptionHandler ( e, "while running the AlarmNotifications daemon.", true );
100  }
101  catch ( ... )
102  {
103  ExceptionHandler ( "while running the AlarmNotifications daemon.", true );
104  }
105 }
106 
107 void Daemon::signalReceiver ( int signum )
108 {
109  if ( signum != SIGINT && signum != SIGHUP && signum != SIGQUIT && signum != SIGUSR1 && signum != SIGUSR2 && signum != SIGTERM )
110  return;
111  instance()._run = false;
112  signal ( SIGINT, instance().hsigint );
113  signal ( SIGHUP, instance().hsighup );
114  signal ( SIGQUIT, instance().hsigquit );
115  signal ( SIGUSR1, instance().hsigusr1 );
116  signal ( SIGUSR2, instance().hsigusr2 );
117  signal ( SIGTERM, instance().hsigterm );
118 }
~Daemon()
Destructor.
Definition: daemon.cpp:77
__sighandler_t hsigusr2
Original SIGUSR2 handler.
Definition: daemon.h:95
AlarmNotifications daemon implementation.
void run()
Main daemon loop.
Definition: daemon.cpp:82
static void signalReceiver(int signum)
POSIX signal handler.
Definition: daemon.cpp:107
static const unsigned short int DaemonSleepTimeout
Timeout for daemon status message.
Definition: daemon.h:59
__sighandler_t hsigquit
Original SIGQUIT handler.
Definition: daemon.h:83
Daemon()
Constructor.
Definition: daemon.cpp:65
__sighandler_t hsigterm
Original SIGTERM handler.
Definition: daemon.h:101
AlarmServerConnector _asc
Connection to the CSS Alarm Server.
Definition: daemon.h:107
__sighandler_t hsighup
Original SIGHUP handler.
Definition: daemon.h:77
Namespace for Alarm Notifications application.
static Daemon & instance()
Get singleton instance.
Definition: daemon.cpp:44
size_t getNumberOfAlarms() const noexcept
Query number of active alarms.
__sighandler_t hsigusr1
Original SIGUSR1 handler.
Definition: daemon.h:89
bool _run
Global daemon run flag.
Definition: daemon.h:65
Alarm Notification daemon.
Definition: daemon.h:51
void ExceptionHandler(std::exception &e, std::string location, const bool quit=false) noexcept
Generic exception handler for known exceptions.
Generic functions for exception handling.
__sighandler_t hsigint
Original SIGINT handler.
Definition: daemon.h:71