<s> from bisect import insort <EOL> from SocketHandler import SocketHandler <EOL> import socket <EOL> from cStringIO import StringIO <EOL> from traceback import print_exc <EOL> from select import error <EOL> from threading import Event <EOL> from clock import clock <EOL> import sys <EOL> try : <EOL> True <EOL> except : <EOL> True = <NUM_LIT:1> <EOL> False = <NUM_LIT:0> <EOL> def autodetect_ipv6 ( ) : <EOL> try : <EOL> assert sys . version_info >= ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> assert socket . has_ipv6 <EOL> socket . socket ( socket . AF_INET6 , socket . SOCK_STREAM ) <EOL> except : <EOL> return <NUM_LIT:0> <EOL> return <NUM_LIT:1> <EOL> def autodetect_socket_style ( ) : <EOL> if sys . platform . find ( '<STR_LIT>' ) < <NUM_LIT:0> : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> try : <EOL> f = open ( '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> dual_socket_style = int ( f . read ( ) ) <EOL> f . close ( ) <EOL> return int ( not dual_socket_style ) <EOL> except : <EOL> return <NUM_LIT:0> <EOL> READSIZE = <NUM_LIT> <EOL> class RawServer : <EOL> def __init__ ( self , doneflag , timeout_check_interval , timeout , noisy = True , <EOL> ipv6_enable = True , failfunc = lambda x : None , errorfunc = None , <EOL> sockethandler = None , excflag = Event ( ) ) : <EOL> self . timeout_check_interval = timeout_check_interval <EOL> self . timeout = timeout <EOL> self . servers = { } <EOL> self . single_sockets = { } <EOL> self . dead_from_write = [ ] <EOL> self . doneflag = doneflag <EOL> self . noisy = noisy <EOL> self . failfunc = failfunc <EOL> self . errorfunc = errorfunc <EOL> self . exccount = <NUM_LIT:0> <EOL> self . funcs = [ ] <EOL> self . externally_added = [ ] <EOL> self . finished = Event ( ) <EOL> self . tasks_to_kill = [ ] <EOL> self . excflag = excflag <EOL> if sockethandler is None : <EOL> sockethandler = SocketHandler ( timeout , ipv6_enable , READSIZE ) <EOL> self . sockethandler = sockethandler <EOL> self . add_task ( self . scan_for_timeouts , timeout_check_interval ) <EOL> def get_exception_flag ( self ) : <EOL> return self . excflag <EOL> def _add_task ( self , func , delay , id = None ) : <EOL> if delay < <NUM_LIT:0> : <EOL> delay = <NUM_LIT:0> <EOL> insort ( self . funcs , ( clock ( ) + delay , func , id ) ) <EOL> def add_task ( self , func , delay = <NUM_LIT:0> , id = None ) : <EOL> if delay < <NUM_LIT:0> : <EOL> delay = <NUM_LIT:0> <EOL> self . externally_added . append ( ( func , delay , id ) ) <EOL> def scan_for_timeouts ( self ) : <EOL> self . add_task ( self . scan_for_timeouts , self . timeout_check_interval ) <EOL> self . sockethandler . scan_for_timeouts ( ) <EOL> def bind ( self , port , bind = '<STR_LIT>' , reuse = False , <EOL> ipv6_socket_style = <NUM_LIT:1> , upnp = False ) : <EOL> self . sockethandler . bind ( port , bind , reuse , ipv6_socket_style , upnp ) <EOL> def find_and_bind ( self , minport , maxport , bind = '<STR_LIT>' , reuse = False , <EOL> ipv6_socket_style = <NUM_LIT:1> , upnp = <NUM_LIT:0> , randomizer = False ) : <EOL> return self . sockethandler . find_and_bind ( minport , maxport , bind , reuse , <EOL> ipv6_socket_style , upnp , randomizer ) <EOL> def start_connection_raw ( self , dns , socktype , handler = None ) : <EOL> return self . sockethandler . start_connection_raw ( dns , socktype , handler ) <EOL> def start_connection ( self , dns , handler = None , randomize = False ) : <EOL> return self . sockethandler . start_connection ( dns , handler , randomize ) <EOL> def get_stats ( self ) : <EOL> return self . sockethandler . get_stats ( ) <EOL> def pop_external ( self ) : <EOL> while self . externally_added : <EOL> ( a , b , c ) = self . externally_added . pop ( <NUM_LIT:0> ) <EOL> self . _add_task ( a , b , c ) <EOL> def listen_forever ( self , handler ) : <EOL> self . sockethandler . set_handler ( handler ) <EOL> try : <EOL> while not self . doneflag . isSet ( ) : <EOL> try : <EOL> self . pop_external ( ) <EOL> self . _kill_tasks ( ) <EOL> if self . funcs : <EOL> period = self . funcs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] + <NUM_LIT> - clock ( ) <EOL> else : <EOL> period = <NUM_LIT:2> ** <NUM_LIT:30> <EOL> if period < <NUM_LIT:0> : <EOL> period = <NUM_LIT:0> <EOL> events = self . sockethandler . do_poll ( period ) <EOL> if self . doneflag . isSet ( ) : <EOL> return <EOL> while self . funcs and self . funcs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <= clock ( ) : <EOL> garbage1 , func , id = self . funcs . pop ( <NUM_LIT:0> ) <EOL> if id in self . tasks_to_kill : <EOL> pass <EOL> try : <EOL> func ( ) <EOL> except ( SystemError , MemoryError ) , e : <EOL> self . failfunc ( str ( e ) ) <EOL> return <EOL> except KeyboardInterrupt : <EOL> return <EOL> except : <EOL> if self . noisy : <EOL> self . exception ( ) <EOL> self . sockethandler . close_dead ( ) <EOL> self . sockethandler . handle_events ( events ) <EOL> if self . doneflag . isSet ( ) : <EOL> return <EOL> self . sockethandler . close_dead ( ) <EOL> except ( SystemError , MemoryError ) , e : <EOL> self . failfunc ( str ( e ) ) <EOL> return <EOL> except error : <EOL> if self . doneflag . isSet ( ) : <EOL> return <EOL> except KeyboardInterrupt : <EOL> return <EOL> except : <EOL> self . exception ( ) <EOL> if self . exccount > <NUM_LIT:10> : <EOL> return <EOL> finally : <EOL> self . finished . set ( ) <EOL> def is_finished ( self ) : <EOL> return self . finished . isSet ( ) <EOL> def wait_until_finished ( self ) : <EOL> self . finished . wait ( ) <EOL> def _kill_tasks ( self ) : <EOL> if self . tasks_to_kill : <EOL> new_funcs = [ ] <EOL> for ( t , func , id ) in self . funcs : <EOL> if id not in self . tasks_to_kill : <EOL> new_funcs . append ( ( t , func , id ) ) <EOL> self . funcs = new_funcs <EOL> self . tasks_to_kill = [ ] <EOL> def kill_tasks ( self , id ) : <EOL> self . tasks_to_kill . append ( id ) <EOL> def exception ( self , kbint = False ) : <EOL> if not kbint : <EOL> self . excflag . set ( ) <EOL> self . exccount += <NUM_LIT:1> <EOL> if self . errorfunc is None : <EOL> print_exc ( ) <EOL> else : <EOL> data = StringIO ( ) <EOL> print_exc ( file = data ) <EOL> if not kbint : <EOL> self . errorfunc ( data . getvalue ( ) ) <EOL> def shutdown ( self ) : <EOL> self . sockethandler . shutdown ( ) </s>
