iro/iro.py
author Sandro Knauß <knauss@netzguerilla.net>
Wed, 21 Mar 2012 19:41:55 +0100
branchdevel
changeset 239 4cf5e664c847
parent 67 d1912509f375
child 302 3f4bdea2abbf
permissions -rw-r--r--
adding tests task: test for error chaining config: test for init function

# -*- coding: utf-8 -*-
#Copyright (C) 2009  Sandro Knauß <bugs@sandroknauss.de>

#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 3 of the License, or 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, see <http://www.gnu.org/licenses/>.

import multiprocessing, logging 
#logging anfangen
logger=logging.getLogger("iro")
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s(%(processName)s)-%(levelname)s: %(message)s')


# Server code
from xmlrpc import SecureUserDBXMLRPCServer,UserDB

from user import User, Admin
import anbieter
import ConfigParser

from job import SMSJob, FAXJob, MailJob
from joblist import Joblist
from providerlist import Providerlist
from acounting import Acounting

class MyUserDB(UserDB):
    def __init__(self, userlist,jobqueue):
        UserDB.__init__(self, None,userlist,jobqueue)
        
    def createUser(self, user):
        self.userlist[self.createHash(user)]=user["class"](user["name"],self.jobqueue)


 

class MySipgate(anbieter.sipgate):
    
    def __init__(self,user="",password="" ):
        anbieter.sipgate.__init__(self, user, password)
    
    def setJob(self, job):
        self.job=job
    
    def updateStatus(self, arranged=None, failed=None):
        if arranged:
            self.job.addGood(arranged)
        
        if failed:
            self.job.addFailed(failed)
            
class MySMTP(anbieter.SMTP):
    
    def __init__(self,config_filename=None,section="smtp"):
        anbieter.SMTP.__init__(self,config_filename,section)
    
    def setJob(self, job):
        self.job=job
    
    def updateStatus(self, arranged=None, failed=None):
        if arranged:
            self.job.addGood(arranged)
        
        if failed:
            self.job.addFailed(failed)
    
class MySmstrade(anbieter.smstrade):
    
    def __init__(self ):
        anbieter.smstrade.__init__(self )
    
    def setJob(self, job):
        self.job=job
    
    def updateStatus(self, arranged=None, failed=None):
        if arranged:
            self.job.addGood(arranged)
        
        if failed:
            self.job.addFailed(failed)



def start(userlist):
    from multiprocessing import Queue
    from multiprocessing.managers import BaseManager

    
    class MyManager(BaseManager):
        pass
    
    MyManager.register('SMSJob', SMSJob) 
    MyManager.register('FaxJob', FAXJob) 
    MyManager.register('MailJob',MailJob) 
    MyManager.register('Providerlist',Providerlist) 
    MyManager.register('Acounting',Acounting)
    manager = MyManager()
    manager.start()
    
    
    conf=["iro.conf", "~/iro.conf","/etc/iro/iro.conf"]

    #anbieter erzeugen und konfigurieren

    sip=MySipgate()
    sip.read_basic_config(conf)
    
    localhost=MySMTP()
    localhost.read_basic_config(conf)

    smstrade=MySmstrade()
    smstrade.read_basic_config(conf)

    cp = ConfigParser.ConfigParser()
    cp.read(conf)
    dbconn={'type':cp.get('db', 'type'),
            'host':cp.get('db', 'host'),
            'db':cp.get('db', 'db'),
            'user':cp.get('db', 'user'),
            'passwd':cp.get('db', 'passwd'),
            'table':cp.get('db', 'table'),
    }
    
    
    #Benutzerdatenbank erstellen
    queue = Queue()
    provider=manager.Providerlist()
    provider.add("sipgate", sip, ["sms", "fax", ])
    provider.add("smstrade", smstrade, ["sms", ])
    #provider.add("geonet", None, ["sms", "fax", ])
    #provider.add("fax.de", None, ["sms", "fax", ])
    provider.add("localhost", localhost, ["mail", ])
    provider.setDefault("sms","smstrade")
    provider.setDefault("fax","sipgate")
    provider.setDefault("mail","localhost")
    jobqueue=Joblist(manager,  queue, provider,dbconn)
   
    userdb=MyUserDB(userlist,jobqueue)

    #working thread erstellen
    from worker import Worker
    worker=Worker(queue)
    worker.start()

    #Server starten
    cp = ConfigParser.ConfigParser()
    cp.read(conf)
    cert=cp.get('server', 'cert')
    key=cp.get('server', 'key')
    server = SecureUserDBXMLRPCServer(addr=("localhost", 8000), 
                                      userdb=userdb,
                                      certificate=cert,privatekey=key)
    server.relam="xmlrpc"
    
    logger.info('Server gestartet...')
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass
    except:
        logger.exception('Äh, ein Fehler ist aufgetreten')
    finally:    
        logger.info('Server wird beendet...')
        queue.close()
        worker.terminate()

if __name__ == '__main__':
    userlist=[{"name":"test","password":"test",  "class":User},
	      {"name":"test2","password":"test2", "class": Admin}]
    start(userlist)