<s> """<STR_LIT>""" <EOL> import logging <EOL> import time <EOL> import traceback <EOL> import tornado <EOL> from tornado . web import HTTPError <EOL> from tornado import stack_context <EOL> from tornado . websocket import WebSocketHandler <EOL> from tornadio2 import proto <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class TornadioWebSocketHandler ( WebSocketHandler ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> def initialize ( self , server ) : <EOL> self . server = server <EOL> self . session = None <EOL> self . _is_active = not self . server . settings [ '<STR_LIT>' ] <EOL> self . _global_heartbeats = self . server . settings [ '<STR_LIT>' ] <EOL> logger . debug ( '<STR_LIT>' % self . name ) <EOL> def _execute ( self , transforms , * args , ** kwargs ) : <EOL> with stack_context . ExceptionStackContext ( self . _handle_websocket_exception ) : <EOL> if self . request . method != '<STR_LIT:GET>' : <EOL> self . stream . write ( tornado . escape . utf8 ( <EOL> "<STR_LIT>" <EOL> ) ) <EOL> self . stream . close ( ) <EOL> return <EOL> if self . request . headers . get ( "<STR_LIT>" , "<STR_LIT>" ) . lower ( ) != '<STR_LIT>' : <EOL> self . stream . write ( tornado . escape . utf8 ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) ) <EOL> self . stream . close ( ) <EOL> return <EOL> if self . request . headers . get ( "<STR_LIT>" , "<STR_LIT>" ) . lower ( ) . find ( '<STR_LIT>' ) == - <NUM_LIT:1> : <EOL> self . stream . write ( tornado . escape . utf8 ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) ) <EOL> self . stream . close ( ) <EOL> return <EOL> super ( TornadioWebSocketHandler , self ) . _execute ( transforms , * args , ** kwargs ) <EOL> def open ( self , session_id ) : <EOL> """<STR_LIT>""" <EOL> self . session = self . server . get_session ( session_id ) <EOL> if self . session is None : <EOL> raise HTTPError ( <NUM_LIT> , "<STR_LIT>" ) <EOL> if not self . _is_active : <EOL> self . write_message ( proto . heartbeat ( ) ) <EOL> self . server . io_loop . add_timeout ( time . time ( ) + self . server . settings [ '<STR_LIT>' ] , <EOL> self . _connection_check ) <EOL> else : <EOL> self . session . set_handler ( self ) <EOL> self . session . reset_heartbeat ( ) <EOL> self . session . flush ( ) <EOL> def _connection_check ( self ) : <EOL> if not self . _is_active : <EOL> self . _detach ( ) <EOL> try : <EOL> self . close ( ) <EOL> except : <EOL> pass <EOL> def _detach ( self ) : <EOL> if self . session is not None : <EOL> if self . _is_active : <EOL> self . session . stop_heartbeat ( ) <EOL> self . session . remove_handler ( self ) <EOL> self . session = None <EOL> def on_message ( self , message ) : <EOL> self . server . stats . on_packet_recv ( <NUM_LIT:1> ) <EOL> if not self . session : <EOL> return <EOL> if not self . _is_active : <EOL> self . session . set_handler ( self ) <EOL> self . session . reset_heartbeat ( ) <EOL> self . session . flush ( ) <EOL> self . _is_active = True <EOL> if not self . _global_heartbeats : <EOL> self . session . delay_heartbeat ( ) <EOL> try : <EOL> self . session . raw_message ( message ) <EOL> except Exception , ex : <EOL> logger . error ( '<STR_LIT>' + traceback . format_exc ( ex ) ) <EOL> if self . session is not None : <EOL> self . session . close ( ) <EOL> def on_close ( self ) : <EOL> self . _detach ( ) <EOL> def send_messages ( self , messages ) : <EOL> self . server . stats . on_packet_sent ( len ( messages ) ) <EOL> try : <EOL> for m in messages : <EOL> self . write_message ( m ) <EOL> except IOError : <EOL> if self . ws_connection and self . ws_connection . client_terminated : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> self . _detach ( ) <EOL> def session_closed ( self ) : <EOL> try : <EOL> self . close ( ) <EOL> except Exception : <EOL> logger . debug ( '<STR_LIT>' , exc_info = True ) <EOL> finally : <EOL> self . _detach ( ) <EOL> def _handle_websocket_exception ( self , type , value , traceback ) : <EOL> if type is IOError : <EOL> self . server . io_loop . add_callback ( self . on_connection_close ) <EOL> logger . debug ( '<STR_LIT>' , exc_info = ( type , value , traceback ) ) <EOL> return True <EOL> def allow_draft76 ( self ) : <EOL> return True <EOL> class TornadioFlashSocketHandler ( TornadioWebSocketHandler ) : <EOL> name = '<STR_LIT>' </s>
