Brad Bishop | c656a86 | 2016-07-25 09:11:41 -0400 | [diff] [blame] | 1 | # Contributors Listed Below - COPYRIGHT 2016 |
| 2 | # [+] International Business Machines Corp. |
| 3 | # |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. See the License for the specific language governing |
| 15 | # permissions and limitations under the License. |
| 16 | |
| 17 | import sys |
| 18 | import dbus |
| 19 | import dbus.mainloop.glib |
| 20 | import gobject |
| 21 | import obmc.mapper |
| 22 | |
| 23 | |
| 24 | class Wait(object): |
| 25 | def __init__(self, bus, waitlist, *a, **kw): |
| 26 | self.bus = bus |
Brad Bishop | cabc638 | 2018-01-29 15:39:07 -0500 | [diff] [blame] | 27 | self.waitlist = dict(list(zip(waitlist, [None] * len(waitlist)))) |
Brad Bishop | c656a86 | 2016-07-25 09:11:41 -0400 | [diff] [blame] | 28 | mapper = bus.get_object( |
| 29 | obmc.mapper.MAPPER_NAME, |
| 30 | obmc.mapper.MAPPER_PATH, |
| 31 | introspect=False) |
Brad Bishop | a562704 | 2016-09-20 16:08:51 -0400 | [diff] [blame] | 32 | self.iface = dbus.Interface( |
Brad Bishop | c656a86 | 2016-07-25 09:11:41 -0400 | [diff] [blame] | 33 | mapper, dbus_interface=obmc.mapper.MAPPER_IFACE) |
| 34 | self.done = False |
| 35 | self.callback = kw.pop('callback', None) |
Brad Bishop | a562704 | 2016-09-20 16:08:51 -0400 | [diff] [blame] | 36 | self.error_callback = kw.pop('error_callback', self.default_error) |
Lei YU | 5205b16 | 2016-12-21 10:49:09 +0800 | [diff] [blame] | 37 | self.busy_retries = kw.pop('busy_retries', 20) |
Brad Bishop | a562704 | 2016-09-20 16:08:51 -0400 | [diff] [blame] | 38 | self.busy_retry_delay_milliseconds = kw.pop( |
Lei YU | 5205b16 | 2016-12-21 10:49:09 +0800 | [diff] [blame] | 39 | 'busy_retry_delay_milliseconds', 500) |
Brad Bishop | a562704 | 2016-09-20 16:08:51 -0400 | [diff] [blame] | 40 | self.waitlist_keyword = kw.pop('waitlist_keyword', None) |
Brad Bishop | c656a86 | 2016-07-25 09:11:41 -0400 | [diff] [blame] | 41 | |
| 42 | self.bus.add_signal_receiver( |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 43 | self.introspection_handler, |
| 44 | dbus_interface=obmc.mapper.MAPPER_IFACE + '.Private', |
| 45 | signal_name='IntrospectionComplete') |
Brad Bishop | c656a86 | 2016-07-25 09:11:41 -0400 | [diff] [blame] | 46 | self.bus.add_signal_receiver( |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 47 | self.introspection_handler, |
| 48 | dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager') |
Brad Bishop | c656a86 | 2016-07-25 09:11:41 -0400 | [diff] [blame] | 49 | |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 50 | self.introspection_handler() |
Brad Bishop | a562704 | 2016-09-20 16:08:51 -0400 | [diff] [blame] | 51 | |
| 52 | @staticmethod |
| 53 | def default_error(e): |
| 54 | raise e |
| 55 | |
| 56 | def force_done(self): |
| 57 | if self.done: |
Brad Bishop | c656a86 | 2016-07-25 09:11:41 -0400 | [diff] [blame] | 58 | return |
| 59 | |
| 60 | self.done = True |
| 61 | self.bus.remove_signal_receiver( |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 62 | self.introspection_handler, |
| 63 | dbus_interface=obmc.mapper.MAPPER_IFACE + '.Private', |
| 64 | signal_name='IntrospectionComplete') |
Brad Bishop | c656a86 | 2016-07-25 09:11:41 -0400 | [diff] [blame] | 65 | self.bus.remove_signal_receiver( |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 66 | self.introspection_handler, |
Brad Bishop | c656a86 | 2016-07-25 09:11:41 -0400 | [diff] [blame] | 67 | dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 68 | signal_name='InterfacesAdded') |
Brad Bishop | a562704 | 2016-09-20 16:08:51 -0400 | [diff] [blame] | 69 | |
| 70 | def check_done(self): |
| 71 | if not all(self.waitlist.values()) or self.done: |
| 72 | return |
| 73 | |
| 74 | self.force_done() |
| 75 | |
Brad Bishop | c656a86 | 2016-07-25 09:11:41 -0400 | [diff] [blame] | 76 | if self.callback: |
Brad Bishop | a562704 | 2016-09-20 16:08:51 -0400 | [diff] [blame] | 77 | kwargs = {} |
| 78 | if self.waitlist_keyword: |
| 79 | kwargs[waitlist_keyword] = self.waitlist |
| 80 | self.callback(**kwargs) |
| 81 | |
| 82 | def get_object_async(self, path, retry): |
| 83 | method = getattr(self.iface, 'GetObject') |
| 84 | method.call_async( |
| 85 | path, |
Brad Bishop | bf464f4 | 2016-11-02 00:37:06 -0400 | [diff] [blame] | 86 | [], |
| 87 | signature='sas', |
Brad Bishop | a562704 | 2016-09-20 16:08:51 -0400 | [diff] [blame] | 88 | reply_handler=lambda x: self.get_object_callback( |
| 89 | path, x), |
| 90 | error_handler=lambda x: self.get_object_error( |
| 91 | path, retry, x)) |
| 92 | return False |
| 93 | |
| 94 | def get_object_error(self, path, retry, e): |
| 95 | if self.done: |
| 96 | return |
| 97 | |
| 98 | if e.get_dbus_name() == 'org.freedesktop.DBus.Error.FileNotFound': |
| 99 | pass |
Matt Spinler | 3c18b9f | 2017-01-27 14:41:48 -0600 | [diff] [blame] | 100 | elif e.get_dbus_name() in \ |
| 101 | ['org.freedesktop.DBus.Error.ObjectPathInUse', |
| 102 | 'org.freedesktop.DBus.Error.LimitsExceeded']: |
Brad Bishop | a562704 | 2016-09-20 16:08:51 -0400 | [diff] [blame] | 103 | if retry > self.busy_retries: |
| 104 | self.force_done() |
| 105 | self.error_callback(e) |
| 106 | else: |
| 107 | gobject.timeout_add( |
| 108 | self.busy_retry_delay_milliseconds, |
| 109 | self.get_object_async, |
| 110 | path, |
| 111 | retry + 1) |
| 112 | else: |
| 113 | self.force_done() |
| 114 | self.error_callback(e) |
Brad Bishop | c656a86 | 2016-07-25 09:11:41 -0400 | [diff] [blame] | 115 | |
| 116 | def get_object_callback(self, path, info): |
| 117 | self.waitlist[path] = list(info)[0] |
| 118 | self.check_done() |
| 119 | |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 120 | def introspection_handler(self, *a, **kw): |
Brad Bishop | c656a86 | 2016-07-25 09:11:41 -0400 | [diff] [blame] | 121 | if self.done: |
| 122 | return |
| 123 | |
Brad Bishop | cabc638 | 2018-01-29 15:39:07 -0500 | [diff] [blame] | 124 | for path in [ |
| 125 | x for x in list(self.waitlist.keys()) if not self.waitlist[x]]: |
Brad Bishop | a562704 | 2016-09-20 16:08:51 -0400 | [diff] [blame] | 126 | self.get_object_async(path, 0) |