fipa_acl  1.4
Static Public Member Functions | List of all members
fipa::acl::StateMachineFactory Class Reference

#include <statemachine_factory.h>

Static Public Member Functions

static void setProtocolResourceDir (const std::string &resourceDir)
 
static void addProtocolResourceDir (const std::string &resourceDir)
 
static StateMachine getStateMachine (const std::string &protocol)
 
static void prepareProtocolsFromResourceDir (const std::string &directory)
 

Detailed Description

Definition at line 14 of file statemachine_factory.h.

Member Function Documentation

◆ addProtocolResourceDir()

void fipa::acl::StateMachineFactory::addProtocolResourceDir ( const std::string &  resourceDir)
static

Add the resource dir where to search for the protocol definitions

Exceptions
std::runtime_errorif resource directory does not exist

Definition at line 24 of file statemachine_factory.cpp.

25 {
26  fs::path protocolDir = fs::path(resourceDir);
27  if(fs::is_directory(protocolDir))
28  {
29  std::vector<std::string>::iterator resourcesIt = std::find(msResourceDirs.begin(), msResourceDirs.end(), protocolDir.string());
30  if(resourcesIt == msResourceDirs.end())
31  {
32  msResourceDirs.push_back(protocolDir.string());
33  msPreparedResourceDir = false;
34  LOG_INFO("Add protocol resource dir: '%s'", protocolDir.string().c_str());
35  } else {
36  LOG_WARN("Protocol resource dir '%s' already added", protocolDir.string().c_str());
37  }
38  } else {
39  std::string msg = "Could not add resource dir: '" + resourceDir + "', since it does not exist";
40  throw std::runtime_error(msg);
41  }
42 }

◆ getStateMachine()

StateMachine fipa::acl::StateMachineFactory::getStateMachine ( const std::string &  protocol)
static

Create a statemachine for a given protocol

Exceptions
runtime_errorif statem machine

Definition at line 93 of file statemachine_factory.cpp.

94 {
95  if(!msPreparedResourceDir)
96  {
97  StateMachineFactory::prepareProtocolsFromResourceDirs();
98  }
99 
100  std::map<std::string, StateMachine>::iterator it = msStateMachines.find(protocol);
101 
102  if(it != msStateMachines.end())
103  {
104  return it->second;
105  }
106 
107  LOG_WARN("StateMachine for protocol '%s' not found", protocol.c_str());
108  throw std::runtime_error("State machine for requested protocol does not exist");
109 }

◆ prepareProtocolsFromResourceDir()

void fipa::acl::StateMachineFactory::prepareProtocolsFromResourceDir ( const std::string &  directory)
static

Instanciates all available machines a resource directory

Definition at line 53 of file statemachine_factory.cpp.

54 {
55  fs::path protocolDir = fs::path(resourceDir);
56  LOG_INFO("Prepare protocols from: '%s'", protocolDir.string().c_str());
57  if(fs::is_directory(protocolDir))
58  {
59  std::vector<fs::path> files;
60  std::copy(fs::directory_iterator(protocolDir), fs::directory_iterator(), std::back_inserter(files));
61 
62  std::vector<fs::path>::iterator it = files.begin();
63  for(;it != files.end(); ++it)
64  {
65  if(fs::is_regular_file(*it))
66  {
67  // Extract protocol name
68  std::string protocolName = it->string();
69  size_t pos = protocolName.find_last_of("/");
70  protocolName.erase(0, pos+1);
71 
72  try {
73  // Loading the state machine from the given spec
74  StateMachine statemachine = msStateMachineReader.loadSpecification(it->string());
75  LOG_INFO("Register protocol %s", protocolName.c_str());
76  std::map<std::string, StateMachine>::const_iterator statemachinesIt = msStateMachines.find(protocolName);
77  if( statemachinesIt != msStateMachines.end())
78  {
79  LOG_WARN("Protocol '%s' already registered - will use statemachine specification from '%s'", protocolName.c_str(), it->string().c_str());
80  }
81  msStateMachines[protocolName] = statemachine;
82  } catch(const std::runtime_error& e)
83  {
84  LOG_ERROR("Error loading specification for: '%s' - %s", protocolName.c_str(), e.what());
85  }
86  }
87  }
88  }
89 
90  msPreparedResourceDir = true;
91 }
StateMachine loadSpecification(const std::string &file)

◆ setProtocolResourceDir()

void fipa::acl::StateMachineFactory::setProtocolResourceDir ( const std::string &  resourceDir)
static

Set the resource dir where to search for the protocol definitions (implies a clearing of all previous entries)

Exceptions
std::runtime_errorif resource directory does not exist

Definition at line 18 of file statemachine_factory.cpp.

19 {
20  msResourceDirs.clear();
21  addProtocolResourceDir(resourceDir);
22 }
static void addProtocolResourceDir(const std::string &resourceDir)

The documentation for this class was generated from the following files: