Woman, Life, Freedom


Adaptive AUTOSAR
ARA public interface header documentation
initial_wait_state.h
1#ifndef INITIAL_WAIT_STATE_H
2#define INITIAL_WAIT_STATE_H
3
4#include <random>
5#include <thread>
6#include <chrono>
7#include "./timer_set_state.h"
8
9namespace ara
10{
11 namespace com
12 {
13 namespace someip
14 {
15 namespace sd
16 {
17 namespace fsm
18 {
22 template <typename T>
24 {
25 protected:
27 const int InitialDelayMin;
28
30 const int InitialDelayMax;
31
32 virtual void SetTimer() override
33 {
34 // Generate a random initial delay
35 std::default_random_engine _generator;
36 std::uniform_int_distribution<int> _distribution(
38 int _randomDely = _distribution(_generator);
39
40 // Sleep for the initali random delay and
41 // then transit to the next state
42 auto _delay = std::chrono::milliseconds(_randomDely);
43 bool _interrupted = this->WaitFor(_delay);
44
45 if (!_interrupted)
46 {
47 // Invoke the on timer expiration callback
48 this->OnTimerExpired();
49 }
50 }
51
52 public:
61 T currentState,
62 T nextState,
63 T stoppedState,
64 std::function<void()> onTimerExpired,
65 int initialDelayMin,
66 int initialDelayMax) : helper::MachineState<T>(currentState),
67 TimerSetState<T>(nextState, stoppedState, onTimerExpired),
68 InitialDelayMin{initialDelayMin},
69 InitialDelayMax{initialDelayMax}
70 {
71 if ((initialDelayMin < 0) ||
72 (initialDelayMax < 0) ||
73 (initialDelayMin > initialDelayMax))
74 {
75 throw std::invalid_argument("Invalid initial delay minimum and/or maximum.");
76 }
77 }
78
79 InitialWaitState() = delete;
80 InitialWaitState(const InitialWaitState &) = delete;
81 InitialWaitState &operator=(const InitialWaitState &) = delete;
82 };
83 }
84 }
85 }
86 }
87}
88
89#endif
MachineState(T state) noexcept
Constructor.
Definition: machine_state.h:70
Server's or client's service initial wait state.
Definition: initial_wait_state.h:24
InitialWaitState(T currentState, T nextState, T stoppedState, std::function< void()> onTimerExpired, int initialDelayMin, int initialDelayMax)
Constructor.
Definition: initial_wait_state.h:60
virtual void SetTimer() override
Set the phase time on state activation.
Definition: initial_wait_state.h:32
const int InitialDelayMin
Initial delay lower bound in milliseconds.
Definition: initial_wait_state.h:27
const int InitialDelayMax
Initial delay higher bound in milliseconds.
Definition: initial_wait_state.h:30
Server's or client's service timer set state.
Definition: timer_set_state.h:24
bool WaitFor(std::chrono::milliseconds duration)
Wait for certian period of time.
Definition: timer_set_state.h:49
const std::function< void()> OnTimerExpired
Delegate which is invoked by timer's thread when the timer is expired.
Definition: timer_set_state.h:76