############################################################################
##
## Copyright (c) 2000-2001 BalaBit IT Ltd, Budapest, Hungary
## All rights reserved.
##
## $Id: Receiver.py,v 1.11.2.2.2.2 2001/09/04 09:42:56 bazsi Exp $
##
## Author  : Bazsi
## Auditor : kisza
## Last version: 1.3
## Notes:
##
############################################################################

"""Module defining the main entry point for Datagram based protocols.

This module defines the class 'Receive', the main entry point for UDP
based protocols.
"""

from Zorp import *
from Service import services

class Receiver:
	"""Class encapsulating a datagram oriented listener.
	
	Base class for datagram listeners to be used with datagram
	oriented proxies.
	
	This class create a datagram socket with Receive and give it
	to the Appropriate Connection tracking Module.
	
	Attributes
	
	  receive -- Underlying Receive instance to create socket.
	"""

	def __init__(self, bindto, service):
		"""Constructor to initialize a Receiver instance.
		
		This constructor first initializes a Receiver instance
		by setting attributes and creating the underlying
		Receive instance.
		Second get the socket fd from Receive class and start
		the given Service for this socket.
		
		Arguments
		
		  bindto	-- the address to bind to

		  service	-- the service to start for incoming datagrams
		  
		"""
		try:
			self.service = services[service]
		except KeyError:
			raise ServiceException, "Service %s not found" % (service,)

		self.receive = Receive(bindto)
		client_fd = self.receive.getFD()
		self.service.startCTInstance(client_fd)

	def destroy(self):
		"""Function to destroys the receiver
		
		This function can be called to destroy this receiver on
		demand. It calls the destroy method of the low-level
		ZorpReceive object.
		
		Arguments

		  self -- this instance
		"""
		self.receive.destroy()
