Sync with 5.4.0
[deliverable/titan.core.git] / core / Runtime.hh
CommitLineData
970ed795 1///////////////////////////////////////////////////////////////////////////////
3abe9331 2// Copyright (c) 2000-2015 Ericsson Telecom AB
970ed795
EL
3// All rights reserved. This program and the accompanying materials
4// are made available under the terms of the Eclipse Public License v1.0
5// which accompanies this distribution, and is available at
6// http://www.eclipse.org/legal/epl-v10.html
7///////////////////////////////////////////////////////////////////////////////
8#ifndef RUNTIME_HH
9#define RUNTIME_HH
10
11#include <sys/types.h>
12#include "Types.h"
13
14class Text_Buf;
15class COMPONENT;
16class VERDICTTYPE;
17class CHARSTRING;
18
19extern "C" {
20typedef void (*signal_handler_type)(int);
21}
22
23/** @brief
24*/
25class TTCN_Runtime {
26public:
27 enum executor_state_enum {
28 UNDEFINED_STATE, // 0
29
30 SINGLE_CONTROLPART, SINGLE_TESTCASE, // 1,2
31
32 HC_INITIAL, HC_IDLE, HC_CONFIGURING, HC_ACTIVE, HC_OVERLOADED, // 3-7
33 HC_OVERLOADED_TIMEOUT, HC_EXIT, // 8-9
34
35 MTC_INITIAL, MTC_IDLE, MTC_CONTROLPART, MTC_TESTCASE, // 10-13
36 MTC_TERMINATING_TESTCASE, MTC_TERMINATING_EXECUTION, MTC_PAUSED, // 14-16
37 MTC_CREATE, MTC_START, MTC_STOP, MTC_KILL, MTC_RUNNING, MTC_ALIVE, // 17-22
38 MTC_DONE, MTC_KILLED, MTC_CONNECT, MTC_DISCONNECT, MTC_MAP, MTC_UNMAP, // 23-28
39 MTC_EXIT, // 29
40
41 PTC_INITIAL, PTC_IDLE, PTC_FUNCTION, PTC_CREATE, PTC_START, PTC_STOP, // 30-35
42 PTC_KILL, PTC_RUNNING, PTC_ALIVE, PTC_DONE, PTC_KILLED, PTC_CONNECT, // 36-41
43 PTC_DISCONNECT, PTC_MAP, PTC_UNMAP, PTC_STOPPED, PTC_EXIT // 42-46
44 };
45private:
46 static executor_state_enum executor_state;
47
48 static qualified_name component_type;
49 static char *component_name;
50 static boolean is_alive;
51
52 static const char *control_module_name;
53 static qualified_name testcase_name;
54
55 static char *host_name;
56
57 static verdicttype local_verdict;
58 static unsigned int verdict_count[5], control_error_count;
59 static CHARSTRING verdict_reason;
60
61 /** TTCN_TryBlock uses the private member in_ttcn_try_block */
62 friend class TTCN_TryBlock;
63 /** true if execution is currently inside a TTCN-3 try{} */
64 static boolean in_ttcn_try_block;
65
66 static char *begin_controlpart_command, *end_controlpart_command,
67 *begin_testcase_command, *end_testcase_command;
68
69 static component create_done_killed_compref;
70 static boolean running_alive_result;
71
72 static alt_status any_component_done_status, all_component_done_status,
73 any_component_killed_status, all_component_killed_status;
74 static int component_status_table_size;
75 static component component_status_table_offset;
76 struct component_status_table_struct;
77 static component_status_table_struct *component_status_table;
78
79 struct component_process_struct;
80 static component_process_struct **components_by_compref,
81 **components_by_pid;
82
83public:
84 inline static executor_state_enum get_state() { return executor_state; }
85 inline static void set_state(executor_state_enum new_state)
86 { executor_state = new_state; }
87
88 /** @name Identifying the type
89 * @{
90 */
91 inline static boolean is_hc()
92 { return executor_state >= HC_INITIAL && executor_state <= HC_EXIT; }
93 inline static boolean is_mtc()
94 { return executor_state >= MTC_INITIAL && executor_state <= MTC_EXIT; }
95 inline static boolean is_ptc()
96 { return executor_state >= PTC_INITIAL && executor_state <= PTC_EXIT; }
97 inline static boolean is_tc()
98 { return executor_state >= MTC_INITIAL && executor_state <= PTC_EXIT; }
99 inline static boolean is_single()
100 { return executor_state >= SINGLE_CONTROLPART &&
101 executor_state <= SINGLE_TESTCASE; }
3abe9331 102 inline static boolean is_undefined() /* e.g.: when listing test cases (<EXE> -l) */
103 { return executor_state == UNDEFINED_STATE; }
970ed795
EL
104 static boolean is_idle();
105 inline static boolean is_overloaded()
106 { return executor_state == HC_OVERLOADED ||
107 executor_state == HC_OVERLOADED_TIMEOUT; }
108 /** @} */
109
110 static boolean is_in_ttcn_try_block() { return in_ttcn_try_block; }
111
112private:
113 inline static boolean in_controlpart()
114 { return executor_state == SINGLE_CONTROLPART ||
115 executor_state == MTC_CONTROLPART; }
116 /** Whether verdict operations are allowed */
117 static boolean verdict_enabled();
118 static void wait_for_state_change();
119 static void clear_qualified_name(qualified_name& q_name);
120 static void clean_up();
121
122 static void initialize_component_type();
123 static void terminate_component_type();
124
125public:
126 static void set_component_type(const char *component_type_module,
127 const char *component_type_name);
128 static void set_component_name(const char *new_component_name);
129 inline static void set_alive_flag(boolean par_is_alive)
130 { is_alive = par_is_alive; }
131 static void set_testcase_name(const char *par_module_name,
132 const char *par_testcase_name);
133
134 inline static const char *get_component_type()
135 { return component_type.definition_name; }
136 inline static const char *get_component_name()
137 { return component_name; }
138 inline static const char *get_testcase_name()
139 { return testcase_name.definition_name; }
140
141 /// Returns a string which must not be freed.
142 static const char *get_host_name();
143
144 static CHARSTRING get_testcase_id_macro();
145 static CHARSTRING get_testcasename();
146
147 static void load_logger_plugins();
148 static void set_logger_parameters();
149
150 static const char *get_signal_name(int signal_number);
151private:
152 static void set_signal_handler(int signal_number, const char *signal_name,
153 signal_handler_type signal_handler);
154 static void restore_default_handler(int signal_number,
155 const char *signal_name);
156 static void ignore_signal(int signal_number, const char *signal_name);
157 static void enable_interrupt_handler();
158 static void disable_interrupt_handler();
159public:
160 static void install_signal_handlers();
161 static void restore_signal_handlers();
162
163public:
164 static int hc_main(const char *local_addr, const char *MC_addr,
165 unsigned short MC_port);
166 static int mtc_main();
167 static int ptc_main();
168
169 static component create_component(const char *created_component_type_module,
170 const char *created_component_type_name,
171 const char *created_component_name,
172 const char *created_component_location,
173 boolean created_component_alive);
174 static void prepare_start_component(const COMPONENT& component_reference,
175 const char *module_name, const char *function_name,
176 Text_Buf& text_buf);
177 static void send_start_component(Text_Buf& text_buf);
178 static void start_function(const char *module_name,
179 const char *function_name, Text_Buf& text_buf);
180 static void function_started(Text_Buf& text_buf);
181 static void prepare_function_finished(const char *return_type,
182 Text_Buf& text_buf);
183 static void send_function_finished(Text_Buf& text_buf);
184 static void function_finished(const char *function_name);
185
186 static alt_status component_done(component component_reference);
187 static alt_status component_done(component component_reference,
188 const char *return_type, Text_Buf*& text_buf);
189 static alt_status component_killed(component component_reference);
190 static boolean component_running(component component_reference);
191 static boolean component_alive(component component_reference);
192 static void stop_component(component component_reference);
193 static void stop_execution()
194 __attribute__ ((__noreturn__));
195 static void kill_component(component component_reference);
196 static void kill_execution()
197 __attribute__ ((__noreturn__));
198
199private:
200 static alt_status ptc_done(component component_reference);
201 static alt_status any_component_done();
202 static alt_status all_component_done();
203 static alt_status ptc_killed(component component_reference);
204 static alt_status any_component_killed();
205 static alt_status all_component_killed();
206 static boolean ptc_running(component component_reference);
207 static boolean any_component_running();
208 static boolean all_component_running();
209 static boolean ptc_alive(component component_reference);
210 static boolean any_component_alive();
211 static boolean all_component_alive();
212 static void stop_mtc()
213 __attribute__ ((__noreturn__));
214 static void stop_ptc(component component_reference);
215 static void stop_all_component();
216 static void kill_ptc(component component_reference);
217 static void kill_all_component();
218
219 static void check_port_name(const char *port_name,
220 const char *operation_name, const char *which_argument);
221public:
222 static void connect_port(
223 const COMPONENT& src_compref, const char *src_port,
224 const COMPONENT& dst_compref, const char *dst_port);
225 static void disconnect_port(
226 const COMPONENT& src_compref, const char *src_port,
227 const COMPONENT& dst_compref, const char *dst_port);
228 static void map_port(
229 const COMPONENT& src_compref, const char *src_port,
230 const COMPONENT& dst_compref, const char *dst_port);
231 static void unmap_port(
232 const COMPONENT& src_compref, const char *src_port,
233 const COMPONENT& dst_compref, const char *dst_port);
234
235 static void begin_controlpart(const char *module_name);
236 static void end_controlpart();
237 static void check_begin_testcase(boolean has_timer, double timer_value);
238 static void begin_testcase(
239 const char *par_module_name, const char *par_testcase_name,
240 const char *mtc_comptype_module, const char *mtc_comptype_name,
241 const char *system_comptype_module, const char *system_comptype_name,
242 boolean has_timer, double timer_value);
243 static verdicttype end_testcase();
244 static void log_verdict_statistics();
245
246 static void begin_action();
247 static void end_action();
248
249 static void setverdict(verdicttype new_value, const char* reason = "");
250 static void setverdict(const VERDICTTYPE& new_value,
251 const char* reason = "");
252 static void set_error_verdict();
253 static verdicttype getverdict();
254private:
255 static void setverdict_internal(verdicttype new_value,
256 const char* reason = "");
257
258public:
259 /** @name Manipulating external commands
260 * @{
261 */
262 static void set_begin_controlpart_command(const char *new_command);
263 static void set_end_controlpart_command(const char *new_command);
264 static void set_begin_testcase_command(const char *new_command);
265 static void set_end_testcase_command(const char *new_command);
266 static void clear_external_commands();
267 /** @} */
268private:
269 static char *shell_escape(const char *command_str);
270 static void execute_command(const char *command_name,
271 const char *argument_string);
272
273public:
274 static void process_create_mtc();
275 static void process_create_ptc(component component_reference,
276 const char *component_type_module, const char *component_type_name,
277 const char *par_component_name, boolean par_is_alive,
278 const char *current_testcase_module, const char *current_testcase_name);
279
280 static void process_create_ack(component new_component);
281 static void process_running(boolean result_value);
282 static void process_alive(boolean result_value);
283 static void process_done_ack(boolean done_status,
284 const char *return_type, int return_value_len,
285 const void *return_value);
286 static void process_killed_ack(boolean killed_status);
287 static void process_ptc_verdict(Text_Buf& text_buf);
288 static void process_kill();
289 static void process_kill_process(component component_reference);
290
291 static void set_component_done(component component_reference,
292 const char *return_type, int return_value_len,
293 const void *return_value);
294 static void set_component_killed(component component_reference);
295 static void cancel_component_done(component component_reference);
296
297private:
298 static int get_component_status_table_index(component component_reference);
299 static alt_status get_killed_status(component component_reference);
300 static boolean in_component_status_table(component component_reference);
301 static void clear_component_status_table();
302
303 static void initialize_component_process_tables();
304 static void add_component(component component_reference, pid_t process_id);
305 static void remove_component(component_process_struct *comp);
306 static component_process_struct *get_component_by_compref(component
307 component_reference);
308 static component_process_struct *get_component_by_pid(pid_t process_id);
309 static void clear_component_process_tables();
310
311 static void successful_process_creation();
312 static void failed_process_creation();
313
314public:
315 static void wait_terminated_processes();
316 static void check_overload();
317};
318
319/** TTCN_TryBlock must be used only as a local variable of a TTCN-3 try{} block.
320 It handles the value of TTCN_Runtime::in_ttcn_try_block using C++'s RAII feature */
321class TTCN_TryBlock {
322 boolean outmost_try;
323public:
324 TTCN_TryBlock() {
325 if (TTCN_Runtime::in_ttcn_try_block) {
326 outmost_try = FALSE;
327 } else {
328 outmost_try = TRUE;
329 TTCN_Runtime::in_ttcn_try_block = TRUE;
330 }
331 }
332 ~TTCN_TryBlock() {
333 if (outmost_try) {
334 TTCN_Runtime::in_ttcn_try_block = FALSE;
335 }
336 }
337};
338
339#endif
This page took 0.11255 seconds and 5 git commands to generate.