Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 1 | # 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 Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 8 | # it under the terms of the GNU General Public License as |
Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 9 | # 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 Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 15 | # GNU General Public License for more details. |
Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 16 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 17 | # You should have received a copy of the GNU General Public License |
Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 20 | import time |
Pau Espin Pedrol | bf176e4 | 2018-03-26 19:13:32 +0200 | [diff] [blame] | 21 | from gi.repository import GLib, GObject |
| 22 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 23 | from . import log |
| 24 | |
Pau Espin Pedrol | bf176e4 | 2018-03-26 19:13:32 +0200 | [diff] [blame] | 25 | class DeferredHandling: |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 26 | |
| 27 | def __init__(self): |
| 28 | self.defer_queue = [] |
Pau Espin Pedrol | bf176e4 | 2018-03-26 19:13:32 +0200 | [diff] [blame] | 29 | |
| 30 | def handle_queue(self): |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 31 | while self.defer_queue: |
Pau Espin Pedrol | bf176e4 | 2018-03-26 19:13:32 +0200 | [diff] [blame] | 32 | 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 | |
| 38 | class WaitRequest: |
Pau Espin Pedrol | bf176e4 | 2018-03-26 19:13:32 +0200 | [diff] [blame] | 39 | |
| 40 | def __init__(self, condition, condition_args, condition_kwargs, timeout, timestep): |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 41 | self.timeout_ack = False |
| 42 | self.condition_ack = False |
Pau Espin Pedrol | bf176e4 | 2018-03-26 19:13:32 +0200 | [diff] [blame] | 43 | 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 | |
| 57 | class EventLoop: |
Pau Espin Pedrol | bf176e4 | 2018-03-26 19:13:32 +0200 | [diff] [blame] | 58 | |
| 59 | def __init__(self): |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 60 | self.poll_funcs = [] |
Pau Espin Pedrol | bf176e4 | 2018-03-26 19:13:32 +0200 | [diff] [blame] | 61 | 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 | |
| 88 | def wait_no_raise(self, log_obj, condition, condition_args, condition_kwargs, timeout, timestep): |
| 89 | if not timeout or timeout < 0: |
| 90 | self = log_obj |
| 91 | raise log.Error('wait() *must* time out at some point.', timeout=timeout) |
| 92 | if timestep < 0.1: |
| 93 | timestep = 0.1 |
| 94 | |
| 95 | wait_req = WaitRequest(condition, condition_args, condition_kwargs, timeout, timestep) |
| 96 | wait_id = GObject.timeout_add(timestep*1000, self._trigger_cb_func, wait_req.condition_check) |
| 97 | while True: |
Pau Espin Pedrol | b6ab1da | 2018-04-07 17:26:43 +0200 | [diff] [blame] | 98 | try: |
| 99 | self.poll(may_block=True) |
| 100 | except Exception: # cleanup of temporary resources in the wait scope |
| 101 | GObject.source_remove(wait_id) |
| 102 | raise |
Pau Espin Pedrol | bf176e4 | 2018-03-26 19:13:32 +0200 | [diff] [blame] | 103 | if wait_req.condition_ack or wait_req.timeout_ack: |
| 104 | GObject.source_remove(wait_id) |
| 105 | success = wait_req.condition_ack |
| 106 | return success |
| 107 | |
| 108 | def wait(self, log_obj, condition, *condition_args, timeout=300, timestep=1, **condition_kwargs): |
| 109 | if not self.wait_no_raise(log_obj, condition, condition_args, condition_kwargs, timeout, timestep): |
| 110 | log.ctx(log_obj) |
Pau Espin Pedrol | e523665 | 2018-05-28 12:46:42 +0200 | [diff] [blame] | 111 | raise log.Error('Wait timeout', condition=condition, timeout=timeout, timestep=timestep) |
Pau Espin Pedrol | bf176e4 | 2018-03-26 19:13:32 +0200 | [diff] [blame] | 112 | |
| 113 | def sleep(self, log_obj, seconds): |
| 114 | assert seconds > 0. |
| 115 | self.wait_no_raise(log_obj, lambda: False, [], {}, timeout=seconds, timestep=seconds) |
| 116 | |
| 117 | |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 118 | MainLoop = EventLoop() |
Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 119 | |
| 120 | |
| 121 | # vim: expandtab tabstop=4 shiftwidth=4 |