blob: 8b2f59fa32615931a4eb1ff6ba35f1b6634980db [file] [log] [blame]
Guido van Rossum0039d7b1999-01-12 20:19:27 +00001# -*- Mode: Python; tab-width: 4 -*-
Guido van Rossum0079b281999-09-14 20:17:50 +00002# Id: asynchat.py,v 2.23 1999/05/01 04:49:24 rushing Exp
Guido van Rossum0039d7b1999-01-12 20:19:27 +00003# Author: Sam Rushing <rushing@nightmare.com>
4
5# ======================================================================
6# Copyright 1996 by Sam Rushing
7#
8# All Rights Reserved
9#
10# 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.
18#
19# 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 Rossum4b8c6ea2000-02-04 15:39:30 +000028"""A class supporting chat-style (command/response) protocols.
29
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
51import string
52
Guido van Rossum0039d7b1999-01-12 20:19:27 +000053class async_chat (asyncore.dispatcher):
54 """This is an abstract class. You must derive from this class, and add
55 the two methods collect_incoming_data() and found_terminator()"""
56
57 # these are overridable defaults
58
59 ac_in_buffer_size = 4096
60 ac_out_buffer_size = 4096
61
62 def __init__ (self, conn=None):
63 self.ac_in_buffer = ''
64 self.ac_out_buffer = ''
65 self.producer_fifo = fifo()
66 asyncore.dispatcher.__init__ (self, conn)
67
68 def set_terminator (self, term):
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +000069 "Set the input delimiter. Can be a fixed string of any length, an integer, or None"
70 self.terminator = term
Guido van Rossum0039d7b1999-01-12 20:19:27 +000071
72 def get_terminator (self):
73 return self.terminator
74
75 # grab some more data from the socket,
76 # throw it to the collector method,
77 # check for the terminator,
78 # if found, transition to the next state.
79
80 def handle_read (self):
81
82 try:
83 data = self.recv (self.ac_in_buffer_size)
84 except socket.error, why:
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +000085 self.handle_error()
Guido van Rossum0039d7b1999-01-12 20:19:27 +000086 return
87
88 self.ac_in_buffer = self.ac_in_buffer + data
89
90 # Continue to search for self.terminator in self.ac_in_buffer,
91 # while calling self.collect_incoming_data. The while loop
92 # is necessary because we might read several data+terminator
93 # combos with a single recv(1024).
94
95 while self.ac_in_buffer:
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +000096 lb = len(self.ac_in_buffer)
Guido van Rossum0039d7b1999-01-12 20:19:27 +000097 terminator = self.get_terminator()
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +000098 if terminator is None:
99 # no terminator, collect it all
100 self.collect_incoming_data (self.ac_in_buffer)
101 self.ac_in_buffer = ''
102 elif type(terminator) == type(0):
103 # numeric terminator
104 n = terminator
105 if lb < n:
106 self.collect_incoming_data (self.ac_in_buffer)
107 self.ac_in_buffer = ''
108 self.terminator = self.terminator - lb
109 else:
110 self.collect_incoming_data (self.ac_in_buffer[:n])
111 self.ac_in_buffer = self.ac_in_buffer[n:]
112 self.terminator = 0
113 self.found_terminator()
114 else:
115 # 3 cases:
116 # 1) end of buffer matches terminator exactly:
117 # collect data, transition
118 # 2) end of buffer matches some prefix:
119 # collect data to the prefix
120 # 3) end of buffer does not match any prefix:
121 # collect data
122 terminator_len = len(terminator)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000123 index = string.find (self.ac_in_buffer, terminator)
124 if index != -1:
125 # we found the terminator
126 self.collect_incoming_data (self.ac_in_buffer[:index])
127 self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:]
128 # This does the Right Thing if the terminator is changed here.
129 self.found_terminator()
130 else:
131 # check for a prefix of the terminator
132 index = find_prefix_at_end (self.ac_in_buffer, terminator)
133 if index:
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000134 if index != lb:
135 # we found a prefix, collect up to the prefix
136 self.collect_incoming_data (self.ac_in_buffer[:-index])
137 self.ac_in_buffer = self.ac_in_buffer[-index:]
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000138 break
139 else:
140 # no prefix, collect it all
141 self.collect_incoming_data (self.ac_in_buffer)
142 self.ac_in_buffer = ''
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000143
144 def handle_write (self):
145 self.initiate_send ()
146
147 def handle_close (self):
148 self.close()
149
150 def push (self, data):
151 self.producer_fifo.push (simple_producer (data))
152 self.initiate_send()
153
154 def push_with_producer (self, producer):
155 self.producer_fifo.push (producer)
156 self.initiate_send()
157
158 def readable (self):
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000159 "predicate for inclusion in the readable for select()"
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000160 return (len(self.ac_in_buffer) <= self.ac_in_buffer_size)
161
162 def writable (self):
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000163 "predicate for inclusion in the writable for select()"
164 # return len(self.ac_out_buffer) or len(self.producer_fifo) or (not self.connected)
165 # this is about twice as fast, though not as clear.
166 return not (
167 (self.ac_out_buffer is '') and
168 self.producer_fifo.is_empty() and
169 self.connected
170 )
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000171
172 def close_when_done (self):
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000173 "automatically close this channel once the outgoing queue is empty"
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000174 self.producer_fifo.push (None)
175
176 # refill the outgoing buffer by calling the more() method
177 # of the first producer in the queue
178 def refill_buffer (self):
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000179 _string_type = type('')
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000180 while 1:
181 if len(self.producer_fifo):
182 p = self.producer_fifo.first()
183 # a 'None' in the producer fifo is a sentinel,
184 # telling us to close the channel.
185 if p is None:
186 if not self.ac_out_buffer:
187 self.producer_fifo.pop()
188 self.close()
189 return
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000190 elif type(p) is _string_type:
191 self.producer_fifo.pop()
192 self.ac_out_buffer = self.ac_out_buffer + p
193 return
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000194 data = p.more()
195 if data:
196 self.ac_out_buffer = self.ac_out_buffer + data
197 return
198 else:
199 self.producer_fifo.pop()
200 else:
201 return
202
203 def initiate_send (self):
204 obs = self.ac_out_buffer_size
205 # try to refill the buffer
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000206 if (len (self.ac_out_buffer) < obs):
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000207 self.refill_buffer()
208
209 if self.ac_out_buffer and self.connected:
210 # try to send the buffer
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000211 try:
212 num_sent = self.send (self.ac_out_buffer[:obs])
213 if num_sent:
214 self.ac_out_buffer = self.ac_out_buffer[num_sent:]
215
216 except socket.error, why:
217 self.handle_error()
218 return
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000219
220 def discard_buffers (self):
221 # Emergencies only!
222 self.ac_in_buffer = ''
223 self.ac_out_buffer == ''
224 while self.producer_fifo:
225 self.producer_fifo.pop()
226
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000227class simple_producer:
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000228
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000229 def __init__ (self, data, buffer_size=512):
230 self.data = data
231 self.buffer_size = buffer_size
232
233 def more (self):
234 if len (self.data) > self.buffer_size:
235 result = self.data[:self.buffer_size]
236 self.data = self.data[self.buffer_size:]
237 return result
238 else:
239 result = self.data
240 self.data = ''
241 return result
242
243class fifo:
244 def __init__ (self, list=None):
245 if not list:
246 self.list = []
247 else:
248 self.list = list
249
250 def __len__ (self):
251 return len(self.list)
252
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000253 def is_empty (self):
254 return self.list == []
255
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000256 def first (self):
257 return self.list[0]
258
259 def push (self, data):
260 self.list.append (data)
261
262 def pop (self):
263 if self.list:
264 result = self.list[0]
265 del self.list[0]
266 return (1, result)
267 else:
268 return (0, None)
269
270# Given 'haystack', see if any prefix of 'needle' is at its end. This
271# assumes an exact match has already been checked. Return the number of
272# characters matched.
273# for example:
274# f_p_a_e ("qwerty\r", "\r\n") => 1
275# f_p_a_e ("qwerty\r\n", "\r\n") => 2
276# f_p_a_e ("qwertydkjf", "\r\n") => 0
277
278# this could maybe be made faster with a computed regex?
279
280##def find_prefix_at_end (haystack, needle):
281## nl = len(needle)
282## result = 0
283## for i in range (1,nl):
284## if haystack[-(nl-i):] == needle[:(nl-i)]:
285## result = nl-i
286## break
287## return result
288
289# yes, this is about twice as fast, but still seems
290# to be neglible CPU. The previous could do about 290
291# searches/sec. the new one about 555/sec.
292
293import regex
294
295prefix_cache = {}
296
297def prefix_regex (needle):
298 if prefix_cache.has_key (needle):
299 return prefix_cache[needle]
300 else:
301 reg = needle[-1]
302 for i in range(1,len(needle)):
303 reg = '%c\(%s\)?' % (needle[-(i+1)], reg)
304 reg = regex.compile (reg+'$')
305 prefix_cache[needle] = reg, len(needle)
306 return reg, len(needle)
307
308def find_prefix_at_end (haystack, needle):
309 reg, length = prefix_regex (needle)
310 lh = len(haystack)
311 result = reg.search (haystack, max(0,lh-length))
312 if result >= 0:
313 return (lh - result)
314 else:
315 return 0