summaryrefslogtreecommitdiff
path: root/examples/python/gi/nm-up-many.py
blob: e5faad194b882ef437e66b824974b9ba597cdf74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python
# SPDX-License-Identifier: LGPL-2.1-or-later

# A example script to activate many profiles in parallel.
#
# It uses entirely asynchronous API. At various points the
# script explicitly iterates the main context, which is unlike
# a more complex application that uses the GMainContext, which
# probably would run the context only at one point as long as
# the application is running (from the main function).

import gi
import os
import sys
import time

gi.require_version("NM", "1.0")
from gi.repository import NM, GLib


class MyError(Exception):
    pass


NUM_PARALLEL_STARTING = 10
NUM_PARALLEL_IN_PROGRESS = 50

s = os.getenv("NUM_PARALLEL_STARTING")
if s:
    NUM_PARALLEL_STARTING = int(s)

s = os.getenv("NUM_PARALLEL_IN_PROGRESS")
if s:
    NUM_PARALLEL_IN_PROGRESS = int(s)


start_time = time.monotonic()


def log(msg):
    # use nm_utils_print(), so that the log messages are in synch with
    # LIBNM_CLIENT_DEBUG=trace messages.
    NM.utils_print(0, "[%015.10f] %s\n" % (time.monotonic() - start_time, msg))


def nmc_new(io_priority=GLib.PRIORITY_DEFAULT, cancellable=None):
    # create a NMClient instance using the async initialization
    # (but the function itself iterates the main context until
    # the initialization completes).

    result = []

    def cb(source_object, res):

        try:
            source_object.init_finish(res)
        except Exception as e:
            result.append(e)
        else:
            result.append(None)

    nmc = NM.Client()
    nmc.init_async(io_priority, cancellable, cb)
    while not result:
        nmc.get_main_context().iteration(may_block=True)

    if result[0]:
        raise result[0]

    log("initialized NMClient cache")

    return nmc


def nmc_destroy(nmc_transfer_ref):

    # Just for fun, show how to completely cleanup a NMClient instance.
    # An NMClient instance registers D-Bus signals and unrefing the instance
    # will cancel/unsubscribe those signals, but there might still be some
    # pending operations scheduled on the main context. That means, after
    # unrefing the NMClient instance, we may need to iterate the GMainContext
    # a bit longer, go get rid of all resources (otherwise, the GMainContext
    # itself cannot be destroyed and leaks).
    #
    # We can use nm_client_get_context_busy_watcher() for that, by subscribing
    # a weak reference and iterating the context as long as the object is
    # alive.

    nmc = nmc_transfer_ref[0]
    del nmc_transfer_ref[0]

    alive = [1]

    def weak_ref_cb(alive):
        del alive[0]

    nmc.get_context_busy_watcher().weak_ref(weak_ref_cb, alive)
    main_context = nmc.get_main_context()

    del nmc

    while alive:
        main_context.iteration(may_block=True)

    log("NMClient instance cleaned up")


def find_connections(nmc, argv):

    # parse the inpurt argv and select the connection profiles to activate.
    # The arguments are either "connection.id" or "connection.uuid", possibly
    # qualified by "id" or "uuid".

    result = []

    while True:
        if not argv:
            break
        arg_type = argv.pop(0)
        if arg_type in ["id", "uuid"]:
            if not argv:
                raise MyError('missing specifier after "%s"' % (arg_type))
            arg_param = argv.pop(0)
        else:
            arg_param = arg_type
            arg_type = "*"

        cc = []
        for c in nmc.get_connections():
            if arg_type in ["id", "*"] and arg_param == c.get_id():
                cc.append(c)
            if arg_type in ["uuid", "*"] and arg_param == c.get_uuid():
                cc.append(c)

        if not cc:
            raise MyError(
                'Could not find a matching connection "%s" "%s"' % (arg_type, arg_param)
            )
        if len(cc) > 1:
            raise MyError(
                'Could not find a unique matching connection "%s" "%s", instead %d profiles found'
                % (arg_type, arg_param, len(cc))
            )

        if cc[0] not in result:
            # we allow duplicates, but combine them.
            result.extend(cc)

    for c in result:
        log(
            "requested connection: %s (%s) (%s)"
            % (c.get_id(), c.get_uuid(), c.get_path())
        )

    return result


class Activation(object):
    ACTIVATION_STATE_START = "start"
    ACTIVATION_STATE_STARTING = "starting"
    ACTIVATION_STATE_WAITING = "waiting"
    ACTIVATION_STATE_DONE = "done"

    def __init__(self, con):
        self.con = con
        self.state = Activation.ACTIVATION_STATE_START
        self.result_msg = None
        self.result_ac = None
        self.ac_result = None
        self.wait_id = None

    def __str__(self):
        return "%s (%s)" % (self.con.get_id(), self.con.get_uuid())

    def is_done(self, log=log):

        if self.state == Activation.ACTIVATION_STATE_DONE:
            return True

        if self.state != Activation.ACTIVATION_STATE_WAITING:
            return False

        def _log_result(self, msg, done_with_success=False):
            log("connection %s done: %s" % (self, msg))
            self.state = Activation.ACTIVATION_STATE_DONE
            self.done_with_success = done_with_success
            return True

        ac = self.result_ac
        if not ac:
            return _log_result(self, "failed activation call (%s)" % (self.result_msg,))

        if ac.get_client() is None:
            return _log_result(self, "active connection disappeared")

        if ac.get_state() > NM.ActiveConnectionState.ACTIVATED:
            return _log_result(
                self, "connection failed to activate (state %s)" % (ac.get_state())
            )

        if ac.get_state() == NM.ActiveConnectionState.ACTIVATED:
            return _log_result(
                self, "connection successfully activated", done_with_success=True
            )

        return False

    def start(self, nmc, cancellable=None, activated_callback=None, log=log):

        # Call nmc.activate_connection_async() and return a user data
        # with the information about the pending operation.

        assert self.state == Activation.ACTIVATION_STATE_START

        self.state = Activation.ACTIVATION_STATE_STARTING

        log("activation %s start asynchronously" % (self))

        def cb_activate_connection(source_object, res):
            assert self.state == Activation.ACTIVATION_STATE_STARTING
            try:
                ac = nmc.activate_connection_finish(res)
            except Exception as e:
                self.result_msg = str(e)
                log(
                    "activation %s started asynchronously failed: %s"
                    % (self, self.result_msg)
                )
            else:
                self.result_msg = "success"
                self.result_ac = ac
                log(
                    "activation %s started asynchronously success: %s"
                    % (self, ac.get_path())
                )
            self.state = Activation.ACTIVATION_STATE_WAITING
            if activated_callback is not None:
                activated_callback(self)

        nmc.activate_connection_async(
            self.con, None, None, cancellable, cb_activate_connection
        )

    def wait(self, done_callback=None, log=log):

        assert self.state == Activation.ACTIVATION_STATE_WAITING
        assert self.result_ac
        assert self.wait_id is None

        def cb_wait(ac, state):
            if self.is_done(log=log):
                self.result_ac.disconnect(self.wait_id)
                self.wait_id = None
                done_callback(self)

        log("waiting for %s to fully activate" % (self))
        self.wait_id = self.result_ac.connect("notify", cb_wait)


class Manager(object):
    def __init__(self, nmc, cons):

        self.nmc = nmc

        self.ac_start = [Activation(c) for c in cons]
        self.ac_starting = []
        self.ac_waiting = []
        self.ac_done = []

    def _log(self, msg):

        lists = [self.ac_start, self.ac_starting, self.ac_waiting, self.ac_done]

        n = sum(len(l) for l in lists)
        n = str(len(str(n)))

        prefix = "/".join((("%0" + n + "d") % len(l)) for l in lists)
        log("%s: %s" % (prefix, msg))

    def ac_run(self):

        loop = GLib.MainLoop(self.nmc.get_main_context())

        while self.ac_start or self.ac_starting or self.ac_waiting:

            rate_limit_parallel_in_progress = (
                len(self.ac_starting) + len(self.ac_waiting) >= NUM_PARALLEL_IN_PROGRESS
            )

            if (
                not rate_limit_parallel_in_progress
                and self.ac_start
                and len(self.ac_starting) < NUM_PARALLEL_STARTING
            ):
                activation = self.ac_start.pop(0)
                self.ac_starting.append(activation)

                def cb_activated(activation2):
                    self.ac_starting.remove(activation2)
                    if activation2.is_done(log=self._log):
                        self.ac_done.append(activation2)
                    else:
                        self.ac_waiting.append(activation2)

                        def cb_done(activation3):
                            self.ac_waiting.remove(activation3)
                            self.ac_done.append(activation3)
                            loop.quit()

                        activation2.wait(done_callback=cb_done, log=self._log)
                    loop.quit()

                activation.start(
                    self.nmc, activated_callback=cb_activated, log=self._log
                )
                continue

            loop.run()

        res_list = [ac.done_with_success for ac in self.ac_done]

        log(
            "%s out of %s activations are now successfully activated"
            % (sum(res_list), len(self.ac_done))
        )

        return all(res_list)


def main():
    nmc = nmc_new()

    cons = find_connections(nmc, sys.argv[1:])

    all_good = Manager(nmc, cons).ac_run()

    nmc_transfer_ref = [nmc]
    del nmc
    nmc_destroy(nmc_transfer_ref)

    sys.exit(0 if all_good else 1)


if __name__ == "__main__":
    main()