blob: 3937cf68cda361e1e4f78b2bb23de02684130014 [file] [log] [blame]
Brad Bishopc656a862016-07-25 09:11:41 -04001# 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
17import sys
18import dbus
19import dbus.mainloop.glib
20import gobject
21import obmc.mapper
22
23
24class Wait(object):
25 def __init__(self, bus, waitlist, *a, **kw):
26 self.bus = bus
27 self.waitlist = dict(zip(waitlist, [None]*len(waitlist)))
28 mapper = bus.get_object(
29 obmc.mapper.MAPPER_NAME,
30 obmc.mapper.MAPPER_PATH,
31 introspect=False)
Brad Bishopa5627042016-09-20 16:08:51 -040032 self.iface = dbus.Interface(
Brad Bishopc656a862016-07-25 09:11:41 -040033 mapper, dbus_interface=obmc.mapper.MAPPER_IFACE)
34 self.done = False
35 self.callback = kw.pop('callback', None)
Brad Bishopa5627042016-09-20 16:08:51 -040036 self.error_callback = kw.pop('error_callback', self.default_error)
Lei YU5205b162016-12-21 10:49:09 +080037 self.busy_retries = kw.pop('busy_retries', 20)
Brad Bishopa5627042016-09-20 16:08:51 -040038 self.busy_retry_delay_milliseconds = kw.pop(
Lei YU5205b162016-12-21 10:49:09 +080039 'busy_retry_delay_milliseconds', 500)
Brad Bishopa5627042016-09-20 16:08:51 -040040 self.waitlist_keyword = kw.pop('waitlist_keyword', None)
Brad Bishopc656a862016-07-25 09:11:41 -040041
42 self.bus.add_signal_receiver(
Brad Bishop829181d2017-02-24 09:49:14 -050043 self.introspection_handler,
44 dbus_interface=obmc.mapper.MAPPER_IFACE + '.Private',
45 signal_name='IntrospectionComplete')
Brad Bishopc656a862016-07-25 09:11:41 -040046 self.bus.add_signal_receiver(
Brad Bishop829181d2017-02-24 09:49:14 -050047 self.introspection_handler,
48 dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager')
Brad Bishopc656a862016-07-25 09:11:41 -040049
Brad Bishop829181d2017-02-24 09:49:14 -050050 self.introspection_handler()
Brad Bishopa5627042016-09-20 16:08:51 -040051
52 @staticmethod
53 def default_error(e):
54 raise e
55
56 def force_done(self):
57 if self.done:
Brad Bishopc656a862016-07-25 09:11:41 -040058 return
59
60 self.done = True
61 self.bus.remove_signal_receiver(
Brad Bishop829181d2017-02-24 09:49:14 -050062 self.introspection_handler,
63 dbus_interface=obmc.mapper.MAPPER_IFACE + '.Private',
64 signal_name='IntrospectionComplete')
Brad Bishopc656a862016-07-25 09:11:41 -040065 self.bus.remove_signal_receiver(
Brad Bishop829181d2017-02-24 09:49:14 -050066 self.introspection_handler,
Brad Bishopc656a862016-07-25 09:11:41 -040067 dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager',
68 signal_name='InterfacesAdded')
Brad Bishopa5627042016-09-20 16:08:51 -040069
70 def check_done(self):
71 if not all(self.waitlist.values()) or self.done:
72 return
73
74 self.force_done()
75
Brad Bishopc656a862016-07-25 09:11:41 -040076 if self.callback:
Brad Bishopa5627042016-09-20 16:08:51 -040077 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 Bishopbf464f42016-11-02 00:37:06 -040086 [],
87 signature='sas',
Brad Bishopa5627042016-09-20 16:08:51 -040088 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 Spinler3c18b9f2017-01-27 14:41:48 -0600100 elif e.get_dbus_name() in \
101 ['org.freedesktop.DBus.Error.ObjectPathInUse',
102 'org.freedesktop.DBus.Error.LimitsExceeded']:
Brad Bishopa5627042016-09-20 16:08:51 -0400103 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 Bishopc656a862016-07-25 09:11:41 -0400115
116 def get_object_callback(self, path, info):
117 self.waitlist[path] = list(info)[0]
118 self.check_done()
119
Brad Bishop829181d2017-02-24 09:49:14 -0500120 def introspection_handler(self, *a, **kw):
Brad Bishopc656a862016-07-25 09:11:41 -0400121 if self.done:
122 return
123
Brad Bishopa5627042016-09-20 16:08:51 -0400124 for path in filter(
125 lambda x: not self.waitlist[x], self.waitlist.keys()):
126 self.get_object_async(path, 0)