Make WvStreams compile with gcc 4.4.
[wvstreams.git] / streams / wvstreamsdaemon.cc
blobf3befb06ff8e43dc9ea214da855c744a5fa5453e
1 /* -*- Mode: C++ -*-
2 * Worldvisions Tunnel Vision Software:
3 * Copyright (C) 1997-2005 Net Integration Technologies, Inc.
5 * High-level abstraction for creating daemon processes that do
6 * nothing but listen on a list of WvStreams and add connections
7 * to the global list.
8 */
10 #include "wvstreamsdaemon.h"
12 #ifndef _WIN32
13 #include <signal.h>
14 #endif
16 void WvStreamsDaemon::init(WvDaemonCallback cb)
18 do_full_close = false;
19 setcallback(cb);
20 #ifndef _WIN32
21 signal(SIGPIPE, SIG_IGN);
22 #endif
25 void WvStreamsDaemon::do_start()
27 WvDaemon::do_start();
29 callback();
32 void WvStreamsDaemon::do_run()
34 if (streams.isempty())
36 log(WvLog::Error, "No streams; exiting\n");
37 die();
40 while (should_run())
42 WvDaemon::do_run();
43 WvIStreamList::globallist.runonce();
47 void WvStreamsDaemon::do_stop()
49 WvIStreamList::Iter stream(streams);
50 for (stream.rewind(); stream.next(); )
51 WvIStreamList::globallist.unlink(stream.ptr());
52 streams.zap();
53 if (do_full_close || want_to_die())
54 WvIStreamList::globallist.zap();
56 WvDaemon::do_stop();
59 void WvStreamsDaemon::add_stream(IWvStream *istream,
60 bool autofree, const char *id)
62 streams.append(istream, false, id);
63 // FIXME: we should pass in "id" here, but things are not happy in
64 // const-correctness-land.
65 WvIStreamList::globallist.append(istream, autofree, id);
68 void WvStreamsDaemon::add_restart_stream(IWvStream *istream, bool autofree,
69 const char *id)
71 add_stream(istream, autofree, id);
73 istream->setclosecallback(wv::bind(&WvStreamsDaemon::restart_close_cb,
74 this, istream, id));
77 void WvStreamsDaemon::add_die_stream(IWvStream *istream,
78 bool autofree, const char *id)
80 add_stream(istream, autofree, id);
82 istream->setclosecallback(wv::bind(&WvStreamsDaemon::die_close_cb, this,
83 istream, id));
86 void WvStreamsDaemon::restart_close_cb(IWvStream *s, const char *id)
88 if (should_run())
90 WvString err = s->geterr() ? s->errstr() : "no error";
91 log(WvLog::Error, "%s is closed (%s); restarting\n",
92 id ? id : "Stream", err);
93 restart();
97 void WvStreamsDaemon::die_close_cb(IWvStream *s, const char *id)
99 if (should_run())
101 WvString err = s->geterr() ? s->errstr() : "no error";
102 log(WvLog::Error, "%s is closed (%s); dying\n",
103 id ? id : "Stream", err);
104 die();
108 void WvStreamsDaemon::setcallback(WvDaemonCallback cb)
110 callback = cb;