-
Notifications
You must be signed in to change notification settings - Fork 0
/
state-test.cpp
121 lines (102 loc) · 2.46 KB
/
state-test.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
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
#include "StateMachine.h"
#include <iostream>
#include <time.h>
#include <unistd.h>
#include <signal.h>
STATE TEST[] = {
STATE_A, STATE_B, STATE_C, STATE_A, STATE_A,
STATE_A, STATE_B, STATE_C, STATE_A, STATE_A,
STATE_A, STATE_B, STATE_C, STATE_A, STATE_A,
STATE_A, STATE_B, STATE_C, STATE_A, STATE_A
};
int TEST_INDEX = 0;
void CreateA (IEvent ** event);
void CreateB (IEvent ** event);
void CreateC (IEvent ** event);
STATE SequenceOne[] = {
STATE_A, STATE_B, STATE_C, STATE_A, STATE_A, STATE_A
};
STATE SequenceTwo[] = {
STATE_A, ANY, STATE_C, ANY
};
StateMachine* engine = NULL;
bool amWorking = true;
void
sig_handler(int signo)
{
if (signo == SIGINT) {
std::cout << "GOT SOFT INT CLOSE NICELY ..." << std::endl;
engine->StopMachine ();
amWorking = false;
}
}
void
matchingHandler (Message* msg) {
std::cout << msg->Name << std::endl;
for (unsigned i = 0; i < msg->ListOfAny.size(); i++) {
std::cout << StateMachine::StateToString((STATE)msg->ListOfAny.at(i)) << " " << std::endl;
}
delete msg;
}
int
main (int argc, char ** argv) {
// Creating the instatnce of new machine.
engine = new StateMachine (matchingHandler);
IEvent* event = NULL;
// Creating two sequences to monitor.
Sequence seqOne = Sequence((int *)SequenceOne, 6, "FIRST");
Sequence seqTwo = Sequence((int *)SequenceTwo, 4, "ANY");
// Adding these two sequences to the engine.
engine->AddSequence(seqOne);
engine->AddSequence(seqTwo);
// Handaling the user kill application signal, for clean application close.
signal(SIGINT, sig_handler);
// Main loop.
while (amWorking) {
switch (TEST[TEST_INDEX]) {
case STATE_A:
CreateA (&event);
break;
case STATE_B:
CreateB (&event);
break;
case STATE_C:
CreateC (&event);
break;
default:
break;
}
if (event != NULL) {
// Announcing the event.
engine->SetEvent (event);
}
// Round test.
if ((++TEST_INDEX) == 20) {
TEST_INDEX = 0;
}
// Fetching event from test repo each one second.
usleep (1000000 * 1);
}
// Resource free.
delete engine;
std::cout << "Exit 'state-test'" << std::endl;
return 0;
}
void
CreateA (IEvent ** event) {
*event = new EventA ();
// Attached event's data.
(*event)->Data = new DataA ();
}
void
CreateB (IEvent ** event) {
*event = new EventB ();
// Attached event's data.
(*event)->Data = new DataB ();
}
void
CreateC (IEvent ** event) {
*event = new EventC ();
// Attached event's data.
(*event)->Data = new DataC ();
}