<s> import pyghmi . exceptions as exc <EOL> import struct <EOL> from pyghmi . ipmi . private import constants <EOL> from pyghmi . ipmi . private import session <EOL> class Console ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , bmc , userid , password , <EOL> iohandler , port = <NUM_LIT> , <EOL> force = False , kg = None ) : <EOL> self . keepaliveid = None <EOL> self . connected = False <EOL> self . broken = False <EOL> self . out_handler = iohandler <EOL> self . remseq = <NUM_LIT:0> <EOL> self . myseq = <NUM_LIT:0> <EOL> self . lastsize = <NUM_LIT:0> <EOL> self . retriedpayload = <NUM_LIT:0> <EOL> self . pendingoutput = [ ] <EOL> self . awaitingack = False <EOL> self . activated = False <EOL> self . force_session = force <EOL> self . ipmi_session = session . Session ( bmc = bmc , <EOL> userid = userid , <EOL> password = password , <EOL> port = port , <EOL> kg = kg , <EOL> onlogon = self . _got_session ) <EOL> session . Session . wait_for_rsp ( <NUM_LIT:0> ) <EOL> def _got_session ( self , response ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:error>' in response : <EOL> self . _print_error ( response [ '<STR_LIT:error>' ] ) <EOL> return <EOL> response = self . ipmi_session . raw_command ( netfn = <NUM_LIT> , command = <NUM_LIT> , <EOL> data = ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> sol_activate_codes = { <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> } <EOL> if '<STR_LIT:code>' in response and response [ '<STR_LIT:code>' ] : <EOL> if response [ '<STR_LIT:code>' ] in constants . ipmi_completion_codes : <EOL> self . _print_error ( <EOL> constants . ipmi_completion_codes [ response [ '<STR_LIT:code>' ] ] ) <EOL> return <EOL> elif response [ '<STR_LIT:code>' ] == <NUM_LIT> : <EOL> if self . force_session and not self . retriedpayload : <EOL> self . retriedpayload = <NUM_LIT:1> <EOL> sessrsp = self . ipmi_session . raw_command ( <EOL> netfn = <NUM_LIT> , <EOL> command = <NUM_LIT> , <EOL> data = ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> self . _got_session ( sessrsp ) <EOL> return <EOL> else : <EOL> self . _print_error ( '<STR_LIT>' ) <EOL> return <EOL> elif response [ '<STR_LIT:code>' ] in sol_activate_codes : <EOL> self . _print_error ( sol_activate_codes [ response [ '<STR_LIT:code>' ] ] ) <EOL> return <EOL> else : <EOL> self . _print_error ( <EOL> '<STR_LIT>' % <EOL> response [ '<STR_LIT:code>' ] ) <EOL> return <EOL> if '<STR_LIT:error>' in response : <EOL> self . _print_error ( response [ '<STR_LIT:error>' ] ) <EOL> return <EOL> self . activated = True <EOL> data = response [ '<STR_LIT:data>' ] <EOL> self . maxoutcount = ( data [ <NUM_LIT:5> ] << <NUM_LIT:8> ) + data [ <NUM_LIT:4> ] <EOL> if ( data [ <NUM_LIT:8> ] + ( data [ <NUM_LIT:9> ] << <NUM_LIT:8> ) ) not in ( <NUM_LIT> , <NUM_LIT> ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> self . keepaliveid = self . ipmi_session . register_keepalive ( <EOL> cmd = { '<STR_LIT>' : <NUM_LIT:6> , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT:data>' : ( <NUM_LIT:1> , <NUM_LIT:1> ) } , <EOL> callback = self . _got_payload_instance_info ) <EOL> self . ipmi_session . sol_handler = self . _got_sol_payload <EOL> self . connected = True <EOL> if len ( self . pendingoutput ) > <NUM_LIT:0> : <EOL> self . _sendpendingoutput ( ) <EOL> def _got_payload_instance_info ( self , response ) : <EOL> if '<STR_LIT:error>' in response : <EOL> self . activated = False <EOL> self . ipmi_session . unregister_keepalive ( self . keepaliveid ) <EOL> self . _print_error ( response [ '<STR_LIT:error>' ] ) <EOL> return <EOL> currowner = struct . unpack ( <EOL> "<STR_LIT>" , struct . pack ( '<STR_LIT>' , * response [ '<STR_LIT:data>' ] [ : <NUM_LIT:4> ] ) ) <EOL> if currowner [ <NUM_LIT:0> ] != self . ipmi_session . sessionid : <EOL> self . activated = False <EOL> self . ipmi_session . unregister_keepalive ( self . keepaliveid ) <EOL> self . _print_error ( '<STR_LIT>' ) <EOL> return <EOL> def _addpendingdata ( self , data ) : <EOL> if isinstance ( data , dict ) : <EOL> self . pendingoutput . append ( data ) <EOL> else : <EOL> if ( len ( self . pendingoutput ) == <NUM_LIT:0> or <EOL> isinstance ( self . pendingoutput [ - <NUM_LIT:1> ] , dict ) ) : <EOL> self . pendingoutput . append ( data ) <EOL> else : <EOL> self . pendingoutput [ - <NUM_LIT:1> ] += data <EOL> def _got_cons_input ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> self . _addpendingdata ( handle . read ( ) ) <EOL> if not self . awaitingack : <EOL> self . _sendpendingoutput ( ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . ipmi_session . unregister_keepalive ( self . keepaliveid ) <EOL> if self . activated : <EOL> try : <EOL> self . ipmi_session . raw_command ( netfn = <NUM_LIT:6> , command = <NUM_LIT> , <EOL> data = ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> except exc . IpmiException : <EOL> pass <EOL> def send_data ( self , data ) : <EOL> if self . broken : <EOL> return <EOL> self . _addpendingdata ( data ) <EOL> if not self . connected : <EOL> return <EOL> if not self . awaitingack : <EOL> self . _sendpendingoutput ( ) <EOL> def send_break ( self ) : <EOL> self . _addpendingdata ( { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> if not self . connected : <EOL> return <EOL> if not self . awaitingack : <EOL> self . _sendpendingoutput ( ) <EOL> @ classmethod <EOL> def wait_for_rsp ( cls , timeout ) : <EOL> """<STR_LIT>""" <EOL> return session . Session . wait_for_rsp ( timeout = timeout ) <EOL> def _sendpendingoutput ( self ) : <EOL> if isinstance ( self . pendingoutput [ <NUM_LIT:0> ] , dict ) : <EOL> if '<STR_LIT>' in self . pendingoutput [ <NUM_LIT:0> ] : <EOL> self . _sendoutput ( "<STR_LIT>" , sendbreak = True ) <EOL> else : <EOL> raise ValueError <EOL> del self . pendingoutput [ <NUM_LIT:0> ] <EOL> return <EOL> if len ( self . pendingoutput [ <NUM_LIT:0> ] ) > self . maxoutcount : <EOL> chunk = self . pendingoutput [ <NUM_LIT:0> ] [ : self . maxoutcount ] <EOL> self . pendingoutput [ <NUM_LIT:0> ] = self . pendingoutput [ <NUM_LIT:0> ] [ self . maxoutcount : ] <EOL> else : <EOL> chunk = self . pendingoutput [ <NUM_LIT:0> ] <EOL> del self . pendingoutput [ <NUM_LIT:0> ] <EOL> self . _sendoutput ( chunk ) <EOL> def _sendoutput ( self , output , sendbreak = False ) : <EOL> self . myseq += <NUM_LIT:1> <EOL> self . myseq &= <NUM_LIT> <EOL> if self . myseq == <NUM_LIT:0> : <EOL> self . myseq = <NUM_LIT:1> <EOL> breakbyte = <NUM_LIT:0> <EOL> if sendbreak : <EOL> breakbyte = <NUM_LIT> <EOL> payload = struct . pack ( "<STR_LIT>" , self . myseq , <NUM_LIT:0> , <NUM_LIT:0> , breakbyte ) <EOL> payload += output <EOL> self . lasttextsize = len ( output ) <EOL> needskeepalive = False <EOL> if self . lasttextsize == <NUM_LIT:0> : <EOL> needskeepalive = True <EOL> self . awaitingack = True <EOL> payload = struct . unpack ( "<STR_LIT>" % len ( payload ) , payload ) <EOL> self . lastpayload = payload <EOL> self . send_payload ( payload , needskeepalive = needskeepalive ) <EOL> def send_payload ( self , payload , payload_type = <NUM_LIT:1> , retry = True , <EOL> needskeepalive = False ) : <EOL> while not ( self . connected or self . broken ) : <EOL> session . Session . wait_for_rsp ( timeout = <NUM_LIT:10> ) <EOL> if not self . ipmi_session . logged : <EOL> raise exc . IpmiException ( '<STR_LIT>' ) <EOL> self . ipmi_session . send_payload ( payload , <EOL> payload_type = payload_type , <EOL> retry = retry , <EOL> needskeepalive = needskeepalive ) <EOL> def _print_info ( self , info ) : <EOL> self . _print_data ( { '<STR_LIT:info>' : info } ) <EOL> def _print_error ( self , error ) : <EOL> self . broken = True <EOL> if type ( error ) == dict : <EOL> self . _print_data ( error ) <EOL> else : <EOL> self . _print_data ( { '<STR_LIT:error>' : error } ) <EOL> def _print_data ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . out_handler ( data ) <EOL> def _got_sol_payload ( self , payload ) : <EOL> """<STR_LIT>""" <EOL> if type ( payload ) == dict : <EOL> self . activated = False <EOL> self . _print_error ( payload ) <EOL> return <EOL> newseq = payload [ <NUM_LIT:0> ] & <NUM_LIT> <EOL> ackseq = payload [ <NUM_LIT:1> ] & <NUM_LIT> <EOL> ackcount = payload [ <NUM_LIT:2> ] <EOL> nacked = payload [ <NUM_LIT:3> ] & <NUM_LIT> <EOL> poweredoff = payload [ <NUM_LIT:3> ] & <NUM_LIT> <EOL> deactivated = payload [ <NUM_LIT:3> ] & <NUM_LIT> <EOL> breakdetected = payload [ <NUM_LIT:3> ] & <NUM_LIT> <EOL> remdata = "<STR_LIT>" <EOL> remdatalen = <NUM_LIT:0> <EOL> if newseq != <NUM_LIT:0> : <EOL> if len ( payload ) > <NUM_LIT:4> : <EOL> remdatalen = len ( payload [ <NUM_LIT:4> : ] ) <EOL> remdata = struct . pack ( "<STR_LIT>" % remdatalen , * payload [ <NUM_LIT:4> : ] ) <EOL> if newseq == self . remseq : <EOL> if remdatalen > self . lastsize : <EOL> remdata = remdata [ <NUM_LIT:4> + self . lastsize : ] <EOL> else : <EOL> remdata = "<STR_LIT>" <EOL> else : <EOL> self . remseq = newseq <EOL> self . lastsize = remdatalen <EOL> if remdata : <EOL> self . _print_data ( remdata ) <EOL> ackpayload = ( <NUM_LIT:0> , self . remseq , remdatalen , <NUM_LIT:0> ) <EOL> try : <EOL> self . send_payload ( ackpayload , retry = False ) <EOL> except exc . IpmiException : <EOL> self . close ( ) <EOL> if self . myseq != <NUM_LIT:0> and ackseq == self . myseq : <EOL> self . awaitingack = False <EOL> if nacked and not breakdetected : <EOL> if poweredoff : <EOL> self . _print_info ( "<STR_LIT>" ) <EOL> if deactivated : <EOL> self . activated = False <EOL> self . _print_error ( "<STR_LIT>" ) <EOL> else : <EOL> newtext = self . lastpayload [ <NUM_LIT:4> + ackcount : ] <EOL> newtext = struct . pack ( "<STR_LIT:B>" * len ( newtext ) , * newtext ) <EOL> if ( self . pendingoutput and <EOL> not isinstance ( self . pendingoutput [ <NUM_LIT:0> ] , dict ) ) : <EOL> self . pendingoutput [ <NUM_LIT:0> ] = newtext + self . pendingoutput [ <NUM_LIT:0> ] <EOL> else : <EOL> self . pendingoutput = [ newtext ] + self . pendingoutput <EOL> self . _sendpendingoutput ( ) <EOL> if len ( self . pendingoutput ) > <NUM_LIT:0> : <EOL> self . _sendpendingoutput ( ) <EOL> elif ackseq != <NUM_LIT:0> and self . awaitingack : <EOL> self . send_payload ( payload = self . lastpayload ) <EOL> def main_loop ( self ) : <EOL> """<STR_LIT>""" <EOL> while ( <NUM_LIT:1> ) : <EOL> session . Session . wait_for_rsp ( timeout = <NUM_LIT> ) </s>
