asynchat_25.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. # -*- Mode: Python; tab-width: 4 -*-
  2. # Id: asynchat.py,v 2.26 2000/09/07 22:29:26 rushing Exp
  3. # Author: Sam Rushing <rushing@nightmare.com>
  4. # ======================================================================
  5. # Copyright 1996 by Sam Rushing
  6. #
  7. # All Rights Reserved
  8. #
  9. # Permission to use, copy, modify, and distribute this software and
  10. # its documentation for any purpose and without fee is hereby
  11. # granted, provided that the above copyright notice appear in all
  12. # copies and that both that copyright notice and this permission
  13. # notice appear in supporting documentation, and that the name of Sam
  14. # Rushing not be used in advertising or publicity pertaining to
  15. # distribution of the software without specific, written prior
  16. # permission.
  17. #
  18. # SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
  20. # NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  22. # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  23. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  24. # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  25. # ======================================================================
  26. r"""A class supporting chat-style (command/response) protocols.
  27. This class adds support for 'chat' style protocols - where one side
  28. sends a 'command', and the other sends a response (examples would be
  29. the common internet protocols - smtp, nntp, ftp, etc..).
  30. The handle_read() method looks at the input stream for the current
  31. 'terminator' (usually '\r\n' for single-line responses, '\r\n.\r\n'
  32. for multi-line output), calling self.found_terminator() on its
  33. receipt.
  34. for example:
  35. Say you build an async nntp client using this class. At the start
  36. of the connection, you'll have self.terminator set to '\r\n', in
  37. order to process the single-line greeting. Just before issuing a
  38. 'LIST' command you'll set it to '\r\n.\r\n'. The output of the LIST
  39. command will be accumulated (using your own 'collect_incoming_data'
  40. method) up to the terminator, and then control will be returned to
  41. you - by calling your self.found_terminator() method.
  42. """
  43. import supervisor.medusa.text_socket as socket
  44. from supervisor.medusa import asyncore_25 as asyncore
  45. from supervisor.compat import long
  46. class async_chat (asyncore.dispatcher):
  47. """This is an abstract class. You must derive from this class, and add
  48. the two methods collect_incoming_data() and found_terminator()"""
  49. # these are overridable defaults
  50. ac_in_buffer_size = 4096
  51. ac_out_buffer_size = 4096
  52. def __init__ (self, conn=None, map=None):
  53. self.ac_in_buffer = ''
  54. self.ac_out_buffer = ''
  55. self.producer_fifo = fifo()
  56. asyncore.dispatcher.__init__ (self, conn, map)
  57. def collect_incoming_data(self, data):
  58. raise NotImplementedError("must be implemented in subclass")
  59. def found_terminator(self):
  60. raise NotImplementedError("must be implemented in subclass")
  61. def set_terminator (self, term):
  62. """Set the input delimiter. Can be a fixed string of any length, an integer, or None"""
  63. self.terminator = term
  64. def get_terminator (self):
  65. return self.terminator
  66. # grab some more data from the socket,
  67. # throw it to the collector method,
  68. # check for the terminator,
  69. # if found, transition to the next state.
  70. def handle_read (self):
  71. try:
  72. data = self.recv (self.ac_in_buffer_size)
  73. except socket.error:
  74. self.handle_error()
  75. return
  76. self.ac_in_buffer += data
  77. # Continue to search for self.terminator in self.ac_in_buffer,
  78. # while calling self.collect_incoming_data. The while loop
  79. # is necessary because we might read several data+terminator
  80. # combos with a single recv(1024).
  81. while self.ac_in_buffer:
  82. lb = len(self.ac_in_buffer)
  83. terminator = self.get_terminator()
  84. if not terminator:
  85. # no terminator, collect it all
  86. self.collect_incoming_data (self.ac_in_buffer)
  87. self.ac_in_buffer = ''
  88. elif isinstance(terminator, int) or isinstance(terminator, long):
  89. # numeric terminator
  90. n = terminator
  91. if lb < n:
  92. self.collect_incoming_data (self.ac_in_buffer)
  93. self.ac_in_buffer = ''
  94. self.terminator -= lb
  95. else:
  96. self.collect_incoming_data (self.ac_in_buffer[:n])
  97. self.ac_in_buffer = self.ac_in_buffer[n:]
  98. self.terminator = 0
  99. self.found_terminator()
  100. else:
  101. # 3 cases:
  102. # 1) end of buffer matches terminator exactly:
  103. # collect data, transition
  104. # 2) end of buffer matches some prefix:
  105. # collect data to the prefix
  106. # 3) end of buffer does not match any prefix:
  107. # collect data
  108. terminator_len = len(terminator)
  109. index = self.ac_in_buffer.find(terminator)
  110. if index != -1:
  111. # we found the terminator
  112. if index > 0:
  113. # don't bother reporting the empty string (source of subtle bugs)
  114. self.collect_incoming_data (self.ac_in_buffer[:index])
  115. self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:]
  116. # This does the Right Thing if the terminator is changed here.
  117. self.found_terminator()
  118. else:
  119. # check for a prefix of the terminator
  120. index = find_prefix_at_end (self.ac_in_buffer, terminator)
  121. if index:
  122. if index != lb:
  123. # we found a prefix, collect up to the prefix
  124. self.collect_incoming_data (self.ac_in_buffer[:-index])
  125. self.ac_in_buffer = self.ac_in_buffer[-index:]
  126. break
  127. else:
  128. # no prefix, collect it all
  129. self.collect_incoming_data (self.ac_in_buffer)
  130. self.ac_in_buffer = ''
  131. def handle_write (self):
  132. self.initiate_send ()
  133. def handle_close (self):
  134. self.close()
  135. def push (self, data):
  136. self.producer_fifo.push (simple_producer (data))
  137. self.initiate_send()
  138. def push_with_producer (self, producer):
  139. self.producer_fifo.push (producer)
  140. self.initiate_send()
  141. def readable (self):
  142. """predicate for inclusion in the readable for select()"""
  143. return len(self.ac_in_buffer) <= self.ac_in_buffer_size
  144. def writable (self):
  145. """predicate for inclusion in the writable for select()"""
  146. # return len(self.ac_out_buffer) or len(self.producer_fifo) or (not self.connected)
  147. # this is about twice as fast, though not as clear.
  148. return not (
  149. (self.ac_out_buffer == '') and
  150. self.producer_fifo.is_empty() and
  151. self.connected
  152. )
  153. def close_when_done (self):
  154. """automatically close this channel once the outgoing queue is empty"""
  155. self.producer_fifo.push (None)
  156. # refill the outgoing buffer by calling the more() method
  157. # of the first producer in the queue
  158. def refill_buffer (self):
  159. while 1:
  160. if len(self.producer_fifo):
  161. p = self.producer_fifo.first()
  162. # a 'None' in the producer fifo is a sentinel,
  163. # telling us to close the channel.
  164. if p is None:
  165. if not self.ac_out_buffer:
  166. self.producer_fifo.pop()
  167. self.close()
  168. return
  169. elif isinstance(p, str):
  170. self.producer_fifo.pop()
  171. self.ac_out_buffer += p
  172. return
  173. data = p.more()
  174. if data:
  175. self.ac_out_buffer = self.ac_out_buffer + data
  176. return
  177. else:
  178. self.producer_fifo.pop()
  179. else:
  180. return
  181. def initiate_send (self):
  182. obs = self.ac_out_buffer_size
  183. # try to refill the buffer
  184. if len (self.ac_out_buffer) < obs:
  185. self.refill_buffer()
  186. if self.ac_out_buffer and self.connected:
  187. # try to send the buffer
  188. try:
  189. num_sent = self.send (self.ac_out_buffer[:obs])
  190. if num_sent:
  191. self.ac_out_buffer = self.ac_out_buffer[num_sent:]
  192. except socket.error:
  193. self.handle_error()
  194. return
  195. def discard_buffers (self):
  196. # Emergencies only!
  197. self.ac_in_buffer = ''
  198. self.ac_out_buffer = ''
  199. while self.producer_fifo:
  200. self.producer_fifo.pop()
  201. class simple_producer:
  202. def __init__ (self, data, buffer_size=512):
  203. self.data = data
  204. self.buffer_size = buffer_size
  205. def more (self):
  206. if len (self.data) > self.buffer_size:
  207. result = self.data[:self.buffer_size]
  208. self.data = self.data[self.buffer_size:]
  209. return result
  210. else:
  211. result = self.data
  212. self.data = ''
  213. return result
  214. class fifo:
  215. def __init__ (self, list=None):
  216. if not list:
  217. self.list = []
  218. else:
  219. self.list = list
  220. def __len__ (self):
  221. return len(self.list)
  222. def is_empty (self):
  223. return self.list == []
  224. def first (self):
  225. return self.list[0]
  226. def push (self, data):
  227. self.list.append(data)
  228. def pop (self):
  229. if self.list:
  230. return 1, self.list.pop(0)
  231. else:
  232. return 0, None
  233. # Given 'haystack', see if any prefix of 'needle' is at its end. This
  234. # assumes an exact match has already been checked. Return the number of
  235. # characters matched.
  236. # for example:
  237. # f_p_a_e ("qwerty\r", "\r\n") => 1
  238. # f_p_a_e ("qwertydkjf", "\r\n") => 0
  239. # f_p_a_e ("qwerty\r\n", "\r\n") => <undefined>
  240. # this could maybe be made faster with a computed regex?
  241. # [answer: no; circa Python-2.0, Jan 2001]
  242. # new python: 28961/s
  243. # old python: 18307/s
  244. # re: 12820/s
  245. # regex: 14035/s
  246. def find_prefix_at_end (haystack, needle):
  247. l = len(needle) - 1
  248. while l and not haystack.endswith(needle[:l]):
  249. l -= 1
  250. return l