blob: 0e2457f8ece6192367c7c944aba069365cbabedc [file] [log] [blame]
Guido van Rossum0039d7b1999-01-12 20:19:27 +00001# -*- Mode: Python; tab-width: 4 -*-
Tim Peters658cba62001-02-09 20:06:00 +00002# Id: asynchat.py,v 2.26 2000/09/07 22:29:26 rushing Exp
Tim Peters146965a2001-01-14 18:09:23 +00003# Author: Sam Rushing <rushing@nightmare.com>
Guido van Rossum0039d7b1999-01-12 20:19:27 +00004
5# ======================================================================
6# Copyright 1996 by Sam Rushing
Tim Peters146965a2001-01-14 18:09:23 +00007#
Guido van Rossum0039d7b1999-01-12 20:19:27 +00008# All Rights Reserved
Tim Peters146965a2001-01-14 18:09:23 +00009#
Guido van Rossum0039d7b1999-01-12 20:19:27 +000010# Permission to use, copy, modify, and distribute this software and
11# its documentation for any purpose and without fee is hereby
12# granted, provided that the above copyright notice appear in all
13# copies and that both that copyright notice and this permission
14# notice appear in supporting documentation, and that the name of Sam
15# Rushing not be used in advertising or publicity pertaining to
16# distribution of the software without specific, written prior
17# permission.
Tim Peters146965a2001-01-14 18:09:23 +000018#
Guido van Rossum0039d7b1999-01-12 20:19:27 +000019# SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
20# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
21# NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR
22# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
23# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
24# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
25# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26# ======================================================================
27
Guido van Rossume4a1b6d2001-04-06 15:30:33 +000028r"""A class supporting chat-style (command/response) protocols.
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +000029
30This class adds support for 'chat' style protocols - where one side
31sends a 'command', and the other sends a response (examples would be
32the common internet protocols - smtp, nntp, ftp, etc..).
33
34The handle_read() method looks at the input stream for the current
35'terminator' (usually '\r\n' for single-line responses, '\r\n.\r\n'
36for multi-line output), calling self.found_terminator() on its
37receipt.
38
39for example:
40Say you build an async nntp client using this class. At the start
41of the connection, you'll have self.terminator set to '\r\n', in
42order to process the single-line greeting. Just before issuing a
43'LIST' command you'll set it to '\r\n.\r\n'. The output of the LIST
44command will be accumulated (using your own 'collect_incoming_data'
45method) up to the terminator, and then control will be returned to
46you - by calling your self.found_terminator() method.
47"""
48
Thomas Wouters74e68c72007-08-31 00:20:14 +000049import sys
Guido van Rossum0039d7b1999-01-12 20:19:27 +000050import socket
51import asyncore
Raymond Hettingerac093c62004-02-07 03:19:10 +000052from collections import deque
Guido van Rossum0039d7b1999-01-12 20:19:27 +000053
Guido van Rossum0039d7b1999-01-12 20:19:27 +000054class async_chat (asyncore.dispatcher):
Tim Peters146965a2001-01-14 18:09:23 +000055 """This is an abstract class. You must derive from this class, and add
56 the two methods collect_incoming_data() and found_terminator()"""
Guido van Rossum0039d7b1999-01-12 20:19:27 +000057
Tim Peters146965a2001-01-14 18:09:23 +000058 # these are overridable defaults
Guido van Rossum0039d7b1999-01-12 20:19:27 +000059
Tim Peters146965a2001-01-14 18:09:23 +000060 ac_in_buffer_size = 4096
61 ac_out_buffer_size = 4096
Guido van Rossum0039d7b1999-01-12 20:19:27 +000062
Tim Peters146965a2001-01-14 18:09:23 +000063 def __init__ (self, conn=None):
Guido van Rossum076da092007-07-12 07:58:54 +000064 self.ac_in_buffer = b''
65 self.ac_out_buffer = b''
Tim Peters146965a2001-01-14 18:09:23 +000066 self.producer_fifo = fifo()
67 asyncore.dispatcher.__init__ (self, conn)
Guido van Rossum0039d7b1999-01-12 20:19:27 +000068
Andrew M. Kuchling7dd5f3c2002-03-08 18:27:11 +000069 def collect_incoming_data(self, data):
Collin Winterce36ad82007-08-30 01:19:48 +000070 raise NotImplementedError("must be implemented in subclass")
Tim Peters863ac442002-04-16 01:38:40 +000071
Andrew M. Kuchling7dd5f3c2002-03-08 18:27:11 +000072 def found_terminator(self):
Collin Winterce36ad82007-08-30 01:19:48 +000073 raise NotImplementedError("must be implemented in subclass")
Tim Peters863ac442002-04-16 01:38:40 +000074
Tim Peters146965a2001-01-14 18:09:23 +000075 def set_terminator (self, term):
76 "Set the input delimiter. Can be a fixed string of any length, an integer, or None"
77 self.terminator = term
Guido van Rossum0039d7b1999-01-12 20:19:27 +000078
Tim Peters146965a2001-01-14 18:09:23 +000079 def get_terminator (self):
80 return self.terminator
Guido van Rossum0039d7b1999-01-12 20:19:27 +000081
Tim Peters146965a2001-01-14 18:09:23 +000082 # grab some more data from the socket,
83 # throw it to the collector method,
84 # check for the terminator,
85 # if found, transition to the next state.
Guido van Rossum0039d7b1999-01-12 20:19:27 +000086
Tim Peters146965a2001-01-14 18:09:23 +000087 def handle_read (self):
Guido van Rossum0039d7b1999-01-12 20:19:27 +000088
Tim Peters146965a2001-01-14 18:09:23 +000089 try:
90 data = self.recv (self.ac_in_buffer_size)
Guido van Rossumb940e112007-01-10 16:19:56 +000091 except socket.error as why:
Tim Peters146965a2001-01-14 18:09:23 +000092 self.handle_error()
93 return
Guido van Rossum0039d7b1999-01-12 20:19:27 +000094
Thomas Wouters74e68c72007-08-31 00:20:14 +000095 if isinstance(data, str):
96 data = data.encode('ascii')
Guido van Rossum076da092007-07-12 07:58:54 +000097 self.ac_in_buffer = self.ac_in_buffer + bytes(data)
Guido van Rossum0039d7b1999-01-12 20:19:27 +000098
Tim Peters146965a2001-01-14 18:09:23 +000099 # Continue to search for self.terminator in self.ac_in_buffer,
100 # while calling self.collect_incoming_data. The while loop
101 # is necessary because we might read several data+terminator
102 # combos with a single recv(1024).
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000103
Tim Peters146965a2001-01-14 18:09:23 +0000104 while self.ac_in_buffer:
105 lb = len(self.ac_in_buffer)
106 terminator = self.get_terminator()
Andrew M. Kuchlingca69f022005-06-09 14:59:45 +0000107 if not terminator:
Tim Peters146965a2001-01-14 18:09:23 +0000108 # no terminator, collect it all
109 self.collect_incoming_data (self.ac_in_buffer)
Guido van Rossum806c2462007-08-06 23:33:07 +0000110 self.ac_in_buffer = b''
Guido van Rossume2a383d2007-01-15 16:59:06 +0000111 elif isinstance(terminator, int) or isinstance(terminator, int):
Tim Peters146965a2001-01-14 18:09:23 +0000112 # numeric terminator
113 n = terminator
114 if lb < n:
115 self.collect_incoming_data (self.ac_in_buffer)
Guido van Rossum076da092007-07-12 07:58:54 +0000116 self.ac_in_buffer = b''
Tim Peters146965a2001-01-14 18:09:23 +0000117 self.terminator = self.terminator - lb
118 else:
119 self.collect_incoming_data (self.ac_in_buffer[:n])
120 self.ac_in_buffer = self.ac_in_buffer[n:]
121 self.terminator = 0
122 self.found_terminator()
123 else:
124 # 3 cases:
125 # 1) end of buffer matches terminator exactly:
126 # collect data, transition
127 # 2) end of buffer matches some prefix:
128 # collect data to the prefix
129 # 3) end of buffer does not match any prefix:
130 # collect data
131 terminator_len = len(terminator)
Thomas Wouters74e68c72007-08-31 00:20:14 +0000132 if isinstance(terminator, str):
133 terminator = terminator.encode('ascii')
Tim Petersb5d13922001-04-05 22:38:32 +0000134 index = self.ac_in_buffer.find(terminator)
Tim Peters146965a2001-01-14 18:09:23 +0000135 if index != -1:
136 # we found the terminator
137 if index > 0:
138 # don't bother reporting the empty string (source of subtle bugs)
139 self.collect_incoming_data (self.ac_in_buffer[:index])
140 self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:]
141 # This does the Right Thing if the terminator is changed here.
142 self.found_terminator()
143 else:
144 # check for a prefix of the terminator
145 index = find_prefix_at_end (self.ac_in_buffer, terminator)
146 if index:
147 if index != lb:
148 # we found a prefix, collect up to the prefix
149 self.collect_incoming_data (self.ac_in_buffer[:-index])
150 self.ac_in_buffer = self.ac_in_buffer[-index:]
151 break
152 else:
153 # no prefix, collect it all
154 self.collect_incoming_data (self.ac_in_buffer)
Guido van Rossum076da092007-07-12 07:58:54 +0000155 self.ac_in_buffer = b''
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000156
Tim Peters146965a2001-01-14 18:09:23 +0000157 def handle_write (self):
158 self.initiate_send ()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000159
Tim Peters146965a2001-01-14 18:09:23 +0000160 def handle_close (self):
161 self.close()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000162
Tim Peters146965a2001-01-14 18:09:23 +0000163 def push (self, data):
164 self.producer_fifo.push (simple_producer (data))
165 self.initiate_send()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000166
Tim Peters146965a2001-01-14 18:09:23 +0000167 def push_with_producer (self, producer):
168 self.producer_fifo.push (producer)
169 self.initiate_send()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000170
Tim Peters146965a2001-01-14 18:09:23 +0000171 def readable (self):
172 "predicate for inclusion in the readable for select()"
173 return (len(self.ac_in_buffer) <= self.ac_in_buffer_size)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000174
Tim Peters146965a2001-01-14 18:09:23 +0000175 def writable (self):
176 "predicate for inclusion in the writable for select()"
177 # return len(self.ac_out_buffer) or len(self.producer_fifo) or (not self.connected)
178 # this is about twice as fast, though not as clear.
179 return not (
Guido van Rossum076da092007-07-12 07:58:54 +0000180 (self.ac_out_buffer == b'') and
Tim Peters146965a2001-01-14 18:09:23 +0000181 self.producer_fifo.is_empty() and
182 self.connected
183 )
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000184
Tim Peters146965a2001-01-14 18:09:23 +0000185 def close_when_done (self):
186 "automatically close this channel once the outgoing queue is empty"
187 self.producer_fifo.push (None)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000188
Tim Peters146965a2001-01-14 18:09:23 +0000189 # refill the outgoing buffer by calling the more() method
190 # of the first producer in the queue
191 def refill_buffer (self):
Tim Peters146965a2001-01-14 18:09:23 +0000192 while 1:
193 if len(self.producer_fifo):
194 p = self.producer_fifo.first()
195 # a 'None' in the producer fifo is a sentinel,
196 # telling us to close the channel.
197 if p is None:
198 if not self.ac_out_buffer:
199 self.producer_fifo.pop()
200 self.close()
201 return
Guido van Rossum076da092007-07-12 07:58:54 +0000202 elif isinstance(p, str) or isinstance(p, bytes):
Thomas Wouters74e68c72007-08-31 00:20:14 +0000203 if isinstance(p, str):
204 p = p.encode('ascii')
Tim Peters146965a2001-01-14 18:09:23 +0000205 self.producer_fifo.pop()
Thomas Wouters74e68c72007-08-31 00:20:14 +0000206 self.ac_out_buffer = self.ac_out_buffer + p
Tim Peters146965a2001-01-14 18:09:23 +0000207 return
208 data = p.more()
209 if data:
Thomas Wouters74e68c72007-08-31 00:20:14 +0000210 if isinstance(data, str):
211 data = data.encode('ascii')
Guido van Rossum076da092007-07-12 07:58:54 +0000212 self.ac_out_buffer = self.ac_out_buffer + bytes(data)
Tim Peters146965a2001-01-14 18:09:23 +0000213 return
214 else:
215 self.producer_fifo.pop()
216 else:
217 return
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000218
Tim Peters146965a2001-01-14 18:09:23 +0000219 def initiate_send (self):
220 obs = self.ac_out_buffer_size
221 # try to refill the buffer
222 if (len (self.ac_out_buffer) < obs):
223 self.refill_buffer()
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000224
Tim Peters146965a2001-01-14 18:09:23 +0000225 if self.ac_out_buffer and self.connected:
226 # try to send the buffer
227 try:
228 num_sent = self.send (self.ac_out_buffer[:obs])
229 if num_sent:
230 self.ac_out_buffer = self.ac_out_buffer[num_sent:]
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000231
Guido van Rossumb940e112007-01-10 16:19:56 +0000232 except socket.error as why:
Tim Peters146965a2001-01-14 18:09:23 +0000233 self.handle_error()
234 return
235
236 def discard_buffers (self):
237 # Emergencies only!
Guido van Rossum076da092007-07-12 07:58:54 +0000238 self.ac_in_buffer = b''
239 self.ac_out_buffer = b''
Tim Peters146965a2001-01-14 18:09:23 +0000240 while self.producer_fifo:
241 self.producer_fifo.pop()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000242
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +0000243
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000244class simple_producer:
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000245
Tim Peters146965a2001-01-14 18:09:23 +0000246 def __init__ (self, data, buffer_size=512):
247 self.data = data
248 self.buffer_size = buffer_size
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000249
Tim Peters146965a2001-01-14 18:09:23 +0000250 def more (self):
251 if len (self.data) > self.buffer_size:
252 result = self.data[:self.buffer_size]
253 self.data = self.data[self.buffer_size:]
254 return result
255 else:
256 result = self.data
Guido van Rossum076da092007-07-12 07:58:54 +0000257 self.data = b''
Tim Peters146965a2001-01-14 18:09:23 +0000258 return result
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000259
260class fifo:
Tim Peters146965a2001-01-14 18:09:23 +0000261 def __init__ (self, list=None):
262 if not list:
Raymond Hettingerac093c62004-02-07 03:19:10 +0000263 self.list = deque()
Tim Peters146965a2001-01-14 18:09:23 +0000264 else:
Raymond Hettingerac093c62004-02-07 03:19:10 +0000265 self.list = deque(list)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000266
Tim Peters146965a2001-01-14 18:09:23 +0000267 def __len__ (self):
268 return len(self.list)
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000269
Tim Peters146965a2001-01-14 18:09:23 +0000270 def is_empty (self):
Armin Rigob562bc62004-09-27 17:49:00 +0000271 return not self.list
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000272
Tim Peters146965a2001-01-14 18:09:23 +0000273 def first (self):
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000274 return self.list[0]
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000275
Tim Peters146965a2001-01-14 18:09:23 +0000276 def push (self, data):
Raymond Hettingerac093c62004-02-07 03:19:10 +0000277 self.list.append(data)
Tim Peters146965a2001-01-14 18:09:23 +0000278
279 def pop (self):
280 if self.list:
Raymond Hettingerac093c62004-02-07 03:19:10 +0000281 return (1, self.list.popleft())
Tim Peters146965a2001-01-14 18:09:23 +0000282 else:
283 return (0, None)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000284
285# Given 'haystack', see if any prefix of 'needle' is at its end. This
286# assumes an exact match has already been checked. Return the number of
287# characters matched.
288# for example:
289# f_p_a_e ("qwerty\r", "\r\n") => 1
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000290# f_p_a_e ("qwertydkjf", "\r\n") => 0
Andrew M. Kuchlingc63a3962002-03-20 02:22:58 +0000291# f_p_a_e ("qwerty\r\n", "\r\n") => <undefined>
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000292
293# this could maybe be made faster with a computed regex?
Andrew M. Kuchlingd305f512001-01-24 21:10:55 +0000294# [answer: no; circa Python-2.0, Jan 2001]
Andrew M. Kuchlingc63a3962002-03-20 02:22:58 +0000295# new python: 28961/s
296# old python: 18307/s
Andrew M. Kuchlingd305f512001-01-24 21:10:55 +0000297# re: 12820/s
298# regex: 14035/s
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000299
300def find_prefix_at_end (haystack, needle):
Tim Peters863ac442002-04-16 01:38:40 +0000301 l = len(needle) - 1
302 while l and not haystack.endswith(needle[:l]):
303 l -= 1
304 return l