<s> """<STR_LIT>""" <EOL> import json <EOL> import time <EOL> from ws4py . client import tornadoclient <EOL> import tornado <EOL> import pyalgotrade . logger <EOL> logger = pyalgotrade . logger . getLogger ( "<STR_LIT>" ) <EOL> class KeepAliveMgr ( object ) : <EOL> def __init__ ( self , wsClient , maxInactivity , responseTimeout ) : <EOL> assert ( maxInactivity > <NUM_LIT:0> ) <EOL> assert ( responseTimeout > <NUM_LIT:0> ) <EOL> self . __callback = None <EOL> self . __wsClient = wsClient <EOL> self . __activityTimeout = maxInactivity <EOL> self . __responseTimeout = responseTimeout <EOL> self . __lastSeen = None <EOL> self . __kaSent = None <EOL> def _keepAlive ( self ) : <EOL> if self . __lastSeen is None : <EOL> return <EOL> inactivity = ( time . time ( ) - self . __lastSeen ) <EOL> if inactivity <= self . __activityTimeout : <EOL> return <EOL> try : <EOL> if self . __kaSent is None : <EOL> self . sendKeepAlive ( ) <EOL> self . __kaSent = time . time ( ) <EOL> elif ( time . time ( ) - self . __kaSent ) > self . __responseTimeout : <EOL> self . __wsClient . onDisconnectionDetected ( ) <EOL> except Exception : <EOL> self . __wsClient . onDisconnectionDetected ( ) <EOL> def getWSClient ( self ) : <EOL> return self . __wsClient <EOL> def setAlive ( self ) : <EOL> self . __lastSeen = time . time ( ) <EOL> self . __kaSent = None <EOL> def start ( self ) : <EOL> self . __callback = tornado . ioloop . PeriodicCallback ( self . _keepAlive , <NUM_LIT:1000> , self . __wsClient . getIOLoop ( ) ) <EOL> self . __callback . start ( ) <EOL> def stop ( self ) : <EOL> if self . __callback is not None : <EOL> self . __callback . stop ( ) <EOL> def sendKeepAlive ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def handleResponse ( self , msg ) : <EOL> raise NotImplementedError ( ) <EOL> class WebSocketClientBase ( tornadoclient . TornadoWebSocketClient ) : <EOL> def __init__ ( self , url ) : <EOL> super ( WebSocketClientBase , self ) . __init__ ( url ) <EOL> self . __keepAliveMgr = None <EOL> self . __connected = False <EOL> def _cleanup ( self ) : <EOL> ret = None <EOL> try : <EOL> ret = super ( WebSocketClientBase , self ) . _cleanup ( ) <EOL> except Exception : <EOL> pass <EOL> return ret <EOL> def getIOLoop ( self ) : <EOL> return tornado . ioloop . IOLoop . instance ( ) <EOL> def setKeepAliveMgr ( self , keepAliveMgr ) : <EOL> if self . __keepAliveMgr is not None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . __keepAliveMgr = keepAliveMgr <EOL> def received_message ( self , message ) : <EOL> try : <EOL> msg = json . loads ( message . data ) <EOL> if self . __keepAliveMgr is not None : <EOL> self . __keepAliveMgr . setAlive ( ) <EOL> if self . __keepAliveMgr . handleResponse ( msg ) : <EOL> return <EOL> self . onMessage ( msg ) <EOL> except Exception , e : <EOL> self . onUnhandledException ( e ) <EOL> def opened ( self ) : <EOL> self . __connected = True <EOL> if self . __keepAliveMgr is not None : <EOL> self . __keepAliveMgr . start ( ) <EOL> self . __keepAliveMgr . setAlive ( ) <EOL> self . onOpened ( ) <EOL> def closed ( self , code , reason = None ) : <EOL> wasConnected = self . __connected <EOL> self . __connected = False <EOL> if self . __keepAliveMgr : <EOL> self . __keepAliveMgr . stop ( ) <EOL> self . __keepAliveMgr = None <EOL> tornado . ioloop . IOLoop . instance ( ) . stop ( ) <EOL> if wasConnected : <EOL> self . onClosed ( code , reason ) <EOL> def isConnected ( self ) : <EOL> return self . __connected <EOL> def startClient ( self ) : <EOL> tornado . ioloop . IOLoop . instance ( ) . start ( ) <EOL> def stopClient ( self ) : <EOL> try : <EOL> if self . __connected : <EOL> self . close ( ) <EOL> self . close_connection ( ) <EOL> except Exception , e : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> def onUnhandledException ( self , exception ) : <EOL> logger . critical ( "<STR_LIT>" , exc_info = exception ) <EOL> raise <EOL> def onOpened ( self ) : <EOL> pass <EOL> def onMessage ( self , msg ) : <EOL> raise NotImplementedError ( ) <EOL> def onClosed ( self , code , reason ) : <EOL> pass <EOL> def onDisconnectionDetected ( self ) : <EOL> pass </s>
