Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6b110354231f6a5583d7c80e06e20075118da1e7 (plain) (blame)
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
# *******************************************************************************
# * Copyright (c) 2011 Wind River Systems, Inc. and others.
# * All rights reserved. This program and the accompanying materials
# * are made available under the terms of the Eclipse Public License v1.0
# * which accompanies this distribution, and is available at
# * http://www.eclipse.org/legal/epl-v10.html
# *
# * Contributors:
# *     Wind River Systems - initial API and implementation
# *******************************************************************************

import exceptions
from tcf import channel
from tcf.services import runcontrol
from tcf.channel.Command import Command

class RunContext(runcontrol.RunControlContext):
    def __init__(self, service, props):
        super(RunContext, self).__init__(props)
        self.service = service

    def getState(self, done):
        service = self.service
        id = self.getID()
        class GetStateCommand(Command):
            def __init__(self):
                super(GetStateCommand, self).__init__(service.channel, service, "getState", (id,))
            def done(self, error, args):
                susp = False
                pc = None
                reason = None
                map = None
                if not error:
                    assert len(args) == 5
                    error = self.toError(args[0])
                    susp = args[1]
                    if args[2]: pc = str(args[2])
                    reason = args[3]
                    map = args[4]
                done.doneGetState(self.token, error, susp, pc, reason, map)
        return GetStateCommand().token

#    def resume(self, mode, count, done):
#        return self._command("resume", [self.getID(), mode, count], done)

    def resume(self, mode, count, params, done):
        if not params:
            return self._command("resume", (self.getID(), mode, count), done)
        else:
            return self._command("resume", (self.getID(), mode, count, params), done)

    def suspend(self, done):
        return self._command("suspend", (self.getID(),), done)

    def terminate(self, done):
        return self._command("terminate", (self.getID(),), done)

    def _command(self, cmd, args, done):
        service = self.service
        class RCCommand(Command):
            def __init__(self, cmd, args):
                super(RCCommand, self).__init__(service.channel, service, cmd, args)
            def done(self, error, args):
                if not error:
                    assert len(args) == 1
                    error = self.toError(args[0])
                done.doneCommand(self.token, error)
        return RCCommand(cmd, args).token

    def __str__(self):
        return "[Run Control Context %s]" % str(self._props)


class ChannelEventListener(channel.EventListener):
    def __init__(self, service, listener):
        self.service = service
        self.listener = listener
    def event(self, name, data):
        try:
            args = channel.fromJSONSequence(data)
            if name == "contextSuspended":
                assert len(args) == 4
                self.listener.contextSuspended(args[0], args[1], args[2], args[3])
            elif name == "contextResumed":
                assert len(args) == 1
                self.listener.contextResumed(args[0])
            elif name == "contextAdded":
                assert len(args) == 1
                self.listener.contextAdded(args[0])
            elif name == "contextChanged":
                assert len(args) == 1
                self.listener.contextChanged(args[0])
            elif name == "contextRemoved":
                assert len(args) == 1
                self.listener.contextRemoved(args[0])
            elif name == "contextException":
                assert len(args) == 2
                self.listener.contextException(args[0], args[1])
            elif name == "containerSuspended":
                assert len(args) == 5
                self.listener.containerSuspended(args[0], args[1], args[2], args[3], args[4])
            elif name == "containerResumed":
                assert len(args) == 1
                self.listener.containerResumed(args[0])
            else:
                raise IOError("RunControl service: unknown event: " + name);
        except exceptions.Exception as x:
            self.service.channel.terminate(x)

class RunControlProxy(runcontrol.RunControlService):
    def __init__(self, channel):
        self.channel = channel
        self.listeners = {}

    def addListener(self, listener):
        l = ChannelEventListener(self, listener)
        self.channel.addEventListener(self, l)
        self.listeners[listener] = l

    def removeListener(self, listener):
        l = self.listeners.get(listener)
        if l:
            del self.listeners[listener]
            self.channel.removeEventListener(self, l)

    def getContext(self, context_id, done):
        service = self
        class GetContextCommand(Command):
            def __init__(self):
                super(GetContextCommand, self).__init__(service.channel, service, "getContext", (context_id,))
            def done(self, error, args):
                ctx = None
                if not error:
                    assert len(args) == 2
                    error = self.toError(args[0])
                    if args[1]: ctx = RunContext(service, args[1])
                done.doneGetContext(self.token, error, ctx)
        return GetContextCommand().token

    def getChildren(self, parent_context_id, done):
        service = self
        class GetChildrenCommand(Command):
            def __init__(self):
                super(GetChildrenCommand, self).__init__(service.channel, service, "getChildren", (parent_context_id,))
            def done(self, error, args):
                contexts = None
                if not error:
                    assert len(args) == 2
                    error = self.toError(args[0])
                    contexts = args[1]
                done.doneGetChildren(self.token, error, contexts)
        return GetChildrenCommand().token

Back to the top