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
|
# *******************************************************************************
# * 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, cStringIO
from tcf import protocol, errors, services
from tcf.channel import Token, toJSONSequence, fromJSONSequence, dumpJSONObject
class Command(object):
"""
This is utility class that helps to implement sending a command and receiving
command result over TCF communication channel. The class uses JSON to encode
command arguments and to decode result data.
The class also provides support for TCF standard error report encoding.
Clients are expected to subclass <code>Command</code> and override <code>done</code> method.
Note: most clients don't need to handle protocol commands directly and
can use service APIs instead. Service API does all command encoding/decoding
for a client.
Typical usage example:
def getContext(self, id, done):
class GetContextCommand(Command):
def done(self, error, args):
ctx = None
if not error:
assert len(args) == 2
error = self.toError(args[0])
if args[1]: ctx = Context(args[1])
done.doneGetContext(self.token, error, ctx)
command = GetContextCommand(self.channel, self, "getContext", [id])
return command.token
"""
__done = False
def __init__(self, channel, service, command, args):
if isinstance(service, services.Service):
service = service.getName()
self.service = service
self.command = command
self.args = args
t = None
try:
# TODO zero_copy
#zero_copy = channel.isZeroCopySupported()
t = channel.sendCommand(service, command, toJSONSequence(args), self)
except exceptions.Exception as y:
t = Token()
protocol.invokeLater(self._error, y)
self.token = t
def _error(self, error):
assert not self.__done
self.__done = True
self.done(error, None)
def progress(self, token, data):
assert self.token is token
def result(self, token, data):
assert self.token is token
error = None
args = None
try:
args = fromJSONSequence(data)
except exceptions.Exception as e:
error = e
assert not self.__done
self.__done = True
self.done(error, args)
def terminated(self, token, error):
assert self.token is token
assert not self.__done
self.__done = True
self.done(error, None)
def done(self, error, args):
raise exceptions.NotImplementedError("Abstract method")
def getCommandString(self):
buf = cStringIO.StringIO()
buf.write(self.service)
buf.write(" ")
buf.write(self.command)
if self.args is not None:
i = 0
for arg in self.args:
if i == 0:
buf.write(" ")
else:
buf.write(", ")
i += 1
try:
dumpJSONObject(arg, buf)
except exceptions.Exception as x:
buf.write("***")
buf.write(x.message)
buf.write("***")
return buf.getvalue()
def toError(self, data, include_command_text=True):
if not isinstance(data, dict): return None
map = data
bf = cStringIO.StringIO()
bf.write("TCF error report:\n")
if include_command_text:
cmd = self.getCommandString()
if len(cmd) > 120: cmd = cmd[:120] + "..."
bf.write("Command: ")
bf.write(cmd)
errors.appendErrorProps(bf, map)
return errors.ErrorReport(bf.getvalue(), map)
|