blob: 4092a669077c3d763161cf5f092f7fdf077b2dc9 [file] [log] [blame]
Pau Espin Pedrol927344b2017-05-22 16:38:49 +02001# osmo_gsm_tester: Event loop
2#
3# Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Pau Espin Pedrol <pespin@sysmocom.de>
6#
7# This program is free software: you can redistribute it and/or modify
Harald Welte27205342017-06-03 09:51:45 +02008# it under the terms of the GNU General Public License as
Pau Espin Pedrol927344b2017-05-22 16:38:49 +02009# published by the Free Software Foundation, either version 3 of the
10# License, or (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
Harald Welte27205342017-06-03 09:51:45 +020015# GNU General Public License for more details.
Pau Espin Pedrol927344b2017-05-22 16:38:49 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Pau Espin Pedrol927344b2017-05-22 16:38:49 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
Pau Espin Pedrol927344b2017-05-22 16:38:49 +020020import time
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +020021from gi.repository import GLib, GObject
22
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020023from . import log
24
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +020025class DeferredHandling:
Pau Espin Pedrol58603672018-08-09 13:45:55 +020026
27 def __init__(self):
28 self.defer_queue = []
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +020029
30 def handle_queue(self):
Pau Espin Pedrol58603672018-08-09 13:45:55 +020031 while self.defer_queue:
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +020032 handler, args, kwargs = self.defer_queue.pop(0)
33 handler(*args, **kwargs)
34
35 def defer(self, handler, *args, **kwargs):
36 self.defer_queue.append((handler, args, kwargs))
37
38class WaitRequest:
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +020039
40 def __init__(self, condition, condition_args, condition_kwargs, timeout, timestep):
Pau Espin Pedrol58603672018-08-09 13:45:55 +020041 self.timeout_ack = False
42 self.condition_ack = False
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +020043 self.timeout_started = time.time()
44 self.timeout = timeout
45 self.condition = condition
46 self.condition_args = condition_args
47 self.condition_kwargs = condition_kwargs
48
49 def condition_check(self):
50 #print("_wait_condition_check")
51 waited = time.time() - self.timeout_started
52 if self.condition(*self.condition_args, **self.condition_kwargs):
53 self.condition_ack = True
54 elif waited > self.timeout:
55 self.timeout_ack = True
56
57class EventLoop:
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +020058
59 def __init__(self):
Pau Espin Pedrol58603672018-08-09 13:45:55 +020060 self.poll_funcs = []
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +020061 self.gloop = GLib.MainLoop()
62 self.gctx = self.gloop.get_context()
63 self.deferred_handling = DeferredHandling()
64
65 def _trigger_cb_func(self, user_data):
66 self.defer(user_data)
67 return True #to retrigger the timeout
68
69 def defer(self, handler, *args, **kwargs):
70 self.deferred_handling.defer(handler, *args, **kwargs)
71
72 def register_poll_func(self, func, timestep=1):
73 id = GObject.timeout_add(timestep*1000, self._trigger_cb_func, func) # in 1/1000th of a sec
74 self.poll_funcs.append((func, id))
75
76 def unregister_poll_func(self, func):
77 for pair in self.poll_funcs:
78 f, id = pair
79 if f == func:
80 GObject.source_remove(id)
81 self.poll_funcs.remove(pair)
82 return
83
84 def poll(self, may_block=False):
85 self.gctx.iteration(may_block)
86 self.deferred_handling.handle_queue()
87
Pau Espin Pedrol664e3832020-06-10 19:30:33 +020088 def wait_no_raise(self, condition, condition_args, condition_kwargs, timeout, timestep):
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +020089 if not timeout or timeout < 0:
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +020090 raise log.Error('wait() *must* time out at some point.', timeout=timeout)
91 if timestep < 0.1:
92 timestep = 0.1
93
94 wait_req = WaitRequest(condition, condition_args, condition_kwargs, timeout, timestep)
95 wait_id = GObject.timeout_add(timestep*1000, self._trigger_cb_func, wait_req.condition_check)
96 while True:
Pau Espin Pedrolb6ab1da2018-04-07 17:26:43 +020097 try:
98 self.poll(may_block=True)
99 except Exception: # cleanup of temporary resources in the wait scope
100 GObject.source_remove(wait_id)
101 raise
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +0200102 if wait_req.condition_ack or wait_req.timeout_ack:
103 GObject.source_remove(wait_id)
104 success = wait_req.condition_ack
105 return success
106
Pau Espin Pedrol664e3832020-06-10 19:30:33 +0200107 def wait(self, condition, *condition_args, timeout=300, timestep=1, **condition_kwargs):
108 if not self.wait_no_raise(condition, condition_args, condition_kwargs, timeout, timestep):
Pau Espin Pedrole5236652018-05-28 12:46:42 +0200109 raise log.Error('Wait timeout', condition=condition, timeout=timeout, timestep=timestep)
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +0200110
Pau Espin Pedrol664e3832020-06-10 19:30:33 +0200111 def sleep(self, seconds):
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +0200112 assert seconds > 0.
Pau Espin Pedrol664e3832020-06-10 19:30:33 +0200113 self.wait_no_raise(lambda: False, [], {}, timeout=seconds, timestep=seconds)
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +0200114
115
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200116MainLoop = EventLoop()
Pau Espin Pedrol927344b2017-05-22 16:38:49 +0200117
118
119# vim: expandtab tabstop=4 shiftwidth=4