def processPacket(self, packet): """ Processes a packet sent from the image repository Args: packet: the incoming packet to process Returns: Nothing """ data = self.__repositoryPacketHandler.readPacket(packet) if (data["packet_type"] == IR_PACKET_T.RETR_REQUEST_RECVD or data["packet_type"] == IR_PACKET_T.STOR_REQUEST_RECVD) : return elif (data["packet_type"] == IR_PACKET_T.RETR_REQUEST_ERROR or data["packet_type"] == IR_PACKET_T.RETR_ERROR or data["packet_type"] == IR_PACKET_T.STOR_REQUEST_ERROR or data["packet_type"] == IR_PACKET_T.STOR_ERROR) : self.__errorDescription = data["errorDescription"] self.__image_not_found = True elif (data["packet_type"] == IR_PACKET_T.RETR_START) : try : ftpClient = FTPClient() ftpClient.connect(self.__ftpServerIP, data['FTPServerPort'], self.__ftpTimeout, data['username'], data['password']) ftpClient.retrieveFile(data['fileName'], self.__transferDirectory, data['serverDirectory']) ftpClient.disconnect() except Exception: self.__errorDescription = ERROR_DESC_T.VMSRVR_FTP_RETR_TRANSFER_ERROR elif (data["packet_type"] == IR_PACKET_T.STOR_START) : try : ftpClient = FTPClient() ftpClient.connect(self.__ftpServerIP, data['FTPServerPort'], self.__ftpTimeout, data['username'], data['password']) ftpClient.storeFile(self.__sourceFilePath, self.__transferDirectory, data['serverDirectory']) ftpClient.disconnect() except Exception: self.__errorDescription = ERROR_DESC_T.VMSRVR_FTP_RETR_TRANSFER_ERROR elif (data["packet_type"] == IR_PACKET_T.ADDED_IMAGE_ID): self.__imageID = data["addedImageID"] self.__operation_completed = True
def processPacket(self, packet): """ Processes a packet sent from the image repository Args: packet: the incoming packet to process Returns: Nothing """ data = self.__repositoryPacketHandler.readPacket(packet) if (data["packet_type"] == IR_PACKET_T.RETR_REQUEST_RECVD or data["packet_type"] == IR_PACKET_T.STOR_REQUEST_RECVD): return elif (data["packet_type"] == IR_PACKET_T.RETR_REQUEST_ERROR or data["packet_type"] == IR_PACKET_T.RETR_ERROR or data["packet_type"] == IR_PACKET_T.STOR_REQUEST_ERROR or data["packet_type"] == IR_PACKET_T.STOR_ERROR): self.__errorDescription = data["errorDescription"] self.__image_not_found = True elif (data["packet_type"] == IR_PACKET_T.RETR_START): try: ftpClient = FTPClient() ftpClient.connect(self.__ftpServerIP, data['FTPServerPort'], self.__ftpTimeout, data['username'], data['password']) ftpClient.retrieveFile(data['fileName'], self.__transferDirectory, data['serverDirectory']) ftpClient.disconnect() except Exception: self.__errorDescription = ERROR_DESC_T.VMSRVR_FTP_RETR_TRANSFER_ERROR elif (data["packet_type"] == IR_PACKET_T.STOR_START): try: ftpClient = FTPClient() ftpClient.connect(self.__ftpServerIP, data['FTPServerPort'], self.__ftpTimeout, data['username'], data['password']) ftpClient.storeFile(self.__sourceFilePath, self.__transferDirectory, data['serverDirectory']) ftpClient.disconnect() except Exception: self.__errorDescription = ERROR_DESC_T.VMSRVR_FTP_RETR_TRANSFER_ERROR elif (data["packet_type"] == IR_PACKET_T.ADDED_IMAGE_ID): self.__imageID = data["addedImageID"] self.__operation_completed = True
def processPacket(self, packet): global user_input global workingDirectory data = self.__repositoryPacketHandler.readPacket(packet) if (data['packet_type'] == PACKET_T.ADDED_IMAGE_ID) : print("The new image ID is: {0}".format(data['addedImageID'])) print("Please write it down. You'll need it later") elif (data['packet_type'] == PACKET_T.RETR_REQUEST_ERROR or data['packet_type'] == PACKET_T.RETR_ERROR) : print("Retrieve error: " + str(data['errorDescription'])) elif (data['packet_type'] == PACKET_T.RETR_REQUEST_RECVD) : print("The image repository says: retrieve request received") elif (data['packet_type'] == PACKET_T.RETR_START) : print("Downloading file...") ftpClient = FTPClient() ftpClient.connect(self.__ip_address, data['FTPServerPort'], 5, data['username'], data['password']) ftpClient.retrieveFile(data['fileName'], workingDirectory, data['serverDirectory']) ftpClient.disconnect() print("Transfer completed") elif (data['packet_type'] == PACKET_T.STOR_REQUEST_ERROR or data['packet_type'] == PACKET_T.STOR_ERROR) : print("Store error: " + str(data['errorCode'])) user_input = False elif (data['packet_type'] == PACKET_T.STOR_REQUEST_RECVD) : print("The image repository says: store request received") elif (data['packet_type'] == PACKET_T.STOR_START) : print("Uploading file...") ftpClient = FTPClient() ftpClient.connect(self.__ip_address, data['FTPServerPort'], 100, data['username'], data['password']) if (data['fileName'] == '') : fileName = raw_input('File to upload (its name MUST be the given image ID): ') else : fileName = data['fileName'] ftpClient.storeFile(fileName, workingDirectory, data['serverDirectory']) ftpClient.disconnect() print("Transfer completed") user_input = False elif (data['packet_type'] == PACKET_T.DELETE_REQUEST_RECVD): print("The image repository says: delete request received") elif (data['packet_type'] == PACKET_T.STATUS_DATA): print("Image repository disk usage: {0} KB free, {1} KB available".format(data["FreeDiskSpace"], data["TotalDiskSpace"])) elif (data['packet_type'] == PACKET_T.IMAGE_EDITION_CANCELLED): print("The image repository says: image edition cancelled") else: print("Error: a packet from an unexpected type has been received " + str(data['packet_type']))