diff --git a/src/client/commands.cpp b/src/client/commands.cpp index 725b273..2574a88 100644 --- a/src/client/commands.cpp +++ b/src/client/commands.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include extern bool is_exiting; @@ -226,7 +228,13 @@ void OpenAuctionCommand::handleCommand(std::string args, UserState &state) { return; } - /* OpenAuctionRequest openAuctionRequest; + // check file size + if (validateFileSize(asset_fname) == INVALID) { + std::cout << "Invalid file size: Must be less than 10MB" << std::endl; + return; + } + + OpenAuctionRequest openAuctionRequest; openAuctionRequest.userID = state.getUserID(); openAuctionRequest.password = state.getPassword(); openAuctionRequest.auctionName = auction_name; @@ -234,16 +242,20 @@ void OpenAuctionCommand::handleCommand(std::string args, UserState &state) { openAuctionRequest.startValue = (uint32_t)std::stoi(start_value); openAuctionRequest.timeActive = (uint32_t)std::stoi(timeactive); - /// need to read the file !!!!!!! + OpenAuctionResponse openAuctionResponse; + state.sendTcpPacketAndWaitForReply(openAuctionRequest, openAuctionResponse); - std::string message = "OAS " + openAuctionRequest.userID + " " + - openAuctionRequest.password + " " + - openAuctionRequest.auctionName + " " + - openAuctionRequest.assetFilename + " " + - std::to_string(openAuctionRequest.startValue) + " " + - std::to_string(openAuctionRequest.timeActive); - - std::cout << message << std::endl; */ + if (openAuctionResponse.status == OpenAuctionResponse::status::OK) { + std::cout << "Open auction successful: " << openAuctionResponse.auctionID + << std::endl; + } else if (openAuctionResponse.status == OpenAuctionResponse::status::NOK) { + std::cout << "Open auction failed: Auction could not be started" + << std::endl; + } else if (openAuctionResponse.status == OpenAuctionResponse::status::NLG) { + std::cout << "Open auction failed: You are not logged in" << std::endl; + } else if (openAuctionResponse.status == OpenAuctionResponse::status::ERR) { + std::cout << "Open auction failed: Server error" << std::endl; + } } void CloseAuctionCommand::handleCommand(std::string args, UserState &state) { @@ -723,9 +735,15 @@ int8_t validateAuctionName(std::string name) { return 0; } -int8_t validateFileSize(std::string fileSize) { - if (!is_digits(fileSize) || fileSize.length() > FILESIZE_MAX) { +int8_t validateFileSize(std::string file_path) { + try { + uint32_t fileSize = (uint32_t)std::filesystem::file_size(file_path); + if (std::to_string(fileSize).length() > FILESIZE_MAX) { + return INVALID; + } + } catch (...) { return INVALID; } + return 0; } diff --git a/src/client/commands.hpp b/src/client/commands.hpp index 3ffa928..a6405c6 100644 --- a/src/client/commands.hpp +++ b/src/client/commands.hpp @@ -289,4 +289,9 @@ int8_t validateAuctionName(std::string assetName); */ int8_t validateAssetFileSize(std::string ass); +/** + * @brief Validate a file based on its size. + */ +int8_t validateFileSize(std::string file_path); + #endif diff --git a/src/utils/constants.hpp b/src/utils/constants.hpp index 3d2fff4..6fdd6dc 100644 --- a/src/utils/constants.hpp +++ b/src/utils/constants.hpp @@ -24,7 +24,7 @@ #define FILESIZE_MAX 8 #define FILE_BUFFER_LEN 512 -#define TCP_WRITE_TIMEOUT_SECONDS 20 +#define TCP_WRITE_TIMEOUT_SECONDS (20 * 60) // 20 minutes #define TCP_READ_TIMEOUT_SECONDS 20 #define PROGRESS_BAR_STEP_SIZE 10 diff --git a/src/utils/protocol.cpp b/src/utils/protocol.cpp index 97d101e..37b2d34 100644 --- a/src/utils/protocol.cpp +++ b/src/utils/protocol.cpp @@ -598,6 +598,54 @@ void TcpPacket::readAndSaveToFile(const int fd, const std::string &file_name, file.close(); } +void OpenAuctionRequest::send(int fd) { + std::stringstream stream; + stream << OpenAuctionRequest::ID << " " << this->userID << " " + << this->password << " " << this->auctionName << " " + << this->startValue << " " << this->timeActive << " " + << this->assetFilename << " " << getFileSize(this->assetFilename) + << " "; + writeString(fd, stream.str()); + + stream.str(std::string()); + stream.clear(); + sendFile(fd, this->assetFilename); + + stream << std::endl; + writeString(fd, stream.str()); +} + +void OpenAuctionRequest::receive(int fd) { + // Serverbound packets don't read their ID + readPacketDelimiter(fd); +} + +void OpenAuctionResponse::send(int fd) { + if (fd == -1) + return; + return; +} + +void OpenAuctionResponse::receive(int fd) { + readPacketId(fd, OpenAuctionResponse::ID); + readSpace(fd); + auto status_str = readString(fd); + if (status_str == "OK") { + this->status = OK; + readSpace(fd); + this->auctionID = readString(fd); + } else if (status_str == "NOK") { + this->status = NOK; + } else if (status_str == "NLG") { + this->status = NLG; + } else if (status_str == "ERR") { + this->status = ERR; + } else { + throw InvalidPacketException(); + } + readPacketDelimiter(fd); +} + void CloseAuctionRequest::send(int fd) { std::stringstream stream; stream << CloseAuctionRequest::ID << " " << this->userID << " " @@ -728,8 +776,8 @@ void sendFile(int fd, std::filesystem::path file_path) { std::cerr << "Error opening file: " << file_path << std::endl; throw PacketSerializationException(); } - char buffer[FILE_BUFFER_LEN]; + while (file) { file.read(buffer, FILE_BUFFER_LEN); ssize_t bytes_read = (ssize_t)file.gcount(); diff --git a/src/utils/protocol.hpp b/src/utils/protocol.hpp index 5bffbe0..a6221fa 100644 --- a/src/utils/protocol.hpp +++ b/src/utils/protocol.hpp @@ -496,6 +496,31 @@ class TcpPacket { virtual ~TcpPacket() = default; }; +class OpenAuctionRequest : public TcpPacket { +public: + static constexpr const char *ID = "OPA"; + std::string userID; + std::string password; + std::string auctionName; + std::string assetFilename; + uint32_t startValue; + uint32_t timeActive; + + void send(int fd); + void receive(int fd); +}; + +class OpenAuctionResponse : public TcpPacket { +public: + enum status { OK, NOK, NLG, ERR }; + static constexpr const char *ID = "ROA"; + status status; + std::string auctionID; + + void send(int fd); + void receive(int fd); +}; + class CloseAuctionRequest : public TcpPacket { public: static constexpr const char *ID = "CLS"; diff --git a/test files/test.txt b/test files/test.txt index 086e296..fce56f3 100644 --- a/test files/test.txt +++ b/test files/test.txt @@ -1,6 +1,5 @@ -logout login 192837 eusofixe -open picasso32 picaso32.png 10000 1000 +open picasso32 Renoir_01.jpg 10000 1000 exit logout exit