Example #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')
Example #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')
Example #3
0
 def createFindIndexServerResponse(self):
     '''
     Create response for find index server request
     :return: find index server response
     '''
     if self.cachedIndexServer[0] != 0:
         return ResponseAssembler.assembleFindIndexServerResponse(
             self.cachedIndexServer[0][0], self.cachedIndexServer[0][1],
             True)
     else:
         return ResponseAssembler.assembleFindIndexServerResponse(
             self.cachedIndexServer[0][0], self.cachedIndexServer[0][1],
             False)
Example #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')
Example #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')
Example #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')
Example #7
0
 def createCloneNodeResponse(self):
     fileDict = dict()
     for fileName in self.fileList:
         content = self.getFileContent(fileName)
         fileDict[fileName] = content
     return ResponseAssembler.assembleCloneNodeResponse(fileDict, self.cachedDirectoryServerAddress,
                                                        self.backupDirectoryServerAddress)
Example #8
0
 def createGetBackupServerResponse(self):
     if len(self.backupServerList) > 0:
         serverId, (serverIp, serverPort) = self.backupServerList[0]
         return ResponseAssembler.assembleGetBackupServerResponse(
             serverId, serverIp, serverPort)
     else:
         raise Exception('No Backup Server')
Example #9
0
 def createNewFileResponse(self, fileName, content):
     if fileName not in self.fileList:
         self.fileList.append(fileName)
         for nodeId, address in self.nodeList:
             self.sendMessage(
                 tuple(address),
                 RequestAssembler.assembleAddFileRequest(
                     fileName, content, False))
         for serverId, address in self.backupServerList:
             try:
                 self.requestCopyServer(address)
             except:
                 print('wait for recovery')
         return ResponseAssembler.assembleNewFileResponse(True)
     else:
         return ResponseAssembler.assembleNewFileResponse(False)
Example #10
0
 def createJoinNetworkResponse(self, nodeId, nodeIp, nodePort):
     self.nodeList.append([nodeId, [nodeIp, nodePort]])
     for serverId, address in self.backupServerList:
         try:
             self.requestCopyServer(address)
         except:
             print('wait for recovery')
     return ResponseAssembler.assembleJoinNetworkResponse(True)
Example #11
0
 def createJoinBackupListResponse(self, serverId, serverIp, serverPort):
     self.backupServerList.append([serverId, [serverIp, serverPort]])
     for serverId, address in self.backupServerList:
         try:
             self.requestCopyServer(address)
         except:
             print('wait for recovery')
     return ResponseAssembler.assembleJoinBackupListResponse(True)
Example #12
0
 def createCloneServerResponse(self, serverId):
     nodeList = self.nodeList
     fileList = self.fileList
     backupList = copy.deepcopy(self.backupServerList)
     backupList.append([self.id, list(self.address)])
     for i in range(len(backupList)):
         if backupList[i][0] == serverId:
             backupList.pop(i)
             break
     return ResponseAssembler.assembleCloneServerResponse(
         nodeList, fileList, backupList)
Example #13
0
 def createRemoveNodeResponse(self, nodeId):
     for i in range(len(self.nodeList)):
         if self.nodeList[i][0] == int(nodeId):
             self.nodeList.pop(i)
             break
     for serverId, address in self.backupServerList:
         try:
             self.requestCopyServer(address)
         except:
             print('wait for recovery')
     return ResponseAssembler.assembleStorageNodeRemoveResponse(True)
Example #14
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')
Example #15
0
 def createAddFileResponse(self, fileName, content, forward):
     targetFilePath = self.getDirectoryPath().joinpath(fileName)
     if not targetFilePath.exists() and fileName not in self.fileList:
         with targetFilePath.open('wb') as file:
             file.write(bytes(content, 'utf-8'))
         self.fileList.append(fileName)
     if forward:
         while True:
             try:
                 self.sendMessage(self.cachedDirectoryServerAddress,
                                  RequestAssembler.assembleNewFileRequest(fileName, content))
                 break
             except ServerError:
                 self.switchBackupServer()
     return ResponseAssembler.assembleAddFileResponse(True)
Example #16
0
 def createCopyServerResponse(self, nodeList, fileList, backupServerList):
     self.setNodeList(nodeList)
     self.setFileList(fileList)
     self.setBackupList(backupServerList)
     return ResponseAssembler.assembleCopyServerResponse(True)
Example #17
0
 def createGetFileListFromServerResponse(self):
     return ResponseAssembler.assembleGetFileListFromServerResponse(
         self.fileList)
Example #18
0
 def createConnectResponse(self):
     nodeId, address = random.choice(self.nodeList)
     return ResponseAssembler.assembleConnectResponse(
         nodeId, address[0], address[1])
Example #19
0
 def createReadFileResponse(self, fileName):
     targetFilePath = self.getDirectoryPath().joinpath(fileName)
     if targetFilePath.exists():
         return ResponseAssembler.assembleReadFileResponse(self.getFileContent(fileName))
     else:
         raise Exception('Reading File Not Exists')