Fawkes API Fawkes Development Version
openprs_agent_thread.cpp
1
2/***************************************************************************
3 * openprs_agent_thread.cpp - OpenPRS agent thread
4 *
5 * Created: Fri Aug 22 13:57:22 2014
6 * Copyright 2014 Tim Niemueller [www.niemueller.de]
7 ****************************************************************************/
8
9/* This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Library General Public License for more details.
18 *
19 * Read the full text in the LICENSE.GPL file in the doc directory.
20 */
21
22#include "openprs_agent_thread.h"
23
24#include <logging/logger.h>
25#include <plugins/openprs/utils/openprs_comm.h>
26#include <utils/time/time.h>
27
28#include <unistd.h>
29
30using namespace fawkes;
31
32/** @class OpenPRSAgentThread "openprs_agent_thread.h"
33 * OpenPRS agent thread.
34 * @author Tim Niemueller
35 */
36
37/** Constructor.
38 * @param oprs_mode whether to operate in console or graphical mode
39 * @param gdb_delay whether to instruct mod_utils to wait for a while for
40 * a gdb connection or not.
41 */
43: Thread("OpenPRSAgentThread", Thread::OPMODE_WAITFORWAKEUP),
44 BlockedTimingAspect(BlockedTimingAspect::WAKEUP_HOOK_THINK),
45 OpenPRSAspect("openprs-agent", oprs_mode)
46{
47 add_openprs_data_path(SRCDIR "/data");
48 set_openprs_gdb_delay(gdb_delay);
49}
50
51/** Destructor. */
53{
54}
55
56void
58{
59 agent_alive_ = false;
60 cfg_agent_ = config->get_string("/openprs-agent/agent");
61 openprs.lock();
62 openprs->signal_msg_rcvd().connect(
63 boost::bind(&OpenPRSAgentThread::handle_message, this, _1, _2));
64 openprs->transmit_command_f(openprs_kernel_name,
65 "add (! (= @@AGENT_NAME \"%s\"))",
66 cfg_agent_.c_str());
67 openprs->transmit_command(openprs_kernel_name, "include \"agent-settings.inc\"");
68 openprs.unlock();
69}
70
71void
73{
74}
75
76void
78{
79 if (agent_alive_) {
80 openprs.lock();
81 fawkes::Time now, now_sys;
82 clock->get_time(now);
83 clock->get_systime(now_sys);
84 openprs->send_message_f(openprs_kernel_name,
85 "(fawkes-time %lill %lill %lill %lill)",
86 now.get_sec(),
87 now.get_usec(),
88 now_sys.get_sec(),
89 now_sys.get_usec());
90 //openprs->transmit_command(openprs_kernel_name, "show intention");
91 openprs.unlock();
92 }
93}
94
95void
96OpenPRSAgentThread::handle_message(std::string sender, std::string message)
97{
98 // remove newlines and anything beyond
99 message.erase(std::remove(message.begin(), message.end(), '\n'), message.end());
100
101 logger->log_debug(name(), "Received message from %s: %s", sender.c_str(), message.c_str());
102 if (sender == openprs_kernel_name && message == "openprs-agent-init-done") {
103 openprs.lock();
104 openprs->transmit_command(openprs_kernel_name, "include \"agent-init.inc\"");
105 openprs->transmit_command_f(openprs_kernel_name, "include \"%s.inc\"", cfg_agent_.c_str());
106 openprs->transmit_command(openprs_kernel_name, "add (agent-init)");
107 openprs.unlock();
108 agent_alive_ = true;
109 } else if (sender == "mp-oprs" && message == ("(unknown " + openprs_kernel_name + ")")) {
110 logger->log_error(name(), "OpenPRS kernel has died, agent no longer alive");
111 agent_alive_ = false;
112 }
113}
virtual void finalize()
Finalize the thread.
OpenPRSAgentThread(OpenPRSAspect::Mode oprs_mode, bool gdb_delay)
Constructor.
virtual ~OpenPRSAgentThread()
Destructor.
virtual void loop()
Code to execute in the thread.
virtual void init()
Initialize the thread.
Thread aspect to use blocked timing.
Clock * clock
By means of this member access to the clock is given.
Definition: clock.h:42
void get_time(struct timeval *tv) const
Returns the time of the selected time source.
Definition: clock.cpp:161
void get_systime(struct timeval *tv) const
Returns the system time.
Definition: clock.cpp:215
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:41
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:41
OpenPRS kernel creation and communication aspect.
Definition: openprs.h:39
LockPtr< OpenPRSComm > openprs
OpenPRS kernel communication wrapper.
Definition: openprs.h:56
void add_openprs_data_path(const std::string &path)
Add an OpenPRS data path.
Definition: openprs.cpp:92
Mode
OPRS kernel operation mode.
Definition: openprs.h:44
void set_openprs_gdb_delay(const bool enable_gdb_delay)
Enable/disable GDB delay.
Definition: openprs.cpp:106
const std::string openprs_kernel_name
The name of the kernel created for this thread.
Definition: openprs.h:57
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
A class for handling time.
Definition: time.h:93
long get_usec() const
Get microseconds.
Definition: time.h:127
long get_sec() const
Get seconds.
Definition: time.h:117
Fawkes library namespace.