#NEWLINE# def monitor_tworld_status( self ) : #NEWLINE# #INDENT# "Check the status of the Tworld connection. If the socket is\n        closed (or has never been opened), try to open it.\n\n        This is called once when the app launches, to open the initial\n        connection, and every few seconds thereafter.\n\n        The tworldtimerbusy flag protects us from really slow connection\n        attempts.\n        \n        This routine is *not* a coroutine, because it doesn't do anything\n        yieldy. Instead, it has an old-fashioned (ugly) callback structure.\n        " #NEWLINE# if self . tworldtimerbusy : #NEWLINE# #INDENT# self . log . warning ( 'monitor_tworld_status: already in flight; did a previous call jam?' ) #NEWLINE# return #NEWLINE# #UNINDENT# if self . tworldavailable : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# self . tworldtimerbusy = True #NEWLINE# try : #NEWLINE# #INDENT# sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM , 0 ) #NEWLINE# sock . connect ( ( 'localhost' , self . app . twopts . tworld_port ) ) #NEWLINE# sock . setblocking ( 0 ) #NEWLINE# tornado . platform . auto . set_close_exec ( sock . fileno ( ) ) #NEWLINE# self . tworld = tornado . iostream . IOStream ( sock ) #NEWLINE# self . twbuffer = bytearray ( ) #NEWLINE# #UNINDENT# except Exception as ex : #NEWLINE# #INDENT# self . log . error ( 'Could not open tworld socket: %s' , ex ) #NEWLINE# self . tworldavailable = False #NEWLINE# self . tworldtimerbusy = False #NEWLINE# return #NEWLINE# #UNINDENT# self . log . info ( 'Tworld socket open' ) #NEWLINE# try : #NEWLINE# #INDENT# arr = [ ] #NEWLINE# for ( connid , conn ) in self . app . twconntable . as_dict ( ) . items ( ) : #NEWLINE# #INDENT# arr . append ( { #NEWLINE# #INDENT# 'connid' : connid , #NEWLINE# 'uid' : str ( conn . uid ) , #NEWLINE# 'email' : conn . email , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# self . tworld . write ( wcproto . message ( 0 , { #NEWLINE# #INDENT# 'cmd' : 'connect' , #NEWLINE# 'connections' : arr , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# #UNINDENT# except Exception as ex : #NEWLINE# #INDENT# self . log . error ( 'Could not write connect message to tworld socket: %s' , ex ) #NEWLINE# self . tworld = None #NEWLINE# self . twbuffer = None #NEWLINE# self . tworldavailable = False #NEWLINE# self . tworldtimerbusy = False #NEWLINE# return #NEWLINE# #UNINDENT# self . tworld . read_until_close ( self . close_tworld , self . read_tworld_data )