Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6b410a57538c91e84b310119b373a1c700587efb (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# *****************************************************************************
# * Copyright (c) 2011, 2013-2014 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
# *****************************************************************************

"""Terminals Service allows to launch a new terminal on the remote target
system.

.. |addListener| replace:: :meth:`TerminalsService.addListener`
.. |exit| replace:: :meth:`TerminalsService.exit`
.. |getContext| replace:: :meth:`TerminalsService.getContext`
.. |getProperties| replace:: :meth:`TerminalContext.getProperties`
.. |launch| replace:: :meth:`TerminalsService.launch`
.. |removeListener| replace:: :meth:`TerminalsService.removeListener`
.. |setWinSize| replace:: :meth:`TerminalsService.setWinSize`
.. |memory| replace:: :mod:`tcf.services.memory`
.. |streams| replace:: :mod:`tcf.services.streams`
.. |DoneCommand| replace:: :class:`DoneCommand`
.. |DoneGetContext| replace:: :class:`DoneGetContext`
.. |DoneLaunch| replace:: :class:`DoneLaunch`
.. |TerminalContext| replace:: :class:`TerminalContext`
.. |TerminalsListener| replace:: :class:`TerminalsListener`

Terminals service provides access to the target OS's terminal login, allows to
start and exit a terminal login, and allows to set the terminal's window size.

If a terminal is launched by this service, its standard input/output streams
are available for client to read/write using |streams| service. Stream type of
such streams is set to ``Terminals``.

Properties
----------
Terminal Context Properties
^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. seealso:: |TerminalContext|, |getProperties|

+-----------------+--------------+--------------------------------------------+
| Name            | Type         | Description                                |
+=================+==============+============================================+
| PROP_ENCODING   | |basestring| | Terminal encoding.                         |
+-----------------+--------------+--------------------------------------------+
| PROP_HEIGHT     | |int|        | Window height size.                        |
+-----------------+--------------+--------------------------------------------+
| PROP_ID         | |basestring| | The TCF context ID.                        |
+-----------------+--------------+--------------------------------------------+
| PROP_PROCESS_ID | |int|        | The process ID of the login process of the |
|                 |              | terminal.                                  |
+-----------------+--------------+--------------------------------------------+
| PROP_PTY_TYPE   | |basestring| | The PTY type.                              |
+-----------------+--------------+--------------------------------------------+
| PROP_STDERR_ID  | |basestring| | Process standard error stream ID.          |
+-----------------+--------------+--------------------------------------------+
| PROP_STDIN_ID   | |basestring| | Process standard input stream ID.          |
+-----------------+--------------+--------------------------------------------+
| PROP_STDOUT_ID  | |basestring| | Process standard output stream ID.         |
+-----------------+--------------+--------------------------------------------+
| PROP_WIDTH      | |int|        | Window width size.                         |
+-----------------+--------------+--------------------------------------------+

Service Methods
---------------
.. autodata:: NAME
.. autoclass:: TerminalsService

addListener
^^^^^^^^^^^
.. automethod:: TerminalsService.addListener

exit
^^^^
.. automethod:: TerminalsService.exit

getContext
^^^^^^^^^^
.. automethod:: TerminalsService.getContext

getName
^^^^^^^
.. automethod:: TerminalsService.getName

launch
^^^^^^
.. automethod:: TerminalsService.launch

removeListener
^^^^^^^^^^^^^^
.. automethod:: TerminalsService.removeListener

setWinSize
^^^^^^^^^^
.. automethod:: TerminalsService.setWinSize

Callback Classes
----------------
DoneCommand
^^^^^^^^^^^
.. autoclass:: DoneCommand
    :members:

DoneGetContext
^^^^^^^^^^^^^^
.. autoclass:: DoneGetContext
    :members:

DoneLaunch
^^^^^^^^^^
.. autoclass:: DoneLaunch
    :members:

Listener
--------
.. autoclass:: TerminalsListener
    :members:

Helper Classes
--------------
TerminalContext
^^^^^^^^^^^^^^^
.. autoclass:: TerminalContext
    :members:
"""

from .. import services

NAME = "Terminals"
"""Terminals service name."""

# Context property names.
PROP_ID = "ID"
PROP_PROCESS_ID = "ProcessID"
PROP_PTY_TYPE = "PtyType"
PROP_ENCODING = "Encoding"
PROP_WIDTH = "Width"
PROP_HEIGHT = "Height"
PROP_STDIN_ID = "StdInID"
PROP_STDOUT_ID = "StdOutID"
PROP_STDERR_ID = "StdErrID"


class TerminalContext(object):
    """Terminal context object."""

    def __init__(self, props):
        self._props = props or {}

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

    def getID(self):
        """Get context ID.

        :returns: A |basestring| representing the TCF ID of this terminal, or
                  an empty string if it is not known.
        """
        return self._props.get(PROP_ID, '')

    def getProcessID(self):
        """Get process ID of the login process of the terminal.

        :returns: A |basestring| representing the process ID of this terminal,
                  or an empty string if it is not known.
        """
        return self._props.get(PROP_PROCESS_ID, '')

    def getPtyType(self):
        """Get terminal PTY type.

        :returns: A |basestring| representing the PTY type of this terminal, or
                  an empty string if it is not known.
        """
        return self._props.get(PROP_PTY_TYPE, '')

    def getEncoding(self):
        """Get terminal encoding.

        :returns: A |basestring| representing the encoding of this terminal, or
                  an empty string if it is not known.
        """
        return self._props.get(PROP_ENCODING, '')

    def getWidth(self):
        """Get terminal width.

        Terminal width is the number of characters printable on one line in the
        terminal.

        :returns: An |int| representing the width of this terminal, or **0**
                  if it is not known.
        """
        return self._props.get(PROP_WIDTH, 0)

    def getHeight(self):
        """Get Terminal height.

        Terminal height is the number of lines printable in the terminal.

        :returns: An |int| representing the height of this terminal, or **0**
                  if it is not known.
        """
        return self._props.get(PROP_HEIGHT, 0)

    def getStdErrID(self):
        """Get standard error stream ID of the terminal.

        :returns: A |basestring| representing the standard error stream ID of
                  this terminal, or an empty string if it is not known.
        """
        return self._props.get(PROP_STDERR_ID, '')

    def getStdInID(self):
        """Get standard input stream ID of the terminal.

        :returns: A |basestring| representing the standard input stream ID of
                  this terminal, or an empty string if it is not known.
        """
        return self._props.get(PROP_STDIN_ID, '')

    def getStdOutID(self):
        """Get standard output stream ID of the terminal.

        :returns: A |basestring| representing the standard output stream ID of
                  this terminal, or an empty string if it is not known.
        """
        return self._props.get(PROP_STDOUT_ID, '')

    def getProperties(self):
        """Get all available context properties.

        :returns: A |dict| of properties definit this system monitor context.

        .. seealso:: `Terminal Context Properties`_
        """
        return self._props

    def exit(self, done):  # @ReservedAssignment
        """Exit the terminal.

        :param done: Call back interface called when operation is completed.
        :type done: |DoneCommand|

        :returns: Pending command handle, can be used to cancel the command.
        """
        raise NotImplementedError("Abstract method")


class TerminalsService(services.Service):
    """TCF terminals service interface"""

    def getName(self):
        """Get this service name.

        :returns: A |basestring| representing this service name, which is the
                  value of :const:`NAME`
        """
        return NAME

    def getContext(self, contextID, done):
        """Retrieve context info for given context ID.

        A context corresponds to an terminal.

        Context IDs are valid across TCF services, so it is allowed to issue
        |getContext| command with a context that was obtained, for example,
        from |memory| service.

        However, |getContext| is supposed to return only terminal specific
        data, if the ID is not a terminal ID,|getContext| may not return any
        useful information

        :param contextID: TCF ID of the terminal context to get.
        :type contextID: |basestring|
        :param done: Call back interface called when operation is completed.
        :type done: |DoneGetContext|

        :returns: Pending command handle, can be used to cancel the command.
        """
        raise NotImplementedError("Abstract method")

    def launch(self, termType, encoding, environment, done):
        """Launch a new terminal to remote machine.

        :param termType: Requested terminal type for the new terminal.
        :type termType: |basestring| or **None**
        :param encoding: Requested encoding for the new terminal.
        :type encoding: |basestring| or **None**
        :param environment: Array of environment variable strings. If **None**
                            then default set of environment variables will be
                            used.
        :type environment: |dict|
        :param done: Call back interface called when operation is completed.
        :type done: |DoneLaunch|

        :returns: Pending command handle, can be used to cancel the command.
        """
        raise NotImplementedError("Abstract method")

    def setWinSize(self, context_id, newWidth, newHeight, done):
        """Set the terminal windows size.

        :param context_id: TCF ID of the terminal to set size for.
        :type context_id: |basestring|
        :param newWidth: New terminal width.
        :type newWidth: |int|
        :param newHeight: New terminal height.
        :type newHeight: |int|
        :param done: Call back interface called when operation is completed.
        :type done: |DoneCommand|

        :returns: Pending command handle, can be used to cancel the command.
        """
        raise NotImplementedError("Abstract method")

    def exit(self, context_id, done):  # @ReservedAssignment
        """Exit a terminal.

        :param context_id: TCF ID of the terminal to terminate.
        :type context_id: |basestring|
        :param done: Call back interface called when operation is completed.
        :type done: |DoneCommand|

        :returns: Pending command handle, can be used to cancel the command.
        """
        raise NotImplementedError("Abstract method")

    def addListener(self, listener):
        """Add terminals service event listener.

        :param listener: Terminal event listener implementation to add to
                         channel.
        :type listener: |TerminalsListener|

        :returns: **None**, always.
        """
        raise NotImplementedError("Abstract method")

    def removeListener(self, listener):
        """Remove terminals service event listener.

        :param listener: Terminal event listener implementation to remove from
                         channel.
        :type listener: |TerminalsListener|

        :returns: **None**, always.
        """
        raise NotImplementedError("Abstract method")


class DoneGetContext(object):
    """Client call back interface for |getContext|."""

    def doneGetContext(self, token, error, context):
        """Called when contexts data retrieval is done.

        :param token: Pending command handle.
        :param error: Error description if operation failed, **None** if
                      succeeded.
        :param context: Retrieved context data.
        :type context: |TerminalContext| or **None**
        """
        pass


class DoneCommand(object):
    """Client call back interface for |exit| or |setWinSize|."""

    def doneCommand(self, token, error):
        """Called when command is done.

        :param token: Pending command handle.
        :param error: Error description if operation failed, **None** if
                      succeeded.
        """
        pass


class DoneLaunch(object):
    """Client call back interface for |launch|."""

    def doneLaunch(self, token, error, terminal):
        """Called when terminal launch is done.

        :param token: Pending command handle.
        :param error: Error description if operation failed, **None** if
                      succeeded.
        :param terminal: Started terminal context.
        :type terminal: |TerminalContext|
        """
        pass


class TerminalsListener(object):
    """Terminals event listener is notified when a terminal exits or is
    resized.

    Events are reported only for terminals that were started by |launch|
    command.
    """

    def exited(self, terminal_id, exit_code):
        """Called when a terminal exits.

        :param terminal_id: TCF ID of the exited terminal.
        :type terminal_id: |basestring|
        :param exit_code: Terminal exit code.
        :type exit_code: |int|

        :returns: **None**, always.
        """
        pass

    def winSizeChanged(self, terminal_id, newWidth, newHeight):
        """Called when a terminal has been resized.

        :param terminal_id: TCF ID of the resized terminal.
        :type terminal_id: |basestring|
        :param newWidth: New terminal width.
        :type newWidth: |int|
        :param newHeight: New terminal height.
        :type newHeight: |int|

        :returns: **None**, always.
        """
        pass

Back to the top