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 #include "logreplay_thread.h"
26 #include "logreplay_bt_thread.h"
27 
28 #include <utils/time/time.h>
29 
30 #include <set>
31 #include <memory>
32 
33 #include <cstring>
34 #include <cerrno>
35 #include <sys/types.h>
36 #include <sys/stat.h>
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  : Plugin(config)
54 {
55  std::set<std::string> logs;
56 
57  std::string prefix = "/fawkes/bblogreplay/";
58 
59  std::string scenario = "";
60  try {
61  scenario = config->get_string((prefix + "scenario").c_str());
62  } catch (Exception &e) {
63  e.append("No scenario defined, configure %sscenario", prefix.c_str());
64  throw;
65  }
66 
67  std::string scenario_prefix = prefix + scenario + "/";
68  std::string logs_prefix = scenario_prefix + "logs/";
69 
70  std::string logdir = LOGDIR;
71  try {
72  logdir = config->get_string((scenario_prefix + "logdir").c_str());
73  } catch (Exception &e) { /* ignored, use default set above */ }
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)",
79  logdir.c_str(), strerror_r(errno, buf, 1024));
80  } else if ( ! S_ISDIR(s.st_mode) ) {
81  throw Exception("Logdir path %s is not a directory", logdir.c_str());
82  }
83 
84  bool scenario_loop_replay = false;
85  bool scenario_non_blocking = false;
86  float scenario_grace_period = 0.001;
87  try {
88  scenario_loop_replay = config->get_bool((prefix + "loop").c_str());
89  } catch (Exception &e) {} // ignored, assume enabled
90  try {
91  scenario_loop_replay = config->get_bool((scenario_prefix + "loop").c_str());
92  } catch (Exception &e) {} // ignored, assume enabled
93  try {
94  scenario_non_blocking = config->get_bool((prefix + "non_blocking").c_str());
95  } catch (Exception &e) {} // ignored, assume enabled
96  try {
97  scenario_non_blocking = config->get_bool((scenario_prefix + "non_blocking").c_str());
98  } catch (Exception &e) {} // ignored, assume enabled
99  try {
100  scenario_grace_period = config->get_float((prefix + "grace_period").c_str());
101  } catch (Exception &e) {} // ignored, assume enabled
102  try {
103  scenario_grace_period = config->get_float((scenario_prefix + "grace_period").c_str());
104  } catch (Exception &e) {} // ignored, assume enabled
105 
106 #if __cplusplus >= 201103L
107  std::unique_ptr<Configuration::ValueIterator> i(config->search(logs_prefix.c_str()));
108 #else
109  std::auto_ptr<Configuration::ValueIterator> i(config->search(logs_prefix.c_str()));
110 #endif
111  while (i->next()) {
112  std::string log_name = std::string(i->path()).substr(logs_prefix.length());
113  log_name = log_name.substr(0, log_name.find("/"));
114 
115  if ( logs.find(log_name) == logs.end() ) {
116  std::string log_prefix = logs_prefix + log_name + "/";
117 
118  printf("Log name: %s log_prefix: %s\n", log_name.c_str(), log_prefix.c_str());
119 
120  std::string log_file = "";
121  bool loop_replay = scenario_loop_replay;
122  bool non_blocking = scenario_non_blocking;
123  float grace_period = scenario_grace_period;
124  std::string hook_str = "";
125 
126  try {
127  log_file = config->get_string((log_prefix + "file").c_str());
128  } catch (Exception &e) {
129  throw;
130  }
131 
132  try {
133  loop_replay = config->get_bool((log_prefix + "loop").c_str());
134  } catch (Exception &e) {} // ignored, assume enabled
135  try {
136  non_blocking = config->get_bool((log_prefix + "non_blocking").c_str());
137  } catch (Exception &e) {} // ignored, assume enabled
138  try {
139  hook_str = config->get_string((log_prefix + "hook").c_str());
140  } catch (Exception &e) {} // ignored, assume enabled
141  try {
142  grace_period = config->get_float((log_prefix + "grace_period").c_str());
143  } catch (Exception &e) {} // ignored, assume enabled
144 
145 
146  if (hook_str != "") {
148  hook = BlockedTimingAspect::WAKEUP_HOOK_PRE_LOOP;
149 
150  if (hook_str == "pre_loop") {
151  hook = BlockedTimingAspect::WAKEUP_HOOK_PRE_LOOP;
152  } else if (hook_str == "sensor_acquire") {
153  hook = BlockedTimingAspect::WAKEUP_HOOK_SENSOR_ACQUIRE;
154  } else if (hook_str == "sensor_prepare") {
155  hook = BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PREPARE;
156  } else if (hook_str == "sensor_process") {
157  hook = BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PROCESS;
158  } else if (hook_str == "worldstate") {
159  hook = BlockedTimingAspect::WAKEUP_HOOK_WORLDSTATE;
160  } else if (hook_str == "think") {
161  hook = BlockedTimingAspect::WAKEUP_HOOK_THINK;
162  } else if (hook_str == "skill") {
163  hook = BlockedTimingAspect::WAKEUP_HOOK_SKILL;
164  } else if (hook_str == "act") {
165  hook = BlockedTimingAspect::WAKEUP_HOOK_ACT;
166  } else if (hook_str == "act_exec") {
167  hook = BlockedTimingAspect::WAKEUP_HOOK_ACT_EXEC;
168  } else if (hook_str == "post_loop") {
169  hook = BlockedTimingAspect::WAKEUP_HOOK_POST_LOOP;
170  } else {
171  throw Exception("Invalid hook '%s' for %s",
172  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(i->get_string().c_str(),
186  logdir.c_str(),
187  scenario.c_str(),
188  grace_period,
189  loop_replay);
190  thread_list.push_back(lr_thread);
191  }
192 
193  logs.insert(log_name);
194  }
195  }
196 
197  if ( thread_list.empty() ) {
198  throw Exception("No interfaces configured for log replay, aborting");
199  }
200 }
201 
202 PLUGIN_DESCRIPTION("Replay BlackBoard log files")
203 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:36
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.
void push_back(Thread *thread)
Add thread to the end.
Interface for configuration handling.
Definition: config.h:67
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:341
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
BlackBoard log Replay thread.