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