<s> from twisted . internet import task , reactor <EOL> from sockjs . cyclone import transports <EOL> from sockjs . cyclone import session <EOL> from sockjs . cyclone import sessioncontainer <EOL> from sockjs . cyclone import proto <EOL> from sockjs . cyclone import static <EOL> from sockjs . cyclone import stats <EOL> DEFAULT_SETTINGS = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:5> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> * <NUM_LIT> , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True <EOL> } <EOL> GLOBAL_HANDLERS = [ <EOL> ( '<STR_LIT>' , transports . XhrSendHandler ) , <EOL> ( '<STR_LIT>' , transports . JSONPSendHandler ) <EOL> ] <EOL> TRANSPORTS = { <EOL> '<STR_LIT>' : transports . WebSocketTransport , <EOL> '<STR_LIT>' : transports . XhrPollingTransport , <EOL> '<STR_LIT>' : transports . XhrStreamingTransport , <EOL> '<STR_LIT>' : transports . JSONPTransport , <EOL> '<STR_LIT>' : transports . EventSourceTransport , <EOL> '<STR_LIT>' : transports . HtmlFileTransport <EOL> } <EOL> STATIC_HANDLERS = { <EOL> '<STR_LIT>' : static . ChunkingTestHandler , <EOL> '<STR_LIT>' : static . InfoHandler , <EOL> '<STR_LIT>' : static . IFrameHandler , <EOL> '<STR_LIT>' : transports . RawWebSocketTransport , <EOL> '<STR_LIT>' : static . GreetingsHandler <EOL> } <EOL> class SockJSRouter ( object ) : <EOL> def __init__ ( self , connection , prefix = '<STR_LIT>' , user_settings = dict ( ) ) : <EOL> self . _connection = connection <EOL> self . settings = DEFAULT_SETTINGS . copy ( ) <EOL> if user_settings : <EOL> self . settings . update ( user_settings ) <EOL> disabled_transports = self . settings [ '<STR_LIT>' ] <EOL> self . websockets_enabled = '<STR_LIT>' not in disabled_transports <EOL> self . cookie_needed = self . settings [ '<STR_LIT>' ] <EOL> self . _initialize_sessions ( ) <EOL> self . _initialize_stats ( ) <EOL> self . _initialize_urls ( prefix ) <EOL> def _initialize_urls ( self , prefix ) : <EOL> base = prefix + r'<STR_LIT>' <EOL> kwargs = dict ( server = self ) <EOL> self . _transport_urls = [ ] <EOL> for handler in GLOBAL_HANDLERS : <EOL> url = '<STR_LIT>' % ( base , handler [ <NUM_LIT:0> ] ) <EOL> reqhandler = handler [ <NUM_LIT:1> ] <EOL> self . _transport_urls . append ( ( url , reqhandler , kwargs ) ) <EOL> for name , transport in TRANSPORTS . iteritems ( ) : <EOL> if name in self . settings [ '<STR_LIT>' ] : <EOL> continue <EOL> url = r'<STR_LIT>' % ( base , name ) <EOL> self . _transport_urls . append ( ( url , transport , kwargs ) ) <EOL> for name , handler in STATIC_HANDLERS . iteritems ( ) : <EOL> url = '<STR_LIT>' % ( prefix , name ) <EOL> self . _transport_urls . append ( ( url , handler , kwargs ) ) <EOL> def _initialize_sessions ( self ) : <EOL> self . _sessions = sessioncontainer . SessionContainer ( ) <EOL> check_interval = self . settings [ '<STR_LIT>' ] <EOL> self . _sessions_cleanup = task . LoopingCall ( self . _sessions . expire ) <EOL> self . _sessions_cleanup . start ( check_interval ) <EOL> reactor . addSystemEventTrigger ( '<STR_LIT>' , '<STR_LIT>' , <EOL> self . _sessions_cleanup . stop ) <EOL> def _initialize_stats ( self ) : <EOL> self . stats = stats . StatsCollector ( ) <EOL> def get_connection_class ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _connection <EOL> @ property <EOL> def urls ( self ) : <EOL> return self . _transport_urls <EOL> def apply_routes ( self , routes ) : <EOL> """<STR_LIT>""" <EOL> routes . extend ( self . _transport_urls ) <EOL> return routes <EOL> def create_session ( self , session_id , register = True , session_factory = None ) : <EOL> """<STR_LIT>""" <EOL> if session_factory is not None : <EOL> sess_factory , sess_args , sess_kwargs = session_factory <EOL> s = sess_factory ( * sess_args , ** sess_kwargs ) <EOL> else : <EOL> s = session . Session ( self . _connection , self , session_id , <EOL> self . settings . get ( '<STR_LIT>' ) ) <EOL> if register : <EOL> self . _sessions . add ( s ) <EOL> return s <EOL> def get_session ( self , session_id ) : <EOL> """<STR_LIT>""" <EOL> return self . _sessions . get ( session_id ) <EOL> def broadcast ( self , clients , msg ) : <EOL> """<STR_LIT>""" <EOL> json_msg = None <EOL> count = <NUM_LIT:0> <EOL> for c in clients : <EOL> sess = c . session <EOL> if not sess . is_closed : <EOL> if sess . send_expects_json : <EOL> if json_msg is None : <EOL> json_msg = proto . json_encode ( msg ) <EOL> sess . send_jsonified ( json_msg , stats = False ) <EOL> else : <EOL> sess . send_message ( msg , stats = False ) <EOL> count += <NUM_LIT:1> <EOL> self . stats . packSent ( count ) </s>
