Exemple #1
0
def main(config):
            
    configureLogging(config)
    log = logging.getLogger('main')
    
    if config.getboolean('daemon', 'daemon'):
        pidfile = config.get('daemon', 'pidfile')
        daemonize(pidfile)
        
    
    pw = config.get('host', 'password')
    h = config.get('host', 'host')
    p = config.get('host', 'port')
    
    #Setting up the DB connection
    log.info('Connecting to DB on: %s ' % (config.get('database', 'url')))

    if ("sqlite" in config.get('database', 'url')):
        engine = create_engine( config.get('database', 'url'), poolclass=NullPool )
        log.warning('You are using SQLite and we will use NullPool as pooling engine.')
    else:
        engine = create_engine( config.get('database', 'url'), pool_recycle=3600 )


    Session = sessionmaker( autoflush=False, autocommit=False, bind=engine)
    
    log.info('Connecting to: %s:%s ' % (h, p))
    con = ESLconnection(h, p, pw)
    con.events('plain', 'CUSTOM conference::maintenance')
    conferences = {}
    while con.connected():
        e = con.recvEvent()

        if e is None:
            continue
            

        conf_uuid = getUnquotedHeader(e, 'Conference-Unique-ID')
        action = getUnquotedHeader(e, 'Action')
        conf_size = getUnquotedHeader(e, 'Conference-Size')
        
        if action == 'stop-recording':
            try:
                TrackerConference.stop_recording(e, config, Session())
                continue
            except Exception, e:
                log.critical('An error has occurred while executing stop-recording!\n%s' % traceback.format_exc())
                continue
        
        if conf_uuid not in conferences.keys():
            try:
                conf = TrackerConference(e, config, Session())
                conferences[conf_uuid] = conf
            except Exception, e:
                log.warning('An error occured creating a new conference object! Continuing... \n%s' % e)
                continue
def main(config):
            
    configureLogging(config)
    log = logging.getLogger('main')
    
    if config.getboolean('daemon', 'daemon'):
        global PIDFILE
        PIDFILE = config.get('daemon', 'pidfile')
        daemonize()
        
    
    pw = config.get('host', 'password')
    h = config.get('host', 'host')
    p = config.get('host', 'port')
    
    #Setting up the DB connection
    log.info('Connecting to DB on: %s ' % (config.get('database', 'url')))

    if ("sqlite" in config.get('database', 'url')):
        engine = create_engine( config.get('database', 'url'), poolclass=NullPool )
        log.warning('You are using SQLite and we will use NullPool as pooling engine.')
    else:
        engine = create_engine( config.get('database', 'url'), pool_recycle=3600 )


    sm = sessionmaker( autoflush=False, autocommit=False, bind=engine)
    Session = scoped_session( sm )
    
    log.info('Connecting to: %s:%s ' % (h, p))
    con = ESLconnection(h, p, pw)
    con.events('plain', 'CUSTOM conference::maintenance')
    while con.connected():
        e = con.recvEvent()

        if e is None:
            continue
        log.debug('Received event:\n%s' % e.serialize())

        conf_uuid = getUnquotedHeader(e, 'Conference-Unique-ID')
        conf_profile = getUnquotedHeader(e, 'Conference-Profile-Name')
        conf_name = getUnquotedHeader(e, 'Conference-Name')
        conf_size = getUnquotedHeader(e, 'Conference-Size')
        caller_id_name = getUnquotedHeader(e, 'Caller-Caller-ID-Name')
        caller_id_number = getUnquotedHeader(e, 'Caller-Caller-ID-Number')
        member_id = getUnquotedHeader(e, 'Member-ID')
        member_type = getUnquotedHeader(e, 'Member-Type')
        action = getUnquotedHeader(e, 'Action')
        event_date = getUnquotedHeader(e, 'Event-Date-Local')
        if event_date is not None:
            # Check the python version, because strptime does not exist in it.
            if sys.version_info[2] < 5:
                spl = event_date.split('-')
                year = int(spl[0])
                month = int(spl[1])
                spl2 = spl[2].split(' ')
                day = int(spl2[0])
                spl3 = spl2[1].split(':')
                hour = int(spl3[0])
                minutes = int(spl3[1])
                seconds = int(spl3[2])
                event_date = datetime(year=year, month=month, day=day, hour=hour, minute=minutes, second=seconds)
            else:
                event_date = datetime.strptime(event_date, '%Y-%m-%d %H:%M:%S')

        if not conf_name:
            log.warning('There is no conf name on this event.\n%s' % e.serialize())
            continue
            
        try:
            owner = Session.query(ModeratorPin).get(conf_name)
        except Exception, err:
            log.critical(traceback.format_exc())
        if owner is None:
            log.critical('Error because we could not get owner.')
            Session.remove()
            continue
            
        confObj = Session.query(Conference).get(conf_uuid)
        if confObj is None:
            log.debug('Conference does not exist, we must create one.')
        
        if action == 'add-member':
            log.info('Member added...')
            if conf_size == '1':
                confObj = Conference(conf_uuid, event_date, unicode(conf_name), unicode(conf_profile))
                owner.conferences.append(confObj)
                Session.add(confObj)
                log.debug('Action taken on %s. %s' % (conf_name, action))
                actionObj = ConferenceAction(unicode(action), 
                                            unicode(caller_id_name),
                                            unicode(caller_id_number),
                                            unicode(conf_size),
                                            unicode(member_type),
                                            event_date,
                                            confObj.id)
                confObj.actions.append(actionObj)
                Session.add(actionObj)
                Session.commit()
                Session.remove()
                log.debug('Conference %s has been created.' % conf_name)
                continue
                
        elif action == 'del-member':
            if conf_size == '0':
                confObj.ended = event_date
                log.debug('Action taken on %s. %s' % (conf_name, action))
                actionObj = ConferenceAction(unicode(action), 
                                            unicode(caller_id_name),
                                            unicode(caller_id_number),
                                            unicode(conf_size),
                                            unicode(member_type),
                                            event_date,
                                            confObj.id)
                confObj.actions.append(actionObj)
                Session.add(actionObj)

                Session.commit()
                Session.remove()
                log.debug('Conference %s has been destroyed.' % conf_name)
                continue
                

        
        log.debug('Action taken on %s. %s' % (conf_name, action))

        # This action comes after the conference has ended.
        if action == 'stop-recording':
            confObj.recording = os.path.join(config.get('mp3', 'store_to'), getUnquotedHeader(e, 'Path').split('/').pop()[:-3] + 'mp3')
            host = unicode(getUnquotedHeader(e, 'FreeSWITCH-IPv4'))
            Session.commit()
            try:
                owner_obj = Session.query(ModeratorPin).get(confObj.owner)
            except Exception, e:
                log.critical('Could not find owner. Db corruption? %s' % e)
                log.critical('Exception: %s' % traceback.format_exc())
                Session.remove()
                continue
                
            t = Thread(target=postRecordingThread, args=(config, getUnquotedHeader(e, 'Path'), confObj, host, owner_obj))
            log.debug('Starting thread %s' % t)
            t.start()
            log.debug('Thread started...')
            Session.remove()
            continue