Esempio n. 1
0
def dispatcher_routine(chan):
    " This listens dispatcher redis channel and send data through operator channel "
    rc = redis.Redis()
    sub = rc.pubsub()
    myrooms = RedisConn.smembers('rooms.%s'%chan.user)
    channels = []
    for rm in myrooms:
        logging.info('rm = %s, type=%s ; decoded: %s'%(rm,type(rm),json.loads(rm)))
        nch = channelpref+json.loads(rm)['id']
        channels.append(nch)
    if chan.user:
        channels.append(channelpref+'@'+chan.user)
    #channels = set([channelpref+rm['id'] for rm in myrooms])
    #substr = ' '.join(channels)
    if len(channels):
        for ochan in channels:
            RedisConn.publish(ochan,json.dumps({'op':'subscribe','user':chan.user,'room':ochan.split('_')[1],'stamp':nowstamp()}))

        logging.info('subscribing to chat at %s'%channels)
        sub.subscribe(channels)
        for msg in sub.listen():
            if msg['type']=='subscribe': 
                logging.info('SKIPPING SUBSCRIBE MESSAGE %s'%msg)
                continue
            logging.info('CHANNEL %s < DISPATCHER MESSAGE %s'%(chan,msg))
            msgd = msg['data']
            chan.send(json.loads(msgd)) #tosend(WS_CHANNELS['BO_CHID'], json.loads(msg['data']))                                                                     
    else:
        logging.info('no channels to subscribe to')
Esempio n. 2
0
    def resub(self,unsub=False):
        if self.dispatcher:
            logging.info('tearing down message dispatcher')
            gevent.kill(self.dispatcher)
            self.dispatcher=None
        if unsub:
            logging.info('looks like we\'re leaving. unsubscribing from channels:')
            while len(RedisConn.smembers('rooms.%s'%self.user)):
                room = json.loads(RedisConn.spop('rooms.%s'%self.user))['id']
                logging.info('unsub: %s'%room)
                ochan = channelpref + room
                RedisConn.srem('users.%s'%room,self.user)
                RedisConn.publish(ochan,json.dumps({'op':'unsubscribe','user':self.user,'room':room,'stamp':nowstamp()}))
                logging.info('publishing unsub event on %s'%ochan)

                lo = {'op':'leave','room':room,'user':self.user,'stamp':nowstamp()} #,'obj':m['obj']}
                writelog(room,json.dumps(lo))

        else:
            logging.info('creating new dispatcher')
            self.dispatcher = gevent.spawn(dispatcher_routine, self)
Esempio n. 3
0
 def firing(self, event_data=None):
     if event_data is None:
         event_data = {}
     event_msg = json.dumps({'event_id': self.id, 'event_data': event_data})
     RedisConn.publish(NOODLES_EVENTS_CHANNEL, event_msg)
     log.debug('Event %s is firing', self.id)
Esempio n. 4
0
 def firing(self, event_data = {}):
     event_msg = {'event_id': self.id, 'event_data': event_data}
     RedisConn.publish(NOODLES_EVENTS_CHANNEL, json.dumps(event_msg))
     print 'Event is firing'
Esempio n. 5
0
 def firing(self, event_data={}):
     event_msg = {'event_id': self.id, 'event_data': event_data}
     RedisConn.publish(NOODLES_EVENTS_CHANNEL, event_msg)
     print 'Event is firing'
Esempio n. 6
0
    def onmessage(self, msg):
        m = msg.data
        logging.info('ONMESSAGE < %s ; %s'%(msg.data,m['op']))
        
        if m['op'] in 'auth':
            rauth = RedisConn.get('auth.%s'%m['authck'])
            if rauth == m['user']:
                self.authenticated=True
                self.user = m['user']
                self.authck = m['authck']
        if not self.authenticated:
            raise Exception('not authenticated')

        if m['op'] in 'auth': pass
        elif m['op'] in 'logout':
            RedisConn.delete('auth.%s'%self.authck)
            logging.info('signing out %s/%s'%(self.user,self.authck))
            self.send({'op':'signoutresult','status':'ok'})
        elif m['op']=='msg':
            pass
        elif m['op']=='join':
            pass
        elif m['op'] in ['create','update']:
            o = m['obj']
            if m['objtype']=='joinedroom':
                roomname = o['id']
                r =  getroom(roomname) ; assert r
                roomkey='rooms.%s'%self.user
                myrooms = RedisConn.smembers(roomkey)
                if roomname not in [json.loads(mr)['id'] for mr in myrooms]:
                    logging.info('%s not in myrooms %s, hence adding'%(roomname,myrooms))
                    RedisConn.sadd(roomkey,json.dumps(o))
                    myrooms = RedisConn.smembers(roomkey)
                    lo = {'op':'join','room':roomname,'user':self.user,'stamp':nowstamp(),'obj':o}
                    writelog(roomname,json.dumps(lo))

                RedisConn.sadd('users.%s'%roomname,self.user)


                self.resub()
                self.send({'op':'joinresult','status':'ok','roomname':roomname,'obj':o,'content':self.roomcontent(roomname),'users':list(RedisConn.smembers('users.%s'%roomname))})
            elif m['objtype']=='message':
                o['sender'] = self.user #self.request.remote_addr
                o['stamp'] = nowstamp()
                line = json.dumps(o)
                writelog(o['room'],line)
                pubchan = channelpref+o['room']
                assert o['room']
                logging.info('PUBLISH(%s) > %s'%(pubchan,m))
                RedisConn.publish(pubchan,json.dumps(m))
                #publish a copy to self in case of privmsg
                if o['room'][0]=='@':
                    no = o.copy()
                    # snd = no['sender']
                    no['sender'] = no['room'][1:]
                    # no['room']='@'+no['sender']
                    nm = m.copy()
                    nm['o']=no
                    pchan = channelpref+'@'+self.user
                    #Exception: {u'objtype': u'message', u'obj': {u'stamp': '2011-08-26 09:50', u'content': u'w00t', u'sender': u'milez', u'room': u'@milez2'}, u'options': {}, u'op': u'create'}
                    logging.info('PUBLISHSELF %s < %s'%(pchan,json.dumps(nm)))
                    RedisConn.publish(pchan,json.dumps(m))
            else:
                raise Exception('unknown objtype %s'%m['objtype'])
        elif m['op'] in ['delete']:
            if m['objtype']=='joinedroom':
                roomname = m['obj']['id']
                r = getroom(roomname) ; assert r
                roomkey = 'rooms.%s'%self.user
                myrooms = RedisConn.smembers(roomkey)
                if roomname in [json.loads(mr)['id'] for mr in myrooms]:
                    RedisConn.srem(roomkey,json.dumps({'id':roomname}))
                    lo = {'op':'leave','room':roomname,'user':self.user,'stamp':nowstamp(),'obj':m['obj']}
                    writelog(roomname,json.dumps(lo))
                    logging.info('DEPARTTURE of %s from %s -> %s'%(self.user,roomname,RedisConn.smembers(roomkey)))
                    
                RedisConn.srem('users.%s'%roomname,self.user)
                self.resub()

                self.send({'op':'leaveresult','status':'ok','roomname':roomname})
            else:
                raise Exception('unknown objtype %s'%m['objtype'])
        elif m['op']=='leaveall':
            while len(RedisConn.smembers('rooms.%s'%self.user)): RedisConn.spop('rooms.%s'%self.user)
            self.resub()
        else:
            raise Exception('unknown op %s'%m['op'])
 def firing(self, event_data=None):
     if event_data is None:
         event_data = {}
     event_msg = json.dumps({'event_id': self.id, 'event_data': event_data})
     RedisConn.publish(NOODLES_EVENTS_CHANNEL, event_msg)
     log.debug('Event %s is firing', self.id)