GammaLib
2.0.0
|
Daemon class. More...
#include <GDaemon.hpp>
Public Member Functions | |
GDaemon (void) | |
Void constructor. More... | |
GDaemon (const GDaemon &daemon) | |
Copy constructor. More... | |
virtual | ~GDaemon (void) |
Destructor. More... | |
GDaemon & | operator= (const GDaemon &daemon) |
Assignment operator. More... | |
virtual void | clear (void) |
Clear Daemon. More... | |
virtual GDaemon * | clone (void) const |
Clone Daemon. More... | |
virtual std::string | classname (void) const |
Return class name. More... | |
virtual std::string | print (const GChatter &chatter=NORMAL) const |
Print Daemon. More... | |
void | start (void) |
Starts the daemon. More... | |
bool | alive (void) const |
Check if daemon is alive. More... | |
Public Member Functions inherited from GBase | |
virtual | ~GBase (void) |
Destructor. More... | |
Protected Member Functions | |
void | init_members (void) |
Initialise class members. More... | |
void | copy_members (const GDaemon &daemon) |
Copy class members. More... | |
void | free_members (void) |
Delete class members. More... | |
void | create_lock_file (void) |
Create the lock file. More... | |
void | delete_lock_file (void) |
Delete daemon lock file. More... | |
void | write_heartbeat (void) |
Write heartbeat file. More... | |
GFilename | lock_filename (void) const |
Returns name of daemon lock file. More... | |
GFilename | heartbeat_filename (void) const |
Returns name of daemon heartbeat file. More... | |
GFilename | statistics_filename (void) const |
pid_t | lock_pid (void) const |
Returns process ID in lock file. More... | |
void | update_statistics (void) |
Update application statistics. More... | |
void | update_host_country (void) |
Update host country. More... | |
void | recover_valid_xml (void) |
Recovers a valid XML file. More... | |
void | create_xml (const GFilename &filename) |
Create high-level statistics XML file. More... | |
void | update_dates (GXml &xml, const GCsv &statistics) |
Update dates in high-level statistics header. More... | |
void | update_countries_header (GXml &xml, const GCsv &statistics) |
Update countries in high-level statistics header. More... | |
void | update_countries_data (GXml &xml, const GCsv &statistics) |
Update countries in high-level statistics data. More... | |
void | update_versions_data (GXml &xml, const GCsv &statistics) |
Update versions in high-level statistics data. More... | |
void | update_daily (GXml &xml, const GCsv &statistics) |
Update daily statistics. More... | |
Protected Attributes | |
pid_t | m_pid |
Process ID. More... | |
int | m_period |
Wake-up period in seconds. More... | |
int | m_heartbeat |
Heartbeat period in seconds. More... | |
GLog | m_log |
Logger. More... | |
GChatter | m_chatter |
Chattiness of logger. More... | |
GDaemon::GDaemon | ( | void | ) |
Void constructor.
Definition at line 65 of file GDaemon.cpp.
References init_members().
Referenced by clone().
GDaemon::GDaemon | ( | const GDaemon & | daemon | ) |
Copy constructor.
[in] | daemon | Daemon. |
Definition at line 80 of file GDaemon.cpp.
References copy_members(), and init_members().
|
virtual |
bool GDaemon::alive | ( | void | ) | const |
Check if daemon is alive.
Checks if the daemon is alive by comparing the time found in the heartbeat file to the current time. If the heartbeat file is not older than twice the heartbeat period the daemon is considered alive.
This method can be used by a client to check whether a daemon is alive. The method does not rely on any process ID and works even over multiple machines that share the same disk space.
Definition at line 282 of file GDaemon.cpp.
References heartbeat_filename(), m_heartbeat, GTime::now(), GFilename::url(), and GTime::utc().
Referenced by main().
|
inlinevirtual |
Return class name.
Implements GBase.
Definition at line 109 of file GDaemon.hpp.
|
virtual |
Clear Daemon.
Implements GBase.
Definition at line 148 of file GDaemon.cpp.
References free_members(), and init_members().
|
virtual |
Clone Daemon.
Implements GBase.
Definition at line 166 of file GDaemon.cpp.
References GDaemon().
|
protected |
Copy class members.
[in] | daemon | Daemon. |
Definition at line 418 of file GDaemon.cpp.
References m_chatter, m_heartbeat, m_log, m_period, and m_pid.
Referenced by GDaemon(), and operator=().
|
protected |
Create the lock file.
Creates the daemon lock file and write process ID into lock file.
Definition at line 450 of file GDaemon.cpp.
References lock_filename(), m_log, m_pid, and GFilename::url().
Referenced by start().
|
protected |
Create high-level statistics XML file.
[in] | filename | File name. |
Creates an empty high-level statistics XML file with the specified filename
. This method overwrites any existing XML file.
Definition at line 866 of file GDaemon.cpp.
References GXmlNode::append(), GXml::append(), m_log, m_pid, GTime::now(), GXml::save(), GFilename::url(), and GTime::utc().
Referenced by recover_valid_xml().
|
protected |
Delete daemon lock file.
Deletes the daemon lock file on disk if the process ID in the lock file corresponds to the process ID of the instance.
Definition at line 483 of file GDaemon.cpp.
References lock_filename(), lock_pid(), m_log, m_pid, and GFilename::url().
Referenced by free_members(), and start().
|
protected |
Delete class members.
Definition at line 435 of file GDaemon.cpp.
References delete_lock_file().
Referenced by clear(), operator=(), and ~GDaemon().
|
inlineprotected |
Returns name of daemon heartbeat file.
Definition at line 133 of file GDaemon.hpp.
References gammalib::gamma_filename().
Referenced by alive(), and write_heartbeat().
|
protected |
Initialise class members.
< No process ID
< Wake-up daemon every hour
< One heartbeat per minute
< NORMAL chatter level
Definition at line 399 of file GDaemon.cpp.
References GLog::clear(), m_chatter, m_heartbeat, m_log, m_period, m_pid, and NORMAL.
Referenced by clear(), GDaemon(), and operator=().
|
inlineprotected |
Returns name of daemon lock file.
Definition at line 121 of file GDaemon.hpp.
References gammalib::gamma_filename().
Referenced by create_lock_file(), delete_lock_file(), and lock_pid().
|
protected |
Returns process ID in lock file.
Returns the process ID that is found in the lock file. If no lock file exists the method returns 0.
Definition at line 546 of file GDaemon.cpp.
References lock_filename(), gammalib::toint(), and GFilename::url().
Referenced by delete_lock_file(), and start().
Assignment operator.
[in] | daemon | Daemon. |
Definition at line 118 of file GDaemon.cpp.
References copy_members(), free_members(), and init_members().
Print Daemon.
[in] | chatter | Chattiness. |
Implements GBase.
Definition at line 362 of file GDaemon.cpp.
References m_chatter, m_heartbeat, m_period, m_pid, gammalib::parformat(), SILENT, and gammalib::str().
|
protected |
Recovers a valid XML file.
This method recovers a valid XML file in statistics.xml
. Several cases are covered.
If none of the files statistics.xml
and statistics.xml~
exists the method will create a new XML file using the create_xml() method.
If only the copy statistics.xml~
exists there was a problem during writing the working file, hence the statistics.xml~
file is copied into statistics.xml
. In case that statistics.xml~
is corrupt a new XML file is created.
If both files exist, the integrity of both files is checked. If only statistics.xml
is corrupted, statistics.xml~
will be copied into statistics.xml
. If only statistics.xml~
is corrupted it is ignored. If both files are corrupted they are secured and a new XML file will be created using the create_xml() method. If both files are okay, the file statistics.xml
is secured and statistics.xml~
is copied into statistics.xml
.
Before existing, any statistics.xml.copy
file is removed.
Definition at line 746 of file GDaemon.cpp.
References create_xml(), GFilename::exists(), gammalib::gamma_filename(), GXml::load(), m_log, m_pid, GTime::now(), GXml::save(), GFilename::url(), and GTime::utc().
Referenced by update_statistics().
void GDaemon::start | ( | void | ) |
Starts the daemon.
Starts up the daemon and entered the never-ending daemon event loop.
Definition at line 177 of file GDaemon.cpp.
References GLog::chatter(), GLog::close(), create_lock_file(), GLog::date(), delete_lock_file(), GLog::flush(), gammalib::gamma_filename(), lock_pid(), m_chatter, m_heartbeat, m_log, m_period, m_pid, GLog::open(), update_host_country(), update_statistics(), and write_heartbeat().
Referenced by main(), and update_dates().
|
protected |
Update countries in high-level statistics data.
[in,out] | xml | High-level statistics XML object. |
[in] | statistics | Low-level statistics CSV object. |
Update country statistics in data block.
Definition at line 1035 of file GDaemon.cpp.
References GXmlNode::append(), GXmlElement::attribute(), GXmlNode::element(), GXml::element(), GXmlNode::elements(), GXmlElement::has_attribute(), GXmlElement::name(), GCsv::nrows(), GCsv::real(), gammalib::str(), GCsv::string(), gammalib::todouble(), and gammalib::toint().
Referenced by update_statistics().
Update countries in high-level statistics header.
[in,out] | xml | High-level statistics XML object. |
[in] | statistics | Low-level statistics CSV object. |
Update country statistics in header block.
Definition at line 978 of file GDaemon.cpp.
References GXmlNode::append(), GXmlNode::element(), GXml::element(), GXmlNode::elements(), GCsv::nrows(), GCsv::string(), and GXmlElement::value().
Referenced by update_statistics().
Update daily statistics.
[in,out] | xml | High-level statistics XML object. |
[in] | statistics | Low-level statistics CSV object. |
Update daily statistics in data block.
Definition at line 1197 of file GDaemon.cpp.
References GXmlNode::append(), GXmlElement::attribute(), GXmlNode::element(), GXml::element(), GXmlNode::elements(), GXmlElement::has_attribute(), GXmlElement::name(), GCsv::nrows(), GCsv::real(), gammalib::str(), GCsv::string(), gammalib::todouble(), gammalib::toint(), and GXmlElement::value().
Referenced by update_statistics().
Update dates in high-level statistics header.
[in,out] | xml | High-level statistics XML object. |
[in] | statistics | Low-level statistics CSV object. |
Updates dates in the high-level statistics header.
Definition at line 918 of file GDaemon.cpp.
References GXmlNode::append(), GXmlNode::element(), GXml::element(), GXmlNode::is_empty(), GTime::now(), GCsv::nrows(), start(), GCsv::string(), and GTime::utc().
Referenced by update_statistics().
|
protected |
Update host country.
Update host country code in $HOME/.gamma/host-country file.
Definition at line 690 of file GDaemon.cpp.
References gammalib::gamma_filename(), gammalib::host_country(), and GFilename::url().
Referenced by start().
|
protected |
Update application statistics.
Update application statistics in the statistics.xml
file by scanning the low-level file statistics.csv
.
Definition at line 584 of file GDaemon.cpp.
References GFilename::exists(), gammalib::gamma_filename(), GXml::is_empty(), GXml::load(), m_log, m_pid, recover_valid_xml(), GXml::save(), update_countries_data(), update_countries_header(), update_daily(), update_dates(), update_versions_data(), and GFilename::url().
Referenced by start().
Update versions in high-level statistics data.
[in,out] | xml | High-level statistics XML object. |
[in] | statistics | Low-level statistics CSV object. |
Update version statistics in data block.
Definition at line 1116 of file GDaemon.cpp.
References GXmlNode::append(), GXmlElement::attribute(), GXmlNode::element(), GXml::element(), GXmlNode::elements(), GXmlElement::has_attribute(), GXmlElement::name(), GCsv::nrows(), GCsv::real(), gammalib::str(), GCsv::string(), gammalib::todouble(), and gammalib::toint().
Referenced by update_statistics().
|
protected |
Write heartbeat file.
Creates and updates the heartbeat file that contains the current UTC time.
Definition at line 512 of file GDaemon.cpp.
References heartbeat_filename(), GTime::now(), GFilename::url(), and GTime::utc().
Referenced by start().
|
protected |
Chattiness of logger.
Definition at line 99 of file GDaemon.hpp.
Referenced by copy_members(), init_members(), print(), and start().
|
protected |
Heartbeat period in seconds.
Definition at line 97 of file GDaemon.hpp.
Referenced by alive(), copy_members(), init_members(), print(), and start().
|
protected |
Logger.
Definition at line 98 of file GDaemon.hpp.
Referenced by copy_members(), create_lock_file(), create_xml(), delete_lock_file(), init_members(), recover_valid_xml(), start(), and update_statistics().
|
protected |
Wake-up period in seconds.
Definition at line 96 of file GDaemon.hpp.
Referenced by copy_members(), init_members(), print(), and start().
|
protected |
Process ID.
Definition at line 95 of file GDaemon.hpp.
Referenced by copy_members(), create_lock_file(), create_xml(), delete_lock_file(), init_members(), print(), recover_valid_xml(), start(), and update_statistics().