Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3be8d694e628cb90e61385c284b18b590ddabf9e (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
# *******************************************************************************
# * 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, threading
from tcf import protocol, channel

class DelegatingEventListener(channel.EventListener):
    def __init__(self, callable):
        self._callable = callable
    def event(self, name, data):
        try:
            args = channel.fromJSONSequence(data)
            self._callable(self.svc_name, name, *args)
        except exceptions.Exception as x:
            protocol.log("Error decoding event data", x)

def _print_event(service, name, *args):
    print "Event: %s.%s%s" % (service, name, tuple(args))

def get_event_printer():
    return DelegatingEventListener(_print_event)

class EventRecorder(object):
    def __init__(self, channel):
        self._channel = channel
        self._events = []
        self._listeners = {}
        self._lock = threading.RLock()
        self._filter = None
    def __del__(self):
        if self._channel.state == channel.STATE_OPEN:
            self.stop()
    def record(self, service, enable=True):
        with self._lock:
            listener = self._listeners.get(service)
            if listener:
                if not enable:
                    protocol.invokeLater(self._channel.removeEventListener, service, listener)
            elif enable:
                recorder = self
                class Listener(channel.EventListener):
                    def event(self, name, data):
                        e = Event(service, name, data)
                        recorder._event(e)
                listener = Listener()
                self._listeners[service] = listener
                protocol.invokeLater(self._channel.addEventListener, service, listener)
            self._recording = enable
    def stop(self, service=None):
        if service:
            self.record(service, False)
        else:
            for service in self._listeners.keys():
                self.record(service, False)
    def get(self):
        with self._lock:
            events = self._events
            self._events = []
        return events
    def _event(self, e):
        with self._lock:
            self._events.append(e)
    def __str__(self):
        events = self.get()
        return "\n".join(map(str, events))
    __repr__ = __str__

class Event(object):
    def __init__(self, service, name, data):
        self.service = service
        self.name = name
        try:
            self.args = channel.fromJSONSequence(data)
        except exceptions.Exception as x:
            protocol.log("Error decoding event data", x)
    def __str__(self):
        return "Event: %s.%s%s" % (self.service, self.name, tuple(self.args))
    __repr__ = __str__

Back to the top