David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 19 | """Implementation of non-blocking server. |
| 20 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 21 | The main idea of the server is to receive and send requests |
| 22 | only from the main thread. |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 23 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 24 | The thread poool should be sized for concurrent tasks, not |
| 25 | maximum connections |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 26 | """ |
| 27 | import threading |
| 28 | import socket |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 29 | import select |
| 30 | import struct |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 31 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 32 | import logging |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 33 | logger = logging.getLogger(__name__) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 34 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 35 | from six.moves import queue |
| 36 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 37 | from thrift.transport import TTransport |
| 38 | from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory |
| 39 | |
| 40 | __all__ = ['TNonblockingServer'] |
| 41 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 42 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 43 | class Worker(threading.Thread): |
| 44 | """Worker is a small helper to process incoming connection.""" |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 45 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 46 | def __init__(self, queue): |
| 47 | threading.Thread.__init__(self) |
| 48 | self.queue = queue |
| 49 | |
| 50 | def run(self): |
| 51 | """Process queries from task queue, stop if processor is None.""" |
| 52 | while True: |
| 53 | try: |
| 54 | processor, iprot, oprot, otrans, callback = self.queue.get() |
| 55 | if processor is None: |
| 56 | break |
| 57 | processor.process(iprot, oprot) |
| 58 | callback(True, otrans.getvalue()) |
| 59 | except Exception: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 60 | logger.exception("Exception while processing request") |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 61 | callback(False, b'') |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 62 | |
| 63 | WAIT_LEN = 0 |
| 64 | WAIT_MESSAGE = 1 |
| 65 | WAIT_PROCESS = 2 |
| 66 | SEND_ANSWER = 3 |
| 67 | CLOSED = 4 |
| 68 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 69 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 70 | def locked(func): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 71 | """Decorator which locks self.lock.""" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 72 | def nested(self, *args, **kwargs): |
| 73 | self.lock.acquire() |
| 74 | try: |
| 75 | return func(self, *args, **kwargs) |
| 76 | finally: |
| 77 | self.lock.release() |
| 78 | return nested |
| 79 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 80 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 81 | def socket_exception(func): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 82 | """Decorator close object on socket.error.""" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 83 | def read(self, *args, **kwargs): |
| 84 | try: |
| 85 | return func(self, *args, **kwargs) |
| 86 | except socket.error: |
| 87 | self.close() |
| 88 | return read |
| 89 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 90 | |
Nobuaki Sukegawa | b9c859a | 2015-12-21 01:10:25 +0900 | [diff] [blame^] | 91 | class Connection(object): |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 92 | """Basic class is represented connection. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 93 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 94 | It can be in state: |
| 95 | WAIT_LEN --- connection is reading request len. |
| 96 | WAIT_MESSAGE --- connection is reading request. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 97 | WAIT_PROCESS --- connection has just read whole request and |
| 98 | waits for call ready routine. |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 99 | SEND_ANSWER --- connection is sending answer string (including length |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 100 | of answer). |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 101 | CLOSED --- socket was closed and connection should be deleted. |
| 102 | """ |
| 103 | def __init__(self, new_socket, wake_up): |
| 104 | self.socket = new_socket |
| 105 | self.socket.setblocking(False) |
| 106 | self.status = WAIT_LEN |
| 107 | self.len = 0 |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 108 | self.message = b'' |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 109 | self.lock = threading.Lock() |
| 110 | self.wake_up = wake_up |
| 111 | |
| 112 | def _read_len(self): |
| 113 | """Reads length of request. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 114 | |
| 115 | It's a safer alternative to self.socket.recv(4) |
| 116 | """ |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 117 | read = self.socket.recv(4 - len(self.message)) |
| 118 | if len(read) == 0: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 119 | # if we read 0 bytes and self.message is empty, then |
| 120 | # the client closed the connection |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 121 | if len(self.message) != 0: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 122 | logger.error("can't read frame size from socket") |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 123 | self.close() |
| 124 | return |
| 125 | self.message += read |
| 126 | if len(self.message) == 4: |
| 127 | self.len, = struct.unpack('!i', self.message) |
| 128 | if self.len < 0: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 129 | logger.error("negative frame size, it seems client " |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 130 | "doesn't use FramedTransport") |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 131 | self.close() |
| 132 | elif self.len == 0: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 133 | logger.error("empty frame, it's really strange") |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 134 | self.close() |
| 135 | else: |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 136 | self.message = b'' |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 137 | self.status = WAIT_MESSAGE |
| 138 | |
| 139 | @socket_exception |
| 140 | def read(self): |
| 141 | """Reads data from stream and switch state.""" |
| 142 | assert self.status in (WAIT_LEN, WAIT_MESSAGE) |
| 143 | if self.status == WAIT_LEN: |
| 144 | self._read_len() |
| 145 | # go back to the main loop here for simplicity instead of |
| 146 | # falling through, even though there is a good chance that |
| 147 | # the message is already available |
| 148 | elif self.status == WAIT_MESSAGE: |
| 149 | read = self.socket.recv(self.len - len(self.message)) |
| 150 | if len(read) == 0: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame] | 151 | logger.error("can't read frame from socket (get %d of " |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 152 | "%d bytes)" % (len(self.message), self.len)) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 153 | self.close() |
| 154 | return |
| 155 | self.message += read |
| 156 | if len(self.message) == self.len: |
| 157 | self.status = WAIT_PROCESS |
| 158 | |
| 159 | @socket_exception |
| 160 | def write(self): |
| 161 | """Writes data from socket and switch state.""" |
| 162 | assert self.status == SEND_ANSWER |
| 163 | sent = self.socket.send(self.message) |
| 164 | if sent == len(self.message): |
| 165 | self.status = WAIT_LEN |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 166 | self.message = b'' |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 167 | self.len = 0 |
| 168 | else: |
| 169 | self.message = self.message[sent:] |
| 170 | |
| 171 | @locked |
| 172 | def ready(self, all_ok, message): |
| 173 | """Callback function for switching state and waking up main thread. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 174 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 175 | This function is the only function witch can be called asynchronous. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 176 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 177 | The ready can switch Connection to three states: |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 178 | WAIT_LEN if request was oneway. |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 179 | SEND_ANSWER if request was processed in normal way. |
| 180 | CLOSED if request throws unexpected exception. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 181 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 182 | The one wakes up main thread. |
| 183 | """ |
| 184 | assert self.status == WAIT_PROCESS |
| 185 | if not all_ok: |
| 186 | self.close() |
| 187 | self.wake_up() |
| 188 | return |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 189 | self.len = 0 |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 190 | if len(message) == 0: |
David Reiss | c51986f | 2009-03-24 20:01:25 +0000 | [diff] [blame] | 191 | # it was a oneway request, do not write answer |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 192 | self.message = b'' |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 193 | self.status = WAIT_LEN |
| 194 | else: |
Todd Lipcon | f5dea4c | 2009-12-03 01:18:44 +0000 | [diff] [blame] | 195 | self.message = struct.pack('!i', len(message)) + message |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 196 | self.status = SEND_ANSWER |
| 197 | self.wake_up() |
| 198 | |
| 199 | @locked |
| 200 | def is_writeable(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 201 | """Return True if connection should be added to write list of select""" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 202 | return self.status == SEND_ANSWER |
| 203 | |
| 204 | # it's not necessary, but... |
| 205 | @locked |
| 206 | def is_readable(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 207 | """Return True if connection should be added to read list of select""" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 208 | return self.status in (WAIT_LEN, WAIT_MESSAGE) |
| 209 | |
| 210 | @locked |
| 211 | def is_closed(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 212 | """Returns True if connection is closed.""" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 213 | return self.status == CLOSED |
| 214 | |
| 215 | def fileno(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 216 | """Returns the file descriptor of the associated socket.""" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 217 | return self.socket.fileno() |
| 218 | |
| 219 | def close(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 220 | """Closes connection""" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 221 | self.status = CLOSED |
| 222 | self.socket.close() |
| 223 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 224 | |
Nobuaki Sukegawa | b9c859a | 2015-12-21 01:10:25 +0900 | [diff] [blame^] | 225 | class TNonblockingServer(object): |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 226 | """Non-blocking server.""" |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 227 | |
| 228 | def __init__(self, |
| 229 | processor, |
| 230 | lsocket, |
| 231 | inputProtocolFactory=None, |
| 232 | outputProtocolFactory=None, |
| 233 | threads=10): |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 234 | self.processor = processor |
| 235 | self.socket = lsocket |
| 236 | self.in_protocol = inputProtocolFactory or TBinaryProtocolFactory() |
| 237 | self.out_protocol = outputProtocolFactory or self.in_protocol |
| 238 | self.threads = int(threads) |
| 239 | self.clients = {} |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 240 | self.tasks = queue.Queue() |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 241 | self._read, self._write = socket.socketpair() |
| 242 | self.prepared = False |
Roger Meier | cfff856 | 2012-04-13 14:24:55 +0000 | [diff] [blame] | 243 | self._stop = False |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 244 | |
| 245 | def setNumThreads(self, num): |
| 246 | """Set the number of worker threads that should be created.""" |
| 247 | # implement ThreadPool interface |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 248 | assert not self.prepared, "Can't change number of threads after start" |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 249 | self.threads = num |
| 250 | |
| 251 | def prepare(self): |
| 252 | """Prepares server for serve requests.""" |
Roger Meier | cfff856 | 2012-04-13 14:24:55 +0000 | [diff] [blame] | 253 | if self.prepared: |
| 254 | return |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 255 | self.socket.listen() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 256 | for _ in range(self.threads): |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 257 | thread = Worker(self.tasks) |
| 258 | thread.setDaemon(True) |
| 259 | thread.start() |
| 260 | self.prepared = True |
| 261 | |
| 262 | def wake_up(self): |
| 263 | """Wake up main thread. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 264 | |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 265 | The server usually waits in select call in we should terminate one. |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 266 | The simplest way is using socketpair. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 267 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 268 | Select always wait to read from the first socket of socketpair. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 269 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 270 | In this case, we can just write anything to the second socket from |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 271 | socketpair. |
| 272 | """ |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 273 | self._write.send(b'1') |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 274 | |
Roger Meier | cfff856 | 2012-04-13 14:24:55 +0000 | [diff] [blame] | 275 | def stop(self): |
| 276 | """Stop the server. |
| 277 | |
| 278 | This method causes the serve() method to return. stop() may be invoked |
| 279 | from within your handler, or from another thread. |
| 280 | |
| 281 | After stop() is called, serve() will return but the server will still |
| 282 | be listening on the socket. serve() may then be called again to resume |
| 283 | processing requests. Alternatively, close() may be called after |
| 284 | serve() returns to close the server socket and shutdown all worker |
| 285 | threads. |
| 286 | """ |
| 287 | self._stop = True |
| 288 | self.wake_up() |
| 289 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 290 | def _select(self): |
| 291 | """Does select on open connections.""" |
| 292 | readable = [self.socket.handle.fileno(), self._read.fileno()] |
| 293 | writable = [] |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 294 | for i, connection in list(self.clients.items()): |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 295 | if connection.is_readable(): |
| 296 | readable.append(connection.fileno()) |
| 297 | if connection.is_writeable(): |
| 298 | writable.append(connection.fileno()) |
| 299 | if connection.is_closed(): |
| 300 | del self.clients[i] |
| 301 | return select.select(readable, writable, readable) |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 302 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 303 | def handle(self): |
| 304 | """Handle requests. |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 305 | |
| 306 | WARNING! You must call prepare() BEFORE calling handle() |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 307 | """ |
| 308 | assert self.prepared, "You have to call prepare before handle" |
| 309 | rset, wset, xset = self._select() |
| 310 | for readable in rset: |
| 311 | if readable == self._read.fileno(): |
| 312 | # don't care i just need to clean readable flag |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 313 | self._read.recv(1024) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 314 | elif readable == self.socket.handle.fileno(): |
| 315 | client = self.socket.accept().handle |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 316 | self.clients[client.fileno()] = Connection(client, |
| 317 | self.wake_up) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 318 | else: |
| 319 | connection = self.clients[readable] |
| 320 | connection.read() |
| 321 | if connection.status == WAIT_PROCESS: |
| 322 | itransport = TTransport.TMemoryBuffer(connection.message) |
| 323 | otransport = TTransport.TMemoryBuffer() |
| 324 | iprot = self.in_protocol.getProtocol(itransport) |
| 325 | oprot = self.out_protocol.getProtocol(otransport) |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 326 | self.tasks.put([self.processor, iprot, oprot, |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 327 | otransport, connection.ready]) |
| 328 | for writeable in wset: |
| 329 | self.clients[writeable].write() |
| 330 | for oob in xset: |
| 331 | self.clients[oob].close() |
| 332 | del self.clients[oob] |
| 333 | |
| 334 | def close(self): |
| 335 | """Closes the server.""" |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 336 | for _ in range(self.threads): |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 337 | self.tasks.put([None, None, None, None, None]) |
| 338 | self.socket.close() |
| 339 | self.prepared = False |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 340 | |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 341 | def serve(self): |
Roger Meier | cfff856 | 2012-04-13 14:24:55 +0000 | [diff] [blame] | 342 | """Serve requests. |
| 343 | |
| 344 | Serve requests forever, or until stop() is called. |
| 345 | """ |
| 346 | self._stop = False |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 347 | self.prepare() |
Roger Meier | cfff856 | 2012-04-13 14:24:55 +0000 | [diff] [blame] | 348 | while not self._stop: |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 349 | self.handle() |