Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0b81bbc738f739a0518763fc72572d80b88bf6e4 (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
153
# *******************************************************************************
# * 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 cStringIO, json, binascii, types, exceptions

# channel states
STATE_OPENING = 0
STATE_OPEN = 1
STATE_CLOSED = 2

class TraceListener(object):
    def onMessageReceived(self, type, token, service, name, data):
        pass

    def onMessageSent(self, type, token, service, name, data):
        pass

    def onChannelClosed(self, error):
        pass

def Proxy(object):
    def onCommand(self, token, service, name, data):
        pass

    def onEvent(self, service, name, data):
        pass

    def onChannelClosed(self, error):
        pass

_token_cnt = 0
class Token(object):
    def __init__(self, id=None, listener=None):
        if id is None:
            global _token_cnt
            id = str(_token_cnt)
            _token_cnt += 1
        else:
            id = str(id)
        self.id = id
        self.listener = listener
    def getID(self):
        return self.id
    def getListener(self):
        return self.listener
    def cancel(self):
        return False

class ChannelListener(object):
    """
    Channel listener interface.
    """

    def onChannelOpened(self):
        """
        Called when a channel is opened or redirected.
        """
        pass

    def onChannelClosed(self, error):
        """
        Called when channel closed. If it is closed because of an error,
        'error' parameter will describe the error. 'error' is null if channel
        is closed normally by calling Channel.close().
        @param error - channel exception or null
        """
        pass

    def congestionLevel(self, level):
        """
        Notifies listeners about channel out-bound traffic congestion level changes.
        When level > 0 client should delay sending more messages.
        @param level - current congestion level
        """
        pass

class EventListener(object):
    """
    A generic interface for service event listener.
    Services usually define a service specific event listener interface,
    which is implemented using this generic listener.
    Clients should user service specific listener interface,
    unless no such interface is defined.
    """
    svc_name = "<unknown>"
    def event(self, name, data):
        """
        Called when service event message is received
        @param name - event name
        @param data - event arguments encoded as bytearray
        """
        pass

class CommandServer(object):
    """
    Command server interface.
    This interface is to be implemented by service providers.
    """
    def command(self, token, name, data):
        """
        Called every time a command is received from remote peer.
        @param token - command handle
        @param name - command name
        @param data - command arguments encoded into array of bytes
        """
        pass

def toJSONSequence(args):
    if args is None:
        return None
    buf = cStringIO.StringIO()
    for arg in args:
        json.dump(arg, buf, separators=(',', ':'), cls=TCFJSONEncoder)
        buf.write('\0')
    return buf.getvalue()

def fromJSONSequence(bytes):
    if bytes[-1] == 0:
        del bytes[-1]
    str = bytes.decode("UTF-8")
    parts = str.split('\0')
    objects = []
    for part in parts:
        if part:
            objects.append(json.loads(part))
        else:
            objects.append(None)
    return objects

def toByteArray(data):
    if data is None: return None
    t = type(data)
    if t is bytearray: return data
    if t is types.StringType:
        return binascii.a2b_base64(data)
    raise exceptions.TypeError(str(t))

class TCFJSONEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, bytearray):
            return binascii.b2a_base64(o)
        elif hasattr('__iter__', o):
            return tuple(o)
        else:
            json.JSONEncoder.default(self, o)

Back to the top