Beispiel #1
0
 def requestGetBackupServer(self):
     while True:
         try:
             return self.sendMessage(self.cachedDirectoryServerAddress,
                                     RequestAssembler.assembleGetBackupServerRequest())
         except ServerError:
             self.switchBackupServer()
Beispiel #2
0
 def requestNewFile(self, fileName, content):
     try:
         return self.sendMessage(
             self.cachedDirectoryServerAddress,
             RequestAssembler.assembleNewFileRequest(fileName, content))
     except ServerError:
         self.switchBackupServer()
         self.requestNewFile(fileName, content)
Beispiel #3
0
 def requestCloneServer(self, targetAddress):
     '''
     Request to make this sever to be another server's replica
     :param targetAddress:
     :return:
     '''
     return self.sendMessage(
         tuple(targetAddress),
         RequestAssembler.assembleCloneServerRequest(self.id))
Beispiel #4
0
 def requestCopyServer(self, targetAddress):
     '''
     Request to make another server to be this server's replica
     :param targetAddress:
     :return:
     '''
     return self.sendMessage(
         tuple(targetAddress),
         RequestAssembler.assembleCopyServerRequest(self.nodeList,
                                                    self.fileList,
                                                    self.backupServerList))
Beispiel #5
0
 def requestJoinBackupList(self, serverId, serverIp, serverPort):
     '''
     Request to add this server to target server's the backup list.
     :param serverId:
     :param serverIp:
     :param serverPort:
     :return:
     '''
     self.sendMessage(
         self.cachedDirectoryServerAddress,
         RequestAssembler.assembleJoinBackupListRequest(
             serverId, serverIp, serverPort))
Beispiel #6
0
 def requestCloneNode(self):
     rawResponse = self.sendMessage(self.cachedDirectoryServerAddress, RequestAssembler.assembleCloneNodeRequest())
     response = json.loads(rawResponse)
     fileDict = response['fileDict']
     cachedDirectoryServer = response['cachedDirectoryServer']
     backupDirectoryServer = response['backupDirectoryServer']
     self.fileList = list(fileDict.keys())
     self.cachedDirectoryServerAddress = tuple(cachedDirectoryServer)
     self.backupDirectoryServerAddress = tuple(backupDirectoryServer)
     for fileName in self.fileList:
         with self.getDirectoryPath().joinpath(fileName).open('wb') as file:
             file.write(bytes(fileDict[fileName], 'utf-8'))
Beispiel #7
0
 def requestAddFile(self, fileName, content):
     if self.cachedNodeAddress != None:
         while True:
             try:
                 return self.sendMessage(
                     self.cachedNodeAddress,
                     RequestAssembler.assembleAddFileRequest(
                         fileName, content, True))
             except ServerError:
                 self.requestRemoveNode(self.cachedNodeId)
                 self.requestConnect()
     else:
         raise Exception('Need Connect To Directory Server First')
Beispiel #8
0
 def requestGetFileListFromServer(self):
     try:
         rawResponse = self.sendMessage(
             self.cachedDirectoryServerAddress,
             RequestAssembler.assembleGetFileListFromServerRequest())
         response = json.loads(rawResponse)
         if self.output != 'false':
             print(
                 '=== Get File List From Directory Server ===\nFile List: {}'
                 .format(response['fileList']))
     except ServerError:
         self.switchBackupServer()
         self.requestGetFileListFromServer()
Beispiel #9
0
 def requestRemoveNode(self, nodeId):
     '''
     Remove a node from the node list
     :param nodeId:
     :return:
     '''
     try:
         return self.sendMessage(
             self.cachedDirectoryServerAddress,
             RequestAssembler.assembleStorageNodeRemoveRequest(str(nodeId)))
     except ServerError:
         self.switchBackupServer()
         self.requestRemoveNode(nodeId)
Beispiel #10
0
 def requestConnect(self):
     '''
     Request a storage node and cache this node.
     :return:
     '''
     try:
         rawResponse = self.sendMessage(
             self.cachedDirectoryServerAddress,
             RequestAssembler.assembleConnectRequest())
         response = json.loads(rawResponse)
         self.cachedNodeId = response['nodeId']
         self.cachedNodeAddress = (response['nodeIp'], response['nodePort'])
     except ServerError:
         self.switchBackupServer()
         self.requestConnect()
Beispiel #11
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)
Beispiel #12
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)
Beispiel #13
0
 def requestJoinNetwork(self):
     '''
     Request to add this node to directory server's node list.
     :return:
     '''
     while True:
         try:
             rawResponse = self.sendMessage(self.cachedDirectoryServerAddress,
                                            RequestAssembler.assembleJoinNetworkRequest(self.id, self.address[0],
                                                                                        self.address[1]))
             break
         except ServerError:
             self.switchBackupServer()
     response = json.loads(rawResponse)
     if response['result'] != True:
         raise Exception('Join Network Error')
Beispiel #14
0
 def requestGetFileListFromNode(self):
     if self.cachedNodeAddress != None:
         while True:
             try:
                 rawResponse = self.sendMessage(
                     self.cachedNodeAddress,
                     RequestAssembler.assembleGetFileListFromNodeRequest())
                 response = json.loads(rawResponse)
                 if self.output != 'false':
                     print('=== Get File List From Node ===\nFile List: {}'.
                           format(response['fileList']))
                 break
             except ServerError:
                 self.requestRemoveNode(self.cachedNodeId)
                 self.requestConnect()
     else:
         raise Exception('Need Connect To Directory Server First')
Beispiel #15
0
 def requestReadFile(self, fileName):
     if self.cachedNodeAddress != None:
         while True:
             try:
                 rawResponse = self.sendMessage(
                     self.cachedNodeAddress,
                     RequestAssembler.assembleReadFileRequest(fileName))
                 response = json.loads(rawResponse)
                 if self.output != 'false':
                     print('=== Read File ===\nFile Name: {}\nContent: {}'.
                           format(fileName, response['content']))
                 break
             except ServerError:
                 self.requestRemoveNode(self.cachedNodeId)
                 self.requestConnect()
     else:
         raise Exception('Need Connect To Directory Server First')
Beispiel #16
0
 def requestGetBackupServer(self):
     return self.sendMessage(
         self.cachedDirectoryServerAddress,
         RequestAssembler.assembleGetBackupServerRequest())