#include <wvstreamsdaemon.h>
This is generally what a modern WvStreams-based daemon should look like.
The WvStreamsDaemonCallback function passed in the constructor is used to populate the globallist with streams that are necessary when the daemon starts, such as listening sockets. These streams are added using the WvStreamsDaemon::add_stream, WvStreamsDaemon::add_die_stream and WvStreamsDaemon::add_restart_stream members, the last two governing what happens to the daemon when the stream is !isok().
Sample usage:
#include <wvstreams/wvdaemon.h> void client_cb(WvStream &stream, void *) { // Echo everything back const char *line = stream.getline(); if (line) stream.print("You said: %s\n", line); } void startup(WvStreamsDaemon &daemon, void *) { WvUnixListener *listener = new WvUnixListener("/tmp/socket", 0700); listener->auto_accept(&WvIStreamList::globallist, client_cb); daemon.add_die_stream(listener, true, "Listener"); } int main(int argc, char **argv) { WvStreamsDaemon daemon("Sample Daemon", "0.1", startup); return daemon.run(argc, argv); }
Definition at line 61 of file wvstreamsdaemon.h.
Public Member Functions | |
WvStreamsDaemon (WvStringParm name, WvStringParm version, WvStreamsDaemonCallback cb, void *ud=NULL) | |
Construct a new WvStreamsDaemon with given name and version, and use the cb function to populate the daemon with its initial streams. | |
WvStreamsDaemon (WvStringParm name, WvStreamsDaemonCallback cb, void *ud=NULL) | |
Construct a new WvStreamsDaemon with given name and use the cb function to populate the daemon with its initial streams. | |
void | add_stream (IWvStream *istream, bool auto_free, char *id) |
Add a stream to the daemon; don't do anything if it goes !isok(). | |
void | add_restart_stream (IWvStream *istream, bool auto_free, char *id) |
Add a stream to the daemon; the daemon will restart, re-populating the initial streams using the callback passed to the constructor, if the stream goes !isok(). | |
void | add_die_stream (IWvStream *istream, bool auto_free, char *id) |
Add a stream to the daemon; if the stream goes !isok() the daemon will exit. | |
void | close_existing_connections_on_restart () |
If this member is called then any existing streams on the globallist added *after* the WvStreamsDaemonCallback was executed will be closed if the daemon restarts; otherwise, they will persist after the restart. | |
void | setcallback (WvStreamsDaemonCallback cb, void *ud=NULL) |
Change the callback function and userdata. | |
const char * | wstype () const |
int | run (const char *argv0) |
Run the daemon with no argument processing. Returns exit status. | |
int | run (int argc, char **argv) |
Run the daemon after doing argument processing. Returns exit status. | |
void | restart () |
Force the daemon to restart as soon as the run callback exits. | |
void | die (int status=0) |
Force the daemon to exit as soon as the run callback exits. | |
bool | want_to_restart () const |
Whether the daemon will restart when the run callback exits. | |
bool | want_to_die () const |
Whether the daemon will quit when the run callback exits. | |
bool | should_run () const |
Whether the daemon should continue runnning. | |
const WvStringList & | extra_args () const |
Remaining args. | |
Static Public Member Functions | |
static WvDaemon * | me () |
Public Attributes | |
WvString | name |
The name and version of the daemon; used for -V and logging. | |
WvString | version |
WvString | pid_file |
The path to the pid file to use for the daemon; defaults to /var/run/name.pid, where name is above. | |
bool | daemonize |
Whether the daemon should daemonize by default (it can be changed by the default options); defaults to false. | |
WvArgs | args |
The arguments the daemon accepts; the defaults are described above. | |
WvLog | log |
The daemon's log mechanism. | |
WvLog::LogLevel | log_level |
bool | syslog |
WvDaemonCallback | load_callback |
See the class description. | |
WvDaemonCallback | start_callback |
WvDaemonCallback | run_callback |
WvDaemonCallback | stop_callback |
WvDaemonCallback | unload_callback |
Protected Member Functions | |
virtual void | do_start () |
virtual void | do_run () |
virtual void | do_stop () |
virtual void | do_load () |
virtual void | do_unload () |
bool | dec_log_level (void *) |
bool | inc_log_level (void *) |
Protected Attributes | |
WvStringList | _extra_args |
void WvStreamsDaemon::add_stream | ( | IWvStream * | istream, | |
bool | auto_free, | |||
char * | id | |||
) |
Add a stream to the daemon; don't do anything if it goes !isok().
This should be called from the WvStreamsDaemonCallback function passed to the constructor.
Definition at line 59 of file wvstreamsdaemon.cc.
References WvIStreamList::append().
Referenced by add_die_stream(), and add_restart_stream().
void WvStreamsDaemon::add_restart_stream | ( | IWvStream * | istream, | |
bool | auto_free, | |||
char * | id | |||
) |
Add a stream to the daemon; the daemon will restart, re-populating the initial streams using the callback passed to the constructor, if the stream goes !isok().
This should be called from the WvStreamsDaemonCallback function passed to the constructor.
Definition at line 68 of file wvstreamsdaemon.cc.
References add_stream().
void WvStreamsDaemon::add_die_stream | ( | IWvStream * | istream, | |
bool | auto_free, | |||
char * | id | |||
) |
Add a stream to the daemon; if the stream goes !isok() the daemon will exit.
This should be called from the WvStreamsDaemonCallback function passed to the constructor.
Definition at line 77 of file wvstreamsdaemon.cc.
References add_stream().
void WvStreamsDaemon::close_existing_connections_on_restart | ( | ) | [inline] |
If this member is called then any existing streams on the globallist added *after* the WvStreamsDaemonCallback was executed will be closed if the daemon restarts; otherwise, they will persist after the restart.
Definition at line 131 of file wvstreamsdaemon.h.
WvArgs WvDaemon::args [inherited] |
The arguments the daemon accepts; the defaults are described above.
Definition at line 118 of file wvdaemon.h.
Referenced by WvDaemon::run().