def on_get_chunk(self, interface, response): '''Handle receiving a chunk of block headers''' if self.bc_requests: req_if, data = self.bc_requests[0] req_idx = data.get('chunk_idx') # Ignore unsolicited chunks if req_if == interface and req_idx == response['params'][0]: idx = self.blockchain.connect_chunk(req_idx, response['result']) # If not finished, get the next chunk if idx < 0 or not idx or self.get_local_height() >= data['if_height']: self.bc_requests.popleft() if not idx: interface.print_error("header didn't match checkpoint, dismissing interface") interface.stop() else: self.request_chunk(interface, data, idx)
def on_get_header(self, interface, response): '''Handle receiving a single block header''' if self.bc_requests: req_if, data = self.bc_requests[0] req_height = data.get('header_height', -1) # Ignore unsolicited headers if req_if == interface and req_height == response['params'][0]: next_height = self.blockchain.connect_header(data['chain'], response['result']) # If not finished, get the next header if next_height in [True, False]: self.bc_requests.popleft() if next_height: self.notify('updated') else: interface.print_error("header didn't connect, dismissing interface") interface.stop() else: self.request_header(interface, data, next_height)
def on_get_chunk(self, interface, response): '''Handle receiving a chunk of block headers''' if self.bc_requests: req_if, data = self.bc_requests[0] req_idx = data.get('chunk_idx') # Ignore unsolicited chunks if req_if == interface and req_idx == response['params'][0]: idx = self.blockchain.connect_chunk(req_idx, response['result']) # If not finished, get the next chunk if idx < 0 or not idx or self.get_local_height( ) >= data['if_height']: self.bc_requests.popleft() if not idx: interface.print_error( "header didn't match checkpoint, dismissing interface" ) interface.stop() else: self.request_chunk(interface, data, idx)
def handle_bc_requests(self): '''Work through each interface that has notified us of a new header. Send it requests if it is ahead of our blockchain object''' while self.bc_requests: interface, data = self.bc_requests.popleft() # If the connection was lost move on if not interface.is_connected(): continue req_time = data.get('req_time') if not req_time: # No requests sent yet. This interface has a new height. # Request headers if it is ahead of our blockchain if not self.bc_request_headers(interface, data): continue elif time.time() - req_time > 10: interface.print_error("blockchain request timed out") interface.stop() continue # Put updated request state back at head of deque self.bc_requests.appendleft((interface, data)) break
# seconds/0.03 seconds ~ 20 times. for i in range(20): if (interface.readButton() == 1): x += 1 # if the button is recorded to be pressed, stop the robot. if (x >= 1): interface.drive(0, 0, 0, 0) interface.sleep() x = 0 interface.sleep() # if the button is pressed again, breaks the loop and starts # the robot. while (interface.readButton() == 0): if (interface.readButton() == 1): break # waits 0.015 seconds for codes to process. interface.sleep() # resets the button press count to 0. x = 0 # waits another 0.015 seconds for the codes to process. interface.sleep() # stops the robot from driving. interface.drive(0, 0, 0, 0) # stops robot. interface.stop() # closes connection. connection.close()