def block(self, connection_ind, msg): #IF BLOCK IF FOUND THIS WILL BE REMOVED #and it is not corrupted, try to write it to #the respective position in file,]. l = -1 for s in UserLib.requests: if s[0] == msg['chunkpos']: l = 0 UserLib.requests.remove(s) break if l == -1 or bitmap.bmp_isvalid(UserLib.bmp, msg['chunkpos'], UserLib.lock): raise UserLib.UnexpectedBlock if mysha1lib.mysha1( msg['chunk']) == UserLib.sha1s_map[msg['chunkpos']]: try: self.f.seek(int(msg['chunkpos']) * UserDefs.PieceSize) self.f.write(msg['chunk']) #if an Exception occurs close file stream and raise it. except IOError as detail: print("<block>:: File Error at Leecher thread:", detail) self.f.close() raise #if an Exception occurs close file stream and raise it. except Exception as detail: print("<block>:: Unexpected Exception :", detail) self.f.close() raise try: # mutual exclusion before informing bitmap. bitmap.bmp_setvalid(UserLib.bmp, msg['chunkpos'], UserLib.lock) except Exception as detail: print("<block>:: Unexpected Exception while setting bitmap:", detail) raise # increase chunks downloaded from current connection. UserLib.DownConn_map[connection_ind][2] += 1 UserLib.DownConn_map[connection_ind][8] = time.time() # inclease total chunks downloaded. UserLib.PiecesDownloaded += 1 sys.stderr.flush() sys.stderr.write( "%.2f%%\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" % (100 * UserLib.PiecesDownloaded / UserLib.BMPLENGTH)) else: print("Received corrupted block") raise UserLib.BlockCorrupted
def block(self,connection_ind,msg): #IF BLOCK IF FOUND THIS WILL BE REMOVED #and it is not corrupted, try to write it to #the respective position in file,]. l=-1 for s in UserLib.requests: if s[0] == msg['chunkpos']: l=0 UserLib.requests.remove(s) break if l == -1 or bitmap.bmp_isvalid(UserLib.bmp,msg['chunkpos'],UserLib.lock) : raise UserLib.UnexpectedBlock if mysha1lib.mysha1(msg['chunk'])==UserLib.sha1s_map[msg['chunkpos']]: try: self.f.seek(int(msg['chunkpos'])*UserDefs.PieceSize) self.f.write(msg['chunk']) #if an Exception occurs close file stream and raise it. except IOError as detail: print("<block>:: File Error at Leecher thread:",detail) self.f.close() raise #if an Exception occurs close file stream and raise it. except Exception as detail: print("<block>:: Unexpected Exception :",detail) self.f.close() raise try: # mutual exclusion before informing bitmap. bitmap.bmp_setvalid(UserLib.bmp,msg['chunkpos'],UserLib.lock) except Exception as detail: print("<block>:: Unexpected Exception while setting bitmap:",detail) raise # increase chunks downloaded from current connection. UserLib.DownConn_map[connection_ind][2]+=1 UserLib.DownConn_map[connection_ind][8]=time.time() # inclease total chunks downloaded. UserLib.PiecesDownloaded+=1 sys.stderr.flush() sys.stderr.write("%.2f%%\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"%(100*UserLib.PiecesDownloaded/UserLib.BMPLENGTH)) else: print("Received corrupted block") raise UserLib.BlockCorrupted
def create_metadatafile(filename): #try to open file for reading and go to the begginning. try: f=open(filename,'rb') filesize=f.seek(0,2) f.seek(0) #If an Exception occurs, raise it. except IOError as detail: print("Error Opening File:",detail) raise #If an Exception occurs, raise it. except Exception as detail: print("Unexpected Exception::",detail) raise block_size=UserDefs.PieceSize #Block size FileMd5=mymd5.mymd5_file(f,1000*4096) #THIS WILL BE OF NO NEED BMPLENGTH=math.ceil(filesize/UserDefs.PieceSize) #calculate length of bitmap. f.seek(0) sha1s=[] #constructor of sha-1 hash. metadata_hash=hashlib.new('sha1') #create hash of metadata file, which will be used as file identifier. for i in range(0,BMPLENGTH): try: chunk=f.read(UserDefs.PieceSize) #read each block of file #If an Exception occurs, raise it. except IOError as detail: print("IOFile Error <block> at server thread:",detail) raise #If an Exception occurs, raise it. except Exception as detail: print("Unexpected Exception <block> at server thread:",detail) raise sha1s.append(mysha1lib.mysha1(chunk)) #create sha1 for current block. metadata_hash.update(chunk) #update hash of metadata f.close() #close file metadata_dict={'filename':filename #create final metadata block ,'filesize':filesize ,'FileMd5':FileMd5 ,'sha1s_map':sha1s ,'block_size':block_size ,'metadata_hash':metadata_hash.hexdigest() } pickled=pickle.dumps(metadata_dict) #try to open file for writting and go to the begginning. try: f=open(filename+'.ant','wb') #If an Exception occurs, raise it. except IOError as detail: print("Error Opening File: ",filename,":: ",detail) raise #try to position at the begginning of the file. try: f.seek(0) #If an Exception occurs close file and raise it. except IOError as detail: print("Error Seeking File: ",filename,":: ",detail) f.close() raise #try to write metadata block into file. try: f.write(pickled) #If an Exception occurs close file and raise it. except Exception as detail: print("Error Writting File: ",filename,":: ",detail) f.close() raise f.close() #close file print("metadata successfully written:",metadata_dict['metadata_hash'],BMPLENGTH," pieces in file.") return metadata_dict #return metadata block.
def create_metadatafile(filename): #try to open file for reading and go to the begginning. try: f = open(filename, 'rb') filesize = f.seek(0, 2) f.seek(0) #If an Exception occurs, raise it. except IOError as detail: print("Error Opening File:", detail) raise #If an Exception occurs, raise it. except Exception as detail: print("Unexpected Exception::", detail) raise block_size = UserDefs.PieceSize #Block size FileMd5 = mymd5.mymd5_file(f, 1000 * 4096) #THIS WILL BE OF NO NEED BMPLENGTH = math.ceil(filesize / UserDefs.PieceSize) #calculate length of bitmap. f.seek(0) sha1s = [] #constructor of sha-1 hash. metadata_hash = hashlib.new( 'sha1' ) #create hash of metadata file, which will be used as file identifier. for i in range(0, BMPLENGTH): try: chunk = f.read(UserDefs.PieceSize) #read each block of file #If an Exception occurs, raise it. except IOError as detail: print("IOFile Error <block> at server thread:", detail) raise #If an Exception occurs, raise it. except Exception as detail: print("Unexpected Exception <block> at server thread:", detail) raise sha1s.append(mysha1lib.mysha1(chunk)) #create sha1 for current block. metadata_hash.update(chunk) #update hash of metadata f.close() #close file metadata_dict = { 'filename': filename #create final metadata block , 'filesize': filesize, 'FileMd5': FileMd5, 'sha1s_map': sha1s, 'block_size': block_size, 'metadata_hash': metadata_hash.hexdigest() } pickled = pickle.dumps(metadata_dict) #try to open file for writting and go to the begginning. try: f = open(filename + '.ant', 'wb') #If an Exception occurs, raise it. except IOError as detail: print("Error Opening File: ", filename, ":: ", detail) raise #try to position at the begginning of the file. try: f.seek(0) #If an Exception occurs close file and raise it. except IOError as detail: print("Error Seeking File: ", filename, ":: ", detail) f.close() raise #try to write metadata block into file. try: f.write(pickled) #If an Exception occurs close file and raise it. except Exception as detail: print("Error Writting File: ", filename, ":: ", detail) f.close() raise f.close() #close file print("metadata successfully written:", metadata_dict['metadata_hash'], BMPLENGTH, " pieces in file.") return metadata_dict #return metadata block.