-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModelGuard.cpp
executable file
·82 lines (72 loc) · 3.82 KB
/
ModelGuard.cpp
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
#include "ModelGuard.hpp"
namespace HLA {
/**
* @brief ModelGuard::ModelGuard
* @param fed pointer on target federate based on BaseFederate
*/
ModelGuard::ModelGuard(BaseFederate* fed) : _federate(fed){
if(_federate) // Check if federate pointer nullptr
_lock = std::unique_lock<std::mutex>(_federate->_smutex); // Lock federate state mutex and take control under federate state
else
throw std::runtime_error("Nullptr federate"); // If federate pointer is nullptr throw run-time error
if(_federate->_f_modeling){
if(_federate->_mode == MODELMODE::FREE_THREADING) // If federate model mode is master Threading start Free Threading control
ModelingControl<MODELMODE::FREE_THREADING>();
else if(_federate->_mode == MODELMODE::FREE_FOLLOWING) // If federate model mode is master Following start Free Following control
ModelingControl<MODELMODE::FREE_FOLLOWING>();
else if(_federate->_mode == MODELMODE::MANAGING_FOLLOWING) // If federate model mode is slave Following start Managing Following control
ModelingControl<MODELMODE::MANAGING_FOLLOWING>();
else if(_federate->_mode == MODELMODE::MANAGING_THREADING) // If federate model mode is slave Threading start Managing Threading control
ModelingControl<MODELMODE::MANAGING_THREADING>();
}
}
/**
* @brief ModelGuard::~ModelGuard
* When ModelGuard is destructed the scope thread go on
*/
ModelGuard::~ModelGuard(){
_lock.unlock(); // Release the federate state mutex
}
template<>
/**
* @brief ModelGuard::ModelingControl<ModelMode::FREE_THREADING>
* Method which control execution of federate with Threading Model Mode
*/
void ModelGuard::ModelingControl<MODELMODE::FREE_THREADING>(){
_federate->_condition.wait(_lock,[this]{ // Wait for DOING federate state, federate notify ModelGuard about state change
return _federate->_state == STATE::DOING;
});
_federate->_state = STATE::PROCESSING; // Set federate step for proccessing
}
template<>
/**
* @brief ModelGuard::ModelingControl<ModelMode::FREE_FOLLOWING>
* Method which control execution of federate with Following Model Mode
*/
void ModelGuard::ModelingControl<MODELMODE::FREE_FOLLOWING>(){
_federate->_state = STATE::PROCESSING; // Set federate step for proccessing
_federate->Modeling<MODELMODE::FREE_FOLLOWING>(); // Run federate follow modeling method
_federate->_last_time = std::chrono::steady_clock::now(); // Save last time
}
template<>
/**
* @brief ModelGuard::ModelingControl<MODELMODE::MANAGING_FOLLOWING>
* Method which control execution of slave federate with Following Model Mode
*/
void ModelGuard::ModelingControl<MODELMODE::MANAGING_FOLLOWING>(){
_federate->_state = STATE::READY; // Set federate state to READY
_federate->ReadyToGo(); // Send READY time stamp
_federate->_condition.wait(_lock,[this]{ // Wait for GO federate state, federate notify ModelGuard about state change
return _federate->_state == STATE::PROCESSING || !_federate->_f_modeling;
});
_federate->Modeling<MODELMODE::MANAGING_FOLLOWING>(); // Start processing
}
template<>
/**
* @brief ModelGuard::ModelingControl<MODELMODE::MANAGING_THREADING>
* Method which control execution of slave federate with Threading Model Mode
*/
void ModelGuard::ModelingControl<MODELMODE::MANAGING_THREADING>(){
this->~ModelGuard(); // It isn't nessesary to control this type here
}
}