<s> """<STR_LIT>""" <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> import sys <EOL> from zope . interface import implements <EOL> from twisted . internet . interfaces import IReactorFDSet <EOL> from twisted . internet . posixbase import PosixReactorBase , _Waker <EOL> from twisted . internet . selectreactor import _NO_FILENO , _NO_FILEDESC <EOL> from twisted . python import log <EOL> from CoreFoundation import ( <EOL> CFRunLoopAddSource , CFRunLoopRemoveSource , CFRunLoopGetMain , CFRunLoopRun , <EOL> CFRunLoopStop , CFRunLoopTimerCreate , CFRunLoopAddTimer , <EOL> CFRunLoopTimerInvalidate , kCFAllocatorDefault , kCFRunLoopCommonModes , <EOL> CFAbsoluteTimeGetCurrent ) <EOL> from CFNetwork import ( <EOL> CFSocketCreateWithNative , CFSocketSetSocketFlags , CFSocketEnableCallBacks , <EOL> CFSocketCreateRunLoopSource , CFSocketDisableCallBacks , CFSocketInvalidate , <EOL> kCFSocketWriteCallBack , kCFSocketReadCallBack , kCFSocketConnectCallBack , <EOL> kCFSocketAutomaticallyReenableReadCallBack , <EOL> kCFSocketAutomaticallyReenableWriteCallBack ) <EOL> _READ = <NUM_LIT:0> <EOL> _WRITE = <NUM_LIT:1> <EOL> _preserveSOError = <NUM_LIT:1> << <NUM_LIT:6> <EOL> class _WakerPlus ( _Waker ) : <EOL> """<STR_LIT>""" <EOL> def doRead ( self ) : <EOL> """<STR_LIT>""" <EOL> result = _Waker . doRead ( self ) <EOL> self . reactor . _scheduleSimulate ( True ) <EOL> return result <EOL> class CFReactor ( PosixReactorBase ) : <EOL> """<STR_LIT>""" <EOL> implements ( IReactorFDSet ) <EOL> def __init__ ( self , runLoop = None , runner = None ) : <EOL> self . _fdmap = { } <EOL> self . _idmap = { } <EOL> if runner is None : <EOL> runner = CFRunLoopRun <EOL> self . _runner = runner <EOL> if runLoop is None : <EOL> runLoop = CFRunLoopGetMain ( ) <EOL> self . _cfrunloop = runLoop <EOL> PosixReactorBase . __init__ ( self ) <EOL> def installWaker ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . waker : <EOL> self . waker = _WakerPlus ( self ) <EOL> self . _internalReaders . add ( self . waker ) <EOL> self . addReader ( self . waker ) <EOL> def _socketCallback ( self , cfSocket , callbackType , <EOL> ignoredAddress , ignoredData , context ) : <EOL> """<STR_LIT>""" <EOL> ( fd , smugglesrc ) = context <EOL> if fd not in self . _fdmap : <EOL> CFRunLoopRemoveSource ( <EOL> self . _cfrunloop , smugglesrc , kCFRunLoopCommonModes <EOL> ) <EOL> return <EOL> try : <EOL> src , skt , readWriteDescriptor , rw = self . _fdmap [ fd ] <EOL> why = None <EOL> isRead = callbackType == kCFSocketReadCallBack <EOL> try : <EOL> if isRead : <EOL> if rw [ _READ ] : <EOL> why = readWriteDescriptor . doRead ( ) <EOL> else : <EOL> if rw [ _WRITE ] : <EOL> why = readWriteDescriptor . doWrite ( ) <EOL> except : <EOL> why = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> log . err ( ) <EOL> handfn = getattr ( readWriteDescriptor , '<STR_LIT>' , None ) <EOL> if handfn is None : <EOL> why = _NO_FILENO <EOL> elif handfn ( ) == - <NUM_LIT:1> : <EOL> why = _NO_FILEDESC <EOL> if why : <EOL> self . _disconnectSelectable ( readWriteDescriptor , why , isRead ) <EOL> except : <EOL> log . err ( ) <EOL> def _watchFD ( self , fd , descr , flag ) : <EOL> """<STR_LIT>""" <EOL> if fd == - <NUM_LIT:1> : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if fd in self . _fdmap : <EOL> src , cfs , gotdescr , rw = self . _fdmap [ fd ] <EOL> else : <EOL> ctx = [ ] <EOL> ctx . append ( fd ) <EOL> cfs = CFSocketCreateWithNative ( <EOL> kCFAllocatorDefault , fd , <EOL> kCFSocketReadCallBack | kCFSocketWriteCallBack | <EOL> kCFSocketConnectCallBack , <EOL> self . _socketCallback , ctx <EOL> ) <EOL> CFSocketSetSocketFlags ( <EOL> cfs , <EOL> kCFSocketAutomaticallyReenableReadCallBack | <EOL> kCFSocketAutomaticallyReenableWriteCallBack | <EOL> _preserveSOError <EOL> ) <EOL> src = CFSocketCreateRunLoopSource ( kCFAllocatorDefault , cfs , <NUM_LIT:0> ) <EOL> ctx . append ( src ) <EOL> CFRunLoopAddSource ( self . _cfrunloop , src , kCFRunLoopCommonModes ) <EOL> CFSocketDisableCallBacks ( <EOL> cfs , <EOL> kCFSocketReadCallBack | kCFSocketWriteCallBack | <EOL> kCFSocketConnectCallBack <EOL> ) <EOL> rw = [ False , False ] <EOL> self . _idmap [ id ( descr ) ] = fd <EOL> self . _fdmap [ fd ] = src , cfs , descr , rw <EOL> rw [ self . _flag2idx ( flag ) ] = True <EOL> CFSocketEnableCallBacks ( cfs , flag ) <EOL> def _flag2idx ( self , flag ) : <EOL> """<STR_LIT>""" <EOL> return { kCFSocketReadCallBack : _READ , <EOL> kCFSocketWriteCallBack : _WRITE } [ flag ] <EOL> def _unwatchFD ( self , fd , descr , flag ) : <EOL> """<STR_LIT>""" <EOL> if id ( descr ) not in self . _idmap : <EOL> return <EOL> if fd == - <NUM_LIT:1> : <EOL> realfd = self . _idmap [ id ( descr ) ] <EOL> else : <EOL> realfd = fd <EOL> src , cfs , descr , rw = self . _fdmap [ realfd ] <EOL> CFSocketDisableCallBacks ( cfs , flag ) <EOL> rw [ self . _flag2idx ( flag ) ] = False <EOL> if not rw [ _READ ] and not rw [ _WRITE ] : <EOL> del self . _idmap [ id ( descr ) ] <EOL> del self . _fdmap [ realfd ] <EOL> CFRunLoopRemoveSource ( self . _cfrunloop , src , kCFRunLoopCommonModes ) <EOL> CFSocketInvalidate ( cfs ) <EOL> def addReader ( self , reader ) : <EOL> """<STR_LIT>""" <EOL> self . _watchFD ( reader . fileno ( ) , reader , kCFSocketReadCallBack ) <EOL> def addWriter ( self , writer ) : <EOL> """<STR_LIT>""" <EOL> self . _watchFD ( writer . fileno ( ) , writer , kCFSocketWriteCallBack ) <EOL> def removeReader ( self , reader ) : <EOL> """<STR_LIT>""" <EOL> self . _unwatchFD ( reader . fileno ( ) , reader , kCFSocketReadCallBack ) <EOL> def removeWriter ( self , writer ) : <EOL> """<STR_LIT>""" <EOL> self . _unwatchFD ( writer . fileno ( ) , writer , kCFSocketWriteCallBack ) <EOL> def removeAll ( self ) : <EOL> """<STR_LIT>""" <EOL> allDesc = set ( [ descr for src , cfs , descr , rw in self . _fdmap . values ( ) ] ) <EOL> allDesc -= set ( self . _internalReaders ) <EOL> for desc in allDesc : <EOL> self . removeReader ( desc ) <EOL> self . removeWriter ( desc ) <EOL> return list ( allDesc ) <EOL> def getReaders ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ descr for src , cfs , descr , rw in self . _fdmap . values ( ) <EOL> if rw [ _READ ] ] <EOL> def getWriters ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ descr for src , cfs , descr , rw in self . _fdmap . values ( ) <EOL> if rw [ _WRITE ] ] <EOL> def _moveCallLaterSooner ( self , tple ) : <EOL> """<STR_LIT>""" <EOL> result = PosixReactorBase . _moveCallLaterSooner ( self , tple ) <EOL> self . _scheduleSimulate ( ) <EOL> return result <EOL> _inCFLoop = False <EOL> def mainLoop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _inCFLoop = True <EOL> try : <EOL> self . _runner ( ) <EOL> finally : <EOL> self . _inCFLoop = False <EOL> _currentSimulator = None <EOL> def _scheduleSimulate ( self , force = False ) : <EOL> """<STR_LIT>""" <EOL> if self . _currentSimulator is not None : <EOL> CFRunLoopTimerInvalidate ( self . _currentSimulator ) <EOL> self . _currentSimulator = None <EOL> timeout = self . timeout ( ) <EOL> if force : <EOL> timeout = <NUM_LIT:0.0> <EOL> if timeout is not None : <EOL> fireDate = ( CFAbsoluteTimeGetCurrent ( ) + timeout ) <EOL> def simulate ( cftimer , extra ) : <EOL> self . _currentSimulator = None <EOL> self . runUntilCurrent ( ) <EOL> self . _scheduleSimulate ( ) <EOL> c = self . _currentSimulator = CFRunLoopTimerCreate ( <EOL> kCFAllocatorDefault , fireDate , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , simulate , None <EOL> ) <EOL> CFRunLoopAddTimer ( self . _cfrunloop , c , kCFRunLoopCommonModes ) <EOL> def callLater ( self , _seconds , _f , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> delayedCall = PosixReactorBase . callLater ( <EOL> self , _seconds , _f , * args , ** kw <EOL> ) <EOL> self . _scheduleSimulate ( ) <EOL> return delayedCall <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> PosixReactorBase . stop ( self ) <EOL> self . _scheduleSimulate ( True ) <EOL> def crash ( self ) : <EOL> """<STR_LIT>""" <EOL> wasStarted = self . _started <EOL> PosixReactorBase . crash ( self ) <EOL> if self . _inCFLoop : <EOL> self . _stopNow ( ) <EOL> else : <EOL> if wasStarted : <EOL> self . callLater ( <NUM_LIT:0> , self . _stopNow ) <EOL> def _stopNow ( self ) : <EOL> """<STR_LIT>""" <EOL> CFRunLoopStop ( self . _cfrunloop ) <EOL> def iterate ( self , delay = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . callLater ( delay , self . _stopNow ) <EOL> self . mainLoop ( ) <EOL> def install ( runLoop = None , runner = None ) : <EOL> """<STR_LIT>""" <EOL> reactor = CFReactor ( runLoop = runLoop , runner = runner ) <EOL> from twisted . internet . main import installReactor <EOL> installReactor ( reactor ) <EOL> return reactor </s>
