Exemple #1
0
    def on_open(self, request):    
        log('ws connecting')
        self.api_key = request.get_argument('api_key') 
        self.user_id = request.get_argument('user_id')     
        self.is_authenticated = False
        
        # get conn id    
        r = redis_connect(db=1)
        self.conn_id = str(r.incr('conn_id'))    

        CLIENTS.setdefault(self.api_key, {})   
        
        client = CLIENTS[self.api_key]
        client.setdefault('conns', set()).add(self)
        client.setdefault('event_auth', {})
        client.setdefault('subs', {})
        client.setdefault('presence:joined', set())
        client.setdefault('presence:left', set()) 
        client.setdefault('usage', {})
        client.setdefault('conn_ids', {})
        client['conn_ids'][self.conn_id] = self
        client['usage'].setdefault('messages', 0)
        client['usage'].setdefault('connections', 0)
        client['usage']['connections'] += 1

        # check api_key exists in db 
        try:
            db = database.Connection(settings.DB_HOST, settings.DB_DATABASE, user=settings.DB_USER, password=settings.DB_PASSWORD)
            api_result = db.get('SELECT id, callback_url, disabled from common_apiaccount where api_key="%s"' % self.api_key)
            db.close()
            if api_result:
                if api_result['disabled'] == 1:
                    return False
                    
                self.callback_url = api_result['callback_url']         
            else:
                return False     
        except:
            return False       

        # start a redis listener thread for this api_key
        if not REDIS_THREADS.has_key(self.api_key):
            redis_thread = threading.Thread(target=redis_listener, args=(self.api_key,))
            redis_thread.daemon = True  
            REDIS_THREADS[self.api_key] = redis_thread 
            redis_thread.start()
            
        # emit presence event (broadcast)       
        r = redis_connect()     
        r.publish(self.api_key, json.dumps({'action':'broadcast',
                                            'api_key':self.api_key,
                                            'conn_id':self.conn_id,
                                            'channel':'presence:joined',
                                            'message':json.dumps({'conn_id':self.conn_id, 'user_id':self.user_id})}))   
        self.add_msg_count() 
        
        return self.conn_id  
Exemple #2
0
def usage_update():
    '''
    updates the main db with connection and message numbers
    '''
    db = database.Connection(settings.DB_HOST,
                             settings.DB_DATABASE,
                             user=settings.DB_USER,
                             password=settings.DB_PASSWORD)

    for k, v in CLIENTS.iteritems():
        msgs = v['usage']['messages']
        conns = v['usage']['connections']
        if msgs == 0:
            continue

        v['usage']['messages'] = 0

        api_account = db.get(
            'select id from common_apiaccount where api_key="%s"' % k)
        if api_account:
            db.execute(
                'insert into common_usagetransaction (api_account_id, created, messages, connections) \
                   values (%s, NOW(), %s, %s)' %
                (api_account['id'], msgs, conns))

    db.close()
Exemple #3
0
def usage_update():
    '''
    updates the main db with connection and message numbers
    '''
    db = database.Connection(settings.DB_HOST, settings.DB_DATABASE, user=settings.DB_USER, 
                             password=settings.DB_PASSWORD)
                             
    for k, v in CLIENTS.iteritems():
        msgs = v['usage']['messages']
        conns = v['usage']['connections']
        if msgs == 0:
            continue
            
        v['usage']['messages'] = 0
        
        api_account = db.get('select id from common_apiaccount where api_key="%s"' % k)
        if api_account:
            db.execute('insert into common_usagetransaction (api_account_id, created, messages, connections) \
                   values (%s, NOW(), %s, %s)' % (api_account['id'], msgs, conns))
                   
    db.close()
Exemple #4
0
    def on_open(self, request):
        log('ws connecting')
        self.api_key = request.get_argument('api_key')
        self.user_id = request.get_argument('user_id')
        self.is_authenticated = False

        # get conn id
        r = redis_connect(db=1)
        self.conn_id = str(r.incr('conn_id'))

        CLIENTS.setdefault(self.api_key, {})

        client = CLIENTS[self.api_key]
        client.setdefault('conns', set()).add(self)
        client.setdefault('event_auth', {})
        client.setdefault('subs', {})
        client.setdefault('presence:joined', set())
        client.setdefault('presence:left', set())
        client.setdefault('usage', {})
        client.setdefault('conn_ids', {})
        client['conn_ids'][self.conn_id] = self
        client['usage'].setdefault('messages', 0)
        client['usage'].setdefault('connections', 0)
        client['usage']['connections'] += 1

        # check api_key exists in db
        try:
            db = database.Connection(settings.DB_HOST,
                                     settings.DB_DATABASE,
                                     user=settings.DB_USER,
                                     password=settings.DB_PASSWORD)
            api_result = db.get(
                'SELECT id, callback_url, disabled from common_apiaccount where api_key="%s"'
                % self.api_key)
            db.close()
            if api_result:
                if api_result['disabled'] == 1:
                    return False

                self.callback_url = api_result['callback_url']
            else:
                return False
        except:
            return False

        # start a redis listener thread for this api_key
        if not REDIS_THREADS.has_key(self.api_key):
            redis_thread = threading.Thread(target=redis_listener,
                                            args=(self.api_key, ))
            redis_thread.daemon = True
            REDIS_THREADS[self.api_key] = redis_thread
            redis_thread.start()

        # emit presence event (broadcast)
        r = redis_connect()
        r.publish(
            self.api_key,
            json.dumps({
                'action':
                'broadcast',
                'api_key':
                self.api_key,
                'conn_id':
                self.conn_id,
                'channel':
                'presence:joined',
                'message':
                json.dumps({
                    'conn_id': self.conn_id,
                    'user_id': self.user_id
                })
            }))
        self.add_msg_count()

        return self.conn_id