VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/tests/serial/loopback.py@ 71710

Last change on this file since 71710 was 70797, checked in by vboxsync, 7 years ago

ValidationKit/serial/tdSerial1: Add loopback module for TCP/named pipe modes to relay sent data back to the guest. Named pipe support for Windows is missing

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: loopback.py 70797 2018-01-29 18:04:20Z vboxsync $
3
4"""
5VirtualBox Validation Kit - Serial loopback module.
6"""
7
8__copyright__ = \
9"""
10Copyright (C) 2018 Oracle Corporation
11
12This file is part of VirtualBox Open Source Edition (OSE), as
13available from http://www.virtualbox.org. This file is free software;
14you can redistribute it and/or modify it under the terms of the GNU
15General Public License (GPL) as published by the Free Software
16Foundation, in version 2 as it comes in the "COPYING" file of the
17VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19
20The contents of this file may alternatively be used under the terms
21of the Common Development and Distribution License Version 1.0
22(CDDL) only, as it comes in the "COPYING.CDDL" file of the
23VirtualBox OSE distribution, in which case the provisions of the
24CDDL are applicable instead of those of the GPL.
25
26You may elect to license modified versions of this file under the
27terms and conditions of either the GPL or the CDDL or both.
28"""
29__version__ = "$Revision: 70797 $"
30
31# Standard Python imports.
32#import os;
33import socket;
34import threading;
35
36
37g_ksLoopbackTcpServ = 'TcpServ';
38g_ksLoopbackTcpClient = 'TcpClient';
39g_ksLoopbackNamedPipeServ = 'NamedPipeServ';
40g_ksLoopbackNamedPipeClient = 'NamedPipeClient';
41
42class SerialLoopbackTcpServ(object):
43 """
44 Handler for a server TCP style connection.
45 """
46 def __init__(self, sLocation, iTimeout):
47 sHost, sPort = sLocation.split(':');
48 self.oConn = None;
49 self.oSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
50 self.oSock.settimeout(iTimeout);
51 self.oSock.bind((sHost, int(sPort)));
52 self.oSock.listen(1);
53 self.iTimeout = iTimeout;
54
55 def __del__(self):
56 if self.oConn is not None:
57 self.oConn.close();
58 self.oSock.close();
59
60 def pumpIo(self):
61 """
62 Main I/O pumping routine.
63 """
64 try:
65 if self.oConn is None:
66 oConn, _ = self.oSock.accept();
67 self.oConn = oConn;
68 else:
69 abData = self.oConn.recv(1024);
70 if abData is not None:
71 self.oConn.send(abData);
72 except:
73 pass;
74
75class SerialLoopbackTcpClient(object):
76 """
77 Handler for a client TCP style connection.
78 """
79 def __init__(self, sLocation, iTimeout):
80 sHost, sPort = sLocation.split(':');
81 self.oConn = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
82 self.oConn.connect((sHost, int(sPort)));
83 self.oConn.settimeout(iTimeout);
84 self.iTimeout = iTimeout;
85
86 def __del__(self):
87 self.oConn.close();
88
89 def pumpIo(self):
90 """
91 Main I/O pumping routine.
92 """
93 try:
94 abData = self.oConn.recv(1024);
95 if abData is not None:
96 self.oConn.send(abData);
97 except:
98 pass;
99
100class SerialLoopbackNamedPipeServ(object):
101 """
102 Handler for a named pipe server style connection.
103 """
104 def __init__(self, sLocation, iTimeout):
105 self.oConn = None;
106 self.oSock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM);
107 self.oSock.settimeout(iTimeout);
108 self.oSock.bind(sLocation);
109 self.oSock.listen(1);
110 self.iTimeout = iTimeout;
111
112 def __del__(self):
113 if self.oConn is not None:
114 self.oConn.close();
115 self.oSock.close();
116
117 def pumpIo(self):
118 """
119 Main I/O pumping routine.
120 """
121 try:
122 if self.oConn is None:
123 oConn, _ = self.oSock.accept();
124 self.oConn = oConn;
125 else:
126 abData = self.oConn.recv(1024);
127 if abData is not None:
128 self.oConn.send(abData);
129 except:
130 pass;
131
132class SerialLoopbackNamedPipeClient(object):
133 """
134 Handler for a named pipe client style connection.
135 """
136 def __init__(self, sLocation, iTimeout):
137 self.oConn = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM);
138 self.oConn.connect(sLocation);
139 self.oConn.settimeout(iTimeout);
140 self.iTimeout = iTimeout;
141
142 def __del__(self):
143 self.oConn.close();
144
145 def pumpIo(self):
146 """
147 Main I/O pumping routine.
148 """
149 try:
150 abData = self.oConn.recv(1024);
151 if abData is not None:
152 self.oConn.send(abData);
153 except:
154 pass;
155
156class SerialLoopback(object):
157 """
158 Serial port loopback module working with TCP and named pipes.
159 """
160
161 def __init__(self, sType, sLocation):
162 self.fShutdown = False;
163 self.sType = sType;
164 self.sLocation = sLocation;
165 self.oLock = threading.Lock();
166 self.oThread = threading.Thread(target=self.threadWorker, args=(), name=('SerLoopback'));
167
168 if sType == g_ksLoopbackTcpServ:
169 self.oIoPumper = SerialLoopbackTcpServ(sLocation, 0.5);
170 self.oThread.start();
171 elif sType == g_ksLoopbackNamedPipeServ:
172 self.oIoPumper = SerialLoopbackNamedPipeServ(sLocation, 0.5); # pylint: disable=R0204
173 self.oThread.start();
174
175 def connect(self):
176 """
177 Connects to the server for a client type version.
178 """
179 fRc = True;
180 try:
181 if self.sType == g_ksLoopbackTcpClient:
182 self.oIoPumper = SerialLoopbackTcpClient(self.sLocation, 0.5);
183 elif self.sType == g_ksLoopbackNamedPipeClient:
184 self.oIoPumper = SerialLoopbackNamedPipeClient(self.sLocation, 0.5); # pylint: disable=R0204
185 except:
186 fRc = False;
187 else:
188 self.oThread.start();
189 return fRc;
190
191 def shutdown(self):
192 """
193 Shutdown any connection and wait for it to become idle.
194 """
195 self.oLock.acquire();
196 self.fShutdown = True;
197 self.oLock.release();
198
199 def isShutdown(self):
200 """
201 Returns whether the I/O pumping thread should shut down.
202 """
203 self.oLock.acquire();
204 fShutdown = self.fShutdown;
205 self.oLock.release();
206
207 return fShutdown;
208
209 def threadWorker(self):
210 """
211 The threaded worker.
212 """
213 while not self.isShutdown():
214 self.oIoPumper.pumpIo();
215
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette