<s> import json <EOL> import logging <EOL> import time <EOL> import datetime <EOL> import hashlib <EOL> import random <EOL> from django_sockjs_server . lib . token import Token <EOL> from django_sockjs_server . lib . redis_client import redis_client <EOL> class Subscribe ( object ) : <EOL> def __init__ ( self , connection ) : <EOL> u'''<STR_LIT>''' <EOL> self . conn = connection <EOL> self . logger = logging . getLogger ( __name__ ) <EOL> self . redis = redis_client <EOL> def get_host ( self ) : <EOL> return self . conn . sockjs_server . queue <EOL> def _compat_transform ( self , json_obj ) : <EOL> data = json_obj [ '<STR_LIT:data>' ] <EOL> if '<STR_LIT>' not in data and '<STR_LIT>' in data : <EOL> data [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> def add ( self , data ) : <EOL> try : <EOL> json_obj = json . loads ( data ) <EOL> token = Token ( ) <EOL> self . _compat_transform ( json_obj ) <EOL> if token . get_data ( json_obj [ '<STR_LIT>' ] , json_obj [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] ) : <EOL> host = self . get_host ( ) <EOL> room = json_obj [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] <EOL> uid = self . conn . id <EOL> self . conn . sockjs_server . add_subscriber_room ( <EOL> room , self . conn <EOL> ) <EOL> self . logger . debug ( <EOL> '<STR_LIT>' % room <EOL> ) <EOL> self . redis . lpush ( <EOL> room , <EOL> json . dumps ( { '<STR_LIT:host>' : host , '<STR_LIT:id>' : uid } ) <EOL> ) <EOL> except ( KeyError , TypeError ) : <EOL> pass <EOL> def remove ( self ) : <EOL> host = self . get_host ( ) <EOL> uid = self . conn . id <EOL> for room in self . conn . sockjs_server . subscription_dict [ uid ] : <EOL> self . redis . lrem ( <EOL> room , <EOL> <NUM_LIT:0> , <EOL> json . dumps ( { '<STR_LIT:id>' : uid , '<STR_LIT:host>' : host } ) <EOL> ) <EOL> self . conn . sockjs_server . remove_subscriber ( uid ) <EOL> self . logger . debug ( <EOL> '<STR_LIT>' % uid <EOL> ) </s>
