<s> """<STR_LIT>""" <EOL> import exceptions <EOL> import select <EOL> import socket <EOL> import sys <EOL> import time <EOL> import os <EOL> from errno import EALREADY , EINPROGRESS , EWOULDBLOCK , ECONNRESET , ENOTCONN , ESHUTDOWN , EINTR , EISCONN <EOL> try : <EOL> socket_map <EOL> except NameError : <EOL> socket_map = { } <EOL> class ExitNow ( exceptions . Exception ) : <EOL> pass <EOL> def read ( obj ) : <EOL> try : <EOL> obj . handle_read_event ( ) <EOL> except ExitNow : <EOL> raise <EOL> except : <EOL> obj . handle_error ( ) <EOL> def write ( obj ) : <EOL> try : <EOL> obj . handle_write_event ( ) <EOL> except ExitNow : <EOL> raise <EOL> except : <EOL> obj . handle_error ( ) <EOL> def readwrite ( obj , flags ) : <EOL> try : <EOL> if flags & select . POLLIN : <EOL> obj . handle_read_event ( ) <EOL> if flags & select . POLLOUT : <EOL> obj . handle_write_event ( ) <EOL> except ExitNow : <EOL> raise <EOL> except : <EOL> obj . handle_error ( ) <EOL> def poll ( timeout = <NUM_LIT:0.0> , map = None ) : <EOL> if map is None : <EOL> map = socket_map <EOL> if map : <EOL> r = [ ] ; w = [ ] ; e = [ ] <EOL> for fd , obj in map . items ( ) : <EOL> if obj . readable ( ) : <EOL> r . append ( fd ) <EOL> if obj . writable ( ) : <EOL> w . append ( fd ) <EOL> if [ ] == r == w == e : <EOL> time . sleep ( timeout ) <EOL> else : <EOL> try : <EOL> r , w , e = select . select ( r , w , e , timeout ) <EOL> except select . error , err : <EOL> if err [ <NUM_LIT:0> ] != EINTR : <EOL> raise <EOL> else : <EOL> return <EOL> for fd in r : <EOL> obj = map . get ( fd ) <EOL> if obj is None : <EOL> continue <EOL> read ( obj ) <EOL> for fd in w : <EOL> obj = map . get ( fd ) <EOL> if obj is None : <EOL> continue <EOL> write ( obj ) <EOL> def poll2 ( timeout = <NUM_LIT:0.0> , map = None ) : <EOL> import poll <EOL> if map is None : <EOL> map = socket_map <EOL> if timeout is not None : <EOL> timeout = int ( timeout * <NUM_LIT:1000> ) <EOL> if map : <EOL> l = [ ] <EOL> for fd , obj in map . items ( ) : <EOL> flags = <NUM_LIT:0> <EOL> if obj . readable ( ) : <EOL> flags = poll . POLLIN <EOL> if obj . writable ( ) : <EOL> flags = flags | poll . POLLOUT <EOL> if flags : <EOL> l . append ( ( fd , flags ) ) <EOL> r = poll . poll ( l , timeout ) <EOL> for fd , flags in r : <EOL> obj = map . get ( fd ) <EOL> if obj is None : <EOL> continue <EOL> readwrite ( obj , flags ) <EOL> def poll3 ( timeout = <NUM_LIT:0.0> , map = None ) : <EOL> if map is None : <EOL> map = socket_map <EOL> if timeout is not None : <EOL> timeout = int ( timeout * <NUM_LIT:1000> ) <EOL> pollster = select . poll ( ) <EOL> if map : <EOL> for fd , obj in map . items ( ) : <EOL> flags = <NUM_LIT:0> <EOL> if obj . readable ( ) : <EOL> flags = select . POLLIN <EOL> if obj . writable ( ) : <EOL> flags = flags | select . POLLOUT <EOL> if flags : <EOL> pollster . register ( fd , flags ) <EOL> try : <EOL> r = pollster . poll ( timeout ) <EOL> except select . error , err : <EOL> if err [ <NUM_LIT:0> ] != EINTR : <EOL> raise <EOL> r = [ ] <EOL> for fd , flags in r : <EOL> obj = map . get ( fd ) <EOL> if obj is None : <EOL> continue <EOL> readwrite ( obj , flags ) <EOL> def loop ( timeout = <NUM_LIT> , use_poll = <NUM_LIT:0> , map = None ) : <EOL> if map is None : <EOL> map = socket_map <EOL> if use_poll : <EOL> if hasattr ( select , '<STR_LIT>' ) : <EOL> poll_fun = poll3 <EOL> else : <EOL> poll_fun = poll2 <EOL> else : <EOL> poll_fun = poll <EOL> while map : <EOL> poll_fun ( timeout , map ) <EOL> class dispatcher : <EOL> debug = <NUM_LIT:0> <EOL> connected = <NUM_LIT:0> <EOL> accepting = <NUM_LIT:0> <EOL> closing = <NUM_LIT:0> <EOL> addr = None <EOL> def __init__ ( self , sock = None , map = None ) : <EOL> if sock : <EOL> self . set_socket ( sock , map ) <EOL> self . socket . setblocking ( <NUM_LIT:0> ) <EOL> self . connected = <NUM_LIT:1> <EOL> try : <EOL> self . addr = sock . getpeername ( ) <EOL> except socket . error : <EOL> pass <EOL> else : <EOL> self . socket = None <EOL> def __repr__ ( self ) : <EOL> status = [ self . __class__ . __module__ + "<STR_LIT:.>" + self . __class__ . __name__ ] <EOL> if self . accepting and self . addr : <EOL> status . append ( '<STR_LIT>' ) <EOL> elif self . connected : <EOL> status . append ( '<STR_LIT>' ) <EOL> if self . addr is not None : <EOL> try : <EOL> status . append ( '<STR_LIT>' % self . addr ) <EOL> except TypeError : <EOL> status . append ( repr ( self . addr ) ) <EOL> MAX = <NUM_LIT:2> L * sys . maxint + <NUM_LIT:1> <EOL> return '<STR_LIT>' % ( '<STR_LIT:U+0020>' . join ( status ) , id ( self ) & MAX ) <EOL> def add_channel ( self , map = None ) : <EOL> if map is None : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> map = self . _map <EOL> del self . _map <EOL> else : <EOL> map = socket_map <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _fileno = self . socket . fileno ( ) <EOL> map [ self . _fileno ] = self <EOL> def del_channel ( self , map = None ) : <EOL> fd = self . _fileno <EOL> if map is None : <EOL> map = socket_map <EOL> if map . has_key ( fd ) : <EOL> del map [ fd ] <EOL> def create_socket ( self , family , type ) : <EOL> self . family_and_type = family , type <EOL> self . socket = socket . socket ( family , type ) <EOL> self . socket . setblocking ( <NUM_LIT:0> ) <EOL> def set_socket ( self , sock , map = None ) : <EOL> self . socket = sock <EOL> if sock . fileno ( ) : <EOL> self . add_channel ( map ) <EOL> else : <EOL> self . _map = map <EOL> def set_reuse_addr ( self ) : <EOL> try : <EOL> self . socket . setsockopt ( <EOL> socket . SOL_SOCKET , socket . SO_REUSEADDR , <EOL> self . socket . getsockopt ( socket . SOL_SOCKET , <EOL> socket . SO_REUSEADDR ) | <NUM_LIT:1> <EOL> ) <EOL> except socket . error : <EOL> pass <EOL> def readable ( self ) : <EOL> return True <EOL> if os . name == '<STR_LIT>' : <EOL> def writable ( self ) : <EOL> return not self . accepting <EOL> else : <EOL> def writable ( self ) : <EOL> return True <EOL> def listen ( self , num ) : <EOL> self . accepting = <NUM_LIT:1> <EOL> if os . name == '<STR_LIT>' and num > <NUM_LIT:5> : <EOL> num = <NUM_LIT:1> <EOL> ret = self . socket . listen ( num ) <EOL> self . add_channel ( ) <EOL> return ret <EOL> def bind ( self , addr ) : <EOL> self . addr = addr <EOL> return self . socket . bind ( addr ) <EOL> def connect ( self , address ) : <EOL> self . connected = <NUM_LIT:0> <EOL> err = self . socket . connect_ex ( address ) <EOL> if err in ( EINPROGRESS , EALREADY , EWOULDBLOCK ) : <EOL> return <EOL> if err in ( <NUM_LIT:0> , EISCONN ) : <EOL> self . add_channel ( ) <EOL> self . addr = address <EOL> self . connected = <NUM_LIT:1> <EOL> self . handle_connect ( ) <EOL> else : <EOL> raise socket . error , err <EOL> def accept ( self ) : <EOL> try : <EOL> conn , addr = self . socket . accept ( ) <EOL> self . add_channel ( ) <EOL> return conn , addr <EOL> except socket . error , why : <EOL> if why [ <NUM_LIT:0> ] == EWOULDBLOCK : <EOL> pass <EOL> else : <EOL> raise socket . error , why <EOL> def send ( self , data ) : <EOL> try : <EOL> result = self . socket . send ( data ) <EOL> return result <EOL> except socket . error , why : <EOL> if why [ <NUM_LIT:0> ] == EWOULDBLOCK : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> raise socket . error , why <EOL> return <NUM_LIT:0> <EOL> def recv ( self , buffer_size ) : <EOL> try : <EOL> data = self . socket . recv ( buffer_size ) <EOL> if not data : <EOL> self . handle_close ( ) <EOL> return '<STR_LIT>' <EOL> else : <EOL> return data <EOL> except socket . error , why : <EOL> if why [ <NUM_LIT:0> ] in [ ECONNRESET , ENOTCONN , ESHUTDOWN ] : <EOL> self . handle_close ( ) <EOL> return '<STR_LIT>' <EOL> else : <EOL> raise socket . error , why <EOL> def close ( self ) : <EOL> self . del_channel ( ) <EOL> self . socket . close ( ) <EOL> def __getattr__ ( self , attr ) : <EOL> return getattr ( self . socket , attr ) <EOL> def log ( self , message ) : <EOL> sys . stderr . write ( '<STR_LIT>' % str ( message ) ) <EOL> def log_info ( self , message , type = '<STR_LIT:info>' ) : <EOL> if __debug__ or type != '<STR_LIT:info>' : <EOL> print '<STR_LIT>' % ( type , message ) <EOL> def handle_read_event ( self ) : <EOL> if self . accepting : <EOL> if not self . connected : <EOL> self . connected = <NUM_LIT:1> <EOL> self . handle_accept ( ) <EOL> elif not self . connected : <EOL> self . handle_connect ( ) <EOL> self . connected = <NUM_LIT:1> <EOL> self . handle_read ( ) <EOL> else : <EOL> self . handle_read ( ) <EOL> def handle_write_event ( self ) : <EOL> if not self . connected : <EOL> self . handle_connect ( ) <EOL> self . connected = <NUM_LIT:1> <EOL> self . handle_write ( ) <EOL> def handle_expt_event ( self ) : <EOL> self . handle_expt ( ) <EOL> def handle_error ( self ) : <EOL> nil , t , v , tbinfo = compact_traceback ( ) <EOL> try : <EOL> self_repr = repr ( self ) <EOL> except : <EOL> self_repr = '<STR_LIT>' % id ( self ) <EOL> self . log_info ( <EOL> '<STR_LIT>' % ( <EOL> self_repr , <EOL> t , <EOL> v , <EOL> tbinfo <EOL> ) , <EOL> '<STR_LIT:error>' <EOL> ) <EOL> self . close ( ) <EOL> def handle_expt ( self ) : <EOL> self . log_info ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def handle_read ( self ) : <EOL> self . log_info ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def handle_write ( self ) : <EOL> self . log_info ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def handle_connect ( self ) : <EOL> self . log_info ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def handle_accept ( self ) : <EOL> self . log_info ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def handle_close ( self ) : <EOL> self . log_info ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . close ( ) <EOL> class dispatcher_with_send ( dispatcher ) : <EOL> def __init__ ( self , sock = None ) : <EOL> dispatcher . __init__ ( self , sock ) <EOL> self . out_buffer = '<STR_LIT>' <EOL> def initiate_send ( self ) : <EOL> num_sent = <NUM_LIT:0> <EOL> num_sent = dispatcher . send ( self , self . out_buffer [ : <NUM_LIT> ] ) <EOL> self . out_buffer = self . out_buffer [ num_sent : ] <EOL> def handle_write ( self ) : <EOL> self . initiate_send ( ) <EOL> def writable ( self ) : <EOL> return ( not self . connected ) or len ( self . out_buffer ) <EOL> def send ( self , data ) : <EOL> if self . debug : <EOL> self . log_info ( '<STR_LIT>' % repr ( data ) ) <EOL> self . out_buffer = self . out_buffer + data <EOL> self . initiate_send ( ) <EOL> def compact_traceback ( ) : <EOL> t , v , tb = sys . exc_info ( ) <EOL> tbinfo = [ ] <EOL> assert tb <EOL> while tb : <EOL> tbinfo . append ( ( <EOL> tb . tb_frame . f_code . co_filename , <EOL> tb . tb_frame . f_code . co_name , <EOL> str ( tb . tb_lineno ) <EOL> ) ) <EOL> tb = tb . tb_next <EOL> del tb <EOL> file , function , line = tbinfo [ - <NUM_LIT:1> ] <EOL> info = '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' % x for x in tbinfo ] ) <EOL> return ( file , function , line ) , t , v , info <EOL> def close_all ( map = None ) : <EOL> if map is None : <EOL> map = socket_map <EOL> for x in map . values ( ) : <EOL> x . socket . close ( ) <EOL> map . clear ( ) <EOL> if os . name == '<STR_LIT>' : <EOL> import fcntl <EOL> class file_wrapper : <EOL> def __init__ ( self , fd ) : <EOL> self . fd = fd <EOL> def recv ( self , * args ) : <EOL> return os . read ( self . fd , * args ) <EOL> def send ( self , * args ) : <EOL> return os . write ( self . fd , * args ) <EOL> read = recv <EOL> write = send <EOL> def close ( self ) : <EOL> return os . close ( self . fd ) <EOL> def fileno ( self ) : <EOL> return self . fd <EOL> class file_dispatcher ( dispatcher ) : <EOL> def __init__ ( self , fd ) : <EOL> dispatcher . __init__ ( self ) <EOL> self . connected = <NUM_LIT:1> <EOL> flags = fcntl . fcntl ( fd , fcntl . F_GETFL , <NUM_LIT:0> ) <EOL> flags = flags | os . O_NONBLOCK <EOL> fcntl . fcntl ( fd , fcntl . F_SETFL , flags ) <EOL> self . set_file ( fd ) <EOL> def set_file ( self , fd ) : <EOL> self . _fileno = fd <EOL> self . socket = file_wrapper ( fd ) <EOL> self . add_channel ( ) </s>
