Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

VOSDaemon Class Reference
[Applibs]

This class contains many convenient features useful for running a background service, a.k.a. More...

#include <vosdaemon.hh>

List of all members.

Public Member Functions


Detailed Description

This class contains many convenient features useful for running a background service, a.k.a.

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:

--help, -h and -?
Print help message and quit
--log, -L
Set log file
--loglevel, -E
Set log level
--save, -s
Reserved for a future autosave feature
--safe-feq, -S
Reserved for a future autosave feature
--load, -l
Reserved for a future COD loading feature
--name
Reserved for a future COD loading feature

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 & Destructor Documentation

VOSDaemon::VOSDaemon char *  progName,
LocalSite site = 0
 

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.

VOSDaemon::~VOSDaemon  )  [virtual]
 

Destructor.

Definition at line 82 of file vosdaemon.cc.


Member Function Documentation

void VOSDaemon::addArg string  argName,
string  valueLabel,
string  description,
string  defaultValue,
char  argChar,
string  envVar = ""
 

Definition at line 539 of file vosdaemon.cc.

void VOSDaemon::addArg string  argName,
string  valueLabel,
string  description,
char  argChar,
string  envVar = ""
 

Definition at line 526 of file vosdaemon.cc.

Referenced by addArg(), and addFlag().

void VOSDaemon::addFlag string  flagName,
string  description,
char  flagChar,
string  envVar
 

Add flag argument .

This function must be called before calling run().

Definition at line 545 of file vosdaemon.cc.

bool VOSDaemon::argGiven string  argName  ) 
 

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

void VOSDaemon::checkArgs int  argc,
char **  argv
[virtual]
 

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.

void VOSDaemon::debugArgs  ) 
 

Print the internal command-line argument map to stderr.

Definition at line 565 of file vosdaemon.cc.

string VOSDaemon::getArg string  argName  ) 
 

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.

bool VOSDaemon::getFlag string  argName  )  [inline]
 

Same as argGiven.

Definition at line 268 of file vosdaemon.hh.

LocalSite* VOSDaemon::getSite  )  [inline]
 

Get the site.

Definition at line 155 of file vosdaemon.hh.

virtual void VOSDaemon::loop  )  [inline, virtual]
 

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

void VOSDaemon::printHelp  )  [virtual]
 

Print out help for command-line arguments.

Definition at line 317 of file vosdaemon.cc.

Referenced by checkArgs().

void VOSDaemon::run  )  [virtual]
 

Start running the daemon in the background.

Definition at line 396 of file vosdaemon.cc.

void VOSDaemon::save  )  [virtual]
 

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.

void VOSDaemon::setLogFile char *  file  ) 
 

Log to the named file.

Must be called before run.

Definition at line 144 of file vosdaemon.cc.

void VOSDaemon::setLogLevel int  lev  ) 
 

Set the log level.

Definition at line 148 of file vosdaemon.cc.

void VOSDaemon::setRootName char *  name  ) 
 

Set the name of the root object (for loading).

Must be called before run.

void VOSDaemon::setRootObject Vobject *  rood  ) 
 

Set the root object (for saving).

Must be called before run.

void VOSDaemon::setSaveFile char *  savefile  ) 
 

Set the save file.

Must be called before run.

void VOSDaemon::setSaveFrequency int  freq  ) 
 

Set the save frequency.

Must be called before run.


The documentation for this class was generated from the following files:
Generated on Tue Aug 12 03:55:58 2003 for Interreality Project - VOS by doxygen 1.3.2