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 update(self, storage=None, **kwargs):
        '''update time expire'''
        print 'updating::'
        id = mkey(REDIS_NAMESPACE, self.collection_name, self.id)

        if 'expire' in kwargs:
            print TIME_TO_OVERWRITE_CLIENT_COOKIE, RedisConn.ttl(id)
            if  TIME_TO_OVERWRITE_CLIENT_COOKIE > RedisConn.ttl(id):
                result = RedisConn.expire(id, kwargs['expire'])
                logging.info('UPDATE LIFETIME TO: %s SECONDS'
                             % kwargs['expire'])
                return result
            else:
                logging.debug('non_update_SESSION')

        else:
            raise Exception('unknown action!!!')
Esempio n. 3
0
 def save(self, storage=None):
     " Save object to redis storage"
     if self.embedded:
         logging.warning('You should save embedded objects with high level object')
         return
     if not self.id:
         new_id = RedisConn.incr(mkey(REDIS_NAMESPACE,
                                      self.__class__.__name__.lower() + '_key'))
         if self.__salt__:
             self.id = hashlib.md5(str(new_id) + self.__salt__).hexdigest()
         else:
             self.id = new_id
     RedisConn.set(mkey(REDIS_NAMESPACE, self.collection_name, self.id),
                   json.dumps(self.__instdict__))
     if self.expire != None:
         RedisConn.expire(mkey(REDIS_NAMESPACE, self.collection_name,
                               self.id), self.expire)
Esempio n. 4
0
def index(request,room=None):
    authck = request.cookies.get('auth')
    logging.info('current auth cookie is %s (%s)'%(authck,RedisConn.get('auth.%s'%authck)))
    setauthck=None
    if not authck or not RedisConn.get('auth.%s'%authck):
        un = request.params.get('username','')
        pw = request.params.get('password','')
        err=''
        if un:
            u = getuser(un)
            if u:
                logging.info('user exists')
                u = u[0]
                hpw = hashlib.md5(pw).hexdigest()
                #raise Exception('comparing %s with %s'%(u,hpw))
                if u['password']==hpw:
                    logging.info('generating auth cookie')
                    setauthck = ''.join(random.choice('abcdefghijklmnopqrstuvwxyz') for i in xrange(16))
                else:
                    err='invalid login'
            else:
                logging.info('creating new user')
                user={'id':un,'password':hashlib.md5(pw).hexdigest()}
                users.append(user)
                saveusers()
                err='user %s created succesfully. please log in'%un
        context = {'username':un,'password':pw,'err':err}
        rtpl='auth.html'
    else:
        
        rtpl='index.html'
        global rooms
        if room: openrooms = room.split(',')
        else: openrooms = []
        openrooms = [getroom(rn) for rn in openrooms if rn[0]!='@']
        context = {'rooms':json.dumps(rooms),'openrooms':json.dumps(openrooms),'user':RedisConn.get('auth.%s'%authck),'authck':authck,'rowlimit':ROWLIMIT}

    rendered_page = render_to_string(rtpl, context, request)
    rsp= Response(rendered_page)
    if setauthck: 
        RedisConn.set('auth.%s'%setauthck,un)
        logging.info('setting auth cookie = %s (redis value = %s)'%(setauthck,RedisConn.get('auth.%s'%setauthck)))
        rsp =  Redirect('/')
        rsp.set_cookie('auth',setauthck)
    return rsp
Esempio n. 5
0
 def __init__(self):
     # Get id for web-socket session
     self.id = RedisConn.incr( "".join( [self.__class__.__name__, '_ids'] ) )
     self.output_queue = Queue()
     self.params = {'wssid': self.id} # Session specific parameters
     self.greenlets = {} # The dictionary that storages all greenlets associated with this session
     # except of input/output servelet handlers and main servelet function
     self.terminators = {} # list of functions for execute while session is terminating
     self._collection[self.id] = self
     self.semaphore = Semaphore() # use it concurent data access conditions
Esempio n. 6
0
 def __init__(self):
     # Get id for web-socket session
     self.id = RedisConn.incr("".join([self.__class__.__name__, '_ids']))
     self.output_queue = Queue()
     self.params = {'wssid': self.id}  # Session specific parameters
     self.greenlets = {}  # The dictionary that storages all greenlets associated with this session
     # except of input/output servelet handlers and main servelet function
     self.terminators = {}  # list of functions for execute while session is terminating
     self._collection[self.id] = self
     self.semaphore = Semaphore()  # use it concurent data access conditions
Esempio n. 7
0
    def dispatcher_routine(self):
        """This listens dispatcher redis channel and send data through channel
        """
        sub = RedisConn.pubsub()
        log.info('subscribing to %s' % self.subscribe_name)
        sub.subscribe(self.subscribe_name)
        for msg in sub.listen():
            if not 'type' in msg:
                continue

            if msg['type'] != 'message':
                continue

            log.info('CHANNEL %s < DISPATCHER MESSAGE %s' % (self, msg))
            self._send_ws(json.loads(msg['data']))
    def dispatcher_routine(self):
        """This listens dispatcher redis channel and send data through channel
        """
        sub = RedisConn.pubsub()
        log.info('subscribing to %s' % self.subscribe_name)
        sub.subscribe(self.subscribe_name)
        for msg in sub.listen():
            if not 'type' in msg:
                continue

            if msg['type'] != 'message':
                continue

            log.info('CHANNEL %s < DISPATCHER MESSAGE %s' % (self, msg))
            self._send_ws(json.loads(msg['data']))
Esempio n. 9
0
 def get(cls, id, storage=None, salt=None):
     "Get object from Redis storage by ID"
     if salt:
         idtoget = hashlib.md5(id + salt).hexdigest()
     else:
         idtoget = id
     # First try to find object by Id
     # example: gameserver:scratchgames:101
     inst_data = RedisConn.get(mkey(REDIS_NAMESPACE,
                                    cls.get_collection_name(), idtoget))
     if not inst_data:  # No objects with such ID
         raise DoesNotExist('No model in Redis srorage with such id')
     else:
         # Copy structure of Class to new dictionary
         instance_dict = json.loads(inst_data.__str__())
         return cls(valuedict=instance_dict)
Esempio n. 10
0
 def _listener():
     pb = RedisConn.pubsub()
     _pubsub = pb.subscribe(channel_name)
     if on_spawn:
         on_spawn(_pubsub)
     for msg in pb.listen():
         try:
             if termination and termination(channel_name):
                 # it could be unsubscribe here
                 return
             if msg['type'] == 'message':
                 SESSION.begin()
                 action(msg, channel_name)
         finally:
             gevent.sleep(0)
             SESSION.close()
Esempio n. 11
0
 def _listener():
     pb = RedisConn.pubsub()
     _pubsub = pb.subscribe(channel_name)
     if on_spawn:
         on_spawn(_pubsub)
     for msg in pb.listen():
         try:
             if termination and termination(channel_name):
                 # it could be unsubscribe here
                 return
             if msg['type'] == 'message':
                 SESSION.begin()
                 action(msg, channel_name)
         finally:
             gevent.sleep(0)
             SESSION.close()
Esempio n. 12
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. 13
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. 14
0
 def __init__(self):
     self.id = RedisConn.incr(self.events_id_key)
     self.callback = None
Esempio n. 15
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. 16
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. 17
0
 def __init__(self):
     self.id = RedisConn.incr(self.events_id_key)
     self.callback = None
Esempio n. 18
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. 19
0
 def delete(cls, id, storage=None):  # storage=None for backword capability
     "Delete key specified by ``id``"
     result = RedisConn.delete(mkey(REDIS_NAMESPACE,
                                    cls.get_collection_name(), id))
     return result
Esempio n. 20
0
 def exists(cls, id, storage=None):  # storage=None for backword capability
     return RedisConn.exists(mkey(REDIS_NAMESPACE,
                                  cls.get_collection_name(), id))
Esempio n. 21
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'])