Repository navigation
Expand file tree
/
Copy pathsmacc_asynchronous_client_behavior.h
More file actions
82 lines (66 loc) · 3.25 KB
/
Copy pathsmacc_asynchronous_client_behavior.h
File metadata and controls
82 lines (66 loc) · 3.25 KB
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
/*****************************************************************************************************************
* ReelRobotix Inc. - Software License Agreement Copyright (c) 2018
* Authors: Pablo Inigo Blasco, Brett Aldrich
*
******************************************************************************************************************/
#pragma once
#include <smacc/smacc_client_behavior_base.h>
#include <smacc/smacc_signal.h>
#include <thread>
#include <condition_variable>
#include <mutex>
#include <future>
namespace smacc
{
template <typename AsyncCB, typename Orthogonal>
struct EvCbFinished : sc::event<EvCbFinished<AsyncCB, Orthogonal>>
{
};
template <typename AsyncCB, typename Orthogonal>
struct EvCbSuccess : sc::event<EvCbSuccess<AsyncCB, Orthogonal>>
{
};
template <typename AsyncCB, typename Orthogonal>
struct EvCbFailure : sc::event<EvCbFailure<AsyncCB, Orthogonal>>
{
};
// Asnchronous client behaviors are used when the onEntry or onExit function execution is slow
// CONCEPT: this funcionality is related with the orthogonality of SmaccState machines.
// No behavior should block the creation of other behaviors, all of them conceptually start in parallel.
// Alternative for long duration behaviors: using default-synchromous SmaccClientBehaviors with the update method
// ASYNCHRONOUS STATE MACHINES DESIGN NOTES: Asynchronous behaviors can safely post events and use its local methods,
// but the interaction with other components or elements of
// the state machine is not by-default thread safe and must be manually implemented. For example, if some element of the architecture
// (components, states, clients) need to access to this behavior client information it is needed to implement a mutex for the internal
// state of this behavior. Other example: if this behavior access to some component located in other thread, it is also may be needed
// to some mutex for that component
class SmaccAsyncClientBehavior : public ISmaccClientBehavior
{
public:
template <typename TOrthogonal, typename TSourceObject>
void onOrthogonalAllocation();
virtual ~SmaccAsyncClientBehavior();
template <typename TCallback, typename T>
boost::signals2::connection onSuccess(TCallback callback, T *object);
template <typename TCallback, typename T>
boost::signals2::connection onFinished(TCallback callback, T *object);
template <typename TCallback, typename T>
boost::signals2::connection onFailure(TCallback callback, T *object);
protected:
virtual void executeOnEntry() override;
virtual void executeOnExit() override;
void postSuccessEvent();
void postFailureEvent();
virtual void dispose() override;
private:
std::future<int> onEntryThread_;
std::future<int> onExitThread_;
std::function<void()> postFinishEventFn_;
std::function<void()> postSuccessEventFn_;
std::function<void()> postFailureEventFn_;
SmaccSignal<void()> onFinished_;
SmaccSignal<void()> onSuccess_;
SmaccSignal<void()> onFailure_;
};
} // namespace smacc
#include <smacc/impl/smacc_asynchronous_client_behavior_impl.h>