Exemplo n.º 1
0
 def handle(self):
     while True:
         #try:
             #receive the gpsdata
             self.data = self.request.recv(4096)
             if not self.data : break
             data = rsakey.decrypt_text(self.data)
             data = bytes.decode(data)
             
             managed_data = data.split("-")
             gps_data = managed_data[0]
             number_data = managed_data[1]
             data_from = int(number_data)
             print(data)
             
             if data_from != 0:        
                 # to record how many data has been transfered
                 global num_of_gpsdata
                 num_of_gpsdata +=1
                     
                 # write the data to the file 'gps_data.txt'
                 with open('gps_data.txt','a') as f:
                     f.write(data + '\n')
                     f.close()
                  
             global mark_str
             global num_of_node
             if num_of_gpsdata % (num_of_node - 1) == 0:
                 beidou_location = beidou_data.beidou_chuankou()
                 beidou_location_str = str(beidou_location, encoding = "utf8")
                 with open('gps_data.txt','a') as f:
                     f.write(beidou_location_str + '-' + mark_str + '\n')
                     f.close()
                 print(beidou_location_str + '-' + mark_str )
                  
             # decide when to add a new block    
             global break_of_block   
             if num_of_gpsdata == break_of_block :
                 with open('gps_data.txt') as f:
                     contents = f.read()
                     f.close()
                 with open('gps_data.txt', 'w') as f:
                     f.truncate()
                     f.close()
                     
                 # Add blocks to the chain
                 global previous_block
                 global blockchain
                 block_to_add = block.next_block(previous_block,contents)
                 blockchain.append(block_to_add)
                 previous_block = block_to_add
                 # Tell everyone about it!
                 #print(previous_block.data)
                 print ("Block #{} has been added to the blockchain!".format(block_to_add.index))
                 print ("Hash: {}\n".format(block_to_add.hash))
                 #send the block to the computer
                 data = client.client(host_list[0], 9000, pickle.dumps(previous_block))
                 
             #decied when to change the server    
             if num_of_gpsdata == break_of_block or num_of_gpsdata == break_of_block + 1:
                 
                 global mark_int
                 #change the server to raspberry 3
                 previous_block.change_miner(mark_int + 1)
                 
                 #copy the previous_block
                 block_to_send = copy.deepcopy(previous_block)
                 
                 for i in range(1, num_of_node + 1):
                     if data_from == i:
                         block_to_send.data = str.encode(block_to_send.data)
                         block_to_send.data = rsakey.encrypt_text(i, block_to_send.data)                                                                                                              
                         
                 #send the block to the client
                 packaged_block_to_send = pickle.dumps(block_to_send)
                 self.request.sendall(packaged_block_to_send)
                 
                 if num_of_gpsdata == break_of_block + 1:
                     with open('gps_data.txt', 'w') as f:
                         f.truncate()
                         f.close()
                     print('exit')
                     self.server.shutdown()
                     self.request.close()
                     break
             
             #send blockchian to the client
             block_to_send = copy.deepcopy(previous_block)
             for i in range(1, num_of_node + 1):
                 if data_from == i:
                     block_to_send.data = str.encode(block_to_send.data)
                     block_to_send.data = rsakey.encrypt_text(i, block_to_send.data)                                                                                                                                                                                                                          
                     
             #send the block to the client
             packaged_block_to_send = pickle.dumps(block_to_send)
             self.request.sendall(packaged_block_to_send)
Exemplo n.º 2
0
blockchain = [previous_block]
#the record of the block index
block_index = 0
#the record of the block hash
hash_check = previous_block.hash
time.sleep(break_of_time)

#the first server is raspberry 1
host = host_list[1]
key_num = 1

while True:

    while True:
        #run the client
        beidou_location = beidou_data.beidou_chuankou()
        beidou_location_str = str(beidou_location, encoding = "utf8")
        message_to_send = (beidou_location_str + "-" + mark_str).encode("utf8")
        
        
        encrypt_message_to_send = rsakey.encrypt_text(key_num, message_to_send)
        data = client.client(host, port, encrypt_message_to_send)
        
        previous_block = pickle.loads(data)
        previous_block.data = rsakey.decrypt_text(previous_block.data)
        previous_block.data = bytes.decode(previous_block.data)
        
        #decide whne to add the new block
        if previous_block.index != block_index:
            if previous_block.previous_hash == hash_check:
                #add the last block to the blockchain