Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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  std::auto_ptr<Configuration::ValueIterator> i(config->search(logs_prefix.c_str()));
107  while (i->next()) {
108  std::string log_name = std::string(i->path()).substr(logs_prefix.length());
109  log_name = log_name.substr(0, log_name.find("/"));
110 
111  if ( logs.find(log_name) == logs.end() ) {
112  std::string log_prefix = logs_prefix + log_name + "/";
113 
114  printf("Log name: %s log_prefix: %s\n", log_name.c_str(), log_prefix.c_str());
115 
116  std::string log_file = "";
117  bool loop_replay = scenario_loop_replay;
118  bool non_blocking = scenario_non_blocking;
119  float grace_period = scenario_grace_period;
120  std::string hook_str = "";
121 
122  try {
123  log_file = config->get_string((log_prefix + "file").c_str());
124  } catch (Exception &e) {
125  throw;
126  }
127 
128  try {
129  loop_replay = config->get_bool((log_prefix + "loop").c_str());
130  } catch (Exception &e) {} // ignored, assume enabled
131  try {
132  non_blocking = config->get_bool((log_prefix + "non_blocking").c_str());
133  } catch (Exception &e) {} // ignored, assume enabled
134  try {
135  hook_str = config->get_string((log_prefix + "hook").c_str());
136  } catch (Exception &e) {} // ignored, assume enabled
137  try {
138  grace_period = config->get_float((log_prefix + "grace_period").c_str());
139  } catch (Exception &e) {} // ignored, assume enabled
140 
141 
142  if (hook_str != "") {
144  hook = BlockedTimingAspect::WAKEUP_HOOK_PRE_LOOP;
145 
146  if (hook_str == "pre_loop") {
147  hook = BlockedTimingAspect::WAKEUP_HOOK_PRE_LOOP;
148  } else if (hook_str == "sensor_acquire") {
149  hook = BlockedTimingAspect::WAKEUP_HOOK_SENSOR_ACQUIRE;
150  } else if (hook_str == "sensor_prepare") {
151  hook = BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PREPARE;
152  } else if (hook_str == "sensor_process") {
153  hook = BlockedTimingAspect::WAKEUP_HOOK_SENSOR_PROCESS;
154  } else if (hook_str == "worldstate") {
155  hook = BlockedTimingAspect::WAKEUP_HOOK_WORLDSTATE;
156  } else if (hook_str == "think") {
157  hook = BlockedTimingAspect::WAKEUP_HOOK_THINK;
158  } else if (hook_str == "skill") {
159  hook = BlockedTimingAspect::WAKEUP_HOOK_SKILL;
160  } else if (hook_str == "act") {
161  hook = BlockedTimingAspect::WAKEUP_HOOK_ACT;
162  } else if (hook_str == "act_exec") {
163  hook = BlockedTimingAspect::WAKEUP_HOOK_ACT_EXEC;
164  } else if (hook_str == "post_loop") {
165  hook = BlockedTimingAspect::WAKEUP_HOOK_POST_LOOP;
166  } else {
167  throw Exception("Invalid hook '%s' for %s",
168  hook_str.c_str(), i->get_string().c_str());
169  }
170 
171  BBLogReplayBlockedTimingThread *lrbt_thread;
172  lrbt_thread = new BBLogReplayBlockedTimingThread(hook,
173  i->get_string().c_str(),
174  logdir.c_str(),
175  scenario.c_str(),
176  grace_period,
177  loop_replay,
178  non_blocking);
179  thread_list.push_back(lrbt_thread);
180  } else {
181  BBLogReplayThread *lr_thread = new BBLogReplayThread(i->get_string().c_str(),
182  logdir.c_str(),
183  scenario.c_str(),
184  grace_period,
185  loop_replay);
186  thread_list.push_back(lr_thread);
187  }
188 
189  logs.insert(log_name);
190  }
191  }
192 
193  if ( thread_list.empty() ) {
194  throw Exception("No interfaces configured for log replay, aborting");
195  }
196 }
197 
198 PLUGIN_DESCRIPTION("Replay BlackBoard log files")
199 EXPORT_PLUGIN(BlackBoardLogReplayPlugin)