blob: a97de93f27053edc6a95610872a36552a205b50a [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
Guido van Rossum0039d7b1999-01-12 20:19:27 +000049import socket
50import asyncore
Raymond Hettingerac093c62004-02-07 03:19:10 +000051from collections import deque
Brett Cannon1eaf0742008-09-02 01:25:16 +000052from sys import py3kwarning
Brett Cannonc1b76e42008-08-09 23:06:16 +000053from test.test_support import catch_warning
Brett Cannon1eaf0742008-09-02 01:25:16 +000054from warnings import filterwarnings, catch_warnings
Guido van Rossum0039d7b1999-01-12 20:19:27 +000055
Guido van Rossum0039d7b1999-01-12 20:19:27 +000056class async_chat (asyncore.dispatcher):
Tim Peters146965a2001-01-14 18:09:23 +000057 """This is an abstract class. You must derive from this class, and add
58 the two methods collect_incoming_data() and found_terminator()"""
Guido van Rossum0039d7b1999-01-12 20:19:27 +000059
Tim Peters146965a2001-01-14 18:09:23 +000060 # these are overridable defaults
Guido van Rossum0039d7b1999-01-12 20:19:27 +000061
Tim Peters146965a2001-01-14 18:09:23 +000062 ac_in_buffer_size = 4096
63 ac_out_buffer_size = 4096
Guido van Rossum0039d7b1999-01-12 20:19:27 +000064
Josiah Carlsonff5f4202008-07-07 04:51:46 +000065 def __init__ (self, sock=None, map=None):
Josiah Carlson1a72d882008-06-10 05:00:08 +000066 # for string terminator matching
Tim Peters146965a2001-01-14 18:09:23 +000067 self.ac_in_buffer = ''
Josiah Carlson1a72d882008-06-10 05:00:08 +000068
69 # we use a list here rather than cStringIO for a few reasons...
70 # del lst[:] is faster than sio.truncate(0)
71 # lst = [] is faster than sio.truncate(0)
72 # cStringIO will be gaining unicode support in py3k, which
73 # will negatively affect the performance of bytes compared to
74 # a ''.join() equivalent
75 self.incoming = []
76
77 # we toss the use of the "simple producer" and replace it with
78 # a pure deque, which the original fifo was a wrapping of
79 self.producer_fifo = deque()
Josiah Carlsonff5f4202008-07-07 04:51:46 +000080 asyncore.dispatcher.__init__ (self, sock, map)
Guido van Rossum0039d7b1999-01-12 20:19:27 +000081
Andrew M. Kuchling7dd5f3c2002-03-08 18:27:11 +000082 def collect_incoming_data(self, data):
Josiah Carlson1a72d882008-06-10 05:00:08 +000083 raise NotImplementedError("must be implemented in subclass")
84
85 def _collect_incoming_data(self, data):
86 self.incoming.append(data)
87
88 def _get_data(self):
89 d = ''.join(self.incoming)
90 del self.incoming[:]
91 return d
Tim Peters863ac442002-04-16 01:38:40 +000092
Andrew M. Kuchling7dd5f3c2002-03-08 18:27:11 +000093 def found_terminator(self):
Josiah Carlson1a72d882008-06-10 05:00:08 +000094 raise NotImplementedError("must be implemented in subclass")
Tim Peters863ac442002-04-16 01:38:40 +000095
Tim Peters146965a2001-01-14 18:09:23 +000096 def set_terminator (self, term):
97 "Set the input delimiter. Can be a fixed string of any length, an integer, or None"
98 self.terminator = term
Guido van Rossum0039d7b1999-01-12 20:19:27 +000099
Tim Peters146965a2001-01-14 18:09:23 +0000100 def get_terminator (self):
101 return self.terminator
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000102
Tim Peters146965a2001-01-14 18:09:23 +0000103 # grab some more data from the socket,
104 # throw it to the collector method,
105 # check for the terminator,
106 # if found, transition to the next state.
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000107
Tim Peters146965a2001-01-14 18:09:23 +0000108 def handle_read (self):
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000109
Tim Peters146965a2001-01-14 18:09:23 +0000110 try:
111 data = self.recv (self.ac_in_buffer_size)
112 except socket.error, why:
113 self.handle_error()
114 return
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000115
Tim Peters146965a2001-01-14 18:09:23 +0000116 self.ac_in_buffer = self.ac_in_buffer + data
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000117
Tim Peters146965a2001-01-14 18:09:23 +0000118 # Continue to search for self.terminator in self.ac_in_buffer,
119 # while calling self.collect_incoming_data. The while loop
120 # is necessary because we might read several data+terminator
Josiah Carlson1a72d882008-06-10 05:00:08 +0000121 # combos with a single recv(4096).
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000122
Tim Peters146965a2001-01-14 18:09:23 +0000123 while self.ac_in_buffer:
124 lb = len(self.ac_in_buffer)
125 terminator = self.get_terminator()
Andrew M. Kuchlingca69f022005-06-09 14:59:45 +0000126 if not terminator:
Tim Peters146965a2001-01-14 18:09:23 +0000127 # no terminator, collect it all
128 self.collect_incoming_data (self.ac_in_buffer)
129 self.ac_in_buffer = ''
Andrew M. Kuchlingca69f022005-06-09 14:59:45 +0000130 elif isinstance(terminator, int) or isinstance(terminator, long):
Tim Peters146965a2001-01-14 18:09:23 +0000131 # numeric terminator
132 n = terminator
133 if lb < n:
134 self.collect_incoming_data (self.ac_in_buffer)
135 self.ac_in_buffer = ''
136 self.terminator = self.terminator - lb
137 else:
138 self.collect_incoming_data (self.ac_in_buffer[:n])
139 self.ac_in_buffer = self.ac_in_buffer[n:]
140 self.terminator = 0
141 self.found_terminator()
142 else:
143 # 3 cases:
144 # 1) end of buffer matches terminator exactly:
145 # collect data, transition
146 # 2) end of buffer matches some prefix:
147 # collect data to the prefix
148 # 3) end of buffer does not match any prefix:
149 # collect data
150 terminator_len = len(terminator)
Tim Petersb5d13922001-04-05 22:38:32 +0000151 index = self.ac_in_buffer.find(terminator)
Tim Peters146965a2001-01-14 18:09:23 +0000152 if index != -1:
153 # we found the terminator
154 if index > 0:
155 # don't bother reporting the empty string (source of subtle bugs)
156 self.collect_incoming_data (self.ac_in_buffer[:index])
157 self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:]
158 # This does the Right Thing if the terminator is changed here.
159 self.found_terminator()
160 else:
161 # check for a prefix of the terminator
162 index = find_prefix_at_end (self.ac_in_buffer, terminator)
163 if index:
164 if index != lb:
165 # we found a prefix, collect up to the prefix
166 self.collect_incoming_data (self.ac_in_buffer[:-index])
167 self.ac_in_buffer = self.ac_in_buffer[-index:]
168 break
169 else:
170 # no prefix, collect it all
171 self.collect_incoming_data (self.ac_in_buffer)
172 self.ac_in_buffer = ''
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000173
Tim Peters146965a2001-01-14 18:09:23 +0000174 def handle_write (self):
Josiah Carlson1a72d882008-06-10 05:00:08 +0000175 self.initiate_send()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000176
Tim Peters146965a2001-01-14 18:09:23 +0000177 def handle_close (self):
178 self.close()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000179
Tim Peters146965a2001-01-14 18:09:23 +0000180 def push (self, data):
Josiah Carlson1a72d882008-06-10 05:00:08 +0000181 sabs = self.ac_out_buffer_size
182 if len(data) > sabs:
183 for i in xrange(0, len(data), sabs):
184 self.producer_fifo.append(data[i:i+sabs])
185 else:
186 self.producer_fifo.append(data)
Tim Peters146965a2001-01-14 18:09:23 +0000187 self.initiate_send()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000188
Tim Peters146965a2001-01-14 18:09:23 +0000189 def push_with_producer (self, producer):
Josiah Carlson1a72d882008-06-10 05:00:08 +0000190 self.producer_fifo.append(producer)
Tim Peters146965a2001-01-14 18:09:23 +0000191 self.initiate_send()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000192
Tim Peters146965a2001-01-14 18:09:23 +0000193 def readable (self):
194 "predicate for inclusion in the readable for select()"
Josiah Carlson1a72d882008-06-10 05:00:08 +0000195 # cannot use the old predicate, it violates the claim of the
196 # set_terminator method.
197
198 # return (len(self.ac_in_buffer) <= self.ac_in_buffer_size)
199 return 1
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000200
Tim Peters146965a2001-01-14 18:09:23 +0000201 def writable (self):
202 "predicate for inclusion in the writable for select()"
Josiah Carlson1a72d882008-06-10 05:00:08 +0000203 return self.producer_fifo or (not self.connected)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000204
Tim Peters146965a2001-01-14 18:09:23 +0000205 def close_when_done (self):
206 "automatically close this channel once the outgoing queue is empty"
Josiah Carlson1a72d882008-06-10 05:00:08 +0000207 self.producer_fifo.append(None)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000208
Josiah Carlson1a72d882008-06-10 05:00:08 +0000209 def initiate_send(self):
210 while self.producer_fifo and self.connected:
211 first = self.producer_fifo[0]
212 # handle empty string/buffer or None entry
213 if not first:
214 del self.producer_fifo[0]
215 if first is None:
216 self.handle_close()
Tim Peters146965a2001-01-14 18:09:23 +0000217 return
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000218
Josiah Carlson1a72d882008-06-10 05:00:08 +0000219 # handle classic producer behavior
220 obs = self.ac_out_buffer_size
Tim Peters146965a2001-01-14 18:09:23 +0000221 try:
Brett Cannon1eaf0742008-09-02 01:25:16 +0000222 with catch_warnings():
223 if py3kwarning:
224 filterwarnings("ignore", ".*buffer", DeprecationWarning)
Brett Cannonc1b76e42008-08-09 23:06:16 +0000225 data = buffer(first, 0, obs)
Josiah Carlson1a72d882008-06-10 05:00:08 +0000226 except TypeError:
227 data = first.more()
228 if data:
229 self.producer_fifo.appendleft(data)
230 else:
231 del self.producer_fifo[0]
232 continue
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000233
Josiah Carlson1a72d882008-06-10 05:00:08 +0000234 # send the data
235 try:
236 num_sent = self.send(data)
237 except socket.error:
Tim Peters146965a2001-01-14 18:09:23 +0000238 self.handle_error()
239 return
240
Josiah Carlson1a72d882008-06-10 05:00:08 +0000241 if num_sent:
242 if num_sent < len(data) or obs < len(first):
243 self.producer_fifo[0] = first[num_sent:]
244 else:
245 del self.producer_fifo[0]
246 # we tried to send some actual data
247 return
248
Tim Peters146965a2001-01-14 18:09:23 +0000249 def discard_buffers (self):
250 # Emergencies only!
251 self.ac_in_buffer = ''
Josiah Carlson1a72d882008-06-10 05:00:08 +0000252 del self.incoming[:]
253 self.producer_fifo.clear()
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +0000254
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000255class simple_producer:
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000256
Tim Peters146965a2001-01-14 18:09:23 +0000257 def __init__ (self, data, buffer_size=512):
258 self.data = data
259 self.buffer_size = buffer_size
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000260
Tim Peters146965a2001-01-14 18:09:23 +0000261 def more (self):
262 if len (self.data) > self.buffer_size:
263 result = self.data[:self.buffer_size]
264 self.data = self.data[self.buffer_size:]
265 return result
266 else:
267 result = self.data
268 self.data = ''
269 return result
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000270
271class fifo:
Tim Peters146965a2001-01-14 18:09:23 +0000272 def __init__ (self, list=None):
273 if not list:
Raymond Hettingerac093c62004-02-07 03:19:10 +0000274 self.list = deque()
Tim Peters146965a2001-01-14 18:09:23 +0000275 else:
Raymond Hettingerac093c62004-02-07 03:19:10 +0000276 self.list = deque(list)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000277
Tim Peters146965a2001-01-14 18:09:23 +0000278 def __len__ (self):
279 return len(self.list)
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000280
Tim Peters146965a2001-01-14 18:09:23 +0000281 def is_empty (self):
Armin Rigob562bc62004-09-27 17:49:00 +0000282 return not self.list
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000283
Tim Peters146965a2001-01-14 18:09:23 +0000284 def first (self):
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000285 return self.list[0]
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000286
Tim Peters146965a2001-01-14 18:09:23 +0000287 def push (self, data):
Raymond Hettingerac093c62004-02-07 03:19:10 +0000288 self.list.append(data)
Tim Peters146965a2001-01-14 18:09:23 +0000289
290 def pop (self):
291 if self.list:
Raymond Hettingerac093c62004-02-07 03:19:10 +0000292 return (1, self.list.popleft())
Tim Peters146965a2001-01-14 18:09:23 +0000293 else:
294 return (0, None)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000295
296# Given 'haystack', see if any prefix of 'needle' is at its end. This
297# assumes an exact match has already been checked. Return the number of
298# characters matched.
299# for example:
300# f_p_a_e ("qwerty\r", "\r\n") => 1
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000301# f_p_a_e ("qwertydkjf", "\r\n") => 0
Andrew M. Kuchlingc63a3962002-03-20 02:22:58 +0000302# f_p_a_e ("qwerty\r\n", "\r\n") => <undefined>
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000303
304# this could maybe be made faster with a computed regex?
Andrew M. Kuchlingd305f512001-01-24 21:10:55 +0000305# [answer: no; circa Python-2.0, Jan 2001]
Andrew M. Kuchlingc63a3962002-03-20 02:22:58 +0000306# new python: 28961/s
307# old python: 18307/s
Andrew M. Kuchlingd305f512001-01-24 21:10:55 +0000308# re: 12820/s
309# regex: 14035/s
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000310
311def find_prefix_at_end (haystack, needle):
Tim Peters863ac442002-04-16 01:38:40 +0000312 l = len(needle) - 1
313 while l and not haystack.endswith(needle[:l]):
314 l -= 1
315 return l