#include <vosdaemon.hh>
a daemon.
It will fork and run in the background automatically. To use it for your daemon, simply subclass this class, and instantiate it from your program. You can add and check for command-line arguments with addArg, addFlag, getArg and getFlag. You can also turn on automatic saving, loading and periodic saving by enabling the Save and Load built in command line parameters. Then call checkArgs, and run. You can override loop() to do something every program loop, or you can use a site callback. Other virtual methods may be overridden, but be sure to call the method in the parent class (VOSDaemon::) from your method.
VOSDaemon will automatically handle the following arguments:
Here is an example of use:
#include <iostream> // for cerr #include <vos/corelibs/vos/vos.hh> #include <vos/metaobjects/property/property.hh> #include <vosdaemon.hh> class MyDaemon : public virtual VOSDaemon { public: MyDaemon() : VOSDaemon("example") { } void setup() { addArg("example", "value", "Example argument. <value> is any string.", 'e', "VOSDAEMON_EXAMPLE"); addFlag("test", "Test flag.", 't', "VOSDAEMON_TEST_FLAG"); } void preRun() { if(argGiven("example")) cerr << "example arg is: " << getArg("example") << endl; if(argGiven("test")) cerr << "test flag was given.\n"; else cerr << "test flag was not given.\n"; } virtual void loop() { cerr << "loop!\n"; } }; int main(int argc, char** argv) { Property::registerExtenders(); MyDaemon myDaemon("mydaemon"); myDaemon->setup(); myDaemon->checkArgs(argc, argv); myDaemon->preRun(); cerr << "Now running in the background...\n"; myDaemon->run(); }
Definition at line 110 of file vosdaemon.hh.
|
Constructor. If site is omitted, a LocalSlocketSite will be created with select timeout -1. (You can dynamic_cast the LocalSite (returned by getSite()) to LocalSocketSite) once its created Definition at line 94 of file vosdaemon.cc. |
|
Destructor.
Definition at line 82 of file vosdaemon.cc. |
|
Definition at line 539 of file vosdaemon.cc. |
|
Definition at line 526 of file vosdaemon.cc. |
|
Add flag argument . This function must be called before calling run(). Definition at line 545 of file vosdaemon.cc. |
|
Get presence of a command line option with no argiment (ie, a flag). If it was given, true will be returned. If it wasn't then false will be returned. If it wasn't added using addArg, then a runtime_error exception will be thrown. Definition at line 559 of file vosdaemon.cc. Referenced by getFlag(). |
|
Parse the command line arguments. Some built in arguments may override any previously set settings. Any arguments you added with addArg will also be recognized, and after a call to this function, will be available from getArg. The following built in command line arguments may override some settings: -L Log file -E Log level -s Save file (if enabled) -S Save Frequency (if save is enabled) -l Load state from this file (if enabled) -N don't fork (if fork is available on this platform) Definition at line 154 of file vosdaemon.cc. |
|
Print the internal command-line argument map to stderr.
Definition at line 565 of file vosdaemon.cc. |
|
Get value of a command line argument. If it wasn't given then the default value is returned. If it wasn't added using addArg(), or it was added using addFlag(), then a runtime_error exception will be thrown. Definition at line 551 of file vosdaemon.cc. |
|
Same as argGiven.
Definition at line 268 of file vosdaemon.hh. |
|
Get the site.
Definition at line 155 of file vosdaemon.hh. |
|
Virtual method called right after flushIncomingBuffers. Subclasses can use this to perform actions during the run loop. Note that the frequency with which this gets called depends on the select timeout (see setTimeoutOnSelect()). Definition at line 201 of file vosdaemon.hh. Referenced by run(). |
|
Print out help for command-line arguments.
Definition at line 317 of file vosdaemon.cc. Referenced by checkArgs(). |
|
Start running the daemon in the background.
Definition at line 396 of file vosdaemon.cc. |
|
Save current state of the site and it's objects to the file previously set by setSaveFile or command line arguments.
Definition at line 500 of file vosdaemon.cc. |
|
Log to the named file. Must be called before run. Definition at line 144 of file vosdaemon.cc. |
|
Set the log level.
Definition at line 148 of file vosdaemon.cc. |
|
Set the name of the root object (for loading). Must be called before run. |
|
Set the root object (for saving). Must be called before run. |
|
Set the save file. Must be called before run. |
|
Set the save frequency. Must be called before run. |