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 }
virtual void loop()
Code to execute in the thread.
virtual bool prepare_finalize_user()
Prepare finalization user implementation.
virtual void init()
Initialize the thread.
virtual ~AgentControlThread()
Destructor.
AgentControlThread(EclipseAgentThread *eclipse_thread)
Constructor.
virtual void finalize()
Finalize the thread.
This thread creates an ECLiPSe context in which the ECLiPSe interpreter and the program are loaded.
void post_event(const char *)
Post an event to the ECLiPSe context.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Definition: blackboard.h:44
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
virtual void close(Interface *interface)=0
Close interface.
Thread aspect to use blocked timing.
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:41
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
EclipseDebuggerInterface Fawkes BlackBoard Interface.
ExitSimulationMessage Fawkes BlackBoard Interface Message.
ExitSimulationInterface Fawkes BlackBoard Interface.
bool msgq_first_is()
Check if first message has desired type.
Definition: interface.h:351
void msgq_pop()
Erase first message from queue.
Definition: interface.cpp:1215
Message * msgq_first()
Get the first message from the message queue.
Definition: interface.cpp:1200
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:501
bool msgq_empty()
Check if queue is empty.
Definition: interface.cpp:1062
bool has_writer() const
Check if there is a writer for the interface.
Definition: interface.cpp:848
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:41
CalculateMessage Fawkes BlackBoard Interface Message.
int32_t summand() const
Get summand value.
int32_t addend() const
Get addend value.
TestInterface Fawkes BlackBoard Interface.
Definition: TestInterface.h:34
void set_result(const int32_t new_result)
Set result value.
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
Fawkes library namespace.