Fawkes API  Fawkes Development Version
bblogreplay_plugin.cpp
1 
2 /***************************************************************************
3  * bblogreplay_plugin.cpp - Fawkes BlackBoard Log Replay Plugin
4  *
5  * Created: Wed Feb 17 01:53:00 2010
6  * Copyright 2010 Tim Niemueller [www.niemueller.de]
7  * 2010 Masrur Doostdar <doostdar@kbsg.rwth-aachen.de>
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL file in the doc directory.
22  */
23 
24 #include "bblogreplay_plugin.h"
25 
26 #include "logreplay_bt_thread.h"
27 #include "logreplay_thread.h"
28 
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <utils/time/time.h>
32 
33 #include <cerrno>
34 #include <cstring>
35 #include <memory>
36 #include <set>
37 #include <unistd.h>
38 
39 using namespace fawkes;
40 
41 /** @class BlackBoardLogReplayPlugin "bblogger_plugin.h"
42  * BlackBoard log replay plugin.
43  * This plugin replay one or more logfiles into interfaces of the local blackboard
44  *
45  * @author Masrur Doostdar
46  * @author Tim Niemueller
47  */
48 
49 /** Constructor.
50  * @param config Fawkes configuration
51  */
53 {
54  std::set<std::string> logs;
55 
56  std::string prefix = "/fawkes/bblogreplay/";
57 
58  std::string scenario = "";
59  try {
60  scenario = config->get_string((prefix + "scenario").c_str());
61  } catch (Exception &e) {
62  e.append("No scenario defined, configure %sscenario", prefix.c_str());
63  throw;
64  }
65 
66  std::string scenario_prefix = prefix + scenario + "/";
67  std::string logs_prefix = scenario_prefix + "logs/";
68 
69  std::string logdir = LOGDIR;
70  try {
71  logdir = config->get_string((scenario_prefix + "logdir").c_str());
72  } catch (Exception &e) { /* ignored, use default set above */
73  }
74  struct stat s;
75  int err = stat(logdir.c_str(), &s);
76  if (err != 0) {
77  char buf[1024];
78  Exception se("Cannot access logdir %s (%s)", logdir.c_str(), strerror_r(errno, buf, 1024));
79  } else if (!S_ISDIR(s.st_mode)) {
80  throw Exception("Logdir path %s is not a directory", logdir.c_str());
81  }
82 
83  bool scenario_loop_replay = false;
84  bool scenario_non_blocking = false;
85  float scenario_grace_period = 0.001;
86  try {
87  scenario_loop_replay = config->get_bool((prefix + "loop").c_str());
88  } catch (Exception &e) {
89  } // ignored, assume enabled
90  try {
91  scenario_loop_replay = config->get_bool((scenario_prefix + "loop").c_str());
92  } catch (Exception &e) {
93  } // ignored, assume enabled
94  try {
95  scenario_non_blocking = config->get_bool((prefix + "non_blocking").c_str());
96  } catch (Exception &e) {
97  } // ignored, assume enabled
98  try {
99  scenario_non_blocking = config->get_bool((scenario_prefix + "non_blocking").c_str());
100  } catch (Exception &e) {
101  } // ignored, assume enabled
102  try {
103  scenario_grace_period = config->get_float((prefix + "grace_period").c_str());
104  } catch (Exception &e) {
105  } // ignored, assume enabled
106  try {
107  scenario_grace_period = config->get_float((scenario_prefix + "grace_period").c_str());
108  } catch (Exception &e) {
109  } // ignored, assume enabled
110 
111 #if __cplusplus >= 201103L
112  std::unique_ptr<Configuration::ValueIterator> i(config->search(logs_prefix.c_str()));
113 #else
114  std::auto_ptr<Configuration::ValueIterator> i(config->search(logs_prefix.c_str()));
115 #endif
116  while (i->next()) {
117  std::string log_name = std::string(i->path()).substr(logs_prefix.length());
118  log_name = log_name.substr(0, log_name.find("/"));
119 
120  if (logs.find(log_name) == logs.end()) {
121  std::string log_prefix = logs_prefix + log_name + "/";
122 
123  printf("Log name: %s log_prefix: %s\n", log_name.c_str(), log_prefix.c_str());
124 
125  bool loop_replay = scenario_loop_replay;
126  bool non_blocking = scenario_non_blocking;
127  float grace_period = scenario_grace_period;
128  std::string hook_str;
129 
130  try {
131  loop_replay = config->get_bool((log_prefix + "loop").c_str());
132  } catch (Exception &e) {
133  } // ignored, assume enabled
134  try {
135  non_blocking = config->get_bool((log_prefix + "non_blocking").c_str());
136  } catch (Exception &e) {
137  } // ignored, assume enabled
138  try {
139  hook_str = config->get_string((log_prefix + "hook").c_str());
140  } catch (Exception &e) {
141  } // ignored, assume enabled
142  try {
143  grace_period = config->get_float((log_prefix + "grace_period").c_str());
144  } catch (Exception &e) {
145  } // ignored, assume enabled
146 
147  if (hook_str != "") {
149  hook = BlockedTimingAspect::WAKEUP_HOOK_PRE_LOOP;
150 
151  if (hook_str == "pre_loop") {
152  hook = BlockedTimingAspect::WAKEUP_HOOK_PRE_LOOP;
153  } else if (hook_str == "sensor_acquire") {
154  hook = BlockedTimingAspect::WAKEUP_HOOK_SENSOR_ACQUIRE;
155  } else if (hook_str == "sensor_prepare") {
156  hook = BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PREPARE;
157  } else if (hook_str == "sensor_process") {
158  hook = BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PROCESS;
159  } else if (hook_str == "worldstate") {
160  hook = BlockedTimingAspect::WAKEUP_HOOK_WORLDSTATE;
161  } else if (hook_str == "think") {
162  hook = BlockedTimingAspect::WAKEUP_HOOK_THINK;
163  } else if (hook_str == "skill") {
164  hook = BlockedTimingAspect::WAKEUP_HOOK_SKILL;
165  } else if (hook_str == "act") {
166  hook = BlockedTimingAspect::WAKEUP_HOOK_ACT;
167  } else if (hook_str == "act_exec") {
168  hook = BlockedTimingAspect::WAKEUP_HOOK_ACT_EXEC;
169  } else if (hook_str == "post_loop") {
170  hook = BlockedTimingAspect::WAKEUP_HOOK_POST_LOOP;
171  } else {
172  throw Exception("Invalid hook '%s' for %s", hook_str.c_str(), i->get_string().c_str());
173  }
174 
175  BBLogReplayBlockedTimingThread *lrbt_thread;
176  lrbt_thread = new BBLogReplayBlockedTimingThread(hook,
177  i->get_string().c_str(),
178  logdir.c_str(),
179  scenario.c_str(),
180  grace_period,
181  loop_replay,
182  non_blocking);
183  thread_list.push_back(lrbt_thread);
184  } else {
185  BBLogReplayThread *lr_thread = new BBLogReplayThread(
186  i->get_string().c_str(), logdir.c_str(), scenario.c_str(), grace_period, loop_replay);
187  thread_list.push_back(lr_thread);
188  }
189 
190  logs.insert(log_name);
191  }
192  }
193 
194  if (thread_list.empty()) {
195  throw Exception("No interfaces configured for log replay, aborting");
196  }
197 }
198 
199 PLUGIN_DESCRIPTION("Replay BlackBoard log files")
200 EXPORT_PLUGIN(BlackBoardLogReplayPlugin)
Plugin interface class.
Definition: plugin.h:33
BlackBoardLogReplayPlugin(fawkes::Configuration *config)
Constructor.
Fawkes library namespace.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
BlackBoard log replay blocked timing thread.
BlackBoard log replay plugin.
virtual ValueIterator * search(const char *path)=0
Iterator with search results.
virtual bool next()=0
Check if there is another element and advance to this if possible.
WakeupHook
Type to define at which hook the thread is woken up.
Base class for exceptions in Fawkes.
Definition: exception.h:35
ThreadList thread_list
Thread list member.
Definition: plugin.h:53
virtual std::string get_string() const =0
Get string value.
virtual const char * path() const =0
Path of value.
Configuration * config
Fawkes configuration.
Definition: plugin.h:58
void push_back(Thread *thread)
Add thread to the end.
Interface for configuration handling.
Definition: config.h:64
virtual float get_float(const char *path)=0
Get value from configuration which is of type float.
void append(const char *format,...)
Append messages to the message list.
Definition: exception.cpp:333
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
BlackBoard log Replay thread.