<s> """<STR_LIT>""" <EOL> import os <EOL> import select <EOL> import platform <EOL> from errno import EBADF , EINTR <EOL> from select import error as SelectError <EOL> from socket import error as SocketError , create_connection , socket as create_socket , AF_INET , SOCK_STREAM , socket <EOL> from threading import Thread <EOL> from circuits . core . handlers import handler <EOL> from . events import Event <EOL> from . components import BaseComponent <EOL> class _read ( Event ) : <EOL> """<STR_LIT>""" <EOL> class _write ( Event ) : <EOL> """<STR_LIT>""" <EOL> class _error ( Event ) : <EOL> """<STR_LIT>""" <EOL> class _disconnect ( Event ) : <EOL> """<STR_LIT>""" <EOL> class BasePoller ( BaseComponent ) : <EOL> channel = None <EOL> def __init__ ( self , channel = channel ) : <EOL> super ( BasePoller , self ) . __init__ ( channel = channel ) <EOL> self . _read = [ ] <EOL> self . _write = [ ] <EOL> self . _targets = { } <EOL> self . _ctrl_recv , self . _ctrl_send = self . _create_control_con ( ) <EOL> def _create_control_con ( self ) : <EOL> if platform . system ( ) == "<STR_LIT>" : <EOL> return os . pipe ( ) <EOL> server = create_socket ( AF_INET , SOCK_STREAM ) <EOL> server . bind ( ( "<STR_LIT:localhost>" , <NUM_LIT:0> ) ) <EOL> server . listen ( <NUM_LIT:1> ) <EOL> res_list = [ ] <EOL> def accept ( ) : <EOL> sock , _ = server . accept ( ) <EOL> sock . setblocking ( False ) <EOL> res_list . append ( sock ) <EOL> at = Thread ( target = accept ) <EOL> at . start ( ) <EOL> clnt_sock = create_connection ( server . getsockname ( ) ) <EOL> at . join ( ) <EOL> return ( res_list [ <NUM_LIT:0> ] , clnt_sock ) <EOL> @ handler ( "<STR_LIT>" , priority = - <NUM_LIT:9> ) <EOL> def _on_generate_events ( self , event ) : <EOL> """<STR_LIT>""" <EOL> event . stop ( ) <EOL> self . _generate_events ( event ) <EOL> def resume ( self ) : <EOL> if isinstance ( self . _ctrl_send , socket ) : <EOL> self . _ctrl_send . send ( b"<STR_LIT>" ) <EOL> else : <EOL> os . write ( self . _ctrl_send , b"<STR_LIT>" ) <EOL> def _read_ctrl ( self ) : <EOL> try : <EOL> if isinstance ( self . _ctrl_recv , socket ) : <EOL> return self . _ctrl_recv . recv ( <NUM_LIT:1> ) <EOL> else : <EOL> return os . read ( self . _ctrl_recv , <NUM_LIT:1> ) <EOL> except : <EOL> return b"<STR_LIT>" <EOL> def addReader ( self , source , fd ) : <EOL> channel = getattr ( source , "<STR_LIT>" , "<STR_LIT:*>" ) <EOL> self . _read . append ( fd ) <EOL> self . _targets [ fd ] = channel <EOL> def addWriter ( self , source , fd ) : <EOL> channel = getattr ( source , "<STR_LIT>" , "<STR_LIT:*>" ) <EOL> self . _write . append ( fd ) <EOL> self . _targets [ fd ] = channel <EOL> def removeReader ( self , fd ) : <EOL> if fd in self . _read : <EOL> self . _read . remove ( fd ) <EOL> if not ( fd in self . _read or fd in self . _write ) and fd in self . _targets : <EOL> del self . _targets [ fd ] <EOL> def removeWriter ( self , fd ) : <EOL> if fd in self . _write : <EOL> self . _write . remove ( fd ) <EOL> if not ( fd in self . _read or fd in self . _write ) and fd in self . _targets : <EOL> del self . _targets [ fd ] <EOL> def isReading ( self , fd ) : <EOL> return fd in self . _read <EOL> def isWriting ( self , fd ) : <EOL> return fd in self . _write <EOL> def discard ( self , fd ) : <EOL> if fd in self . _read : <EOL> self . _read . remove ( fd ) <EOL> if fd in self . _write : <EOL> self . _write . remove ( fd ) <EOL> if fd in self . _targets : <EOL> del self . _targets [ fd ] <EOL> def getTarget ( self , fd ) : <EOL> return self . _targets . get ( fd , self . parent ) <EOL> class Select ( BasePoller ) : <EOL> """<STR_LIT>""" <EOL> channel = "<STR_LIT>" <EOL> def __init__ ( self , channel = channel ) : <EOL> super ( Select , self ) . __init__ ( channel = channel ) <EOL> self . _read . append ( self . _ctrl_recv ) <EOL> def _preenDescriptors ( self ) : <EOL> for socks in ( self . _read [ : ] , self . _write [ : ] ) : <EOL> for sock in socks : <EOL> try : <EOL> select . select ( [ sock ] , [ sock ] , [ sock ] , <NUM_LIT:0> ) <EOL> except Exception : <EOL> self . discard ( sock ) <EOL> def _generate_events ( self , event ) : <EOL> try : <EOL> if not any ( [ self . _read , self . _write ] ) : <EOL> return <EOL> timeout = event . time_left <EOL> if timeout < <NUM_LIT:0> : <EOL> r , w , _ = select . select ( self . _read , self . _write , [ ] ) <EOL> else : <EOL> r , w , _ = select . select ( self . _read , self . _write , [ ] , timeout ) <EOL> except ValueError as e : <EOL> return self . _preenDescriptors ( ) <EOL> except TypeError as e : <EOL> return self . _preenDescriptors ( ) <EOL> except ( SelectError , SocketError , IOError ) as e : <EOL> if e . args [ <NUM_LIT:0> ] in ( <NUM_LIT:0> , <NUM_LIT:2> ) : <EOL> if ( not self . _read ) and ( not self . _write ) : <EOL> return <EOL> else : <EOL> raise <EOL> elif e . args [ <NUM_LIT:0> ] == EINTR : <EOL> return <EOL> elif e . args [ <NUM_LIT:0> ] == EBADF : <EOL> return self . _preenDescriptors ( ) <EOL> else : <EOL> raise <EOL> for sock in w : <EOL> if self . isWriting ( sock ) : <EOL> self . fire ( _write ( sock ) , self . getTarget ( sock ) ) <EOL> for sock in r : <EOL> if sock == self . _ctrl_recv : <EOL> self . _read_ctrl ( ) <EOL> continue <EOL> if self . isReading ( sock ) : <EOL> self . fire ( _read ( sock ) , self . getTarget ( sock ) ) <EOL> class Poll ( BasePoller ) : <EOL> """<STR_LIT>""" <EOL> channel = "<STR_LIT>" <EOL> def __init__ ( self , channel = channel ) : <EOL> super ( Poll , self ) . __init__ ( channel = channel ) <EOL> self . _map = { } <EOL> self . _poller = select . poll ( ) <EOL> self . _disconnected_flag = ( <EOL> select . POLLHUP <EOL> | select . POLLERR <EOL> | select . POLLNVAL <EOL> ) <EOL> self . _read . append ( self . _ctrl_recv ) <EOL> self . _updateRegistration ( self . _ctrl_recv ) <EOL> def _updateRegistration ( self , fd ) : <EOL> fileno = fd . fileno ( ) if not isinstance ( fd , int ) else fd <EOL> try : <EOL> self . _poller . unregister ( fileno ) <EOL> except ( KeyError , ValueError ) : <EOL> pass <EOL> mask = <NUM_LIT:0> <EOL> if fd in self . _read : <EOL> mask = mask | select . POLLIN <EOL> if fd in self . _write : <EOL> mask = mask | select . POLLOUT <EOL> if mask : <EOL> self . _poller . register ( fd , mask ) <EOL> self . _map [ fileno ] = fd <EOL> else : <EOL> super ( Poll , self ) . discard ( fd ) <EOL> try : <EOL> del self . _map [ fileno ] <EOL> except KeyError : <EOL> pass <EOL> def addReader ( self , source , fd ) : <EOL> super ( Poll , self ) . addReader ( source , fd ) <EOL> self . _updateRegistration ( fd ) <EOL> def addWriter ( self , source , fd ) : <EOL> super ( Poll , self ) . addWriter ( source , fd ) <EOL> self . _updateRegistration ( fd ) <EOL> def removeReader ( self , fd ) : <EOL> super ( Poll , self ) . removeReader ( fd ) <EOL> self . _updateRegistration ( fd ) <EOL> def removeWriter ( self , fd ) : <EOL> super ( Poll , self ) . removeWriter ( fd ) <EOL> self . _updateRegistration ( fd ) <EOL> def discard ( self , fd ) : <EOL> super ( Poll , self ) . discard ( fd ) <EOL> self . _updateRegistration ( fd ) <EOL> def _generate_events ( self , event ) : <EOL> try : <EOL> timeout = event . time_left <EOL> if timeout < <NUM_LIT:0> : <EOL> l = self . _poller . poll ( ) <EOL> else : <EOL> l = self . _poller . poll ( <NUM_LIT:1000> * timeout ) <EOL> except SelectError as e : <EOL> if e . args [ <NUM_LIT:0> ] == EINTR : <EOL> return <EOL> else : <EOL> raise <EOL> for fileno , event in l : <EOL> self . _process ( fileno , event ) <EOL> def _process ( self , fileno , event ) : <EOL> if fileno not in self . _map : <EOL> return <EOL> fd = self . _map [ fileno ] <EOL> if fd == self . _ctrl_recv : <EOL> self . _read_ctrl ( ) <EOL> return <EOL> if event & self . _disconnected_flag and not ( event & select . POLLIN ) : <EOL> self . fire ( _disconnect ( fd ) , self . getTarget ( fd ) ) <EOL> self . _poller . unregister ( fileno ) <EOL> super ( Poll , self ) . discard ( fd ) <EOL> del self . _map [ fileno ] <EOL> else : <EOL> try : <EOL> if event & select . POLLIN : <EOL> self . fire ( _read ( fd ) , self . getTarget ( fd ) ) <EOL> if event & select . POLLOUT : <EOL> self . fire ( _write ( fd ) , self . getTarget ( fd ) ) <EOL> except Exception as e : <EOL> self . fire ( _error ( fd , e ) , self . getTarget ( fd ) ) <EOL> self . fire ( _disconnect ( fd ) , self . getTarget ( fd ) ) <EOL> self . _poller . unregister ( fileno ) <EOL> super ( Poll , self ) . discard ( fd ) <EOL> del self . _map [ fileno ] <EOL> class EPoll ( BasePoller ) : <EOL> """<STR_LIT>""" <EOL> channel = "<STR_LIT>" <EOL> def __init__ ( self , channel = channel ) : <EOL> super ( EPoll , self ) . __init__ ( channel = channel ) <EOL> self . _map = { } <EOL> self . _poller = select . epoll ( ) <EOL> self . _disconnected_flag = ( select . EPOLLHUP | select . EPOLLERR ) <EOL> self . _read . append ( self . _ctrl_recv ) <EOL> self . _updateRegistration ( self . _ctrl_recv ) <EOL> def _updateRegistration ( self , fd ) : <EOL> try : <EOL> fileno = fd . fileno ( ) if not isinstance ( fd , int ) else fd <EOL> self . _poller . unregister ( fileno ) <EOL> except ( SocketError , IOError , ValueError ) as e : <EOL> if e . args [ <NUM_LIT:0> ] == EBADF : <EOL> keys = [ k for k , v in list ( self . _map . items ( ) ) if v == fd ] <EOL> for key in keys : <EOL> del self . _map [ key ] <EOL> mask = <NUM_LIT:0> <EOL> if fd in self . _read : <EOL> mask = mask | select . EPOLLIN <EOL> if fd in self . _write : <EOL> mask = mask | select . EPOLLOUT <EOL> if mask : <EOL> self . _poller . register ( fd , mask ) <EOL> self . _map [ fileno ] = fd <EOL> else : <EOL> super ( EPoll , self ) . discard ( fd ) <EOL> def addReader ( self , source , fd ) : <EOL> super ( EPoll , self ) . addReader ( source , fd ) <EOL> self . _updateRegistration ( fd ) <EOL> def addWriter ( self , source , fd ) : <EOL> super ( EPoll , self ) . addWriter ( source , fd ) <EOL> self . _updateRegistration ( fd ) <EOL> def removeReader ( self , fd ) : <EOL> super ( EPoll , self ) . removeReader ( fd ) <EOL> self . _updateRegistration ( fd ) <EOL> def removeWriter ( self , fd ) : <EOL> super ( EPoll , self ) . removeWriter ( fd ) <EOL> self . _updateRegistration ( fd ) <EOL> def discard ( self , fd ) : <EOL> super ( EPoll , self ) . discard ( fd ) <EOL> self . _updateRegistration ( fd ) <EOL> def _generate_events ( self , event ) : <EOL> try : <EOL> timeout = event . time_left <EOL> if timeout < <NUM_LIT:0> : <EOL> l = self . _poller . poll ( ) <EOL> else : <EOL> l = self . _poller . poll ( timeout ) <EOL> except IOError as e : <EOL> if e . args [ <NUM_LIT:0> ] == EINTR : <EOL> return <EOL> except SelectError as e : <EOL> if e . args [ <NUM_LIT:0> ] == EINTR : <EOL> return <EOL> else : <EOL> raise <EOL> for fileno , event in l : <EOL> self . _process ( fileno , event ) <EOL> def _process ( self , fileno , event ) : <EOL> if fileno not in self . _map : <EOL> return <EOL> fd = self . _map [ fileno ] <EOL> if fd == self . _ctrl_recv : <EOL> self . _read_ctrl ( ) <EOL> return <EOL> if event & self . _disconnected_flag and not ( event & select . POLLIN ) : <EOL> self . fire ( _disconnect ( fd ) , self . getTarget ( fd ) ) <EOL> self . _poller . unregister ( fileno ) <EOL> super ( EPoll , self ) . discard ( fd ) <EOL> del self . _map [ fileno ] <EOL> else : <EOL> try : <EOL> if event & select . EPOLLIN : <EOL> self . fire ( _read ( fd ) , self . getTarget ( fd ) ) <EOL> if event & select . EPOLLOUT : <EOL> self . fire ( _write ( fd ) , self . getTarget ( fd ) ) <EOL> except Exception as e : <EOL> self . fire ( _error ( fd , e ) , self . getTarget ( fd ) ) <EOL> self . fire ( _disconnect ( fd ) , self . getTarget ( fd ) ) <EOL> self . _poller . unregister ( fileno ) <EOL> super ( EPoll , self ) . discard ( fd ) <EOL> del self . _map [ fileno ] <EOL> class KQueue ( BasePoller ) : <EOL> """<STR_LIT>""" <EOL> channel = "<STR_LIT>" <EOL> def __init__ ( self , channel = channel ) : <EOL> super ( KQueue , self ) . __init__ ( channel = channel ) <EOL> self . _map = { } <EOL> self . _poller = select . kqueue ( ) <EOL> self . _read . append ( self . _ctrl_recv ) <EOL> self . _map [ self . _ctrl_recv . fileno ( ) ] = self . _ctrl_recv <EOL> self . _poller . control ( <EOL> [ <EOL> select . kevent ( <EOL> self . _ctrl_recv , select . KQ_FILTER_READ , select . KQ_EV_ADD <EOL> ) <EOL> ] , <NUM_LIT:0> <EOL> ) <EOL> def addReader ( self , source , sock ) : <EOL> super ( KQueue , self ) . addReader ( source , sock ) <EOL> self . _map [ sock . fileno ( ) ] = sock <EOL> self . _poller . control ( <EOL> [ select . kevent ( sock , select . KQ_FILTER_READ , select . KQ_EV_ADD ) ] , <NUM_LIT:0> <EOL> ) <EOL> def addWriter ( self , source , sock ) : <EOL> super ( KQueue , self ) . addWriter ( source , sock ) <EOL> self . _map [ sock . fileno ( ) ] = sock <EOL> self . _poller . control ( <EOL> [ select . kevent ( sock , select . KQ_FILTER_WRITE , select . KQ_EV_ADD ) ] , <NUM_LIT:0> <EOL> ) <EOL> def removeReader ( self , sock ) : <EOL> super ( KQueue , self ) . removeReader ( sock ) <EOL> self . _poller . control ( <EOL> [ <EOL> select . kevent ( sock , select . KQ_FILTER_READ , select . KQ_EV_DELETE ) <EOL> ] , <EOL> <NUM_LIT:0> <EOL> ) <EOL> def removeWriter ( self , sock ) : <EOL> super ( KQueue , self ) . removeWriter ( sock ) <EOL> self . _poller . control ( <EOL> [ select . kevent ( sock , select . KQ_FILTER_WRITE , select . KQ_EV_DELETE ) ] , <EOL> <NUM_LIT:0> <EOL> ) <EOL> def discard ( self , sock ) : <EOL> super ( KQueue , self ) . discard ( sock ) <EOL> del self . _map [ sock . fileno ( ) ] <EOL> self . _poller . control ( <EOL> [ <EOL> select . kevent ( <EOL> sock , <EOL> select . KQ_FILTER_WRITE | select . KQ_FILTER_READ , <EOL> select . KQ_EV_DELETE <EOL> ) <EOL> ] , <EOL> <NUM_LIT:0> <EOL> ) <EOL> def _generate_events ( self , event ) : <EOL> try : <EOL> timeout = event . time_left <EOL> if timeout < <NUM_LIT:0> : <EOL> l = self . _poller . control ( None , <NUM_LIT:1000> ) <EOL> else : <EOL> l = self . _poller . control ( None , <NUM_LIT:1000> , timeout ) <EOL> except SelectError as e : <EOL> if e [ <NUM_LIT:0> ] == EINTR : <EOL> return <EOL> else : <EOL> raise <EOL> for event in l : <EOL> self . _process ( event ) <EOL> def _process ( self , event ) : <EOL> if event . ident not in self . _map : <EOL> self . _poller . control ( <EOL> [ <EOL> select . kevent ( <EOL> event . ident , event . filter , select . KQ_EV_DELETE <EOL> ) <EOL> ] , <EOL> <NUM_LIT:0> <EOL> ) <EOL> return <EOL> sock = self . _map [ event . ident ] <EOL> if sock == self . _ctrl_recv : <EOL> self . _read_ctrl ( ) <EOL> return <EOL> if event . flags & select . KQ_EV_ERROR : <EOL> self . fire ( _error ( sock , "<STR_LIT:error>" ) , self . getTarget ( sock ) ) <EOL> elif event . flags & select . KQ_EV_EOF : <EOL> self . fire ( _disconnect ( sock ) , self . getTarget ( sock ) ) <EOL> elif event . filter == select . KQ_FILTER_WRITE : <EOL> self . fire ( _write ( sock ) , self . getTarget ( sock ) ) <EOL> elif event . filter == select . KQ_FILTER_READ : <EOL> self . fire ( _read ( sock ) , self . getTarget ( sock ) ) <EOL> Poller = Select <EOL> __all__ = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) </s>
