OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """This is a python sync server used for testing Chrome Sync. | 6 """This is a python sync server used for testing Chrome Sync. |
7 | 7 |
8 By default, it listens on an ephemeral port and xmpp_port and sends the port | 8 By default, it listens on an ephemeral port and xmpp_port and sends the port |
9 numbers back to the originating process over a pipe. The originating process can | 9 numbers back to the originating process over a pipe. The originating process can |
10 specify an explicit port and xmpp_port if necessary. | 10 specify an explicit port and xmpp_port if necessary. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 except Exception: | 63 except Exception: |
64 self.handle_error(request, client_address) | 64 self.handle_error(request, client_address) |
65 self.close_request(request) | 65 self.close_request(request) |
66 | 66 |
67 def SetAuthenticated(self, auth_valid): | 67 def SetAuthenticated(self, auth_valid): |
68 self.authenticated = auth_valid | 68 self.authenticated = auth_valid |
69 | 69 |
70 def GetAuthenticated(self): | 70 def GetAuthenticated(self): |
71 return self.authenticated | 71 return self.authenticated |
72 | 72 |
73 def serve_forever(self): | 73 def handle_request(self): |
74 """This is a merge of asyncore.loop() and SocketServer.serve_forever(). | 74 """Adaptation of asyncore.loop""" |
75 """ | |
76 | |
77 def HandleXmppSocket(fd, socket_map, handler): | 75 def HandleXmppSocket(fd, socket_map, handler): |
78 """Runs the handler for the xmpp connection for fd. | 76 """Runs the handler for the xmpp connection for fd. |
79 | 77 |
80 Adapted from asyncore.read() et al. | 78 Adapted from asyncore.read() et al. |
81 """ | 79 """ |
82 | 80 |
83 xmpp_connection = socket_map.get(fd) | 81 xmpp_connection = socket_map.get(fd) |
84 # This could happen if a previous handler call caused fd to get | 82 # This could happen if a previous handler call caused fd to get |
85 # removed from socket_map. | 83 # removed from socket_map. |
86 if xmpp_connection is None: | 84 if xmpp_connection is None: |
87 return | 85 return |
88 try: | 86 try: |
89 handler(xmpp_connection) | 87 handler(xmpp_connection) |
90 except (asyncore.ExitNow, KeyboardInterrupt, SystemExit): | 88 except (asyncore.ExitNow, KeyboardInterrupt, SystemExit): |
91 raise | 89 raise |
92 except: | 90 except: |
93 xmpp_connection.handle_error() | 91 xmpp_connection.handle_error() |
94 | 92 |
95 while True: | 93 read_fds = [ self.fileno() ] |
96 read_fds = [ self.fileno() ] | 94 write_fds = [] |
97 write_fds = [] | 95 exceptional_fds = [] |
98 exceptional_fds = [] | |
99 | 96 |
100 for fd, xmpp_connection in self._xmpp_socket_map.items(): | 97 for fd, xmpp_connection in self._xmpp_socket_map.items(): |
101 is_r = xmpp_connection.readable() | 98 is_r = xmpp_connection.readable() |
102 is_w = xmpp_connection.writable() | 99 is_w = xmpp_connection.writable() |
103 if is_r: | 100 if is_r: |
104 read_fds.append(fd) | 101 read_fds.append(fd) |
105 if is_w: | 102 if is_w: |
106 write_fds.append(fd) | 103 write_fds.append(fd) |
107 if is_r or is_w: | 104 if is_r or is_w: |
108 exceptional_fds.append(fd) | 105 exceptional_fds.append(fd) |
109 | 106 |
110 try: | 107 try: |
111 read_fds, write_fds, exceptional_fds = ( | 108 read_fds, write_fds, exceptional_fds = ( |
112 select.select(read_fds, write_fds, exceptional_fds)) | 109 select.select(read_fds, write_fds, exceptional_fds)) |
113 except select.error, err: | 110 except select.error, err: |
114 if err.args[0] != errno.EINTR: | 111 if err.args[0] != errno.EINTR: |
115 raise | 112 raise |
116 else: | 113 else: |
117 continue | 114 return |
118 | 115 |
119 for fd in read_fds: | 116 for fd in read_fds: |
120 if fd == self.fileno(): | 117 if fd == self.fileno(): |
121 self.HandleRequestNoBlock() | 118 self.HandleRequestNoBlock() |
122 continue | 119 return |
123 HandleXmppSocket(fd, self._xmpp_socket_map, | 120 HandleXmppSocket(fd, self._xmpp_socket_map, |
124 asyncore.dispatcher.handle_read_event) | 121 asyncore.dispatcher.handle_read_event) |
125 | 122 |
126 for fd in write_fds: | 123 for fd in write_fds: |
127 HandleXmppSocket(fd, self._xmpp_socket_map, | 124 HandleXmppSocket(fd, self._xmpp_socket_map, |
128 asyncore.dispatcher.handle_write_event) | 125 asyncore.dispatcher.handle_write_event) |
129 | 126 |
130 for fd in exceptional_fds: | 127 for fd in exceptional_fds: |
131 HandleXmppSocket(fd, self._xmpp_socket_map, | 128 HandleXmppSocket(fd, self._xmpp_socket_map, |
132 asyncore.dispatcher.handle_expt_event) | 129 asyncore.dispatcher.handle_expt_event) |
133 | 130 |
134 | 131 |
135 class SyncPageHandler(testserver_base.BasePageHandler): | 132 class SyncPageHandler(testserver_base.BasePageHandler): |
136 """Handler for the main HTTP sync server.""" | 133 """Handler for the main HTTP sync server.""" |
137 | 134 |
138 def __init__(self, request, client_address, sync_http_server): | 135 def __init__(self, request, client_address, sync_http_server): |
139 get_handlers = [self.ChromiumSyncTimeHandler, | 136 get_handlers = [self.ChromiumSyncTimeHandler, |
140 self.ChromiumSyncMigrationOpHandler, | 137 self.ChromiumSyncMigrationOpHandler, |
141 self.ChromiumSyncCredHandler, | 138 self.ChromiumSyncCredHandler, |
142 self.ChromiumSyncXmppCredHandler, | 139 self.ChromiumSyncXmppCredHandler, |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 testserver_base.TestServerRunner.add_options(self) | 554 testserver_base.TestServerRunner.add_options(self) |
558 self.option_parser.add_option('--xmpp-port', default='0', type='int', | 555 self.option_parser.add_option('--xmpp-port', default='0', type='int', |
559 help='Port used by the XMPP server. If ' | 556 help='Port used by the XMPP server. If ' |
560 'unspecified, the XMPP server will listen on ' | 557 'unspecified, the XMPP server will listen on ' |
561 'an ephemeral port.') | 558 'an ephemeral port.') |
562 # Override the default logfile name used in testserver.py. | 559 # Override the default logfile name used in testserver.py. |
563 self.option_parser.set_defaults(log_file='sync_testserver.log') | 560 self.option_parser.set_defaults(log_file='sync_testserver.log') |
564 | 561 |
565 if __name__ == '__main__': | 562 if __name__ == '__main__': |
566 sys.exit(SyncServerRunner().main()) | 563 sys.exit(SyncServerRunner().main()) |
OLD | NEW |