<s> """<STR_LIT>""" <EOL> import warnings <EOL> from twisted . python import log <EOL> from twisted . application import service <EOL> from twisted . internet import task <EOL> from twisted . internet . defer import CancelledError <EOL> def _maybeGlobalReactor ( maybeReactor ) : <EOL> """<STR_LIT>""" <EOL> if maybeReactor is None : <EOL> from twisted . internet import reactor <EOL> return reactor <EOL> else : <EOL> return maybeReactor <EOL> class _VolatileDataService ( service . Service ) : <EOL> volatile = [ ] <EOL> def __getstate__ ( self ) : <EOL> d = service . Service . __getstate__ ( self ) <EOL> for attr in self . volatile : <EOL> if attr in d : <EOL> del d [ attr ] <EOL> return d <EOL> class _AbstractServer ( _VolatileDataService ) : <EOL> """<STR_LIT>""" <EOL> volatile = [ '<STR_LIT>' ] <EOL> method = None <EOL> reactor = None <EOL> _port = None <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . args = args <EOL> if '<STR_LIT>' in kwargs : <EOL> self . reactor = kwargs . pop ( "<STR_LIT>" ) <EOL> self . kwargs = kwargs <EOL> def privilegedStartService ( self ) : <EOL> service . Service . privilegedStartService ( self ) <EOL> self . _port = self . _getPort ( ) <EOL> def startService ( self ) : <EOL> service . Service . startService ( self ) <EOL> if self . _port is None : <EOL> self . _port = self . _getPort ( ) <EOL> def stopService ( self ) : <EOL> service . Service . stopService ( self ) <EOL> if self . _port is not None : <EOL> d = self . _port . stopListening ( ) <EOL> del self . _port <EOL> return d <EOL> def _getPort ( self ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( _maybeGlobalReactor ( self . reactor ) , <EOL> '<STR_LIT>' % ( self . method , ) ) ( * self . args , ** self . kwargs ) <EOL> class _AbstractClient ( _VolatileDataService ) : <EOL> """<STR_LIT>""" <EOL> volatile = [ '<STR_LIT>' ] <EOL> method = None <EOL> reactor = None <EOL> _connection = None <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . args = args <EOL> if '<STR_LIT>' in kwargs : <EOL> self . reactor = kwargs . pop ( "<STR_LIT>" ) <EOL> self . kwargs = kwargs <EOL> def startService ( self ) : <EOL> service . Service . startService ( self ) <EOL> self . _connection = self . _getConnection ( ) <EOL> def stopService ( self ) : <EOL> service . Service . stopService ( self ) <EOL> if self . _connection is not None : <EOL> self . _connection . disconnect ( ) <EOL> del self . _connection <EOL> def _getConnection ( self ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( _maybeGlobalReactor ( self . reactor ) , <EOL> '<STR_LIT>' % ( self . method , ) ) ( * self . args , ** self . kwargs ) <EOL> _doc = { <EOL> '<STR_LIT>' : <EOL> """<STR_LIT>""" , <EOL> '<STR_LIT>' : <EOL> """<STR_LIT>""" , <EOL> } <EOL> import types <EOL> for tran in '<STR_LIT>' . split ( ) : <EOL> for side in '<STR_LIT>' . split ( ) : <EOL> if tran == "<STR_LIT>" and side == "<STR_LIT>" : <EOL> continue <EOL> base = globals ( ) [ '<STR_LIT>' + side ] <EOL> method = { '<STR_LIT>' : '<STR_LIT>' } . get ( tran , tran ) <EOL> doc = _doc [ side ] % vars ( ) <EOL> klass = types . ClassType ( tran + side , ( base , ) , <EOL> { '<STR_LIT>' : method , '<STR_LIT>' : doc } ) <EOL> globals ( ) [ tran + side ] = klass <EOL> class GenericServer ( _AbstractServer ) : <EOL> """<STR_LIT>""" <EOL> method = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> warnings . warn ( <EOL> '<STR_LIT>' , <EOL> category = DeprecationWarning , <EOL> stacklevel = <NUM_LIT:2> ) <EOL> _AbstractServer . __init__ ( self , * args , ** kwargs ) <EOL> class GenericClient ( _AbstractClient ) : <EOL> """<STR_LIT>""" <EOL> method = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> warnings . warn ( <EOL> '<STR_LIT>' , <EOL> category = DeprecationWarning , <EOL> stacklevel = <NUM_LIT:2> ) <EOL> _AbstractClient . __init__ ( self , * args , ** kwargs ) <EOL> class TimerService ( _VolatileDataService ) : <EOL> """<STR_LIT>""" <EOL> volatile = [ '<STR_LIT>' ] <EOL> def __init__ ( self , step , callable , * args , ** kwargs ) : <EOL> self . step = step <EOL> self . call = ( callable , args , kwargs ) <EOL> def startService ( self ) : <EOL> service . Service . startService ( self ) <EOL> callable , args , kwargs = self . call <EOL> self . _loop = task . LoopingCall ( callable , * args , ** kwargs ) <EOL> self . _loop . start ( self . step , now = True ) . addErrback ( self . _failed ) <EOL> def _failed ( self , why ) : <EOL> self . _loop . running = False <EOL> log . err ( why ) <EOL> def stopService ( self ) : <EOL> if self . _loop . running : <EOL> self . _loop . stop ( ) <EOL> return service . Service . stopService ( self ) <EOL> class CooperatorService ( service . Service ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . coop = task . Cooperator ( started = False ) <EOL> def coiterate ( self , iterator ) : <EOL> return self . coop . coiterate ( iterator ) <EOL> def startService ( self ) : <EOL> self . coop . start ( ) <EOL> def stopService ( self ) : <EOL> self . coop . stop ( ) <EOL> class StreamServerEndpointService ( service . Service , object ) : <EOL> """<STR_LIT>""" <EOL> _raiseSynchronously = None <EOL> def __init__ ( self , endpoint , factory ) : <EOL> self . endpoint = endpoint <EOL> self . factory = factory <EOL> self . _waitingForPort = None <EOL> def privilegedStartService ( self ) : <EOL> """<STR_LIT>""" <EOL> service . Service . privilegedStartService ( self ) <EOL> self . _waitingForPort = self . endpoint . listen ( self . factory ) <EOL> raisedNow = [ ] <EOL> def handleIt ( err ) : <EOL> if self . _raiseSynchronously : <EOL> raisedNow . append ( err ) <EOL> elif not err . check ( CancelledError ) : <EOL> log . err ( err ) <EOL> self . _waitingForPort . addErrback ( handleIt ) <EOL> if raisedNow : <EOL> raisedNow [ <NUM_LIT:0> ] . raiseException ( ) <EOL> def startService ( self ) : <EOL> """<STR_LIT>""" <EOL> service . Service . startService ( self ) <EOL> if self . _waitingForPort is None : <EOL> self . privilegedStartService ( ) <EOL> def stopService ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _waitingForPort . cancel ( ) <EOL> def stopIt ( port ) : <EOL> if port is not None : <EOL> return port . stopListening ( ) <EOL> d = self . _waitingForPort . addCallback ( stopIt ) <EOL> def stop ( passthrough ) : <EOL> self . running = False <EOL> return passthrough <EOL> d . addBoth ( stop ) <EOL> return d <EOL> __all__ = ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] + <EOL> [ tran + side <EOL> for tran in '<STR_LIT>' . split ( ) <EOL> for side in '<STR_LIT>' . split ( ) ] ) </s>
