<s> '''<STR_LIT>''' <EOL> import os , random , select , socket , sys , threading , time <EOL> from threading import Lock <EOL> import cPickle as pickle <EOL> from concoord . pack import * <EOL> from concoord . enums import * <EOL> from concoord . utils import * <EOL> from concoord . exception import * <EOL> from concoord . connection import ConnectionPool , Connection <EOL> from concoord . message import * <EOL> from concoord . pvalue import PValueSet <EOL> try : <EOL> import dns <EOL> import dns . resolver <EOL> import dns . exception <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> class ClientProxy ( ) : <EOL> def __init__ ( self , bootstrap , timeout = <NUM_LIT> , debug = False , token = None ) : <EOL> self . debug = debug <EOL> self . timeout = timeout <EOL> self . domainname = None <EOL> self . token = token <EOL> self . socket = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> self . socket . setsockopt ( socket . IPPROTO_TCP , socket . TCP_NODELAY , <NUM_LIT:1> ) <EOL> self . writelock = Lock ( ) <EOL> self . bootstraplist = self . discoverbootstrap ( bootstrap ) <EOL> if len ( self . bootstraplist ) == <NUM_LIT:0> : <EOL> raise ConnectionError ( "<STR_LIT>" ) <EOL> if not self . connecttobootstrap ( ) : <EOL> raise ConnectionError ( "<STR_LIT>" ) <EOL> myaddr = findOwnIP ( ) <EOL> myport = self . socket . getsockname ( ) [ <NUM_LIT:1> ] <EOL> self . me = Peer ( myaddr , myport , NODE_CLIENT ) <EOL> self . commandnumber = random . randint ( <NUM_LIT:1> , sys . maxint ) <EOL> def _getipportpairs ( self , bootaddr , bootport ) : <EOL> for node in socket . getaddrinfo ( bootaddr , bootport , socket . AF_INET , socket . SOCK_STREAM ) : <EOL> yield ( node [ <NUM_LIT:4> ] [ <NUM_LIT:0> ] , bootport ) <EOL> def getbootstrapfromdomain ( self , domainname ) : <EOL> tmpbootstraplist = [ ] <EOL> try : <EOL> answers = dns . resolver . query ( '<STR_LIT>' + domainname , '<STR_LIT>' ) <EOL> for rdata in answers : <EOL> for peer in self . _getipportpairs ( str ( rdata . target ) , rdata . port ) : <EOL> if peer not in tmpbootstraplist : <EOL> tmpbootstraplist . append ( peer ) <EOL> except ( dns . resolver . NXDOMAIN , dns . exception . Timeout ) : <EOL> if self . debug : print "<STR_LIT>" <EOL> return tmpbootstraplist <EOL> def discoverbootstrap ( self , givenbootstrap ) : <EOL> tmpbootstraplist = [ ] <EOL> try : <EOL> for bootstrap in givenbootstrap . split ( "<STR_LIT:U+002C>" ) : <EOL> bootstrap = bootstrap . strip ( ) <EOL> if bootstrap . find ( "<STR_LIT::>" ) >= <NUM_LIT:0> : <EOL> bootaddr , bootport = bootstrap . split ( "<STR_LIT::>" ) <EOL> for peer in self . _getipportpairs ( bootaddr , int ( bootport ) ) : <EOL> if peer not in tmpbootstraplist : <EOL> tmpbootstraplist . append ( peer ) <EOL> else : <EOL> self . domainname = bootstrap <EOL> tmpbootstraplist = self . getbootstrapfromdomain ( self . domainname ) <EOL> except ValueError : <EOL> if self . debug : print "<STR_LIT>" <EOL> self . _graceexit ( ) <EOL> return tmpbootstraplist <EOL> def connecttobootstrap ( self ) : <EOL> connected = False <EOL> for boottuple in self . bootstraplist : <EOL> try : <EOL> self . socket . close ( ) <EOL> self . socket = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> self . socket . connect ( boottuple ) <EOL> self . conn = Connection ( self . socket ) <EOL> self . conn . settimeout ( CLIENTRESENDTIMEOUT ) <EOL> self . bootstrap = boottuple <EOL> connected = True <EOL> if self . debug : print "<STR_LIT>" , boottuple <EOL> break <EOL> except socket . error , e : <EOL> if self . debug : print "<STR_LIT>" , e <EOL> continue <EOL> return connected <EOL> def trynewbootstrap ( self ) : <EOL> if self . domainname : <EOL> self . bootstraplist = self . getbootstrapfromdomain ( self . domainname ) <EOL> else : <EOL> oldbootstrap = self . bootstraplist . pop ( <NUM_LIT:0> ) <EOL> self . bootstraplist . append ( oldbootstrap ) <EOL> return self . connecttobootstrap ( ) <EOL> def invoke_command ( self , * args ) : <EOL> resend = True <EOL> sendcount = - <NUM_LIT:1> <EOL> lastreplycode = - <NUM_LIT:1> <EOL> self . commandnumber += <NUM_LIT:1> <EOL> clientmsg = create_message ( MSG_CLIENTREQUEST , self . me , <EOL> { FLD_PROPOSAL : Proposal ( self . me , self . commandnumber , args ) , <EOL> FLD_TOKEN : self . token , <EOL> FLD_CLIENTBATCH : False } ) <EOL> while True : <EOL> sendcount += <NUM_LIT:1> <EOL> clientmsg [ FLD_SENDCOUNT ] = sendcount <EOL> if resend : <EOL> success = self . conn . send ( clientmsg ) <EOL> if not success : <EOL> self . reconfigure ( ) <EOL> continue <EOL> resend = False <EOL> try : <EOL> for reply in self . conn . received_bytes ( ) : <EOL> if reply and reply . type == MSG_CLIENTREPLY : <EOL> if reply . replycode == CR_OK : <EOL> return reply . reply <EOL> elif reply . replycode == CR_UNBLOCK : <EOL> assert lastreplycode == CR_BLOCK , "<STR_LIT>" <EOL> return reply . reply <EOL> elif reply . replycode == CR_EXCEPTION : <EOL> raise Exception ( pickle . loads ( reply . reply ) ) <EOL> elif reply . replycode == CR_INPROGRESS or reply . replycode == CR_BLOCK : <EOL> lastreplycode = reply . replycode <EOL> continue <EOL> elif reply . replycode == CR_REJECTED or reply . replycode == CR_LEADERNOTREADY : <EOL> resend = True <EOL> self . reconfigure ( ) <EOL> continue <EOL> else : <EOL> print "<STR_LIT>" <EOL> except ConnectionError : <EOL> resend = True <EOL> self . reconfigure ( ) <EOL> continue <EOL> except KeyboardInterrupt : <EOL> self . _graceexit ( ) <EOL> def reconfigure ( self ) : <EOL> if not self . trynewbootstrap ( ) : <EOL> raise ConnectionError ( "<STR_LIT>" ) <EOL> def _graceexit ( self ) : <EOL> os . _exit ( <NUM_LIT:0> ) </s>
