00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "bblogreplay_plugin.h"
00025 #include "logreplay_thread.h"
00026 #include "logreplay_bt_thread.h"
00027
00028 #include <utils/time/time.h>
00029
00030 #include <set>
00031 #include <memory>
00032
00033 #include <cstring>
00034 #include <cerrno>
00035 #include <sys/types.h>
00036 #include <sys/stat.h>
00037 #include <unistd.h>
00038
00039 using namespace fawkes;
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 BlackBoardLogReplayPlugin::BlackBoardLogReplayPlugin(Configuration *config)
00053 : Plugin(config)
00054 {
00055 std::set<std::string> logs;
00056
00057 std::string prefix = "/fawkes/bblogreplay/";
00058
00059 std::string scenario = "";
00060 try {
00061 scenario = config->get_string((prefix + "scenario").c_str());
00062 } catch (Exception &e) {
00063 e.append("No scenario defined, configure %sscenario", prefix.c_str());
00064 throw;
00065 }
00066
00067 std::string scenario_prefix = prefix + scenario + "/";
00068 std::string logs_prefix = scenario_prefix + "logs/";
00069
00070 std::string logdir = LOGDIR;
00071 try {
00072 logdir = config->get_string((scenario_prefix + "logdir").c_str());
00073 } catch (Exception &e) { }
00074 struct stat s;
00075 int err = stat(logdir.c_str(), &s);
00076 if (err != 0) {
00077 char buf[1024];
00078 Exception se ("Cannot access logdir %s (%s)",
00079 logdir.c_str(), strerror_r(errno, buf, 1024));
00080 } else if ( ! S_ISDIR(s.st_mode) ) {
00081 throw Exception("Logdir path %s is not a directory", logdir.c_str());
00082 }
00083
00084 bool scenario_loop_replay = false;
00085 bool scenario_non_blocking = false;
00086 float scenario_grace_period = 0.001;
00087 try {
00088 scenario_loop_replay = config->get_bool((prefix + "loop").c_str());
00089 } catch (Exception &e) {}
00090 try {
00091 scenario_loop_replay = config->get_bool((scenario_prefix + "loop").c_str());
00092 } catch (Exception &e) {}
00093 try {
00094 scenario_non_blocking = config->get_bool((prefix + "non_blocking").c_str());
00095 } catch (Exception &e) {}
00096 try {
00097 scenario_non_blocking = config->get_bool((scenario_prefix + "non_blocking").c_str());
00098 } catch (Exception &e) {}
00099 try {
00100 scenario_grace_period = config->get_float((prefix + "grace_period").c_str());
00101 } catch (Exception &e) {}
00102 try {
00103 scenario_grace_period = config->get_float((scenario_prefix + "grace_period").c_str());
00104 } catch (Exception &e) {}
00105
00106 std::auto_ptr<Configuration::ValueIterator> i(config->search(logs_prefix.c_str()));
00107 while (i->next()) {
00108 std::string log_name = std::string(i->path()).substr(logs_prefix.length());
00109 log_name = log_name.substr(0, log_name.find("/"));
00110
00111 if ( logs.find(log_name) == logs.end() ) {
00112 std::string log_prefix = logs_prefix + log_name + "/";
00113
00114 printf("Log name: %s log_prefix: %s\n", log_name.c_str(), log_prefix.c_str());
00115
00116 std::string log_file = "";
00117 bool loop_replay = scenario_loop_replay;
00118 bool non_blocking = scenario_non_blocking;
00119 float grace_period = scenario_grace_period;
00120 std::string hook_str = "";
00121
00122 try {
00123 log_file = config->get_string((log_prefix + "file").c_str());
00124 } catch (Exception &e) {
00125 throw;
00126 }
00127
00128 try {
00129 loop_replay = config->get_bool((log_prefix + "loop").c_str());
00130 } catch (Exception &e) {}
00131 try {
00132 non_blocking = config->get_bool((log_prefix + "non_blocking").c_str());
00133 } catch (Exception &e) {}
00134 try {
00135 hook_str = config->get_string((log_prefix + "hook").c_str());
00136 } catch (Exception &e) {}
00137 try {
00138 grace_period = config->get_float((log_prefix + "grace_period").c_str());
00139 } catch (Exception &e) {}
00140
00141
00142 if (hook_str != "") {
00143 BlockedTimingAspect::WakeupHook hook;
00144 hook = BlockedTimingAspect::WAKEUP_HOOK_PRE_LOOP;
00145
00146 if (hook_str == "pre_loop") {
00147 hook = BlockedTimingAspect::WAKEUP_HOOK_PRE_LOOP;
00148 } else if (hook_str == "sensor") {
00149 hook = BlockedTimingAspect::WAKEUP_HOOK_SENSOR;
00150 } else if (hook_str == "sensor_process") {
00151 hook = BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PROCESS;
00152 } else if (hook_str == "worldstate") {
00153 hook = BlockedTimingAspect::WAKEUP_HOOK_WORLDSTATE;
00154 } else if (hook_str == "think") {
00155 hook = BlockedTimingAspect::WAKEUP_HOOK_THINK;
00156 } else if (hook_str == "skill") {
00157 hook = BlockedTimingAspect::WAKEUP_HOOK_SKILL;
00158 } else if (hook_str == "act") {
00159 hook = BlockedTimingAspect::WAKEUP_HOOK_ACT;
00160 } else if (hook_str == "act_exec") {
00161 hook = BlockedTimingAspect::WAKEUP_HOOK_ACT_EXEC;
00162 } else if (hook_str == "post_loop") {
00163 hook = BlockedTimingAspect::WAKEUP_HOOK_POST_LOOP;
00164 } else {
00165 throw Exception("Invalid hook '%s' for %s",
00166 hook_str.c_str(), i->get_string().c_str());
00167 }
00168
00169 BBLogReplayBlockedTimingThread *lrbt_thread;
00170 lrbt_thread = new BBLogReplayBlockedTimingThread(hook,
00171 i->get_string().c_str(),
00172 logdir.c_str(),
00173 scenario.c_str(),
00174 grace_period,
00175 loop_replay,
00176 non_blocking);
00177 thread_list.push_back(lrbt_thread);
00178 } else {
00179 BBLogReplayThread *lr_thread = new BBLogReplayThread(i->get_string().c_str(),
00180 logdir.c_str(),
00181 scenario.c_str(),
00182 grace_period,
00183 loop_replay);
00184 thread_list.push_back(lr_thread);
00185 }
00186
00187 logs.insert(log_name);
00188 }
00189 }
00190
00191 if ( thread_list.empty() ) {
00192 throw Exception("No interfaces configured for log replay, aborting");
00193 }
00194 }
00195
00196 PLUGIN_DESCRIPTION("Replay BlackBoard log files")
00197 EXPORT_PLUGIN(BlackBoardLogReplayPlugin)