<s> from twisted . python import log <EOL> from twisted . python . failure import Failure <EOL> from twisted . internet . defer import fail <EOL> from twisted . internet . protocol import ServerFactory , ClientCreator <EOL> from twisted . spread . pb import Referenceable , Error , DeadReferenceError , PBConnectionLost <EOL> from rce . slave . protocol import Loopback , RCEInternalProtocol <EOL> class ConnectionError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class Endpoint ( Referenceable ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , reactor , loader , commPort ) : <EOL> """<STR_LIT>""" <EOL> self . _avatar = None <EOL> self . _reactor = reactor <EOL> self . _loader = loader <EOL> reactor . listenTCP ( commPort , _RCEInternalServerFactory ( self ) ) <EOL> self . _namespaces = set ( ) <EOL> self . _loopback = None <EOL> self . _pendingConnections = { } <EOL> self . _protocols = set ( ) <EOL> @ property <EOL> def reactor ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _reactor <EOL> @ property <EOL> def loader ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _loader <EOL> def registerAvatar ( self , avatar ) : <EOL> """<STR_LIT>""" <EOL> assert self . _avatar is None <EOL> self . _avatar = avatar <EOL> def registerNamespace ( self , namespace ) : <EOL> assert namespace not in self . _namespaces <EOL> self . _namespaces . add ( namespace ) <EOL> def unregisterNamespace ( self , namespace ) : <EOL> assert namespace in self . _namespaces <EOL> self . _namespaces . remove ( namespace ) <EOL> self . referenceDied ( '<STR_LIT>' , namespace ) <EOL> def remote_getLoopback ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _loopback : <EOL> self . _loopback = Loopback ( self ) <EOL> return self . _loopback <EOL> def remote_prepareConnection ( self , connID , key , auth ) : <EOL> """<STR_LIT>""" <EOL> assert connID not in self . _pendingConnections <EOL> self . _pendingConnections [ connID ] = [ key , auth ] <EOL> def remote_connect ( self , connID , addr ) : <EOL> """<STR_LIT>""" <EOL> assert connID in self . _pendingConnections <EOL> info = self . _pendingConnections [ connID ] <EOL> key , auth = info <EOL> info [ <NUM_LIT:0> ] = None <EOL> client = ClientCreator ( self . _reactor , RCEInternalProtocol , self ) <EOL> d = client . connectTCP ( * addr ) <EOL> d . addCallback ( lambda p : p . sendInit ( connID , key ) ) <EOL> d . addErrback ( self . _connectError , auth ) <EOL> def _connectError ( self , failure , auth ) : <EOL> failure . printTraceback ( ) <EOL> def processInit ( self , protocol , connID , remoteKey ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> key , auth = self . _pendingConnections [ connID ] <EOL> except KeyError : <EOL> return fail ( Failure ( ConnectionError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) ) <EOL> try : <EOL> if key : <EOL> protocol . sendInit ( connID , key ) <EOL> except Exception as e : <EOL> failure = Failure ( e ) <EOL> return fail ( failure ) <EOL> else : <EOL> return auth . callRemote ( '<STR_LIT>' , remoteKey , protocol ) <EOL> raise RuntimeError <EOL> def registerProtocol ( self , protocol ) : <EOL> assert protocol not in self . _protocols <EOL> self . _protocols . add ( protocol ) <EOL> def unregisterProtocol ( self , protocol ) : <EOL> assert protocol in self . _protocols <EOL> self . _protocols . remove ( protocol ) <EOL> self . referenceDied ( '<STR_LIT>' , protocol ) <EOL> def referenceDied ( self , method , reference ) : <EOL> """<STR_LIT>""" <EOL> def eb ( failure ) : <EOL> if not failure . check ( PBConnectionLost ) : <EOL> log . err ( failure ) <EOL> try : <EOL> self . _avatar . callRemote ( method , reference ) . addErrback ( eb ) <EOL> except ( DeadReferenceError , PBConnectionLost ) : <EOL> pass <EOL> def terminate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _pendingConnections = { } <EOL> for protocol in self . _protocols . copy ( ) : <EOL> protocol . remote_destroy ( ) <EOL> if self . _loopback : <EOL> self . _loopback . remote_destroy ( ) <EOL> self . _loopback = None <EOL> for namespace in self . _namespaces . copy ( ) : <EOL> namespace . remote_destroy ( ) <EOL> assert len ( self . _namespaces ) == <NUM_LIT:0> <EOL> self . _factory = None <EOL> class _RCEInternalServerFactory ( ServerFactory ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , endpoint ) : <EOL> self . _endpoint = endpoint <EOL> def buildProtocol ( self , addr ) : <EOL> return RCEInternalProtocol ( self . _endpoint ) </s>
