# -*- Mode: Python; tab-width: 4 -*-
# 	$Id: $
#	Author: Sam Rushing <rushing@nightmare.com>
#
# modified by pascal TROUVIN
# 14/04/97: DEBUG variable access
# 07/06/97: add sock_dgram support (UDP)

# found via http://groups.google.com/groups?selm=01IJZ5MISU7M0016PY%40INTEGD.INTEGRALIS.CO.UK&oe=UTF-8&output=gplain
# From: pascal.trouvin@integralis.co.uk
# Subject: UDP support in asyncore.py
# Date: 1997/06/12
# Message-ID: <01IJZ5MISU7M0016PY@INTEGD.INTEGRALIS.CO.UK>
# Newsgroups: comp.lang.python

import select
import socket
import sys

import os
if os.name == 'nt':
	EWOULDBLOCK	= 10035
	EINPROGRESS	= 10036
	EALREADY	= 10037
else:
	from errno import EALREADY, EINPROGRESS, EWOULDBLOCK

socket_map = {}

DEBUG=0

def poll (timeout=0.0):
	if socket_map:
		sockets = socket_map.keys()
		r = filter (lambda x: x.readable(), sockets)
		w = filter (lambda x: x.writable(), sockets)
		e = sockets[:]

		(r,w,e) = select.select (r,w,e, timeout)
		for x in r:
			try:
				x.handle_read_event()
			except:
				x.handle_error (sys.exc_type, sys.exc_value, sys.exc_traceback)
		for x in w:
			try:
				x.handle_write_event()
			except:
				x.handle_error (sys.exc_type, sys.exc_value, sys.exc_traceback)
		for x in e:
			try:
				x.handle_expt_event()
			except:
				x.handle_error (sys.exc_type, sys.exc_value, sys.exc_traceback)

def loop (timeout=30.0):
	while socket_map:
		poll (timeout)

class dispatcher:
	debug = 0
	connected = 0
	accepting = 0
	closing = 0
	write_blocked = 1
	_fileno = None

	def __init__ (self, sock=None):
		if sock:
			self.set_socket (sock)
			# I think it should inherit this anyway
			self.socket.setblocking (0)
			if self.sock_tcp:
				self.connected = 1

	def add_channel (self):
		self.log ('adding channel %s' % self)
		socket_map [self] = self
		if not self.sock_tcp: # sock_dgram support
			self.accepting = 1

	# we cache the original fileno, because after closing
	# a socket, s.fileno() will return -1
	def fileno (self):
		if self._fileno is None:
			self._fileno = self.socket.fileno()
		return self._fileno

	def del_channel (self):
		if socket_map.has_key (self):
			self.log ('closing channel %d:%s' % (self.fileno(), self))
			del socket_map [self]

	def create_socket (self, family, type):
		self.sock_tcp= (type == socket.SOCK_STREAM) # sock_dgram support
		self.socket = socket.socket (family, type)
		self.socket.setblocking(0)
		self.add_channel()

	def set_socket (self, sock):
		self.socket = sock
		self.sock_tcp=(sock.getsockopt(socket.SOL_SOCKET, socket.SO_TYPE)==socket.SOCK_STREAM)
		self.add_channel()

	# ==================================================
	# predicates for select()
	# these are used as filters for the lists of sockets
	# to pass to select().
	# ==================================================
	def readable (self):
		return self.connected or self.accepting

	def writable (self):
		return self.write_blocked or not self.connected

	# ==================================================
	# socket object methods.
	# ==================================================

	def listen (self, num):
		self.accepting = 1
		if os.name == 'nt' and num > 5:
			num = 1
		if self.sock_tcp: # sock_dgram support
			self.socket.listen (num)
		

	def connect (self, host, port):
		try:
			self.socket.connect (host, port)
		except socket.error, why:
			if type(why) == type(()) \
			   and why[0] in (EINPROGRESS, EALREADY, EWOULDBLOCK):
				return
			else:
				raise socket.error, why
		self.connected = 1
		self.handle_connect()

	def send (self, data):
		if DEBUG&1: print '>',data
		try:
			result = self.socket.send (data)
			if result != len(data):
				self.write_blocked = 1
			else:
				self.write_blocked = 0
			return result
		except socket.error, why:
			if type(why) == type(()) and why[0] == EWOULDBLOCK:
				self.write_blocked = 1
				return 0
			else:
				raise socket.error, why
			return 0

	def recv (self, buffer_size):
		data = self.socket.recv (buffer_size)
		if DEBUG&1: print '<',data
		if not data:
			# a closed connection is indicated by signaling
			# a read condition, and having recv() return 0.
			self.handle_close()
			return ''
		else:
			return data

	def sendto (self, host, port, data):	# sock_dgram support
		if DEBUG&1: print '>',host,port,data
		try:
			result = self.socket.sendto (data,(host,port))
			if result != len(data):
				self.write_blocked = 1
				self.address = (host,port)
			else:
				self.write_blocked = 0
				self.address = None
			return result
		except socket.error, why:
			if type(why) == type(()) and why[0] == EWOULDBLOCK:
				self.write_blocked = 1
				return 0
			else:
				raise socket.error, why
			return 0

	def recvfrom (self, buffer_size):		# sock_dgram support
		data,(host,port) = self.socket.recvfrom (buffer_size)
		if DEBUG&1: print '<',host,port,data
		if not data:
			# a closed connection is indicated by signaling
			# a read condition, and having recv() return 0.
			self.handle_close()
			return ''
		else:
			return host,port,data

	def close (self):
		self.socket.close()
		self.del_channel()

	# cheap inheritance, used to pass all other attribute
	# references to the underlying socket object.
	def __getattr__ (self, attr):
		return getattr (self.socket, attr)

	def log (self, message):
		if DEBUG&512:
			print 'log:', message
		else: pass

	def handle_read_event (self):
		if self.accepting:
			# for an accepting socket, getting a read implies
			# that we are connected
			if self.sock_tcp:		# sock_dgram support
				if not self.connected:
					self.connected = 1
				self.handle_accept()
		elif not self.connected:
			self.handle_connect()
			self.connected = 1
		self.handle_read()

	def more_to_send (self, yesno=1):
		self.write_blocked = yesno

	def handle_write_event (self):
		# getting a write implies that we are connected
		if self.sock_tcp:				# sock_dgram support
			if not self.connected:
				self.handle_connect()
				self.connected = 1
			self.write_blocked = 0
		else:
			self.write_blocked = 0
			self.address = None		# sock_dgram support
		self.handle_write()

	def handle_expt_event (self):
		self.handle_expt()

	def handle_error (self, *info):
		self.log ('uncaptured python exception, closing channel')
		import traceback
		print info[0],':',info[1]
		traceback.print_tb(info[2])
		self.close()

	def handle_expt (self):
		self.log ('unhandled exception')

	def handle_read (self):
		self.log ('unhandled read event')

	def handle_write (self):
		self.log ('unhandled write event')

	def handle_connect (self):
		self.log ('unhandled connect event')

	def handle_oob (self):
		self.log ('unhandled out-of-band event')

	def handle_accept (self):
		self.log ('unhandled accept event')

	def handle_close (self):
		self.log ('unhandled close event')

# ---------------------------------------------------------------------------
# adds simple buffered output capability, useful for simple clients.
# [for more sophisticated usage use asynchat.async_chat]
# ---------------------------------------------------------------------------

class dispatcher_with_send (dispatcher):
	def __init__ (self, sock=None):
		dispatcher.__init__ (self, sock)
		self.out_buffer = ''

	def initiate_send (self):
		while self.out_buffer:
			num_sent = 0
			num_sent = dispatcher.send (self, self.out_buffer[:512])
			self.out_buffer = self.out_buffer[num_sent:]

	def handle_write (self):
		self.initiate_send()

	def send (self, data):
		if self.debug:
			self.log ('sending %s' % repr(data))
		self.out_buffer = data
		self.initiate_send()

# ---------------------------------------------------------------------------
# used for debugging.
# ---------------------------------------------------------------------------

def close_all ():
	for x in socket_map.keys():
		x.socket.close()
	socket_map = {}

# Asynchronous File I/O:
#
# After a little research (reading man pages on various unixen, and
# digging through the linux kernel), I've determined that select()
# isn't meant for doing doing asynchronous file i/o.
# Heartening, though - reading linux/mm/filemap.c shows that linux
# supports asynchronous read-ahead.  So _MOST_ of the time, the data
# will be sitting in memory for us already when we go to read it.
#
# What other OS's (besides NT) support async file i/o?
#
# Regardless, this is useful for pipes.

import os
if os.name == 'posix':
	import fcntl
	import FCNTL
	class file_dispatcher (dispatcher):
		def __init__ (self, fd):
			dispatcher.__init__ (self)
			self.connected = 1
			# set it to non-blocking mode
			flags = fcntl.fcntl (fd.fileno(), FCNTL.F_GETFL, 0)
			flags = flags | FCNTL.O_NONBLOCK
			fcntl.fcntl (fd.fileno(), FCNTL.F_SETFL, flags)
			self.set_file (fd)

		def set_file (self, fd):
			self.socket = fd
			self.add_channel()


