Update README.linux
[deliverable/titan.core.git] / mctr2 / mctr / MainController.h
CommitLineData
970ed795
EL
1///////////////////////////////////////////////////////////////////////////////
2// Copyright (c) 2000-2014 Ericsson Telecom AB
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//
9// Description: Header file for MainController
10// Author: Janos Zoltan Szabo
11// mail: tmpjsz@eth.ericsson.se
12//
13// Copyright Ericsson Telecom AB 2000-2014
14//
15#ifndef MCTR_MAINCONTROLLER_H
16#define MCTR_MAINCONTROLLER_H
17//----------------------------------------------------------------------------
18
19#include <pthread.h>
20#include <sys/types.h>
21#include <netinet/in.h>
22
23#include "../../core/Types.h"
24#include "../../common/NetworkHandler.hh"
25class Text_Buf;
26
27#include "UserInterface.h"
28
29#ifdef USE_EPOLL
30struct epoll_event;
31#else
32struct pollfd;
33#endif
34
35struct sigaction;
36
37//----------------------------------------------------------------------------
38
39namespace mctr {
40
41//----------------------------------------------------------------------------
42
43/* Type definitions */
44
45/** For representing the global state of MC */
46enum mc_state_enum {
47 MC_INACTIVE, MC_LISTENING, MC_LISTENING_CONFIGURED, MC_HC_CONNECTED,
48 MC_CONFIGURING, MC_ACTIVE, MC_SHUTDOWN, MC_CREATING_MTC, MC_READY,
49 MC_TERMINATING_MTC, MC_EXECUTING_CONTROL, MC_EXECUTING_TESTCASE,
50 MC_TERMINATING_TESTCASE, MC_PAUSED
51};
52
53/** Data structure for unknown incoming connections (before receiving
54 * the first message) */
55struct unknown_connection {
56 int fd;
57 IPAddress *ip_addr;
58 Text_Buf *text_buf;
59 unknown_connection *prev, *next;
60 bool unix_socket; // true only if the connection is through unix domain socket
61};
62
63/** Data structure for describing the component location
64 * constraints */
65struct string_set {
66 int n_elements;
67 char **elements;
68};
69
70/** Data structure for describing the component location
71 * constraints */
72struct host_group_struct {
73 char *group_name;
74 boolean has_all_hosts, has_all_components;
75 string_set host_members, assigned_components;
76};
77
78/** Possible states of a HC */
79enum hc_state_enum { HC_IDLE, HC_CONFIGURING, HC_ACTIVE, HC_OVERLOADED,
80 HC_CONFIGURING_OVERLOADED, HC_EXITING, HC_DOWN };
81
82/** Data structure for each host (and the corresponding HC) */
83struct host_struct {
84 IPAddress *ip_addr;
85 char *hostname; /**< hostname retrieved from DNS */
86 char *hostname_local; /**< hostname sent in VERSION message */
87 char *machine_type;
88 char *system_name;
89 char *system_release;
90 char *system_version;
91 boolean transport_supported[TRANSPORT_NUM];
92 char *log_source;
93 hc_state_enum hc_state;
94 int hc_fd;
95 Text_Buf *text_buf;
96 int n_components;
97 component *components;
98 /* to implement load balancing mechanisms */
99 string_set allowed_components;
100 boolean all_components_allowed;
101 boolean local_hostname_different;
102 int n_active_components;
103};
104
105struct component_struct;
106
107/** Container of test components (when a pending operation can be
108 * requested by several components) */
109struct requestor_struct {
110 int n_components;
111 union {
112 component_struct *the_component;
113 component_struct **components;
114 };
115};
116
117/** Possible states of a port connection or mapping */
118enum conn_state_enum { CONN_LISTENING, CONN_CONNECTING, CONN_CONNECTED,
119 CONN_DISCONNECTING, CONN_MAPPING, CONN_MAPPED, CONN_UNMAPPING };
120
121/** Data structure for representing a port connection */
122struct port_connection {
123 conn_state_enum conn_state;
124 transport_type_enum transport_type;
125 struct {
126 component comp_ref;
127 char *port_name;
128 port_connection *next, *prev;
129 } head, tail;
130 requestor_struct requestors;
131};
132
133
134/** Structure for timers */
135struct timer_struct {
136 double expiration;
137 union {
138 void *dummy_ptr;
139 component_struct *component_ptr;
140 } timer_argument;
141 timer_struct *prev, *next;
142};
143
144/** Possible states of a TC (MTC or PTC) */
145enum tc_state_enum { TC_INITIAL, TC_IDLE, TC_CREATE, TC_START, TC_STOP, TC_KILL,
146 TC_CONNECT, TC_DISCONNECT, TC_MAP, TC_UNMAP, TC_STOPPING, TC_EXITING,
147 TC_EXITED,
148 MTC_CONTROLPART, MTC_TESTCASE, MTC_ALL_COMPONENT_STOP,
149 MTC_ALL_COMPONENT_KILL, MTC_TERMINATING_TESTCASE, MTC_PAUSED,
150 PTC_FUNCTION, PTC_STARTING, PTC_STOPPED, PTC_KILLING, PTC_STOPPING_KILLING,
151 PTC_STALE, TC_SYSTEM };
152
153/** Data structure for each TC */
154struct component_struct {
155 component comp_ref;
156 qualified_name comp_type;
157 char *comp_name;
158 char *log_source; /**< used for console log messages. format: name\@host */
159 host_struct *comp_location;
160 tc_state_enum tc_state;
161 verdicttype local_verdict;
162 char* verdict_reason;
163 int tc_fd;
164 Text_Buf *text_buf;
165 /** Identifier of the TTCN-3 testcase or function that is currently being
166 * executed on the test component */
167 qualified_name tc_fn_name;
168 /* fields for implementing the construct 'value returning done' */
169 char *return_type;
170 int return_value_len;
171 void *return_value;
172 boolean is_alive;
173 boolean stop_requested; /**< only for 'all component.running' */
174 boolean process_killed;
175 union {
176 /** used in state TC_INITIAL */
177 struct {
178 component_struct *create_requestor;
179 char *location_str;
180 } initial;
181 /** used in state PTC_STARTING */
182 struct {
183 component_struct *start_requestor;
184 int arguments_len;
185 void *arguments_ptr;
186 requestor_struct cancel_done_sent_to;
187 } starting;
188 /** used in states TC_STOPPING, PTC_STOPPING_KILLING, PTC_KILLING */
189 struct {
190 requestor_struct stop_requestors;
191 requestor_struct kill_requestors;
192 } stopping_killing;
193 };
194 requestor_struct done_requestors;
195 requestor_struct killed_requestors;
196 requestor_struct cancel_done_sent_for;
197 timer_struct *kill_timer;
198 /* fields for registering port connections */
199 port_connection *conn_head_list, *conn_tail_list;
200 int conn_head_count, conn_tail_count;
201};
202
203/** Selector for the table of file descriptors */
204enum fd_type_enum { FD_UNUSED, FD_PIPE, FD_SERVER, FD_UNKNOWN, FD_HC, FD_TC };
205
206/** Element of the file descriptor table. The table is indexed by the
207 * fd itself. */
208struct fd_table_struct {
209 fd_type_enum fd_type;
210 union {
211 unknown_connection *unknown_ptr;
212 host_struct *host_ptr;
213 component_struct *component_ptr;
214 void *dummy_ptr;
215 };
216};
217
218/** Structure for storing the checksum of a module */
219struct module_version_info {
220 char *module_name;
221 int checksum_length;
222 unsigned char *module_checksum;
223};
224
225/** Possible reasons for waking up the MC thread from the main thread. */
226enum wakeup_reason_t { REASON_NOTHING, REASON_SHUTDOWN, REASON_MTC_KILL_TIMER };
227
228/** The MainController class. The collection of all functions and data
229 * structures */
230class MainController {
231 /* private members */
232 static UserInterface *ui;
233 static NetworkHandler nh;
234
235 static mc_state_enum mc_state;
236 static char *mc_hostname;
237
238 static int server_fd;
239 static int server_fd_unix; // for efficient local communication
240 static boolean server_fd_disabled;
241 static void disable_server_fd();
242 static void enable_server_fd();
243
244 static pthread_mutex_t mutex;
245 static void lock();
246 static void unlock();
247
248#ifdef USE_EPOLL
249 static const int EPOLL_SIZE_HINT = 1000;
250 static const int EPOLL_MAX_EVENTS = 250;
251 static epoll_event *epoll_events;
252 static int epfd;
253#else
254 static unsigned int nfds, new_nfds;
255 static struct pollfd *ufds, *new_ufds;
256 static boolean pollfds_modified;
257 static void update_pollfds();
258#endif
259 static void add_poll_fd(int fd);
260 static void remove_poll_fd(int fd);
261
262 static int fd_table_size;
263 static fd_table_struct *fd_table;
264 static void add_fd_to_table(int fd);
265 static void remove_fd_from_table(int fd);
266
267 static void set_close_on_exec(int fd);
268
269 static unknown_connection *unknown_head, *unknown_tail;
270 static unknown_connection *new_unknown_connection(bool unix_socket);
271 static void delete_unknown_connection(unknown_connection *conn);
272 static void close_unknown_connection(unknown_connection *conn);
273
274 static void init_string_set(string_set *set);
275 static void free_string_set(string_set *set);
276 static void add_string_to_set(string_set *set, const char *str);
277 static void remove_string_from_set(string_set *set, const char *str);
278 static boolean set_has_string(const string_set *set, const char *str);
279 static const char *get_string_from_set(const string_set *set, int index);
280
281 static int n_host_groups;
282 static host_group_struct *host_groups;
283 static string_set assigned_components;
284 static boolean all_components_assigned;
285 static host_group_struct *add_host_group(const char *group_name);
286 static host_group_struct *lookup_host_group(const char *group_name);
287 static boolean is_similar_hostname(const char *host1, const char *host2);
288 static boolean host_has_name(const host_struct *host, const char *name);
289 static boolean member_of_group(const host_struct *host,
290 const host_group_struct *group);
291 static void add_allowed_components(host_struct *host);
292 static host_struct *choose_ptc_location(const char *component_type,
293 const char *component_name, const char *component_location);
294
295 static int n_hosts;
296 static host_struct **hosts;
297 static char *config_str;
298 static host_struct *add_new_host(unknown_connection *conn);
299 static void close_hc_connection(host_struct *hc);
300 static boolean is_hc_in_state(hc_state_enum checked_state);
301 static boolean all_hc_in_state(hc_state_enum checked_state);
302 static void configure_host(host_struct *host, boolean should_notify);
303 static void check_all_hc_configured();
304 static void add_component_to_host(host_struct *host,
305 component_struct *comp);
306 static void remove_component_from_host(component_struct *comp);
307
308 static boolean version_known;
309 static int n_modules;
310 static module_version_info *modules;
311 static boolean check_version(unknown_connection *conn);
312
313 static int n_components, n_active_ptcs, max_ptcs;
314 static component_struct **components;
315 static component_struct *mtc, *system;
316 static component next_comp_ref, tc_first_comp_ref;
317 static boolean any_component_done_requested, any_component_done_sent,
318 all_component_done_requested, any_component_killed_requested,
319 all_component_killed_requested;
320 static void add_component(component_struct *comp);
321 static component_struct *lookup_component(component comp_ref);
322 static void destroy_all_components();
323 static void close_tc_connection(component_struct *comp);
324 static boolean stop_after_tc, stop_requested;
325 static boolean ready_to_finish_testcase();
326 static void finish_testcase();
327 static boolean message_expected(component_struct *from,
328 const char *message_name);
329 static boolean request_allowed(component_struct *from,
330 const char *message_name);
331 static boolean valid_endpoint(component component_reference,
332 boolean new_connection, component_struct *requestor,
333 const char *operation);
334 static void destroy_connection(port_connection *conn, component_struct *tc);
335 static void destroy_mapping(port_connection *conn);
336 static boolean stop_all_components();
337 static void check_all_component_stop();
338 static void send_stop_ack_to_requestors(component_struct *tc);
339 static boolean kill_all_components(boolean testcase_ends);
340 static void check_all_component_kill();
341 static void send_kill_ack_to_requestors(component_struct *tc);
342 static void send_component_status_to_requestor(component_struct *tc,
343 component_struct *requestor, boolean done_status,
344 boolean killed_status);
345 static void component_stopped(component_struct *tc);
346 static void component_terminated(component_struct *tc);
347 static void done_cancelled(component_struct *from,
348 component_struct *started_tc);
349 static void start_kill_timer(component_struct *tc);
350
351 static boolean component_is_alive(component_struct *tc);
352 static boolean component_is_running(component_struct *tc);
353 static boolean component_is_done(component_struct *tc);
354 static boolean is_any_component_alive();
355 static boolean is_all_component_alive();
356 static boolean is_any_component_running();
357 static boolean is_all_component_running();
358 static boolean is_any_component_done();
359
360 static void init_connections(component_struct *tc);
361 static void add_connection(port_connection *c);
362 static void remove_connection(port_connection *c);
363 static port_connection *find_connection(component head_comp,
364 const char *head_port, component tail_comp, const char *tail_port);
365 static void remove_all_connections(component head_or_tail);
366 static transport_type_enum choose_port_connection_transport(
367 component head_comp, component tail_comp);
368 static void send_connect_ack_to_requestors(port_connection *conn);
369 static void send_error_to_connect_requestors(port_connection *conn,
370 const char *fmt, ...)
371 __attribute__ ((__format__ (__printf__, 2, 3)));
372 static void send_disconnect_to_server(port_connection *conn);
373 static void send_disconnect_ack_to_requestors(port_connection *conn);
374
375 static void init_requestors(requestor_struct *reqs, component_struct *tc);
376 static void add_requestor(requestor_struct *reqs, component_struct *tc);
377 static void remove_requestor(requestor_struct *reqs, component_struct *tc);
378 static boolean has_requestor(const requestor_struct *reqs,
379 component_struct *tc);
380 static component_struct *get_requestor(const requestor_struct *reqs,
381 int index);
382 static void free_requestors(requestor_struct *reqs);
383
384 static void init_qualified_name(qualified_name *name);
385 static void free_qualified_name(qualified_name *name);
386
387 static double kill_timer;
388 static double time_now();
389 static timer_struct *timer_head, *timer_tail;
390 static void register_timer(timer_struct *timer);
391 static void cancel_timer(timer_struct *timer);
392 static int get_poll_timeout();
393 static void handle_expired_timers();
394 static void handle_kill_timer(timer_struct *timer);
395
396 // Custom signal handling for termination signals to remove temporary
397 // files /tmp/ttcn3-mctr-*. Related to HP67376.
398 static struct sigaction new_action, old_action;
399 static void register_termination_handlers();
400 static void termination_handler(int signum);
401
402public:
403 static void error(const char *fmt, ...)
404 __attribute__ ((__format__ (__printf__, 1, 2)));
405private:
406 static void notify(const char *fmt, ...)
407 __attribute__ ((__format__ (__printf__, 1, 2)));
408 static void notify(const struct timeval *timestamp, const char *source,
409 int severity, const char *message);
410 static void status_change();
411
412 static void fatal_error(const char *fmt, ...)
413 __attribute__ ((__format__ (__printf__, 1, 2), __noreturn__));
414
415 static void *thread_main(void *arg);
416 static void dispatch_socket_event(int fd);
417 static int pipe_fd[2];
418 static wakeup_reason_t wakeup_reason;
419 static void wakeup_thread(wakeup_reason_t reason);
420
421 static void handle_pipe();
422 static void handle_incoming_connection(int p_serverfd);
423 static int recv_to_buffer(int fd, Text_Buf& text_buf,
424 boolean recv_from_socket);
425 static void handle_unknown_data(unknown_connection *conn);
426 static void handle_hc_data(host_struct *hc, boolean recv_from_socket);
427 static void handle_tc_data(component_struct *tc, boolean recv_from_socket);
428
429 static void unlink_unix_socket(int socket_fd);
430
431 static void shutdown_server();
432 static void perform_shutdown();
433
434 static void clean_up();
435
436 static const char *get_host_name(const struct in_addr *ip_address);
437 static boolean get_ip_address(struct in_addr *ip_address,
438 const char *host_name);
439
440 /* Messages to HCs */
441 static void send_configure(host_struct *hc, const char *config_file);
442 static void send_exit_hc(host_struct *hc);
443 static void send_create_mtc(host_struct *hc);
444 static void send_create_ptc(host_struct *hc, component component_reference,
445 const qualified_name& component_type, const char *component_name,
446 boolean is_alive, const qualified_name& current_testcase);
447 static void send_kill_process(host_struct *hc,
448 component component_reference);
449
450 /* Messages to TCs */
451 static void send_create_ack(component_struct *tc,
452 component component_reference);
453 static void send_start_ack(component_struct *tc);
454 static void send_stop(component_struct *tc);
455 static void send_stop_ack(component_struct *tc);
456 static void send_kill_ack(component_struct *tc);
457 static void send_running(component_struct *tc, boolean answer);
458 static void send_alive(component_struct *tc, boolean answer);
459 static void send_done_ack(component_struct *tc, boolean answer,
460 const char *return_type, int return_value_len,
461 const void *return_value);
462 static void send_killed_ack(component_struct *tc, boolean answer);
463 static void send_connect_listen(component_struct *tc,
464 const char *local_port, component remote_comp,
465 const char *remote_comp_name, const char *remote_port,
466 transport_type_enum transport_type);
467 static void send_connect(component_struct *tc,
468 const char *local_port, component remote_comp,
469 const char *remote_comp_name, const char *remote_port,
470 transport_type_enum transport_type, int remote_address_len,
471 const void *remote_address);
472 static void send_connect_ack(component_struct *tc);
473 static void send_disconnect(component_struct *tc,
474 const char *local_port, component remote_comp, const char *remote_port);
475 static void send_disconnect_ack(component_struct *tc);
476 static void send_map(component_struct *tc,
477 const char *local_port, const char *system_port);
478 static void send_map_ack(component_struct *tc);
479 static void send_unmap(component_struct *tc,
480 const char *local_port, const char *system_port);
481 static void send_unmap_ack(component_struct *tc);
482
483 /* Messages to MTC */
484 static void send_cancel_done_mtc(component component_reference,
485 boolean cancel_any);
486 static void send_component_status_mtc(component component_reference,
487 boolean is_done, boolean is_killed, boolean is_any_done,
488 boolean is_all_done, boolean is_any_killed, boolean is_all_killed,
489 const char *return_type, int return_value_len,
490 const void *return_value);
491 static void send_execute_control(const char *module_name);
492 static void send_execute_testcase(const char *module_name,
493 const char *testcase_name);
494 static void send_ptc_verdict(boolean continue_execution);
495 static void send_continue();
496 static void send_exit_mtc();
497
498 /** Messages to PTCs */
499 static void send_cancel_done_ptc(component_struct *tc,
500 component component_reference);
501 static void send_component_status_ptc(component_struct *tc,
502 component component_reference,
503 boolean is_done, boolean is_killed, const char *return_type,
504 int return_value_len, const void *return_value);
505 static void send_start(component_struct *tc,
506 const qualified_name& function_name, int arg_len, const void *arg_ptr);
507 static void send_kill(component_struct *tc);
508
509 static void send_error(int fd, const char *fmt, ...)
510 __attribute__ ((__format__ (__printf__, 2, 3)));
511 static void send_error_str(int fd, const char *reason);
512 static void send_message(int fd, Text_Buf& text_buf);
513
514 /* Incoming messages on unknown connections (generic and first messages) */
515 static void process_error(unknown_connection *conn);
516 static void process_log(unknown_connection *conn);
517 static void process_version(unknown_connection *conn);
518 static void process_mtc_created(unknown_connection *conn);
519 static void process_ptc_created(unknown_connection *conn);
520
521 /* Incoming messages from HCs */
522 static void process_error(host_struct *hc);
523 static void process_log(host_struct *hc);
524 static void process_configure_ack(host_struct *hc);
525 static void process_configure_nak(host_struct *hc);
526 static void process_create_nak(host_struct *hc);
527 static void process_hc_ready(host_struct *hc);
528
529 /* Incoming messages from TCs */
530 static void process_error(component_struct *tc);
531 static void process_log(component_struct *tc);
532 static void process_create_req(component_struct *tc);
533 static void process_start_req(component_struct *tc, int message_end);
534 static void process_stop_req(component_struct *tc);
535 static void process_kill_req(component_struct *tc);
536 static void process_is_running(component_struct *tc);
537 static void process_is_alive(component_struct *tc);
538 static void process_done_req(component_struct *tc);
539 static void process_killed_req(component_struct *tc);
540 static void process_cancel_done_ack(component_struct *tc);
541 static void process_connect_req(component_struct *tc);
542 static void process_connect_listen_ack(component_struct *tc, int message_end);
543 static void process_connected(component_struct *tc);
544 static void process_connect_error(component_struct *tc);
545 static void process_disconnect_req(component_struct *tc);
546 static void process_disconnected(component_struct *tc);
547 static void process_map_req(component_struct *tc);
548 static void process_mapped(component_struct *tc);
549 static void process_unmap_req(component_struct *tc);
550 static void process_unmapped(component_struct *tc);
551
552 /* Incoming messages from MTC */
553 static void process_testcase_started();
554 static void process_testcase_finished();
555 static void process_mtc_ready();
556
557 /* Incoming messages from PTCs */
558 static void process_stopped(component_struct *tc, int message_end);
559 static void process_stopped_killed(component_struct *tc, int message_end);
560 static void process_killed(component_struct *tc);
561
562public:
563 static void initialize(UserInterface& par_ui, int par_max_ptcs);
564 static void terminate();
565
566 static void add_host(const char *group_name, const char *host_name);
567 static void assign_component(const char *host_or_group,
568 const char *component_id);
569 static void destroy_host_groups();
570
571 static void set_kill_timer(double timer_val);
572
573 static unsigned short start_session(const char *local_address,
574 unsigned short tcp_port, bool unix_sockets_enabled);
575 static void shutdown_session();
576
577 static void configure(const char *config_file);
578
579 static void create_mtc(int host_index);
580 static void exit_mtc();
581
582 static void execute_control(const char *module_name);
583 static void execute_testcase(const char *module_name,
584 const char *testcase_name);
585 static void stop_after_testcase(boolean new_state);
586 static void continue_testcase();
587 static void stop_execution();
588
589 static mc_state_enum get_state();
590 static boolean get_stop_after_testcase();
591
592 static int get_nof_hosts();
593 static host_struct *get_host_data(int host_index);
594 static component_struct *get_component_data(int component_reference);
595 static void release_data();
596
597 static const char *get_mc_state_name(mc_state_enum state);
598 static const char *get_hc_state_name(hc_state_enum state);
599 static const char *get_tc_state_name(tc_state_enum state);
600 static const char *get_transport_name(transport_type_enum transport);
601};
602
603//----------------------------------------------------------------------------
604
605} /* namespace mctr */
606
607//----------------------------------------------------------------------------
608#endif // MCTR_MAINCONTROLLER_H
609
610// Local Variables:
611// mode: C++
612// indent-tabs-mode: nil
613// c-basic-offset: 2
614// End:
This page took 0.067283 seconds and 5 git commands to generate.