26 #include <core/threading/thread.h> 27 #include <utils/system/signal.h> 28 #include <utils/system/argparser.h> 29 #include <blackboard/remote.h> 30 #include <interfaces/SwitchInterface.h> 31 #include <utils/time/time.h> 39 # include <libdaemon/dfork.h> 40 # include <libdaemon/dlog.h> 41 # include <libdaemon/dpid.h> 42 # include <sys/stat.h> 43 # include <sys/wait.h> 69 while (! (__bb && __bb->is_alive() && __switch_if->is_valid())) {
71 printf(
"Lost connection to blackboard\n");
72 __bb->close(__switch_if);
77 printf(
"Trying to connect to remote BB...");
80 printf(
"succeeded\n");
91 if ((now - __until) >= 0) {
98 while (! __switch_if->msgq_empty()) {
101 if (msg->
value() > 0.0) {
102 __beep.beep_on(msg->
value());
112 float duration = fabs(msg->
duration());
113 float value = fabs(msg->
value());
116 __until =
new Time();
117 *__until += duration;
118 __beep.beep_on(value);
125 __switch_if->msgq_pop();
150 usage(
const char *progname)
155 #ifdef HAVE_LIBDAEMON 159 daemon_retval_send(-1);
160 daemon_retval_done();
161 daemon_pid_file_remove();
165 daemonize(
int argc,
char **argv)
168 mode_t old_umask = umask(0);
171 daemon_retval_init();
174 if ((pid = daemon_fork()) < 0) {
181 if ((ret = daemon_retval_wait(20)) < 0) {
182 daemon_log(LOG_ERR,
"Could not recieve return value from daemon process.");
187 daemon_log(LOG_ERR,
"*** Daemon startup failed, see syslog for details. ***");
190 daemon_log(LOG_ERR,
"Daemon failed to close file descriptors");
193 daemon_log(LOG_ERR,
"Daemon failed to create PID file");
202 #ifdef DAEMON_CLOSE_ALL_AVAILABLE 203 if (daemon_close_all(-1) < 0) {
204 daemon_log(LOG_ERR,
"Failed to close all file descriptors: %s", strerror(errno));
206 daemon_retval_send(1);
212 if (daemon_pid_file_create() < 0) {
213 printf(
"Could not create PID file (%s).", strerror(errno));
214 daemon_log(LOG_ERR,
"Could not create PID file (%s).", strerror(errno));
217 daemon_retval_send(2);
222 daemon_retval_send(0);
224 daemon_log(LOG_INFO,
"Sucessfully started");
233 const char *fawkes_pid_file;
239 fawkes_daemon_pid_file_proc()
241 return fawkes_pid_file;
243 #endif // HAVE_LIBDAEMON 250 main(
int argc,
char **argv)
255 const char *user = NULL;
256 const char *group = NULL;
258 user = argp->
arg(
"u");
261 group = argp->
arg(
"g");
264 #ifdef HAVE_LIBDAEMON 270 daemon_pid_file_ident = daemon_log_ident = daemon_ident_from_argv0(argv[0]);
271 if ( argp->
arg(
"D") != NULL ) {
272 fawkes_pid_file = argp->
arg(
"D");
273 daemon_pid_file_proc = fawkes_daemon_pid_file_proc;
279 if ((pid = daemon_pid_file_is_running()) < 0) {
280 daemon_log(LOG_ERR,
"Fawkes daemon not running.");
285 if ((ret = daemon_pid_file_kill_wait(SIGINT, 5)) < 0) {
286 daemon_log(LOG_WARNING,
"Failed to kill daemon");
288 return (ret < 0) ? 1 : 0;
293 return (daemon_pid_file_is_running() < 0);
297 if ((pid = daemon_pid_file_is_running()) >= 0) {
298 daemon_log(LOG_ERR,
"Daemon already running on (PID %u)", pid);
302 pid = daemonize(argc, argv);
313 printf(
"Daemonizing support is not available.\n" 314 "(libdaemon[-devel] was not available at compile time)\n");
328 SignalManager::register_handler(SIGINT, &beepd);
329 SignalManager::register_handler(SIGTERM, &beepd);
334 Thread::destroy_main();
336 #ifdef HAVE_LIBDAEMON float value() const
Get value value.
SetMessage Fawkes BlackBoard Interface Message.
const char * arg(const char *argn)
Get argument value.
void handle_signal(int signum)
Handle signals.
Fawkes library namespace.
Interface for signal handling.
Parse command line arguments.
A class for handling time.
float value() const
Get value value.
Thread class encapsulation of pthreads.
Simple speaker beep controller.
bool is_enabled() const
Get enabled value.
SwitchInterface Fawkes BlackBoard Interface.
Base class for exceptions in Fawkes.
DisableSwitchMessage Fawkes BlackBoard Interface Message.
float duration() const
Get duration value.
EnableSwitchMessage Fawkes BlackBoard Interface Message.
FawkesBeepDaemon()
Constructor.
void join()
Join the thread.
bool has_arg(const char *argn)
Check if argument has been supplied.
EnableDurationMessage Fawkes BlackBoard Interface Message.
The BlackBoard abstract class.
virtual void loop()
Code to execute in the thread.
void start(bool wait=true)
Call this method to start the thread.