Fawkes API  Fawkes Development Version
control_thread.cpp
1 
2 /***************************************************************************
3  * control_thread.cpp - Fawkes ECLiPSe Control Thread
4  *
5  * Created: Wed Jul 15 15:09:09 2009
6  * Copyright 2009 Daniel Beck
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "control_thread.h"
24 
25 #include "eclipse_thread.h"
26 
27 #include <core/exception.h>
28 #include <interfaces/TestInterface.h>
29 
30 #include <cstdlib>
31 #include <cstring>
32 
33 /** @class AgentControlThread "control_thread.h"
34  * This thread controls the agent thread by sending signals.
35  * @author Daniel Beck
36  */
37 
38 using namespace fawkes;
39 
40 /** Constructor.
41  * @param eclipse_thread the ECLiPSe agent thread
42  */
44 : Thread("AgentControlThread", Thread::OPMODE_WAITFORWAKEUP),
45  BlockedTimingAspect(BlockedTimingAspect::WAKEUP_HOOK_THINK),
46  m_eclipse_thread(eclipse_thread)
47 {
48 }
49 
50 /** Destructor. */
52 {
53 }
54 
55 void
57 {
58  // open & register interfaces
59  m_test_iface = blackboard->open_for_writing<TestInterface>("eclipse_clp_test");
60  m_debug_iface = blackboard->open_for_reading<EclipseDebuggerInterface>("eclipse_clp_connect");
61 
62  fawkes_path_ = "";
63  simulation_shutdown_script_ = "";
64  allow_shutdown_ = false;
65  try {
66  logger->log_info(name(), "reading config /eclipse-clp/gazebo/allow-shutdown");
67  allow_shutdown_ = config->get_bool("/eclipse-clp/gazebo/allow-shutdown");
68  } catch (...) {
69  }
70  try {
71  logger->log_info(name(), "reading config fawkes-path");
72  fawkes_path_ = strdup(config->get_string("/eclipse-clp/gazebo/fawkes-path").c_str());
73  } catch (...) {
74  logger->log_error(name(), "error reading config value: /eclipse-clp/gazebo/fawkes-path");
75  allow_shutdown_ = false;
76  }
77  try {
78  logger->log_info(name(), "reading config simulation_shutdown_script_");
79  simulation_shutdown_script_ =
80  strdup(config->get_string("/eclipse-clp/gazebo/simulation-shutdown-script").c_str());
81  } catch (...) {
83  "error reading config value: /eclipse-clp/gazebo/simulation-shutdown-script");
84  allow_shutdown_ = false;
85  }
86 
87  logger->log_info(name(), "testing allow_shutdown_");
88  if (allow_shutdown_) {
89  logger->log_info(name(), "opening ExitSimulationInterface");
90  m_exit_iface = blackboard->open_for_writing<ExitSimulationInterface>("eclipse_clp_exit");
91  }
92 }
93 
94 bool
96 {
97  m_eclipse_thread->post_event("terminate");
98 
99  return true;
100 }
101 
102 void
104 {
105  // close interfaces
106  blackboard->close(m_test_iface);
107  blackboard->close(m_debug_iface);
108  if (allow_shutdown_) {
109  blackboard->close(m_exit_iface);
110  }
111 }
112 
113 void
115 {
116  //if the debug interface has a writer (so tktools module is loaded),
117  //then post event to check for tktool connection request within eclipse
118  if (m_debug_iface->has_writer()) {
119  m_eclipse_thread->post_event("check_debug_msg");
120  }
121 
122  // this is only used by the dummy agent
123  while (!m_test_iface->msgq_empty()) {
124  if (m_test_iface->msgq_first_is<TestInterface::CalculateMessage>()) {
127 
128  m_test_iface->set_result(msg->summand() + msg->addend());
129  }
130 
131  m_test_iface->msgq_pop();
132  }
133  m_test_iface->write();
134 
135  // this is used to receive exit messages to quit fawkes and the simulation
136  if (allow_shutdown_) {
137  while (!m_exit_iface->msgq_empty()) {
139  logger->log_info(name(),
140  "shutting down: %s%s",
141  fawkes_path_.c_str(),
142  simulation_shutdown_script_.c_str());
143  std::string command = fawkes_path_ + simulation_shutdown_script_;
144  int cmd_rv = system(command.c_str());
145  if (cmd_rv != 0) {
146  logger->log_warn(name(),
147  "Failed to execute '%s'. Return value %d",
148  command.c_str(),
149  cmd_rv);
150  }
151  }
152  m_exit_iface->msgq_pop();
153  }
154  }
155 
156  //m_eclipse_thread->post_event( "update" );
157 
158  /* call eclipse thread (that thread has no blocked timing aspect
159  * and is therefore not called by the mainapp, so this has to be
160  * done here)
161  */
162  //m_eclipse_thread->loop();
163 }
fawkes::Interface::msgq_first_is
bool msgq_first_is()
Check if first message has desired type.
Definition: interface.h:313
fawkes::Interface::msgq_pop
void msgq_pop()
Erase first message from queue.
Definition: interface.cpp:1182
fawkes::Interface::msgq_empty
bool msgq_empty()
Check if queue is empty.
Definition: interface.cpp:1029
AgentControlThread::AgentControlThread
AgentControlThread(EclipseAgentThread *eclipse_thread)
Constructor.
Definition: control_thread.cpp:43
fawkes::ExitSimulationInterface
Definition: ExitSimulationInterface.h:37
fawkes::TestInterface::set_result
void set_result(const int32_t new_result)
Set result value.
Definition: TestInterface.cpp:244
AgentControlThread::prepare_finalize_user
virtual bool prepare_finalize_user()
Prepare finalization user implementation.
Definition: control_thread.cpp:95
AgentControlThread::finalize
virtual void finalize()
Finalize the thread.
Definition: control_thread.cpp:103
fawkes::TestInterface::CalculateMessage::addend
int32_t addend() const
Get addend value.
Definition: TestInterface.cpp:605
AgentControlThread::~AgentControlThread
virtual ~AgentControlThread()
Destructor.
Definition: control_thread.cpp:51
fawkes::Configuration::get_bool
virtual bool get_bool(const char *path)=0
fawkes::Logger::log_info
virtual void log_info(const char *component, const char *format,...)=0
fawkes::BlockedTimingAspect
Definition: blocked_timing.h:54
fawkes::Thread::name
const char * name() const
Definition: thread.h:99
EclipseAgentThread
Definition: eclipse_thread.h:40
AgentControlThread::loop
virtual void loop()
Code to execute in the thread.
Definition: control_thread.cpp:114
fawkes::LoggingAspect::logger
Logger * logger
Definition: logging.h:50
fawkes::BlackBoard::close
virtual void close(Interface *interface)=0
fawkes::Logger::log_error
virtual void log_error(const char *component, const char *format,...)=0
fawkes::TestInterface::CalculateMessage
Definition: TestInterface.h:129
fawkes
fawkes::EclipseDebuggerInterface
Definition: EclipseDebuggerInterface.h:37
fawkes::Logger::log_warn
virtual void log_warn(const char *component, const char *format,...)=0
fawkes::Interface::has_writer
bool has_writer() const
Check if there is a writer for the interface.
Definition: interface.cpp:817
EclipseAgentThread::post_event
void post_event(const char *)
Post an event to the ECLiPSe context.
Definition: eclipse_thread.cpp:208
fawkes::ConfigurableAspect::config
Configuration * config
Definition: configurable.h:50
AgentControlThread::init
virtual void init()
Initialize the thread.
Definition: control_thread.cpp:56
fawkes::Thread
Definition: thread.h:44
fawkes::BlackBoardAspect::blackboard
BlackBoard * blackboard
Definition: blackboard.h:47
fawkes::Configuration::get_string
virtual std::string get_string(const char *path)=0
fawkes::BlackBoard::open_for_reading
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
fawkes::TestInterface::CalculateMessage::summand
int32_t summand() const
Get summand value.
Definition: TestInterface.cpp:575
fawkes::TestInterface
Definition: TestInterface.h:37
fawkes::Interface::msgq_first
Message * msgq_first()
Get the first message from the message queue.
Definition: interface.cpp:1167
fawkes::Interface::write
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:497
fawkes::ExitSimulationInterface::ExitSimulationMessage
Definition: ExitSimulationInterface.h:62
fawkes::BlackBoard::open_for_writing
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0