<s> from __future__ import absolute_import <EOL> import zmq <EOL> from elevator . constants import * <EOL> from . io import output_result <EOL> from . errors import * <EOL> from . helpers import success , fail <EOL> from . message import Request , ResponseHeader , Response <EOL> class Client ( object ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . protocol = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . endpoint = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . host = "<STR_LIT>" % ( self . protocol , self . endpoint ) <EOL> self . context = None <EOL> self . socket = None <EOL> self . timeout = kwargs . pop ( '<STR_LIT>' , <NUM_LIT> ) <EOL> self . db_uid = None <EOL> self . db_name = None <EOL> self . setup_socket ( ) <EOL> if self . ping ( ) : <EOL> self . connect ( ) <EOL> else : <EOL> failure_msg = '<STR_LIT>' . format ( self . protocol , self . endpoint ) <EOL> output_result ( FAILURE_STATUS , failure_msg ) <EOL> def __del__ ( self ) : <EOL> self . socket . close ( ) <EOL> self . context . term ( ) <EOL> def ping ( self , * args , ** kwargs ) : <EOL> pings = True <EOL> timeout = kwargs . pop ( '<STR_LIT>' , <NUM_LIT:1000> ) <EOL> orig_timeout = self . timeout <EOL> self . socket . setsockopt ( zmq . RCVTIMEO , timeout ) <EOL> request = Request ( db_uid = None , command = "<STR_LIT>" , args = [ ] ) <EOL> self . socket . send_multipart ( [ request ] ) <EOL> try : <EOL> self . socket . recv_multipart ( ) <EOL> except zmq . core . error . ZMQError : <EOL> pings = False <EOL> self . timeout = orig_timeout <EOL> self . socket . setsockopt ( zmq . RCVTIMEO , self . timeout ) <EOL> return pings <EOL> def connect ( self , db_name = None , * args , ** kwargs ) : <EOL> db_name = '<STR_LIT:default>' if db_name is None else db_name <EOL> status , datas = self . send_cmd ( None , '<STR_LIT>' , [ db_name ] , * args , ** kwargs ) <EOL> if status == FAILURE_STATUS : <EOL> return status , datas <EOL> else : <EOL> self . db_uid = datas <EOL> self . db_name = db_name <EOL> def setup_socket ( self ) : <EOL> self . context = zmq . Context ( ) <EOL> self . socket = self . context . socket ( zmq . XREQ ) <EOL> self . socket . setsockopt ( zmq . LINGER , <NUM_LIT:0> ) <EOL> self . socket . setsockopt ( zmq . RCVTIMEO , self . timeout ) <EOL> self . socket . connect ( self . host ) <EOL> def teardown_socket ( self ) : <EOL> self . socket . close ( ) <EOL> self . context . term ( ) <EOL> def _process_request ( self , command , arguments ) : <EOL> if command in [ "<STR_LIT>" ] and arguments : <EOL> return command , [ arguments ] <EOL> return command , arguments <EOL> def _process_response ( self , req_cmd , res_datas ) : <EOL> if req_cmd in [ "<STR_LIT:GET>" , "<STR_LIT>" , "<STR_LIT>" ] and res_datas : <EOL> return res_datas [ <NUM_LIT:0> ] <EOL> return res_datas <EOL> def send_cmd ( self , db_uid , command , arguments , * args , ** kwargs ) : <EOL> command , arguments = self . _process_request ( command , arguments ) <EOL> self . socket . send_multipart ( [ Request ( db_uid = db_uid , <EOL> command = command , <EOL> args = arguments , <EOL> meta = { } ) ] , ) <EOL> try : <EOL> raw_header , raw_response = self . socket . recv_multipart ( ) <EOL> header = ResponseHeader ( raw_header ) <EOL> response = Response ( raw_response ) <EOL> if header . status == FAILURE_STATUS : <EOL> return fail ( ELEVATOR_ERROR [ header . err_code ] , header . err_msg ) <EOL> except zmq . core . error . ZMQError : <EOL> return fail ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return success ( self . _process_response ( command , response . datas ) ) </s>
