<s> """<STR_LIT>""" <EOL> import errno <EOL> from zope . interface import implements <EOL> from select import kqueue , kevent <EOL> from select import KQ_FILTER_READ , KQ_FILTER_WRITE <EOL> from select import KQ_EV_DELETE , KQ_EV_ADD , KQ_EV_EOF <EOL> from twisted . internet . interfaces import IReactorFDSet , IReactorDaemonize <EOL> from twisted . python import log , failure <EOL> from twisted . internet import main , posixbase <EOL> class KQueueReactor ( posixbase . PosixReactorBase ) : <EOL> """<STR_LIT>""" <EOL> implements ( IReactorFDSet , IReactorDaemonize ) <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _kq = kqueue ( ) <EOL> self . _reads = { } <EOL> self . _writes = { } <EOL> self . _selectables = { } <EOL> posixbase . PosixReactorBase . __init__ ( self ) <EOL> def _updateRegistration ( self , fd , filter , op ) : <EOL> """<STR_LIT>""" <EOL> self . _kq . control ( [ kevent ( fd , filter , op ) ] , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> def beforeDaemonize ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _kq . close ( ) <EOL> self . _kq = None <EOL> def afterDaemonize ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _kq = kqueue ( ) <EOL> for fd in self . _reads : <EOL> self . _updateRegistration ( fd , KQ_FILTER_READ , KQ_EV_ADD ) <EOL> for fd in self . _writes : <EOL> self . _updateRegistration ( fd , KQ_FILTER_WRITE , KQ_EV_ADD ) <EOL> def addReader ( self , reader ) : <EOL> """<STR_LIT>""" <EOL> fd = reader . fileno ( ) <EOL> if fd not in self . _reads : <EOL> try : <EOL> self . _updateRegistration ( fd , KQ_FILTER_READ , KQ_EV_ADD ) <EOL> except OSError : <EOL> pass <EOL> finally : <EOL> self . _selectables [ fd ] = reader <EOL> self . _reads [ fd ] = <NUM_LIT:1> <EOL> def addWriter ( self , writer ) : <EOL> """<STR_LIT>""" <EOL> fd = writer . fileno ( ) <EOL> if fd not in self . _writes : <EOL> try : <EOL> self . _updateRegistration ( fd , KQ_FILTER_WRITE , KQ_EV_ADD ) <EOL> except OSError : <EOL> pass <EOL> finally : <EOL> self . _selectables [ fd ] = writer <EOL> self . _writes [ fd ] = <NUM_LIT:1> <EOL> def removeReader ( self , reader ) : <EOL> """<STR_LIT>""" <EOL> wasLost = False <EOL> try : <EOL> fd = reader . fileno ( ) <EOL> except : <EOL> fd = - <NUM_LIT:1> <EOL> if fd == - <NUM_LIT:1> : <EOL> for fd , fdes in self . _selectables . items ( ) : <EOL> if reader is fdes : <EOL> wasLost = True <EOL> break <EOL> else : <EOL> return <EOL> if fd in self . _reads : <EOL> del self . _reads [ fd ] <EOL> if fd not in self . _writes : <EOL> del self . _selectables [ fd ] <EOL> if not wasLost : <EOL> try : <EOL> self . _updateRegistration ( fd , KQ_FILTER_READ , KQ_EV_DELETE ) <EOL> except OSError : <EOL> pass <EOL> def removeWriter ( self , writer ) : <EOL> """<STR_LIT>""" <EOL> wasLost = False <EOL> try : <EOL> fd = writer . fileno ( ) <EOL> except : <EOL> fd = - <NUM_LIT:1> <EOL> if fd == - <NUM_LIT:1> : <EOL> for fd , fdes in self . _selectables . items ( ) : <EOL> if writer is fdes : <EOL> wasLost = True <EOL> break <EOL> else : <EOL> return <EOL> if fd in self . _writes : <EOL> del self . _writes [ fd ] <EOL> if fd not in self . _reads : <EOL> del self . _selectables [ fd ] <EOL> if not wasLost : <EOL> try : <EOL> self . _updateRegistration ( fd , KQ_FILTER_WRITE , KQ_EV_DELETE ) <EOL> except OSError : <EOL> pass <EOL> def removeAll ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _removeAll ( <EOL> [ self . _selectables [ fd ] for fd in self . _reads ] , <EOL> [ self . _selectables [ fd ] for fd in self . _writes ] ) <EOL> def getReaders ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self . _selectables [ fd ] for fd in self . _reads ] <EOL> def getWriters ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self . _selectables [ fd ] for fd in self . _writes ] <EOL> def doKEvent ( self , timeout ) : <EOL> """<STR_LIT>""" <EOL> if timeout is None : <EOL> timeout = <NUM_LIT:1> <EOL> try : <EOL> l = self . _kq . control ( [ ] , len ( self . _selectables ) , timeout ) <EOL> except OSError , e : <EOL> if e [ <NUM_LIT:0> ] == errno . EINTR : <EOL> return <EOL> else : <EOL> raise <EOL> _drdw = self . _doWriteOrRead <EOL> for event in l : <EOL> fd = event . ident <EOL> try : <EOL> selectable = self . _selectables [ fd ] <EOL> except KeyError : <EOL> continue <EOL> else : <EOL> log . callWithLogger ( selectable , _drdw , selectable , fd , event ) <EOL> def _doWriteOrRead ( self , selectable , fd , event ) : <EOL> """<STR_LIT>""" <EOL> why = None <EOL> inRead = False <EOL> ( filter , flags , data , fflags ) = ( <EOL> event . filter , event . flags , event . data , event . fflags ) <EOL> if flags & KQ_EV_EOF and data and fflags : <EOL> why = main . CONNECTION_LOST <EOL> else : <EOL> try : <EOL> if selectable . fileno ( ) == - <NUM_LIT:1> : <EOL> inRead = False <EOL> why = posixbase . _NO_FILEDESC <EOL> else : <EOL> if filter == KQ_FILTER_READ : <EOL> inRead = True <EOL> why = selectable . doRead ( ) <EOL> if filter == KQ_FILTER_WRITE : <EOL> inRead = False <EOL> why = selectable . doWrite ( ) <EOL> except : <EOL> why = failure . Failure ( ) <EOL> log . err ( why , "<STR_LIT>" "<STR_LIT>" ) <EOL> if why : <EOL> self . _disconnectSelectable ( selectable , why , inRead ) <EOL> doIteration = doKEvent <EOL> def install ( ) : <EOL> """<STR_LIT>""" <EOL> p = KQueueReactor ( ) <EOL> from twisted . internet . main import installReactor <EOL> installReactor ( p ) <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] </s>
