]> rtime.felk.cvut.cz Git - boost-statechart-viewer.git/blob - www/test.cpp
Update web page
[boost-statechart-viewer.git] / www / test.cpp
1
2 #include <boost/statechart/state_machine.hpp>
3 #include <boost/statechart/simple_state.hpp>
4 #include <boost/statechart/transition.hpp>
5 #include <boost/statechart/event.hpp>
6 #include <boost/mpl/list.hpp>
7
8 #include <iostream>
9 #include <string>
10 #include <pthread.h>
11
12 using namespace std;
13
14
15 namespace sc = boost::statechart;
16 namespace mpl = boost::mpl;
17 struct state_1; // definice stavu
18 struct state_2; // definice stavu
19 struct state_3; // definice stavu
20 struct state_4; // definice stavu
21 struct EvYes : sc::event< EvYes > {};//stisteno A
22 struct EvNo : sc::event< EvNo > {};//stisteno N
23 struct EvTimer : sc::event< EvTimer > {};//vyprsel cas
24
25 struct DU : sc::state_machine< DU, state_1 > {};//Nastaveni inicializacniho stavu
26
27 DU Zm; // pro posilani udalosti
28
29 void* casovac(void* s)
30 {
31     int es = (intptr_t)s;
32     sleep (es);
33     Zm.process_event (EvTimer());
34     return NULL;
35 }
36
37 struct state_1 : sc::simple_state< state_1, DU> // stav
38 {
39     state_1() { cout<<"Chcete zformatovat vas disk (a/n) ? \n";}//FSM_Entry
40     ~state_1() {}//FSM_Exit
41     typedef mpl::list< // reakce na udalosti
42         sc::transition< EvYes, state_4 >,
43         sc::transition< EvNo, state_2 > > reactions;
44 };
45 struct state_2 : sc::simple_state< state_2, DU >
46 {
47     state_2()
48     {
49          cout<<"Rozmysli si to! \n";
50         pthread_t idthread;
51         int *a = (int* )3;
52         if ( pthread_create(&idthread, NULL, casovac, a) != 0)
53         {
54              cout << "Chyba pri vytvareni vlakna.";
55             abort();
56         }
57         if ( pthread_detach(idthread ) )
58         {
59                  cout << "Error detaching ..\n";
60                 abort ();
61         }
62     }
63     ~state_2() {}
64     typedef sc::transition< EvTimer, state_3 > reactions;
65 };
66 struct state_3 : sc::simple_state< state_3, DU >
67 {
68     pthread_t idthread;
69     state_3() { cout<<"Rychle stiskni n nebo ti zformatuju disk. \n";
70         int *a = (int* ) 1;
71         if ( pthread_create(&idthread, NULL, casovac, a) != 0)
72         {
73              cout << "Chyba pri vytvareni vlakna.";
74             abort();
75         }
76         if ( pthread_detach(idthread ) )
77         {
78                  cout << "Error detaching ..\n";
79                 abort ();
80         }
81     }
82     ~state_3() {}
83     typedef mpl::list<
84         sc::transition< EvNo, state_1 >,
85         sc::transition< EvTimer, state_4 > > reactions;
86 };
87 struct state_4 : sc::simple_state< state_4, DU >
88 {
89     state_4() {
90          cout<<"Formatuji tvuj disk. \n";
91         pthread_t idthread;
92         int *a = (int* )2;
93         if ( pthread_create(&idthread, NULL, casovac, a) != 0)
94         {
95              cout << "Chyba pri vytvareni vlakna.";
96             abort();
97         }
98         if ( pthread_detach(idthread ) )
99         {
100                  cout << "Error detaching ..\n";
101                 abort ();
102         }
103     }
104     ~state_4() { cout<<"HOTOVO.\n";}
105     typedef sc::transition< EvTimer, state_1 > reactions;
106 };
107
108 void *nacitani (void * ptr)
109 {
110      string s = "";
111     while(1)
112     {
113          cin >> s;
114         if (s == "n") Zm.process_event (EvNo());
115         if (s == "a") Zm.process_event (EvYes());
116         if (s == "exit") break;
117     }
118     return NULL;
119 }
120 int main()
121 {
122     Zm.initiate();
123     pthread_t idthread;
124     if ( pthread_create(&idthread, NULL, nacitani, NULL) != 0)
125     {
126          cout << "Chyba pri vytvareni vlakna.";
127         abort();
128     }
129     pthread_join(idthread, NULL);
130     return 0;
131 }