summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/gui/statemachines.py
blob: f8e5479d238a785309d7f249551803df7d799de6 (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# -*- coding: utf-8 -*-
# statemachines.py
# Copyright (C) 2013 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
State machines for the Bitmask app.
"""
import logging

from PySide import QtCore
from PySide.QtCore import QStateMachine, QState, Signal
from PySide.QtCore import QObject

from leap.bitmask.services import connections
from leap.common.check import leap_assert_type

logger = logging.getLogger(__name__)

_tr = QObject().tr

# Indexes for the state dict
_ON = "on"
_OFF = "off"
_CON = "connecting"
_DIS = "disconnecting"


class SignallingState(QState):
    """
    A state that emits a custom signal on entry.
    """
    def __init__(self, signal, parent=None, name=None):
        """
        Initializer.
        :param signal: the signal to be emitted on entry on this state.
        :type signal: QtCore.QSignal
        """
        super(SignallingState, self).__init__(parent)
        self._signal = signal
        self._name = name

    def onEntry(self, *args):
        """
        Emits the signal on entry.
        """
        logger.debug('State %s::%s entered. Emitting signal ...'
                     % (self._name, self.objectName()))
        if self._signal is not None:
            self._signal.emit()


class States(object):
    """
    States for composite objects
    """

    class Off(SignallingState):
        pass

    class Connecting(SignallingState):
        pass

    class On(SignallingState):
        pass

    class Disconnecting(SignallingState):
        pass

    class StepsTrack(QObject):
        state_change = Signal()

        def __init__(self, target):
            super(States.StepsTrack, self).__init__()
            self.received = set([])
            self.target = set(target)

        def is_all_done(self):
            return all([ev in self.target for ev in self.received])

        def is_any_done(self):
            return any([ev in self.target for ev in self.received])

        def seen(self, _type):
            if _type in self.target:
                self.received.add(_type)

        def reset_seen(self):
            self.received = set([])

    class TransitionOR(QtCore.QSignalTransition):

        def __init__(self, state):
            super(States.TransitionOR, self).__init__(
                state, QtCore.SIGNAL('state_change()'))
            self.state = state

        def eventTest(self, e):
            self.state.seen(e.type())
            done = self.state.is_any_done()
            if done:
                self.state.reset_seen()
            return done

        def onTransition(self, e):
            pass

    class TransitionAND(QtCore.QSignalTransition):

        def __init__(self, state):
            super(States.TransitionAND, self).__init__(
                state, QtCore.SIGNAL('state_change()'))
            self.state = state

        def eventTest(self, e):
            self.state.seen(e.type())
            done = self.state.is_all_done()
            if done:
                self.state.reset_seen()
            return done

        def onTransition(self, e):
            pass


class CompositeEvent(QtCore.QEvent):
    def __init__(self):
        super(CompositeEvent, self).__init__(
            QtCore.QEvent.Type(self.ID))


class Composite(object):
    # TODO we should generate the connectingEvents dinamycally,
    # depending on how much composite states do we get.
    # This only supports up to 2 composite states.

    class ConnectingEvent1(CompositeEvent):
        ID = QtCore.QEvent.User + 1

    class ConnectingEvent2(CompositeEvent):
        ID = QtCore.QEvent.User + 2

    class ConnectedEvent1(CompositeEvent):
        ID = QtCore.QEvent.User + 3

    class ConnectedEvent2(CompositeEvent):
        ID = QtCore.QEvent.User + 4

    class DisconnectingEvent1(CompositeEvent):
        ID = QtCore.QEvent.User + 5

    class DisconnectingEvent2(CompositeEvent):
        ID = QtCore.QEvent.User + 6

    class DisconnectedEvent1(CompositeEvent):
        ID = QtCore.QEvent.User + 7

    class DisconnectedEvent2(CompositeEvent):
        ID = QtCore.QEvent.User + 8


class Events(QtCore.QObject):
    """
    A Wrapper object for containing the events that will be
    posted to a composite state machine.
    """
    def __init__(self, parent=None):
        """
        Initializes the QObject with the given parent.
        """
        QtCore.QObject.__init__(self, parent)


class CompositeMachine(QStateMachine):

    def __init__(self, parent=None):
        QStateMachine.__init__(self, parent)

        # events
        self.events = Events(parent)
        self.create_events()

    def create_events(self):
        """
        Creates a bunch of events to be posted to the state machine when
        the transitions say so.
        """
        # XXX refactor into a dictionary?
        self.events.con_ev1 = Composite.ConnectingEvent1()
        self.events.con_ev2 = Composite.ConnectingEvent2()
        self.events.on_ev1 = Composite.ConnectedEvent1()
        self.events.on_ev2 = Composite.ConnectedEvent2()
        self.events.dis_ev1 = Composite.DisconnectingEvent1()
        self.events.dis_ev2 = Composite.DisconnectingEvent2()
        self.events.off_ev1 = Composite.DisconnectedEvent1()
        self.events.off_ev2 = Composite.DisconnectedEvent2()

    def beginSelectTransitions(self, e):
        """
        Weird. Having this method makes underlying backtraces
        to appear magically on the transitions.
        :param e: the received event
        :type e: QEvent
        """
        pass

    def _connect_children(self, child1, child2):
        """
        Connects the state transition signals for children machines.

        :param child1: the first child machine
        :type child1: QStateMachine
        :param child2: the second child machine
        :type child2: QStateMachine
        """
        # TODO refactor and generalize for composites
        # of more than 2 connections.

        c1 = child1.conn
        c1.qtsigs.connecting_signal.connect(self.con_ev1_slot)
        c1.qtsigs.connected_signal.connect(self.on_ev1_slot)
        c1.qtsigs.disconnecting_signal.connect(self.dis_ev1_slot)
        c1.qtsigs.disconnected_signal.connect(self.off_ev1_slot)

        c2 = child2.conn
        c2.qtsigs.connecting_signal.connect(self.con_ev2_slot)
        c2.qtsigs.connected_signal.connect(self.on_ev2_slot)
        c2.qtsigs.disconnecting_signal.connect(self.dis_ev2_slot)
        c2.qtsigs.disconnected_signal.connect(self.off_ev2_slot)

    # XXX why is this getting deletec in c++?
    #Traceback (most recent call last):
    #self.postEvent(self.events.on_ev2)
    #RuntimeError: Internal C++ object (ConnectedEvent2) already deleted.
    # XXX trying the following workaround, since
    # I cannot find why in the world this is getting deleted :(
    # XXX refactor!

    # slots connection1

    def con_ev1_slot(self):
        # XXX if we just postEvent, we get the Internal C++ object deleted...
        # so the workaround is to re-create it each time.
        self.events.con_ev1 = Composite.ConnectingEvent1()
        self.postEvent(self.events.con_ev1)

    def on_ev1_slot(self):
        self.events.on_ev1 = Composite.ConnectedEvent1()
        self.postEvent(self.events.on_ev1)

    def dis_ev1_slot(self):
        self.events.dis_ev1 = Composite.DisconnectingEvent1()
        self.postEvent(self.events.dis_ev1)

    def off_ev1_slot(self):
        self.events.off_ev1 = Composite.DisconnectedEvent1()
        self.postEvent(self.events.off_ev1)

    # slots connection2

    def con_ev2_slot(self):
        self.events.con_ev2 = Composite.ConnectingEvent2()
        self.postEvent(self.events.con_ev2)

    def on_ev2_slot(self):
        self.events.on_ev2 = Composite.ConnectedEvent2()
        self.postEvent(self.events.on_ev2)

    def dis_ev2_slot(self):
        self.events.dis_ev2 = Composite.DisconnectingEvent2()
        self.postEvent(self.events.dis_ev2)

    def off_ev2_slot(self):
        self.events.off_ev2 = Composite.DisconnectedEvent2()
        self.postEvent(self.events.off_ev2)


class ConnectionMachineBuilder(object):
    """
    Builder class for state machines made from LEAPConnections.
    """
    def __init__(self, connection):
        """
        :param connection: an instance of a concrete LEAPConnection
                           we will be building a state machine for.
        :type connection: AbstractLEAPConnection
        """
        self._conn = connection
        leap_assert_type(self._conn, connections.AbstractLEAPConnection)

    def make_machine(self, **kwargs):
        """
        Creates a statemachine associated with the passed controls.

        It returns the state machine if the connection used for initializing
        the ConnectionMachineBuilder inherits exactly from
        LEAPAbstractConnection, and a tuple with the Composite Machine and its
        individual parts in case that it is a composite machine which
        connection definition inherits from more than one class that, on their
        time, inherit from LEAPAbstractConnection.

        :params: see parameters for ``_make_simple_machine``
        :returns: a QStateMachine, or a tuple with the form:
                  (CompositeStateMachine, (StateMachine1, StateMachine2))
        :rtype: QStateMachine or tuple
        """
        components = self._conn.components

        if components is None:
        # simple case: connection definition inherits directly from
        # the abstract connection.

            leap_assert_type(self._conn, connections.AbstractLEAPConnection)
            return self._make_simple_machine(self._conn, **kwargs)

        if components:
            # composite case: connection definition inherits from several
            # classes, each one of which inherit from the abstract connection.
            child_machines = tuple(
                [ConnectionMachineBuilder(connection()).make_machine()
                    for connection in components])
            composite_machine = self._make_composite_machine(
                self._conn, child_machines, **kwargs)

            composite_machine._connect_children(
                *child_machines)

            # XXX should also connect its own states with the signals
            # for the composite machine itself

            return (composite_machine, child_machines)

    def _make_composite_machine(self, conn, children,
                                **kwargs):
        """
        Creates a composite machine.

        :param conn: an instance of a connection definition.
        :type conn: LEAPAbstractConnection
        :param children: children machines
        :type children: tuple of state machines
        :returns: A composite state machine
        :rtype: QStateMachine
        """
        # TODO split this method in smaller utility functions.
        parent = kwargs.get('parent', None)

        # 1. create machine
        machine = CompositeMachine()

        # 2. create states
        off = States.Off(conn.qtsigs.disconnected_signal,
                         parent=machine,
                         name=conn.name)
        off.setObjectName("off")

        on = States.On(conn.qtsigs.connected_signal,
                       parent=machine,
                       name=conn.name)
        on.setObjectName("on")

        connecting_state = States.Connecting(
            conn.qtsigs.connecting_signal,
            parent=machine,
            name=conn.name)
        connecting_state.setObjectName("connecting")

        disconnecting_state = States.Disconnecting(
            conn.qtsigs.disconnecting_signal,
            parent=machine,
            name=conn.name)
        disconnecting_state.setObjectName("disconnecting")

        # 3. TODO create as many connectingEvents as needed (dynamically create
        # classses for that)
        # (we have manually created classes for events under CompositeEvent for
        # now, to begin with the simple 2 states case for mail.

        # 4. state tracking objects for each transition stage

        connecting_track0 = States.StepsTrack(
            (Composite.ConnectingEvent1.ID,
             Composite.ConnectingEvent2.ID))
        connecting_track0.setObjectName("connecting_step_0")

        connecting_track1 = States.StepsTrack(
            (Composite.ConnectedEvent1.ID,
             Composite.ConnectedEvent2.ID))
        connecting_track1.setObjectName("connecting_step_1")

        disconnecting_track0 = States.StepsTrack(
            (Composite.DisconnectingEvent1.ID,
             Composite.DisconnectingEvent2.ID))
        disconnecting_track0.setObjectName("disconnecting_step_0")

        disconnecting_track1 = States.StepsTrack(
            (Composite.DisconnectedEvent1.ID,
             Composite.DisconnectedEvent2.ID))
        disconnecting_track1.setObjectName("disconnecting_step_1")

        # 5. definte the transitions with the matching state-tracking
        # objects.

        # off -> connecting
        connecting_transition = States.TransitionOR(
            connecting_track0)
        connecting_transition.setTargetState(connecting_state)
        off.addTransition(connecting_transition)

        # connecting -> on
        connected_transition = States.TransitionAND(
            connecting_track1)
        connected_transition.setTargetState(on)
        connecting_state.addTransition(connected_transition)

        # on -> disconnecting
        disconnecting_transition = States.TransitionOR(
            disconnecting_track0)
        disconnecting_transition.setTargetState(disconnecting_state)
        on.addTransition(disconnecting_transition)

        # disconnecting -> off
        disconnected_transition = States.TransitionAND(
            disconnecting_track1)
        disconnected_transition.setTargetState(off)
        disconnecting_state.addTransition(disconnected_transition)

        machine.setInitialState(off)
        machine.conn = conn
        return machine

    def _make_simple_machine(self, conn,
                             button=None, action=None, label=None):
        """
        Creates a statemachine associated with the passed controls.

        :param conn: the connection instance that defines this machine.
        :type conn: AbstractLEAPConnection

        :param button: the switch button.
        :type button: QPushButton

        :param action: the actionh that controls connection switch in a menu.
        :type action: QAction

        :param label: the label that displays the connection state
        :type label: QLabel

        :returns: a state machine
        :rtype: QStateMachine
        """
        machine = QStateMachine()
        states = self._make_states(conn, button, action, label)

        # transitions:

        states[_OFF].addTransition(
            conn.qtsigs.do_connect_signal,
            states[_CON])

        # * Clicking the buttons or actions transitions to the
        #   intermediate stage.
        if button:
            states[_OFF].addTransition(
                button.clicked,
                states[_CON])
            states[_ON].addTransition(
                button.clicked,
                states[_DIS])

        if action:
            states[_OFF].addTransition(
                action.triggered,
                states[_CON])
            states[_ON].addTransition(
                action.triggered,
                states[_DIS])

        # * We transition to the completed stages when
        #   we receive the matching signal from the underlying
        #   conductor.

        states[_CON].addTransition(
            conn.qtsigs.connected_signal,
            states[_ON])
        states[_DIS].addTransition(
            conn.qtsigs.disconnected_signal,
            states[_OFF])

        # * If we receive the connection_died, we transition
        #   from on directly to the off state
        states[_ON].addTransition(
            conn.qtsigs.connection_died_signal,
            states[_OFF])

        # XXX adding this---------------------
        states[_ON].addTransition(
            conn.qtsigs.do_disconnect_signal,
            states[_DIS])

        # * If we receive the connection_aborted, we transition
        #   from connecting to the off state
        states[_CON].addTransition(
            conn.qtsigs.connection_aborted_signal,
            states[_OFF])
        # * Connection died can in some cases also be
        #   triggered while we are in CONNECTING
        #   state. I should be avoided, since connection_aborted
        #   is clearer (and reserve connection_died
        #   for transitions from on->off
        states[_CON].addTransition(
            conn.qtsigs.connection_died_signal,
            states[_OFF])

        # adding states to the machine
        for state in states.itervalues():
            machine.addState(state)
        machine.setInitialState(states[_OFF])

        machine.conn = conn
        return machine

    def _make_states(self, conn, button, action, label):
        """
        Creates the four states for the simple state machine.
        Adds the needed properties for the passed controls.

        :param conn: the connection instance that defines this machine.
        :type conn: AbstractLEAPConnection

        :param button: the switch button.
        :type button: QPushButton

        :param action: the actionh that controls connection switch in a menu.
        :type action: QAction

        :param label: the label that displays the connection state
        :type label: QLabel

        :returns: a dict of states
        :rtype: dict
        """
        states = {}

        # TODO add tooltip

        # OFF State ----------------------
        off = SignallingState(
            None, name=conn.name)
        off_label = _tr("Turn {0}").format(
            conn.Connected.short_label)
        if button:
            off.assignProperty(
                button, 'text', off_label)
            off.assignProperty(
                button, 'enabled', True)
        if action:
            off.assignProperty(
                action, 'text', off_label)
            off.assignProperty(
                action, 'enabled', True)
        off.setObjectName(_OFF)
        states[_OFF] = off

        # CONNECTING State ----------------
        connecting = SignallingState(
            conn.qtsigs.connecting_signal,
            name=conn.name)
        on_label = _tr("Turn {0}").format(
            conn.Disconnected.short_label)
        if button:
            connecting.assignProperty(
                button, 'text', on_label)
            connecting.assignProperty(
                button, 'enabled', False)
        if action:
            connecting.assignProperty(
                action, 'text', on_label)
            connecting.assignProperty(
                action, 'enabled', False)
        connecting.setObjectName(_CON)
        states[_CON] = connecting

        # ON State ------------------------
        on = SignallingState(
            None, name=conn.name)
        on_label = _tr("Turn {0}").format(
            conn.Disconnected.short_label)
        if button:
            on.assignProperty(
                button, 'text', on_label)
            on.assignProperty(
                button, 'enabled', True)
        if action:
            on.assignProperty(
                action, 'text', on_label)
            on.assignProperty(
                action, 'enabled', True)
        # TODO set label for ON state
        on.setObjectName(_ON)
        states[_ON] = on

        # DISCONNECTING State -------------
        disconnecting = SignallingState(
            conn.qtsigs.disconnecting_signal,
            name=conn.name)
        if button:
            disconnecting.assignProperty(
                button, 'enabled', False)
        # XXX complete disconnecting
        # TODO disable button
        disconnecting.setObjectName(_DIS)
        states[_DIS] = disconnecting

        return states