Index: net/tools/testserver/testserver_base.py |
diff --git a/net/tools/testserver/testserver_base.py b/net/tools/testserver/testserver_base.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ca6697a5b24bb6d41147ac83e9605c134dc5b09b |
--- /dev/null |
+++ b/net/tools/testserver/testserver_base.py |
@@ -0,0 +1,118 @@ |
+# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import optparse |
+import os |
+import struct |
+import sys |
+import warnings |
+ |
+# Ignore deprecation warnings, they make our output more cluttered. |
+warnings.filterwarnings("ignore", category=DeprecationWarning) |
+ |
+try: |
+ import json |
+except ImportError: |
+ import simplejson as json |
M-A Ruel
2012/08/31 22:04:05
We do not support python < 2.6 anymore, so please
mattm
2012/09/01 01:44:54
Done.
|
+ |
+if sys.platform == 'win32': |
+ import msvcrt |
+ |
+ |
+class FileMultiplexer: |
M-A Ruel
2012/08/31 22:04:05
class FileMultiplexer(object):
and everywhere
mattm
2012/09/01 01:44:54
Done.
|
+ def __init__(self, fd1, fd2) : |
+ self.__fd1 = fd1 |
+ self.__fd2 = fd2 |
+ |
+ def __del__(self) : |
M-A Ruel
2012/08/31 22:04:05
Why?
mattm
2012/09/01 01:44:54
I don't know. This is just moved from testserver.p
|
+ if self.__fd1 != sys.stdout and self.__fd1 != sys.stderr: |
+ self.__fd1.close() |
+ if self.__fd2 != sys.stdout and self.__fd2 != sys.stderr: |
+ self.__fd2.close() |
+ |
+ def write(self, text) : |
+ self.__fd1.write(text) |
+ self.__fd2.write(text) |
+ |
+ def flush(self) : |
+ self.__fd1.flush() |
+ self.__fd2.flush() |
+ |
+ |
+class TestServerRunner: |
+ def __init__(self): |
+ self.option_parser = optparse.OptionParser() |
+ self.add_options() |
+ |
+ def main(self): |
+ self.options, self.args = self.option_parser.parse_args() |
+ |
+ logfile = open('testserver.log', 'w') |
+ sys.stderr = FileMultiplexer(sys.stderr, logfile) |
+ if self.options.log_to_console: |
+ sys.stdout = FileMultiplexer(sys.stdout, logfile) |
+ else: |
+ sys.stdout = logfile |
+ |
+ server_data = {} |
M-A Ruel
2012/08/31 22:04:05
server_data = {
'host': self.options.host,
}
mattm
2012/09/01 01:44:54
Done.
|
+ server_data['host'] = self.options.host |
+ |
M-A Ruel
2012/08/31 22:04:05
why
so
much
whitespace?
mattm
2012/09/01 01:44:54
Done.
|
+ self.server = self.create_server(server_data) |
+ |
+ self._notify_startup_complete(server_data) |
+ |
+ self.run_server() |
+ |
+ def create_server(self, server_data): |
+ """Creates a server object and returns it. |
+ |
+ Must populate server_data['port'], and can set additional server_data |
+ elements if desired.""" |
+ raise NotImplementedError |
M-A Ruel
2012/08/31 22:04:05
raise NotImplementedError()
mattm
2012/09/01 01:44:54
Done.
|
+ |
+ def run_server(self): |
+ try: |
+ self.server.serve_forever() |
+ except KeyboardInterrupt: |
+ print 'shutting down server' |
+ self.server.stop = True |
+ |
+ def add_options(self): |
+ self.option_parser.add_option('', '--startup-pipe', type='int', |
M-A Ruel
2012/08/31 22:04:05
I'm not sure why all the "'', ".
mattm
2012/09/01 01:44:54
dunno either, but fixed.
|
+ dest='startup_pipe', |
+ help='File handle of pipe to parent process') |
+ self.option_parser.add_option('', '--log-to-console', action='store_const', |
+ const=True, default=False, |
+ dest='log_to_console', |
+ help='Enables or disables sys.stdout logging ' |
+ 'to the console.') |
+ self.option_parser.add_option('', '--port', default='0', type='int', |
M-A Ruel
2012/08/31 22:04:05
default=0 since you used type int
mattm
2012/09/01 01:44:54
Done.
|
+ help='Port used by the server. If ' |
+ 'unspecified, the server will listen on an ' |
+ 'ephemeral port.') |
+ self.option_parser.add_option('', '--host', default='127.0.0.1', |
+ dest='host', |
+ help='Hostname or IP upon which the server ' |
+ 'will listen. Client connections will also ' |
+ 'only be allowed from this address.') |
+ |
+ def _notify_startup_complete(self, server_data): |
+ # Notify the parent that we've started. (BaseServer subclasses |
+ # bind their sockets on construction.) |
+ if self.options.startup_pipe is not None: |
M-A Ruel
2012/08/31 22:04:05
if self.options.startup_pipe:
mattm
2012/09/01 01:44:54
0 is a valid file descripton
mattm
2012/09/01 01:45:43
s/descripton/descriptor/
|
+ server_data_json = json.dumps(server_data) |
+ server_data_len = len(server_data_json) |
+ print 'sending server_data: %s (%d bytes)' % ( |
+ server_data_json, server_data_len) |
+ if sys.platform == 'win32': |
+ fd = msvcrt.open_osfhandle(self.options.startup_pipe, 0) |
+ else: |
+ fd = self.options.startup_pipe |
+ startup_pipe = os.fdopen(fd, "w") |
+ # First write the data length as an unsigned 4-byte value. This |
+ # is _not_ using network byte ordering since the other end of the |
+ # pipe is on the same machine. |
+ startup_pipe.write(struct.pack('=L', server_data_len)) |
+ startup_pipe.write(server_data_json) |
+ startup_pipe.close() |