<s> """<STR_LIT>""" <EOL> import datetime <EOL> import twcommon . misc <EOL> import tweblib . handlers <EOL> class ConnectionTable ( object ) : <EOL> def __init__ ( self , app ) : <EOL> self . app = app <EOL> self . table = { } <EOL> self . counter = <NUM_LIT:1> <EOL> def generate_connid ( self ) : <EOL> """<STR_LIT>""" <EOL> res = self . counter <EOL> self . counter += <NUM_LIT:1> <EOL> return res <EOL> def all ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . table . values ( ) ) <EOL> def as_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( self . table ) <EOL> def count ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . table ) <EOL> def for_uid ( self , uid ) : <EOL> """<STR_LIT>""" <EOL> return [ conn for conn in self . table . values ( ) if conn . uid == uid ] <EOL> def add ( self , handler , uid , email , session ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( handler , tweblib . handlers . PlayWebSocketHandler ) <EOL> assert handler . twconnid , '<STR_LIT>' <EOL> conn = Connection ( handler , uid , email , session [ '<STR_LIT>' ] , <EOL> refreshtime = session [ '<STR_LIT>' ] , <EOL> guest = session . get ( '<STR_LIT>' , False ) ) <EOL> self . table [ conn . connid ] = conn <EOL> return conn <EOL> def find ( self , connid ) : <EOL> """<STR_LIT>""" <EOL> return self . table [ connid ] <EOL> def remove ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> if not handler . twconnid : <EOL> return <EOL> conn = self . table . get ( handler . twconnid , None ) <EOL> if not conn : <EOL> return <EOL> assert handler . twconnid == conn . connid , '<STR_LIT>' <EOL> conn . handler = None <EOL> conn . uid = None <EOL> conn . available = False <EOL> del self . table [ handler . twconnid ] <EOL> class Connection ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , handler , uid , email , sessionid , <EOL> refreshtime , guest = False ) : <EOL> self . handler = handler <EOL> self . connid = handler . twconnid <EOL> self . uid = uid <EOL> self . sessionid = sessionid <EOL> self . email = email <EOL> self . guest = guest <EOL> self . starttime = twcommon . misc . now ( ) <EOL> self . lastmsgtime = self . starttime <EOL> self . sessiontime = refreshtime <EOL> self . available = False <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . connid , ) <EOL> def uptime ( self ) : <EOL> """<STR_LIT>""" <EOL> delta = twcommon . misc . now ( ) - self . starttime <EOL> return datetime . timedelta ( seconds = int ( delta . total_seconds ( ) ) ) <EOL> def idletime ( self ) : <EOL> """<STR_LIT>""" <EOL> delta = twcommon . misc . now ( ) - self . lastmsgtime <EOL> return datetime . timedelta ( seconds = int ( delta . total_seconds ( ) ) ) <EOL> def close ( self , errmsg = None ) : <EOL> """<STR_LIT>""" <EOL> if self . handler : <EOL> if ( errmsg ) : <EOL> self . handler . write_tw_error ( errmsg ) <EOL> try : <EOL> self . handler . close ( ) <EOL> except : <EOL> pass <EOL> self . handler . on_close ( ) <EOL> def write_tw_error ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> if self . handler : <EOL> self . handler . write_tw_error ( msg ) </s>
