GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GDaemon Class Reference

Daemon class. More...

#include <GDaemon.hpp>

Inheritance diagram for GDaemon:
GBase

Public Member Functions

 GDaemon (void)
 Void constructor. More...
 
 GDaemon (const GDaemon &daemon)
 Copy constructor. More...
 
virtual ~GDaemon (void)
 Destructor. More...
 
GDaemonoperator= (const GDaemon &daemon)
 Assignment operator. More...
 
virtual void clear (void)
 Clear Daemon. More...
 
virtual GDaemonclone (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...
 

Detailed Description

Daemon class.

Todo:
Add class description.

Definition at line 51 of file GDaemon.hpp.

Constructor & Destructor Documentation

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.

Parameters
[in]daemonDaemon.

Definition at line 80 of file GDaemon.cpp.

References copy_members(), and init_members().

GDaemon::~GDaemon ( void  )
virtual

Destructor.

Definition at line 96 of file GDaemon.cpp.

References free_members().

Member Function Documentation

bool GDaemon::alive ( void  ) const

Check if daemon is alive.

Returns
True 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().

std::string GDaemon::classname ( void  ) const
inlinevirtual

Return class name.

Returns
String containing the class name ("GDaemon").

Implements GBase.

Definition at line 109 of file GDaemon.hpp.

void GDaemon::clear ( void  )
virtual

Clear Daemon.

Implements GBase.

Definition at line 148 of file GDaemon.cpp.

References free_members(), and init_members().

GDaemon * GDaemon::clone ( void  ) const
virtual

Clone Daemon.

Returns
Pointer to deep copy of Daemon.

Implements GBase.

Definition at line 166 of file GDaemon.cpp.

References GDaemon().

void GDaemon::copy_members ( const GDaemon daemon)
protected

Copy class members.

Parameters
[in]daemonDaemon.

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=().

void GDaemon::create_lock_file ( void  )
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().

void GDaemon::create_xml ( const GFilename filename)
protected

Create high-level statistics XML file.

Parameters
[in]filenameFile 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().

void GDaemon::delete_lock_file ( void  )
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().

void GDaemon::free_members ( void  )
protected

Delete class members.

Definition at line 435 of file GDaemon.cpp.

References delete_lock_file().

Referenced by clear(), operator=(), and ~GDaemon().

GFilename GDaemon::heartbeat_filename ( void  ) const
inlineprotected

Returns name of daemon heartbeat file.

Returns
Daemon heartbeat filename.

Definition at line 133 of file GDaemon.hpp.

References gammalib::gamma_filename().

Referenced by alive(), and write_heartbeat().

void GDaemon::init_members ( void  )
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=().

GFilename GDaemon::lock_filename ( void  ) const
inlineprotected

Returns name of daemon lock file.

Returns
Daemon lock filename.

Definition at line 121 of file GDaemon.hpp.

References gammalib::gamma_filename().

Referenced by create_lock_file(), delete_lock_file(), and lock_pid().

pid_t GDaemon::lock_pid ( void  ) const
protected

Returns process ID in lock file.

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().

GDaemon & GDaemon::operator= ( const GDaemon daemon)

Assignment operator.

Parameters
[in]daemonDaemon.
Returns
Daemon.

Definition at line 118 of file GDaemon.cpp.

References copy_members(), free_members(), and init_members().

std::string GDaemon::print ( const GChatter chatter = NORMAL) const
virtual

Print Daemon.

Parameters
[in]chatterChattiness.
Returns
String containing Daemon information.

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().

void GDaemon::recover_valid_xml ( void  )
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().

GFilename GDaemon::statistics_filename ( void  ) const
protected
void GDaemon::update_countries_data ( GXml xml,
const GCsv statistics 
)
protected

Update countries in high-level statistics data.

Parameters
[in,out]xmlHigh-level statistics XML object.
[in]statisticsLow-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().

void GDaemon::update_countries_header ( GXml xml,
const GCsv statistics 
)
protected

Update countries in high-level statistics header.

Parameters
[in,out]xmlHigh-level statistics XML object.
[in]statisticsLow-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().

void GDaemon::update_daily ( GXml xml,
const GCsv statistics 
)
protected

Update daily statistics.

Parameters
[in,out]xmlHigh-level statistics XML object.
[in]statisticsLow-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().

void GDaemon::update_dates ( GXml xml,
const GCsv statistics 
)
protected

Update dates in high-level statistics header.

Parameters
[in,out]xmlHigh-level statistics XML object.
[in]statisticsLow-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().

void GDaemon::update_host_country ( void  )
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().

void GDaemon::update_statistics ( void  )
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().

void GDaemon::update_versions_data ( GXml xml,
const GCsv statistics 
)
protected

Update versions in high-level statistics data.

Parameters
[in,out]xmlHigh-level statistics XML object.
[in]statisticsLow-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().

void GDaemon::write_heartbeat ( void  )
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().

Member Data Documentation

GChatter GDaemon::m_chatter
protected

Chattiness of logger.

Definition at line 99 of file GDaemon.hpp.

Referenced by copy_members(), init_members(), print(), and start().

int GDaemon::m_heartbeat
protected

Heartbeat period in seconds.

Definition at line 97 of file GDaemon.hpp.

Referenced by alive(), copy_members(), init_members(), print(), and start().

GLog GDaemon::m_log
protected
int GDaemon::m_period
protected

Wake-up period in seconds.

Definition at line 96 of file GDaemon.hpp.

Referenced by copy_members(), init_members(), print(), and start().

pid_t GDaemon::m_pid
protected

The documentation for this class was generated from the following files: