blob: 068eca969f83f5cef5b493bfb9449f32db00defb [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:
26 defer_queue = []
27
28 def handle_queue(self):
29 while DeferredHandling.defer_queue:
30 handler, args, kwargs = self.defer_queue.pop(0)
31 handler(*args, **kwargs)
32
33 def defer(self, handler, *args, **kwargs):
34 self.defer_queue.append((handler, args, kwargs))
35
36class WaitRequest:
37 timeout_ack = False
38 condition_ack = False
39
40 def __init__(self, condition, condition_args, condition_kwargs, timeout, timestep):
41 self.timeout_started = time.time()
42 self.timeout = timeout
43 self.condition = condition
44 self.condition_args = condition_args
45 self.condition_kwargs = condition_kwargs
46
47 def condition_check(self):
48 #print("_wait_condition_check")
49 waited = time.time() - self.timeout_started
50 if self.condition(*self.condition_args, **self.condition_kwargs):
51 self.condition_ack = True
52 elif waited > self.timeout:
53 self.timeout_ack = True
54
55class EventLoop:
56 poll_funcs = []
57 gloop = None
58 gctx = None
59 deferred_handling = None
60
61 def __init__(self):
62 self.gloop = GLib.MainLoop()
63 self.gctx = self.gloop.get_context()
64 self.deferred_handling = DeferredHandling()
65
66 def _trigger_cb_func(self, user_data):
67 self.defer(user_data)
68 return True #to retrigger the timeout
69
70 def defer(self, handler, *args, **kwargs):
71 self.deferred_handling.defer(handler, *args, **kwargs)
72
73 def register_poll_func(self, func, timestep=1):
74 id = GObject.timeout_add(timestep*1000, self._trigger_cb_func, func) # in 1/1000th of a sec
75 self.poll_funcs.append((func, id))
76
77 def unregister_poll_func(self, func):
78 for pair in self.poll_funcs:
79 f, id = pair
80 if f == func:
81 GObject.source_remove(id)
82 self.poll_funcs.remove(pair)
83 return
84
85 def poll(self, may_block=False):
86 self.gctx.iteration(may_block)
87 self.deferred_handling.handle_queue()
88
89 def wait_no_raise(self, log_obj, condition, condition_args, condition_kwargs, timeout, timestep):
90 if not timeout or timeout < 0:
91 self = log_obj
92 raise log.Error('wait() *must* time out at some point.', timeout=timeout)
93 if timestep < 0.1:
94 timestep = 0.1
95
96 wait_req = WaitRequest(condition, condition_args, condition_kwargs, timeout, timestep)
97 wait_id = GObject.timeout_add(timestep*1000, self._trigger_cb_func, wait_req.condition_check)
98 while True:
99 self.poll(may_block=True)
100 if wait_req.condition_ack or wait_req.timeout_ack:
101 GObject.source_remove(wait_id)
102 success = wait_req.condition_ack
103 return success
104
105 def wait(self, log_obj, condition, *condition_args, timeout=300, timestep=1, **condition_kwargs):
106 if not self.wait_no_raise(log_obj, condition, condition_args, condition_kwargs, timeout, timestep):
107 log.ctx(log_obj)
108 raise log.Error('Wait timeout')
109
110 def sleep(self, log_obj, seconds):
111 assert seconds > 0.
112 self.wait_no_raise(log_obj, lambda: False, [], {}, timeout=seconds, timestep=seconds)
113
114
115evloop = EventLoop()
Pau Espin Pedrol927344b2017-05-22 16:38:49 +0200116
117def register_poll_func(func):
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +0200118 global evloop
119 evloop.register_poll_func(func)
Pau Espin Pedrol927344b2017-05-22 16:38:49 +0200120
121def unregister_poll_func(func):
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +0200122 global evloop
123 evloop.unregister_poll_func(func)
Pau Espin Pedrol927344b2017-05-22 16:38:49 +0200124
125def poll():
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +0200126 global evloop
127 evloop.poll()
Pau Espin Pedrol927344b2017-05-22 16:38:49 +0200128
129def wait_no_raise(log_obj, condition, condition_args, condition_kwargs, timeout, timestep):
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +0200130 global evloop
131 evloop.wait_no_raise(log_obj, condition, condition_args, condition_kwargs, timeout, timestep)
Pau Espin Pedrol927344b2017-05-22 16:38:49 +0200132
133def wait(log_obj, condition, *condition_args, timeout=300, timestep=1, **condition_kwargs):
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +0200134 global evloop
135 evloop.wait(log_obj, condition, *condition_args, timeout=timeout, timestep=timestep, **condition_kwargs)
Pau Espin Pedrol927344b2017-05-22 16:38:49 +0200136
137def sleep(log_obj, seconds):
Pau Espin Pedrolbf176e42018-03-26 19:13:32 +0200138 global evloop
139 evloop.sleep(log_obj, seconds)
140
141def defer(handler, *args, **kwargs):
142 global evloop
143 evloop.defer(handler, *args, **kwargs)
Pau Espin Pedrol927344b2017-05-22 16:38:49 +0200144
145
146# vim: expandtab tabstop=4 shiftwidth=4