Skip to content
API Documentation
BastionGuard™
TimeoutCounter.vala
Go to the documentation of this file.
1
2/*
3 * TimeoutCounter.vala
4 *
5 * Copyright 2012-2018 Tony George <teejeetech@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
21 *
22 *
23 */
24
25
26using TeeJee.Logging;
28using TeeJee.Misc;
29
30public class TimeoutCounter : GLib.Object {
31
32 public bool active = false;
33 public string process_to_kill = "";
34 public const int DEFAULT_SECONDS_TO_WAIT = 60;
35 public int seconds_to_wait = 60;
36 public bool exit_app = false;
37
40
41 this.process_to_kill = process_to_kill;
42 this.seconds_to_wait = seconds_to_wait;
43 this.exit_app = exit_app;
44
45 try {
46 active = true;
47 new Thread<void>.try ("timeout-counter",() => { start_counter_thread();});
48 }
49 catch (Error e) {
50 log_error (e.message);
51 }
52 }
53
55 this.process_to_kill = "";
56 this.seconds_to_wait = seconds_to_wait;
57 this.exit_app = true;
58
59 try {
60 active = true;
61 new Thread<void>.try ("timeout-counter", () => {start_counter_thread();});
62 }
63 catch (Error e) {
64 log_error (e.message);
65 }
66 }
67
68 public void stop(){
69 active = false;
70 }
71
72 public void start_counter_thread(){
73 int secs = 0;
74
75 while (active && (secs < seconds_to_wait)){
76 Thread.usleep((ulong) GLib.TimeSpan.MILLISECOND * 1000);
77 secs += 1;
78 }
79
80 if (active){
81 active = false;
82 stdout.printf("\n");
83
84 if (process_to_kill.length > 0){
85 Posix.system("killall " + process_to_kill);
86 log_debug("[timeout] Killed process" + ": %s".printf(process_to_kill));
87 }
88
89 if (exit_app){
90 log_debug("[timeout] Exit application");
91 exit(0);
92 }
93 }
94 }
95}
96
void exit(int exit_code)
const int DEFAULT_SECONDS_TO_WAIT
void start_counter_thread()
void kill_process_on_timeout(string process_to_kill, int seconds_to_wait=DEFAULT_SECONDS_TO_WAIT, bool exit_app=false)
void exit_on_timeout(int seconds_to_wait=DEFAULT_SECONDS_TO_WAIT)
void log_debug(string message)
void log_error(string message, bool highlight=false, bool is_warning=false)