File size: 15,797 Bytes
68ce2fc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
/*
Author(s): Anton Deguet
Created on: 2014-03-03
(C) Copyright 2014-2026 Johns Hopkins University (JHU), All Rights Reserved.
--- begin cisst license - do not edit ---
This software is provided "as is" under an open source license, with
no warranty. The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.
--- end cisst license ---
*/
#include <cisstMultiTask/mtsCollectorFactory.h>
#include <cisstMultiTask/mtsCollectorState.h>
#include <cisstMultiTask/mtsCollectorEvent.h>
#include <cisstMultiTask/mtsInterfaceRequired.h>
#include <cisstCommon/cmnPath.h>
#if CISST_HAS_JSON
#include <json/json.h>
#endif
class mtsCollectorBaseData
{
public:
mtsCollectorBaseData(mtsCollectorFactory * factory,
const std::string & name,
mtsCollectorBase * collectorComponent):
Factory(factory),
Name(name),
CollectorComponent(collectorComponent)
{}
mtsCollectorFactory * Factory = nullptr;
std::string Name;
mtsCollectorBase * CollectorComponent = nullptr;
mtsInterfaceRequired * InterfaceRequired = nullptr;
mtsFunctionWrite StartCollection;
mtsFunctionWrite StopCollection;
mtsFunctionWrite SetWorkingDirectory;
mtsFunctionVoid SetOutputToDefault;
void SetInterfaceRequired(mtsInterfaceRequired * interface_required) {
interface_required->AddFunction("StartCollection", StartCollection);
interface_required->AddFunction("StopCollection", StopCollection);
interface_required->AddFunction("SetWorkingDirectory", SetWorkingDirectory);
interface_required->AddFunction("SetOutputToDefault", SetOutputToDefault);
interface_required->AddEventHandlerWrite(&mtsCollectorFactory::CollectionStartedEventHandler, Factory,
"CollectionStarted");
interface_required->AddEventHandlerWrite(&mtsCollectorFactory::ProgressEventHandler, Factory,
"Progress");
}
};
class mtsCollectorStateData: public mtsCollectorBaseData
{
public:
mtsCollectorStateData(mtsCollectorFactory * factory,
const std::string & name,
mtsCollectorState * collectorComponent):
mtsCollectorBaseData(factory, name, collectorComponent)
{}
std::list<std::string> Signals;
};
class mtsCollectorEventData: public mtsCollectorBaseData
{
public:
mtsCollectorEventData(mtsCollectorFactory * factory,
const std::string & name,
mtsCollectorEvent * collectorComponent):
mtsCollectorBaseData(factory, name, collectorComponent)
{}
};
mtsCollectorFactory::mtsCollectorFactory(const std::string & componentName):
mtsCollectorBase(componentName, COLLECTOR_FILE_FORMAT_UNDEFINED)
{
}
void mtsCollectorFactory::Configure(const std::string & configuration)
{
// first see if this is a file name
if (!cmnPath::Exists(configuration)) {
CMN_LOG_CLASS_INIT_ERROR << "Configure: can't find file \"" << configuration << "\"" << std::endl;
return;
}
#if CISST_HAS_JSON
std::ifstream jsonStream;
jsonStream.open(configuration.c_str());
Json::Value jsonConfig;
Json::Reader jsonReader;
if (!jsonReader.parse(jsonStream, jsonConfig)) {
CMN_LOG_CLASS_INIT_WARNING << "Configure: failed to parse configuration\n"
<< jsonReader.getFormattedErrorMessages();
return;
} else {
// look for components to collect from
if (jsonConfig.isMember("state-collectors")) {
cmnThrow("Configure: error in configuration file \"" + configuration + "\", state-collectors is now state_collectors");
}
const Json::Value & stateCollectors = jsonConfig["state_collectors"];
for (unsigned int indexCollector = 0;
indexCollector < stateCollectors.size();
++indexCollector) {
std::string componentName = stateCollectors[indexCollector]["component"].asString();
const Json::Value & tables = stateCollectors[indexCollector]["tables"];
for (unsigned int indexTable = 0;
indexTable < tables.size();
++indexTable) {
std::string tableName = tables[indexTable]["table"].asString();
int sampling = tables[indexTable]["sampling"].asInt();
if (sampling < 1) {
sampling = 1;
}
SetSampling(componentName, tableName, sampling);
const Json::Value & signals = tables[indexTable]["signals"];
for (unsigned int signalIndex = 0;
signalIndex < signals.size();
++signalIndex) {
std::string signalName = signals[signalIndex].asString();
AddSignal(componentName, tableName, signalName);
}
}
}
const Json::Value & eventCollectors = jsonConfig["event_collectors"];
for (unsigned int indexCollector = 0;
indexCollector < eventCollectors.size();
++indexCollector) {
std::string componentName = eventCollectors[indexCollector]["component"].asString();
// if interfaces specified
if (eventCollectors[indexCollector].isMember("interfaces")) {
const Json::Value & interfaces = eventCollectors[indexCollector]["interfaces"];
for (unsigned int indexInterface = 0;
indexInterface < interfaces.size();
++indexInterface) {
std::string interfaceName = interfaces[indexInterface]["name"].asString();
// if events are specified
if (interfaces[indexInterface].isMember("events_void")
|| interfaces[indexInterface].isMember("events_write")) {
const Json::Value & w_events = interfaces[indexInterface]["events_write"];
for (unsigned int eventIndex = 0;
eventIndex < w_events.size();
++eventIndex) {
std::string eventName = w_events[eventIndex].asString();
AddEventWrite(componentName, interfaceName, eventName);
}
const Json::Value & v_events = interfaces[indexInterface]["events_void"];
for (unsigned int eventIndex = 0;
eventIndex < v_events.size();
++eventIndex) {
std::string eventName = v_events[eventIndex].asString();
AddEventVoid(componentName, interfaceName, eventName);
}
} else {
// no events specified, collect all
AddAllEvents(componentName, interfaceName);
}
}
} else {
// not interface specified, collect all
AddAllEvents(componentName);
}
}
}
#else
CMN_LOG_CLASS_INIT_WARNING << "Configure: cisst has been compiled without JSON support. Can't check if \""
<< configuration << "\" is a JSON configuration file." << std::endl;
#endif
}
void mtsCollectorFactory::Run(void)
{
ProcessQueuedCommands();
ProcessQueuedEvents();
}
void mtsCollectorFactory::Cleanup(void)
{
}
void mtsCollectorFactory::AddStateCollector(const std::string & component,
const std::string & table)
{
const std::string collector_name = GetName() + "_" + component + "_" + table;
CollectorId collectorId(component, table);
// check if there is already a collector for this component/table
if (mStateCollectors.count(collectorId) == 0) {
// check if there is already a component with that name
mtsComponent * genericComponent = GetManagerComponentServices()->ComponentGet(collector_name);
if (genericComponent) {
CMN_LOG_CLASS_INIT_ERROR << "AddStateCollector: found an existing component with name \""
<< collector_name << "\". Can't create state collector." << std::endl;
return;
}
// create the collector
mtsCollectorState * collector = new mtsCollectorState(collector_name);
collector->UseSeparateLogFileDefault();
// set component and table to collect from
try {
collector->SetStateTable(component, table);
} catch (std::exception & except) {
delete collector;
CMN_LOG_CLASS_INIT_ERROR << "AddStateCollector: SetStateTable failed with error \"" << except.what() << "\"" << std::endl;
return;
}
// default name is based on component/state table name
collector->SetOutputToDefault();
GetManagerComponentServices()->ComponentAdd(collector);
auto collector_data = new mtsCollectorStateData(this, collector_name, collector);
collector_data->SetInterfaceRequired(this->AddInterfaceRequired(collector_name));
mStateCollectors[collectorId] = collector_data;
mAllCollectors.push_back(collector_data);
CMN_LOG_CLASS_INIT_VERBOSE << "AddStateCollector: added state collector \"" << collector_name << "\"" << std::endl;
} else {
CMN_LOG_CLASS_INIT_DEBUG << "AddStateCollector: state collector already exists \"" << collector_name << "\"" << std::endl;
}
}
void mtsCollectorFactory::AddSignal(const std::string & component,
const std::string & table,
const std::string & signal)
{
// add the collector, no effect if already created
AddStateCollector(component, table);
CollectorId collectorId(component, table);
const std::string & collector_name = mStateCollectors[collectorId]->CollectorComponent->GetName();
auto & existingSignals = mStateCollectors[collectorId]->Signals;
const auto it = std::find(existingSignals.begin(), existingSignals.end(), signal);
if (it == existingSignals.end()) {
// add to the list
existingSignals.push_back(signal);
// retrieve that state collector
mtsCollectorState * collector = dynamic_cast<mtsCollectorState *>(GetManagerComponentServices()->ComponentGet(collector_name));
if (!collector) {
CMN_LOG_CLASS_INIT_ERROR << "AddSignal: unable to find state collector \"" << collector_name << "\"" << std::endl;
return;
}
collector->AddSignal(signal);
CMN_LOG_CLASS_INIT_DEBUG << " AddSignal: signal \"" << signal << "\" added to \""
<< collector_name << "\"" << std::endl;
} else {
CMN_LOG_CLASS_INIT_DEBUG << "AddSignal: signal \"" << signal << "\" already added to \""
<< collector_name << "\"" << std::endl;
}
}
void mtsCollectorFactory::SetSampling(const std::string & component,
const std::string & table,
const int sampling)
{
// add the collector, no effect if already created
AddStateCollector(component, table);
CollectorId collectorId(component, table);
auto * collectorComponent = dynamic_cast<mtsCollectorState *>(mStateCollectors[collectorId]->CollectorComponent);
collectorComponent->SetSamplingInterval(sampling);
CMN_LOG_CLASS_INIT_DEBUG << " SetSampling: sampling \"" << sampling << "\" set for \""
<< collectorComponent->GetName() << "\"" << std::endl;
}
mtsCollectorEvent * mtsCollectorFactory::GetEventCollector(const std::string & component_name)
{
const std::string collector_name = GetName() + "_" + component_name + "_events";
const auto collector_it = mEventCollectors.find(collector_name);
if (collector_it != mEventCollectors.end()) {
return dynamic_cast<mtsCollectorEvent*>(collector_it->second->CollectorComponent);
}
// create a new one
mtsCollectorEvent * collector = new mtsCollectorEvent(collector_name);
GetManagerComponentServices()->ComponentAdd(collector);
auto collector_data = new mtsCollectorEventData(this, collector_name, collector);
collector_data->SetInterfaceRequired(this->AddInterfaceRequired(collector_name));
mEventCollectors[collector_name] = collector_data;
mAllCollectors.push_back(collector_data);
CMN_LOG_CLASS_INIT_VERBOSE << "GetEventCollector: added event collector \"" << collector_name << "\"" << std::endl;
return collector;
}
void mtsCollectorFactory::AddEventVoid(const std::string & component_name,
const std::string & interface_name,
const std::string & event_name)
{
auto collector = GetEventCollector(component_name);
collector->AddObservedEventVoid(component_name, interface_name, event_name);
}
void mtsCollectorFactory::AddEventWrite(const std::string & component_name,
const std::string & interface_name,
const std::string & event_name)
{
auto collector = GetEventCollector(component_name);
collector->AddObservedEventWrite(component_name, interface_name, event_name);
}
void mtsCollectorFactory::AddAllEvents(const std::string & component_name,
const std::string & interface_name)
{
auto collector = GetEventCollector(component_name);
collector->AddObservedInterface(component_name, interface_name);
}
void mtsCollectorFactory::AddAllEvents(const std::string & component_name)
{
auto collector = GetEventCollector(component_name);
collector->AddObservedComponent(component_name);
}
bool mtsCollectorFactory::Connect(void)
{
const mtsManagerComponentServices *services = GetManagerComponentServices();
for (auto & collector : mAllCollectors) {
collector->CollectorComponent->Connect();
services->Connect(this->GetName(), collector->Name,
collector->CollectorComponent->GetName(), "Control");
}
return true;
}
void mtsCollectorFactory::GetCollectorsNames(std::list<std::string> & collectors) const
{
collectors.clear();
for (auto & collector : mAllCollectors) {
collectors.push_back(collector->Name);
}
}
void mtsCollectorFactory::StartCollection(const double & delayInSeconds)
{
for (auto collector : mAllCollectors) {
collector->StartCollection(delayInSeconds);
}
}
void mtsCollectorFactory::StopCollection(const double & delayInSeconds)
{
for (auto collector : mAllCollectors) {
collector->StopCollection(delayInSeconds);
}
}
void mtsCollectorFactory::SetWorkingDirectory(const std::string & directory)
{
for (auto collector : mAllCollectors) {
collector->SetWorkingDirectory(directory);
collector->SetOutputToDefault();
}
}
void mtsCollectorFactory::SetOutputToDefault(void)
{
for (auto collector : mAllCollectors) {
collector->SetOutputToDefault();
}
}
void mtsCollectorFactory::CollectionStartedEventHandler(const bool & started)
{
CollectionStartedEventTrigger(started);
}
void mtsCollectorFactory::ProgressEventHandler(const size_t & count)
{
ProgressEventTrigger(count);
}
|