# Copyright (c) 2000-2001 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

"""BaseService module."""

__revision__ = "$Id: BaseService.py,v 1.8 2001/11/06 14:39:46 alf Exp $"

import threading
import select
import time

# Service Interface ############################################################

class ServiceInterface:
    """service interface"""

    def start(self) :
        raise Exception('not implemented')

    def stop(self) :
        raise Exception('not implemented')

# Base Service #################################################################

class BaseService(ServiceInterface) :
    """Base service"""
    
    def __init__(self,name=None) :
        self.thread = None
        self.threadName = name or 'BaseService'
        self.loop = 0

    def start(self) :
        self.thread = threading.Thread(name=self.threadName, target=self._run)
        self.loop = 1
        self.thread.start()
        print 'Service',self.threadName,'started'

    def stop(self) :
        self.loop = 0
        self.thread.join()
        print 'Service',self.threadName,'stopped'
        
    def _run(self) :
        while self.loop :
            time.sleep(1)
            print self.threadName, 'loop...'

# Base Socket Service ##########################################################

import socket
from SocketServer import TCPServer

class BaseSocketService(BaseService, TCPServer) :
    """base socket service"""

    allow_reuse_address = 1

    def __init__(self,port,handler,name=None) :
        BaseService.__init__(self, name or 'BaseSocketService')
        TCPServer.__init__(self, (socket.gethostname(),port), handler)
        print 'Service',self.threadName, "listening on %s:%d" \
              % (socket.gethostname(),port)
        
    def _run(self) :
        while self.loop :
            r,w,e = None,None,None
            try:
                r,w,e = select.select([self.socket],[],[self.socket],1)
            except Exception, e:
                print self.__class__,'select error:',e
            if r :
                request, client_address = self.socket.accept()
                try:
                    self.process_request(request, client_address)
                except:
                    self.handle_error(request, client_address)
