Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(626)

Side by Side Diff: lib/cros_test_proxy.py

Issue 5632002: Create a transparent proxy for test programs to use, with the (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/crosutils.git@master
Patch Set: Review fixes (after the files are saved). Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« bin/cros_au_test_harness.py ('K') | « image_to_live.sh ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
sosa 2010/12/07 17:49:23 Is this meant to be called directly? If it's just
dgarrett 2010/12/09 00:48:38 Um... I had a main I used for initial testing, but
2
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 import select
8 import socket
9 import SocketServer
10 import threading
11
12 class Filter(object):
13 """Base class for data filters.
14
15 Pass subclass of this to CrosTestProxy which will perform whatever
16 connection manipulation you prefer.
17 """
18
19 def setup(self):
20 """This setup method is called once per connection."""
21 pass
22
23 def InBound(self, data):
24 """This method is called once per packet of incoming data.
25
26 The value returned is what is sent through the proxy. If
27 None is returned, the connection will be closed.
28 """
29 return data
30
31 def OutBound(self, data):
32 """This method is called once per packet of outgoing data.
33
34 The value returned is what is sent through the proxy. If
35 None is returned, the connection will be closed.
36 """
37 return data
38
39
40 class CrosTestProxy(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
41 """A transparent proxy for simulating network errors"""
42
43 class _Handler(SocketServer.BaseRequestHandler):
44 """Proxy connection handler that passes data though a filter"""
45
46 def setup(self):
47 """Setup is called once for each connection proxied."""
48 self.server.filter.setup()
49
50 def handle(self):
51 """Handles each incoming connection.
52
53 Opens a new connection to the port we are proxing to, then
54 passes each packet along in both directions after passing
55 them through the filter object passed in.
56 """
57 # Open outgoing socket
58 s_in = self.request
59 s_out = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
60 s_out.connect((self.server.address_out, self.server.port_out))
61
62 while True:
sosa 2010/12/07 17:49:23 Is there any better way to end this? i.e. create
dgarrett 2010/12/09 00:48:38 The only reason I see for introducing another thre
63 rlist, wlist, xlist = select.select([s_in, s_out], [], [])
64
65 if s_in in rlist:
66 data = s_in.recv(1024)
67 data = self.server.filter.InBound(data)
68 if not data: break
69 try:
70 # If there is any error sending data, close both connections.
sosa 2010/12/07 17:49:23 Is there a way to be more specific with the except
dgarrett 2010/12/09 00:48:38 Done.
71 s_out.sendall(data)
72 except:
73 break
74
75 if s_out in rlist:
76 data = s_out.recv(1024)
77 data = self.server.filter.OutBound(data)
78 if not data: break
79 try:
80 # If there is any error sending data, close both connections.
81 s_in.sendall(data)
82 except:
83 break
84
85 s_in.close()
86 s_out.close()
87
88 def __init__(self,
89 filter,
90 port_in=8081,
91 address_out='127.0.0.1', port_out=8080):
92 """Configures the proxy object.
93
94 Args:
95 filter: An instance of a subclass of Filter.
96 port_in: Port on which to listen for incoming connections.
97 address_out: Address to which outgoing connections will go.
98 address_port: Port to which outgoing connections will go.
99 """
100 self.port_in = port_in
101 self.address_out = address_out
102 self.port_out = port_out
103 self.filter = filter
104
105 SocketServer.TCPServer.__init__(self,
106 ('', port_in),
107 self._Handler)
108
109 def serve_forever_in_thread(self):
110 """Helper method to start the server in a new background thread."""
111 server_thread = threading.Thread(target=self.serve_forever)
112 server_thread.setDaemon(True)
113 server_thread.start()
114
115 return server_thread
116
117
118 def main():
119 s = CrosTestProxy(port_in=2222, port_out=22, filter=Filter())
sosa 2010/12/07 17:49:23 Can you comment on what are these constants?
120 s.serve_forever()
121
122
123 if __name__ == '__main__':
124 main()
OLDNEW
« bin/cros_au_test_harness.py ('K') | « image_to_live.sh ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698