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 
30 using 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 
56 void
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 
71 void
73 {
74 }
75 
76 void
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 
95 void
96 OpenPRSAgentThread::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 }
fawkes::Time::get_sec
long get_sec() const
Definition: time.h:123
fawkes::OpenPRSAspect
Definition: openprs.h:44
fawkes::Clock::get_time
void get_time(struct timeval *tv) const
Returns the time of the selected time source.
Definition: clock.cpp:168
fawkes::OpenPRSAspect::set_openprs_gdb_delay
void set_openprs_gdb_delay(const bool enable_gdb_delay)
Enable/disable GDB delay.
Definition: openprs.cpp:112
fawkes::BlockedTimingAspect
Definition: blocked_timing.h:56
fawkes::Thread::name
const char * name() const
Definition: thread.h:100
fawkes::ClockAspect::clock
Clock * clock
Definition: clock.h:56
fawkes::OpenPRSAspect::openprs
LockPtr< OpenPRSComm > openprs
Definition: openprs.h:62
fawkes::OpenPRSAspect::openprs_kernel_name
const std::string openprs_kernel_name
Definition: openprs.h:63
fawkes::LoggingAspect::logger
Logger * logger
Definition: logging.h:53
OpenPRSAgentThread::~OpenPRSAgentThread
virtual ~OpenPRSAgentThread()
Destructor.
Definition: openprs_agent_thread.cpp:52
fawkes::Logger::log_error
virtual void log_error(const char *component, const char *format,...)=0
fawkes
OpenPRSAgentThread::init
virtual void init()
Initialize the thread.
Definition: openprs_agent_thread.cpp:57
fawkes::Clock::get_systime
void get_systime(struct timeval *tv) const
Returns the system time.
Definition: clock.cpp:222
fawkes::ConfigurableAspect::config
Configuration * config
Definition: configurable.h:53
fawkes::Time
Definition: time.h:98
fawkes::OpenPRSAspect::add_openprs_data_path
void add_openprs_data_path(const std::string &path)
Add an OpenPRS data path.
Definition: openprs.cpp:98
fawkes::Thread
Definition: thread.h:45
fawkes::OpenPRSAspect::Mode
Mode
OPRS kernel operation mode.
Definition: openprs.h:50
OpenPRSAgentThread::OpenPRSAgentThread
OpenPRSAgentThread(OpenPRSAspect::Mode oprs_mode, bool gdb_delay)
Constructor.
Definition: openprs_agent_thread.cpp:42
fawkes::Configuration::get_string
virtual std::string get_string(const char *path)=0
fawkes::Time::get_usec
long get_usec() const
Definition: time.h:133
OpenPRSAgentThread::finalize
virtual void finalize()
Finalize the thread.
Definition: openprs_agent_thread.cpp:72
fawkes::Logger::log_debug
virtual void log_debug(const char *component, const char *format,...)=0
OpenPRSAgentThread::loop
virtual void loop()
Code to execute in the thread.
Definition: openprs_agent_thread.cpp:77