<s> from tornado . web import * <EOL> import json <EOL> from invocation import * <EOL> from exceptions import * <EOL> from tornado . options import define , options <EOL> from events import EventManager <EOL> from tornado . websocket import WebSocketHandler <EOL> import logging <EOL> class TotoSocketHandler ( WebSocketHandler ) : <EOL> @ classmethod <EOL> def configure ( cls ) : <EOL> if options . debug : <EOL> import traceback <EOL> def log_error ( self , e ) : <EOL> logging . error ( '<STR_LIT>' % ( traceback . format_exc ( ) , repr ( self . request . headers ) ) ) <EOL> cls . log_error = log_error <EOL> open_function = options . socket_opened_method and options . socket_opened_method . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> cls . _on_open = open_function and getattr ( __import__ ( open_function [ <NUM_LIT:0> ] ) , open_function [ <NUM_LIT:1> ] ) or None <EOL> closed_function = options . socket_closed_method and options . socket_closed_method . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> cls . _on_close = closed_function and getattr ( __import__ ( closed_function [ <NUM_LIT:0> ] ) , closed_function [ <NUM_LIT:1> ] ) or None <EOL> cls . __method = __import__ ( options . socket_method_module ) <EOL> def initialize ( self , db_connection ) : <EOL> self . db_connection = db_connection <EOL> self . db = self . db_connection . db <EOL> self . session = None <EOL> self . registered_event_handlers = [ ] <EOL> def log_error ( self , e ) : <EOL> if isinstance ( exception , TotoException ) : <EOL> logging . error ( "<STR_LIT>" % ( e . code , e . value ) ) <EOL> else : <EOL> logging . error ( "<STR_LIT>" % ( ERROR_SERVER , repr ( e ) ) ) <EOL> def create_session ( self , user_id = None , password = None ) : <EOL> self . session = self . db_connection . create_session ( user_id , password ) <EOL> return self . session <EOL> def retrieve_session ( self , session_id ) : <EOL> self . session = self . db_connection . retrieve_session ( session_id , None , None ) <EOL> return self . session <EOL> def open ( self , session_id = None ) : <EOL> if session_id : <EOL> self . retrieve_session ( session_id ) <EOL> if ( self . _on_open ) : <EOL> self . _on_open ( ) <EOL> def on_message ( self , message_data ) : <EOL> method = self . __method <EOL> try : <EOL> message = json . loads ( message_data ) <EOL> for i in message [ '<STR_LIT>' ] . split ( '<STR_LIT:.>' ) : <EOL> method = getattr ( method , i ) <EOL> method . invoke ( self , message [ '<STR_LIT>' ] ) <EOL> except Exception as e : <EOL> self . log_error ( e ) <EOL> def send_message ( self , data , message_id = None ) : <EOL> self . write_message ( message_id and { '<STR_LIT>' : message_id , '<STR_LIT:data>' : data } or data ) <EOL> def register_event_handler ( self , event_name , handler , run_on_main_loop = True , deregister_on_finish = False ) : <EOL> sig = EventManager . instance ( ) . register_handler ( event_name , handler , run_on_main_loop , self ) <EOL> if deregister_on_finish : <EOL> self . registered_event_handlers . append ( sig ) <EOL> return sig <EOL> def deregister_event_handler ( self , sig ) : <EOL> EventManager . instance ( ) . remove_handler ( sig ) <EOL> self . registered_event_handlers . remove ( sig ) <EOL> def on_close ( self ) : <EOL> while self . registered_event_handlers : <EOL> self . deregister_event_handler ( self . registered_event_handlers [ <NUM_LIT:0> ] ) <EOL> if ( self . _on_close ) : <EOL> self . _on_close ( ) </s>
