<s> """<STR_LIT>""" <EOL> import errno , sys <EOL> from zope . interface import implements <EOL> from kqsyscall import EVFILT_READ , EVFILT_WRITE , EV_DELETE , EV_ADD <EOL> from kqsyscall import kqueue , kevent <EOL> from twisted . internet . interfaces import IReactorFDSet <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 ) <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 , * args ) : <EOL> self . _kq . kevent ( [ kevent ( * args ) ] , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> def addReader ( self , reader ) : <EOL> """<STR_LIT>""" <EOL> fd = reader . fileno ( ) <EOL> if fd not in self . _reads : <EOL> self . _selectables [ fd ] = reader <EOL> self . _reads [ fd ] = <NUM_LIT:1> <EOL> self . _updateRegistration ( fd , EVFILT_READ , EV_ADD ) <EOL> def addWriter ( self , writer ) : <EOL> """<STR_LIT>""" <EOL> fd = writer . fileno ( ) <EOL> if fd not in self . _writes : <EOL> self . _selectables [ fd ] = writer <EOL> self . _writes [ fd ] = <NUM_LIT:1> <EOL> self . _updateRegistration ( fd , EVFILT_WRITE , EV_ADD ) <EOL> def removeReader ( self , reader ) : <EOL> """<STR_LIT>""" <EOL> fd = reader . fileno ( ) <EOL> if fd in self . _reads : <EOL> del self . _reads [ fd ] <EOL> if fd not in self . _writes : <EOL> del self . _selectables [ fd ] <EOL> self . _updateRegistration ( fd , EVFILT_READ , EV_DELETE ) <EOL> def removeWriter ( self , writer ) : <EOL> """<STR_LIT>""" <EOL> fd = writer . fileno ( ) <EOL> if fd in self . _writes : <EOL> del self . _writes [ fd ] <EOL> if fd not in self . _reads : <EOL> del self . _selectables [ fd ] <EOL> self . _updateRegistration ( fd , EVFILT_WRITE , EV_DELETE ) <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> return [ self . _selectables [ fd ] for fd in self . _reads ] <EOL> def getWriters ( self ) : <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:1000> <EOL> else : <EOL> timeout = int ( timeout * <NUM_LIT:1000> ) <EOL> try : <EOL> l = self . _kq . kevent ( [ ] , 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> why = None <EOL> fd , filter = event . ident , event . filter <EOL> try : <EOL> selectable = self . _selectables [ fd ] <EOL> except KeyError : <EOL> continue <EOL> log . callWithLogger ( selectable , _drdw , selectable , fd , filter ) <EOL> def _doWriteOrRead ( self , selectable , fd , filter ) : <EOL> try : <EOL> if filter == EVFILT_READ : <EOL> why = selectable . doRead ( ) <EOL> if filter == EVFILT_WRITE : <EOL> why = selectable . doWrite ( ) <EOL> if not selectable . fileno ( ) == fd : <EOL> why = main . CONNECTION_LOST <EOL> except : <EOL> why = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> log . deferr ( ) <EOL> if why : <EOL> self . removeReader ( selectable ) <EOL> self . removeWriter ( selectable ) <EOL> selectable . connectionLost ( failure . Failure ( why ) ) <EOL> doIteration = doKEvent <EOL> def install ( ) : <EOL> k = KQueueReactor ( ) <EOL> main . installReactor ( k ) <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] </s>
