Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tutorius/service.py
blob: 70efc80ca4b38cd489f51da941bffcff1ef27a2b (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
import dbus

from .engine import Engine
from .dbustools import remote_call
from .TProbe import ProbeManager
import logging
LOGGER = logging.getLogger("sugar.tutorius.service")

_DBUS_SERVICE = "org.tutorius.Service"
_DBUS_PATH = "/org/tutorius/Service"
_DBUS_SERVICE_IFACE = "org.tutorius.Service"

class Service(dbus.service.Object):
    """
    Global tutorius entry point to control the whole system
    """

    def __init__(self):
        bus = dbus.SessionBus()
        bus_name = dbus.service.BusName(_DBUS_SERVICE, bus=bus)
        dbus.service.Object.__init__(self, bus_name, _DBUS_PATH)

        self._engine = None

        self._probeMgr = ProbeManager()

    def start(self):
        """ Start the service itself
        """
        # For the moment there is nothing to do
        LOGGER.debug("Service.start()")


    @dbus.service.method(_DBUS_SERVICE_IFACE,
                          in_signature="s", out_signature="") 
    def launch(self, tutorialID):
        """ Launch a tutorial 
            @param tutorialID unique tutorial identifier used to retrieve it from the disk
        """
        if self._engine == None:
            self._engine = Engine()
        self._engine.launch(tutorialID)

    @dbus.service.method(_DBUS_SERVICE_IFACE,
                          in_signature="", out_signature="") 
    def stop(self):
        """ Stop the current tutorial
        """
        self._engine.stop()

    @dbus.service.method(_DBUS_SERVICE_IFACE,
                          in_signature="", out_signature="") 
    def pause(self):
        """ Interrupt the current tutorial and save its state in the journal
        """
        self._engine.pause()

    @dbus.service.method(_DBUS_SERVICE_IFACE,
                          in_signature="ss", out_signature="") 
    def register_probe(self, process_name, unique_id):
        """ Adds a probe to the known probes, to be used by a tutorial. 

            A generic name for a process (like an Activity) is passed
            so that the execution of a tutorial will use that generic
            name. However, a unique id is also passed to differentiate
            between many instances of the same process.
            
            @param process_name The generic name of a process
            @param unique_id The unique identification associated to this 
                             process
        """
        LOGGER.debug("Service.register_probe(%s,%s)", process_name, unique_id)
        self._probeMgr.register_probe(process_name, unique_id)

    @dbus.service.method(_DBUS_SERVICE_IFACE,
                          in_signature="s", out_signature="") 
    def unregister_probe(self, unique_id):
        """ Remove a probe from the known probes.
            
            @param process_name The generic name of a process
            @param unique_id The unique identification associated to this 
                             process
        """
        LOGGER.debug("Service.unregister_probe(%s)", unique_id)
        self._probeMgr.unregister_probe(unique_id)

class ServiceProxy:
    """ Proxy to connect to the Service object, abstracting the DBus interface"""

    def __init__(self):
        bus = dbus.SessionBus()
        self._object = bus.get_object(_DBUS_SERVICE,_DBUS_PATH)
        self._service = dbus.Interface(self._object, _DBUS_SERVICE_IFACE)

    def launch(self, tutorialID):
        """ Launch a tutorial 
            @param tutorialID unique tutorial identifier used to retrieve it from the disk
        """
        remote_call(self._service.launch, (tutorialID, ), block=False)

    def stop(self):
        """ Stop the current tutorial
        """
        remote_call(self._service.stop, (), block=False)

    def pause(self):
        """ Interrupt the current tutorial and save its state in the journal
        """
        remote_call(self._service.pause, (), block=False)

    def register_probe(self, process_name, unique_id):
        """ Adds a probe to the known probes, to be used by a tutorial. 

            A generic name for a process (like an Activity) is passed
            so that the execution of a tutorial will use that generic
            name. However, a unique id is also passed to differentiate
            between many instances of the same process.
            
            @param process_name The generic name of a process
            @param unique_id The unique identification associated to this 
                             process
        """
        self._service.register_probe(process_name,unique_id)

    def unregister_probe(self, unique_id):
        """ Remove a probe from the known probes.
            
            @param process_name The generic name of a process
            @param unique_id The unique identification associated to this 
                             process
        """
        self._service.unregister_probe(unique_id)

if __name__ == "__main__":
    import dbus.mainloop.glib
    import gobject

    loop = gobject.MainLoop()
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    s = Service()
    loop.run()