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 
52 /** Destructor. */
54 {
55 }
56 
57 
58 void
60 {
61  agent_alive_ = false;
62  cfg_agent_ = config->get_string("/openprs-agent/agent");
63  openprs.lock();
64  openprs->signal_msg_rcvd()
65  .connect(boost::bind(&OpenPRSAgentThread::handle_message, this, _1, _2));
66  openprs->transmit_command_f(openprs_kernel_name, "add (! (= @@AGENT_NAME \"%s\"))", 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 
77 void
79 {
80  if (agent_alive_) {
81  openprs.lock();
82  fawkes::Time now, now_sys;
83  clock->get_time(now);
84  clock->get_systime(now_sys);
85  openprs->send_message_f(openprs_kernel_name, "(fawkes-time %lill %lill %lill %lill)",
86  now.get_sec(), now.get_usec(), now_sys.get_sec(), now_sys.get_usec());
87  //openprs->transmit_command(openprs_kernel_name, "show intention");
88  openprs.unlock();
89  }
90 }
91 
92 
93 void
94 OpenPRSAgentThread::handle_message(std::string sender, std::string message)
95 {
96  // remove newlines and anything beyond
97  message.erase(std::remove(message.begin(), message.end(), '\n'), message.end());
98 
99  logger->log_debug(name(), "Received message from %s: %s", sender.c_str(), message.c_str());
100  if (sender == openprs_kernel_name && message == "openprs-agent-init-done") {
101  openprs.lock();
102  openprs->transmit_command(openprs_kernel_name, "include \"agent-init.inc\"");
103  openprs->transmit_command_f(openprs_kernel_name, "include \"%s.inc\"", cfg_agent_.c_str());
104  openprs->transmit_command(openprs_kernel_name, "add (agent-init)");
105  openprs.unlock();
106  agent_alive_ = true;
107  } else if (sender == "mp-oprs" && message == ("(unknown " + openprs_kernel_name + ")")) {
108  logger->log_error(name(), "OpenPRS kernel has died, agent no longer alive");
109  agent_alive_ = false;
110  }
111 }
Mode
OPRS kernel operation mode.
Definition: openprs.h:45
virtual ~OpenPRSAgentThread()
Destructor.
void get_time(struct timeval *tv) const
Returns the time of the selected time source.
Definition: clock.cpp:176
Fawkes library namespace.
void get_systime(struct timeval *tv) const
Returns the system time.
Definition: clock.cpp:239
const std::string openprs_kernel_name
The name of the kernel created for this thread.
Definition: openprs.h:58
A class for handling time.
Definition: time.h:91
Thread class encapsulation of pthreads.
Definition: thread.h:42
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:44
OpenPRS kernel creation and communication aspect.
Definition: openprs.h:39
void set_openprs_gdb_delay(const bool enable_gdb_delay)
Enable/disable GDB delay.
Definition: openprs.cpp:109
Clock * clock
By means of this member access to the clock is given.
Definition: clock.h:45
Thread aspect to use blocked timing.
virtual void finalize()
Finalize the thread.
virtual void loop()
Code to execute in the thread.
void add_openprs_data_path(const std::string &path)
Add an OpenPRS data path.
Definition: openprs.cpp:94
const char * name() const
Get name of thread.
Definition: thread.h:95
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
OpenPRSAgentThread(OpenPRSAspect::Mode oprs_mode, bool gdb_delay)
Constructor.
LockPtr< OpenPRSComm > openprs
OpenPRS kernel communication wrapper.
Definition: openprs.h:57
long get_sec() const
Get seconds.
Definition: time.h:110
long get_usec() const
Get microseconds.
Definition: time.h:112
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual void init()
Initialize the thread.
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:44
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.