コード例 #1
0
 def send_chunks(self, path,replication_degree):
     f = File(path)
     data = Data(self.db_path)
     if (not data.get_file_id(path)):
         data.add_file(path)
     chunks_number = f.generate_chunks(self.temp_dir)
     file_id=f.generate_file_id()
     date = f.get_modification_date()
     data.add_modification(path, file_id, chunks_number, date)
     chunks=f.fetch_chunks(self.temp_dir)
     acks_chunks=0
     for j in range(len(chunks)):
         chunk_file = open(self.temp_dir +chunks[j], "rb")
         body=chunk_file.read()
         chunk_file.close()
         replication_degree=str(replication_degree)
         chunk_no=str(j)
         message="PUTCHUNK " + VERSION + " " + file_id + " " + chunk_no + " " + replication_degree + CRLF + CRLF + body
         acks=False
         attempts=0
         timeout=TIMEOUT
         while(not acks and attempts<MAX_ATTEMPTS):
             self.mdb.sendto(message, (self.mdb_address, self.mdb_port))
             data.add_chunk(file_id, chunk_no, replication_degree)
             if(self.check_replication_degree(file_id,chunk_no,replication_degree,timeout)):
                 acks=True
             timeout*=2
             attempts+=1
         if(acks): acks_chunks+=1
     self.remove_chunks_from_directory(f.get_name(),self.temp_dir)
     return (acks_chunks>=chunks_number)