#NEWLINE# def connect( self ) : #NEWLINE# #INDENT# 'Connects to a downstream-node server.\n        ' #NEWLINE# if ( self . token is None ) : #NEWLINE# #INDENT# if ( self . address is None ) : #NEWLINE# #INDENT# raise DownstreamError ( 'If no token is specified, address must be.' ) #NEWLINE# #UNINDENT# url = '{0}/new/{1}' . format ( self . api_url , self . address ) #NEWLINE# if ( ( self . msg != '' ) and ( self . sig != '' ) ) : #NEWLINE# #INDENT# data = { #NEWLINE# #INDENT# 'message' : self . msg , #NEWLINE# 'signature' : self . sig , #NEWLINE# #UNINDENT# } #NEWLINE# headers = { #NEWLINE# #INDENT# 'Content-Type' : 'application/json' , #NEWLINE# #UNINDENT# } #NEWLINE# resp = self . session . post ( url , data = json . dumps ( data ) , headers = headers , verify = self . requests_verify_arg ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# resp = self . session . get ( url , verify = self . requests_verify_arg ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# url = '{0}/heartbeat/{1}' . format ( self . api_url , self . token ) #NEWLINE# resp = self . session . get ( url , verify = self . requests_verify_arg ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# r_json = handle_json_response ( resp ) #NEWLINE# #UNINDENT# except DownstreamError as ex : #NEWLINE# #INDENT# raise DownstreamError ( 'Unable to connect: {0}' . format ( str ( ex ) ) ) #NEWLINE# #UNINDENT# for k in [ 'token' , 'heartbeat' , 'type' ] : #NEWLINE# #INDENT# if ( k not in r_json ) : #NEWLINE# #INDENT# raise DownstreamError ( 'Malformed response from server.' ) #NEWLINE# #UNINDENT# #UNINDENT# if ( r_json [ 'type' ] not in heartbeat_types . keys ( ) ) : #NEWLINE# #INDENT# raise DownstreamError ( 'Unknown Heartbeat Type' ) #NEWLINE# #UNINDENT# self . token = r_json [ 'token' ] #NEWLINE# self . heartbeat = heartbeat_types [ r_json [ 'type' ] ] . fromdict ( r_json [ 'heartbeat' ] ) #NEWLINE# token = binascii . unhexlify ( self . token ) #NEWLINE# token_hash = hashlib . sha256 ( token ) . hexdigest ( ) [ : 20 ] #NEWLINE# self . logger . info ( 'Confirmed token: {0}' . format ( self . token ) ) #NEWLINE# self . thread_manager . stats . set ( 'token' , self . token ) #NEWLINE# self . logger . info ( 'Farmer id: {0}' . format ( token_hash ) )