source: rtems-tools/tester/rt/telnet.py

Last change on this file was c73fa16, checked in by Chris Johns <chrisj@…>, on 09/07/21 at 04:09:31

tester/telnet: Only reopen once a second

  • Limit the reopen to once a second
  • Make sure the socket is closed when reopening
  • Property mode set to 100644
File size: 3.7 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2013-2017 Chris Johns (chrisj@rtems.org)
4# All rights reserved.
5#
6# This file is part of the RTEMS Tools package in 'rtems-tools'.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions are met:
10#
11# 1. Redistributions of source code must retain the above copyright notice,
12# this list of conditions and the following disclaimer.
13#
14# 2. Redistributions in binary form must reproduce the above copyright notice,
15# this list of conditions and the following disclaimer in the documentation
16# and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31#
32# RTEMS Testing Consoles
33#
34
35import errno
36import os
37import sys
38import telnetlib
39import time
40
41from rtemstoolkit import error
42from rtemstoolkit import host
43from rtemstoolkit import path
44
45class tty:
46
47    def __init__(self, dev):
48        self.dev = dev
49        self.timeout = 5
50        self.conn = None
51        ds = dev.split(':')
52        self.host = ds[0]
53        if len(ds) == 1:
54            self.port = 23
55        else:
56            try:
57                self.port = int(ds[1])
58            except:
59                raise error.general('invalid port: %s' % (dev))
60        self.conn = telnetlib.Telnet()
61        self._reopen()
62
63    def __del__(self):
64        if self.conn:
65            try:
66                self.conn.close()
67            except:
68                pass
69
70    def __str__(self):
71        s = 'host: %s port: %d' % ((self.host, self.port))
72        return s
73
74    def _reopen(self):
75        if self.conn:
76            self.conn.close()
77            time.sleep(1)
78        try:
79            self.conn.open(self.host, self.port, self.timeout)
80        except IOError as ioe:
81            if self.conn:
82                self.conn.close()
83            raise error.general('opening telnet: %s:%d: %s' % (self.host,
84                                                               self.port,
85                                                               ioe))
86        except:
87            if self.conn:
88                self.conn.close()
89            raise error.general('opening telnet: %s:%d: unknown' % (self.host,
90                                                                    self.port))
91
92    def off(self):
93        self.is_on = False
94
95    def on(self):
96        self.is_on = True
97
98    def set(self, flags):
99        pass
100
101    def read(self):
102        reopen = False
103        try:
104            data = self.conn.read_very_eager()
105        except IOError as ioe:
106            if ioe.errno == errno.ECONNREFUSED:
107                reopen = True
108            data = ''
109        except EOFError as ose:
110            reopen = True
111            data = ''
112        if reopen:
113            self._reopen()
114        return data
115
116if __name__ == "__main__":
117    if len(sys.argv) == 2:
118        import time
119        t = tty(sys.argv[1])
120        print(t)
121        while True:
122            time.sleep(0.05)
123            c = t.read()
124            sys.stdout.write(c)
Note: See TracBrowser for help on using the repository browser.