Пример #1
0
 def createUpdateFileIndexResponse(self, fileName, fileMd5, peerId):
     '''
     Create response for update file index request
     :param fileName: file name
     :param fileMd5: file md5
     :param peerId: peer id
     :return: update file index response
     '''
     try:
         try:
             self.fileIndexTable[fileName].add(
                 (peerId, str(self.peerAddressTable[peerId])))
         except KeyError:
             self.fileIndexTable[fileName] = set()
             self.fileIndexTable[fileName].add(
                 (peerId, str(self.peerAddressTable[peerId])))
         try:
             self.peerFileTable[peerId].add(fileName)
         except KeyError:
             self.peerFileTable[peerId] = set()
             self.peerFileTable[peerId].add(fileName)
         self.fileMd5Table[fileName] = fileMd5
         if self.output == 'debug':
             print('peer file table: ', self.peerFileTable)
             print('peer address table: ', self.peerAddressTable)
             print('file index table: ', self.fileIndexTable)
             print('file md5 table: ', self.fileMd5Table)
         return ResponseAssembler.assembleUpdateFileIndexResponse(
             fileName, fileMd5, True)
     except:
         return ResponseAssembler.assembleErrorResponse(
             'updateFileIndexError')
Пример #2
0
 def createDownloadResponse(self, fileName, index, chunks):
     '''
     Create response for download request
     :param fileName: file name
     :param index: index
     :param chunks: chunks
     :return: download response
     '''
     try:
         with self.getDirectoryPath().joinpath(fileName).open('rb') as file:
             fileContent = file.read()
             remainder = len(fileContent) % chunks
             chunkSize = int((len(fileContent) - remainder) / chunks)
             startIndex = index * chunkSize
             if (startIndex + chunkSize) < len(fileContent):
                 endIndex = startIndex + chunkSize
             else:
                 endIndex = len(fileContent)
             if index == chunks - 1:
                 endIndex = len(fileContent)
             if self.output == 'clean' or self.output == 'debug':
                 print('====================================')
                 print(
                     'download from peer {} for file {} \nrequest file chunk: {} \nfile Length: {} \ndownload piece from: {} to {} \n====================================\n'
                     .format(self.id, fileName, index, len(fileContent),
                             startIndex, endIndex))
             returnContent = fileContent[startIndex:endIndex]
             return ResponseAssembler.assembleDownloadResponse(
                 fileName, index, chunks,
                 hashlib.md5(returnContent).hexdigest(),
                 str(returnContent, 'utf-8'))
     except Exception:
         traceback.print_exc()
         return ResponseAssembler.assembleErrorResponse(
             'ResponseDownloadError')
Пример #3
0
 def createCloneNodeResponse(self, request):
     if len(self.nodeList) > 0:
         node = self.nodeList[0]
         while True:
             try:
                 response = self.sendMessage(tuple(node[1]), request)
                 break
             except:
                 self.nodeList.remove(node)
                 node = random.choice(self.nodeList)
         return response
     return ResponseAssembler.assembleErrorResponse('First Node Join')
Пример #4
0
 def createJoinPeerNetworkResponse(self, peerId, address):
     '''
     Create response for join peer network request
     :param peerId: peer id
     :param address: peer address
     :return: join peer network response
     '''
     try:
         self.peerList.append((peerId, address))
         return ResponseAssembler.assembleJoinPeerNetworkResponse(
             peerId, address, True)
     except:
         return ResponseAssembler.assembleErrorResponse(
             'joinPeerNetworkError')
Пример #5
0
 def createUpdatePeerAddressResponse(self, peerId, address):
     '''
     Create response for update peer address response
     :param peerId: peer id
     :param address: peer address
     :return: update peer address response
     '''
     try:
         self.peerAddressTable[peerId] = address
         return ResponseAssembler.assembleUpdatePeerAddressResponse(
             peerId, address, True)
     except:
         return ResponseAssembler.assembleErrorResponse(
             'updatePeerAddressError')
Пример #6
0
 def createFileIndexResponse(self, fileName):
     '''
     Create response for file index request
     :param fileName: file name
     :return: file index response
     '''
     if self.isFileIndexServer:
         peerSet = self.fileIndexTable[fileName]
         fileMd5 = self.fileMd5Table[fileName]
         chunks = len(peerSet)
         return ResponseAssembler.assembleFileIndexResponse(
             fileName, fileMd5, chunks, str(peerSet))
     else:
         return ResponseAssembler.assembleErrorResponse(
             'ResponseFileIndexError')