<s> from Timeout import Timeout <EOL> from Udp_server import Udp_server , Udp_server_opts <EOL> from Time . MonoTime import MonoTime <EOL> from Math . recfilter import recfilter <EOL> from Rtp_proxy_cmd import Rtp_proxy_cmd <EOL> from time import time <EOL> from hashlib import md5 <EOL> from random import random <EOL> def getnretrans ( first_rert , timeout ) : <EOL> n = <NUM_LIT:0> <EOL> while True : <EOL> timeout -= first_rert <EOL> if timeout < <NUM_LIT:0> : <EOL> break <EOL> first_rert *= <NUM_LIT> <EOL> n += <NUM_LIT:1> <EOL> return n <EOL> class Rtp_proxy_client_udp ( object ) : <EOL> pending_requests = None <EOL> is_local = False <EOL> worker = None <EOL> uopts = None <EOL> global_config = None <EOL> delay_flt = None <EOL> ploss_out_rate = <NUM_LIT:0.0> <EOL> pdelay_out_max = <NUM_LIT:0.0> <EOL> def __init__ ( self , global_config , address , bind_address = None , family = None , nworkers = None ) : <EOL> self . address = address <EOL> self . is_local = False <EOL> self . uopts = Udp_server_opts ( bind_address , self . process_reply , family ) <EOL> self . uopts . flags = <NUM_LIT:0> <EOL> self . uopts . ploss_out_rate = self . ploss_out_rate <EOL> self . uopts . pdelay_out_max = self . pdelay_out_max <EOL> if nworkers != None : <EOL> self . uopts . nworkers = nworkers <EOL> self . worker = Udp_server ( global_config , self . uopts ) <EOL> self . pending_requests = { } <EOL> self . global_config = global_config <EOL> self . delay_flt = recfilter ( <NUM_LIT> , <NUM_LIT> ) <EOL> def send_command ( self , command , result_callback = None , * callback_parameters ) : <EOL> cookie = md5 ( str ( random ( ) ) + str ( time ( ) ) ) . hexdigest ( ) <EOL> next_retr = self . delay_flt . lastval * <NUM_LIT> <EOL> rtime = <NUM_LIT> <EOL> if isinstance ( command , Rtp_proxy_cmd ) : <EOL> if command . type == '<STR_LIT:I>' : <EOL> rtime = <NUM_LIT> <EOL> if command . type == '<STR_LIT>' : <EOL> rtime = <NUM_LIT:1.0> <EOL> nretr = command . nretr <EOL> command = str ( command ) <EOL> else : <EOL> if command . startswith ( '<STR_LIT:I>' ) : <EOL> rtime = <NUM_LIT> <EOL> elif command . startswith ( '<STR_LIT>' ) : <EOL> rtime = <NUM_LIT:1.0> <EOL> nretr = None <EOL> if nretr == None : <EOL> nretr = getnretrans ( next_retr , rtime ) <EOL> command = '<STR_LIT>' % ( cookie , command ) <EOL> timer = Timeout ( self . retransmit , next_retr , <NUM_LIT:1> , cookie ) <EOL> stime = MonoTime ( ) <EOL> self . worker . send_to ( command , self . address ) <EOL> nretr -= <NUM_LIT:1> <EOL> self . pending_requests [ cookie ] = ( next_retr , nretr , timer , command , result_callback , stime , callback_parameters ) <EOL> def retransmit ( self , cookie ) : <EOL> next_retr , triesleft , timer , command , result_callback , stime , callback_parameters = self . pending_requests [ cookie ] <EOL> if triesleft <= <NUM_LIT:0> or self . worker == None : <EOL> del self . pending_requests [ cookie ] <EOL> self . go_offline ( ) <EOL> if result_callback != None : <EOL> result_callback ( None , * callback_parameters ) <EOL> return <EOL> next_retr *= <NUM_LIT:2> <EOL> timer = Timeout ( self . retransmit , next_retr , <NUM_LIT:1> , cookie ) <EOL> stime = MonoTime ( ) <EOL> self . worker . send_to ( command , self . address ) <EOL> triesleft -= <NUM_LIT:1> <EOL> self . pending_requests [ cookie ] = ( next_retr , triesleft , timer , command , result_callback , stime , callback_parameters ) <EOL> def process_reply ( self , data , address , worker , rtime ) : <EOL> try : <EOL> cookie , result = data . split ( None , <NUM_LIT:1> ) <EOL> except : <EOL> print ( '<STR_LIT>' % data ) <EOL> return <EOL> parameters = self . pending_requests . pop ( cookie , None ) <EOL> if parameters == None : <EOL> return <EOL> next_retr , triesleft , timer , command , result_callback , stime , callback_parameters = parameters <EOL> timer . cancel ( ) <EOL> if result_callback != None : <EOL> result_callback ( result . strip ( ) , * callback_parameters ) <EOL> self . delay_flt . apply ( rtime - stime ) <EOL> def reconnect ( self , address , bind_address = None ) : <EOL> self . address = address <EOL> if bind_address != self . uopts . laddress : <EOL> self . uopts . laddress = bind_address <EOL> self . worker . shutdown ( ) <EOL> self . worker = Udp_server ( self . global_config , self . uopts ) <EOL> self . delay_flt = recfilter ( <NUM_LIT> , <NUM_LIT> ) <EOL> def shutdown ( self ) : <EOL> self . worker . shutdown ( ) <EOL> self . worker = None <EOL> def get_rtpc_delay ( self ) : <EOL> return self . delay_flt . lastval <EOL> class selftest ( object ) : <EOL> def gotreply ( self , * args ) : <EOL> from twisted . internet import reactor <EOL> print args <EOL> reactor . crash ( ) <EOL> def run ( self ) : <EOL> import os <EOL> from twisted . internet import reactor <EOL> global_config = { } <EOL> global_config [ '<STR_LIT>' ] = os . getpid ( ) <EOL> rtpc = Rtp_proxy_client_udp ( global_config , ( '<STR_LIT:127.0.0.1>' , <NUM_LIT> ) , None ) <EOL> os . system ( '<STR_LIT>' % global_config [ '<STR_LIT>' ] ) <EOL> rtpc . send_command ( '<STR_LIT>' , self . gotreply ) <EOL> reactor . run ( ) <EOL> rtpc . reconnect ( ( '<STR_LIT:localhost>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> os . system ( '<STR_LIT>' % global_config [ '<STR_LIT>' ] ) <EOL> rtpc . send_command ( '<STR_LIT>' , self . gotreply ) <EOL> reactor . run ( ) <EOL> rtpc . reconnect ( ( '<STR_LIT:localhost>' , <NUM_LIT> ) , ( '<STR_LIT:127.0.0.1>' , <NUM_LIT> ) ) <EOL> os . system ( '<STR_LIT>' % global_config [ '<STR_LIT>' ] ) <EOL> rtpc . send_command ( '<STR_LIT>' , self . gotreply ) <EOL> reactor . run ( ) <EOL> rtpc . shutdown ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> selftest ( ) . run ( ) </s>
