Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Open Command [TCP] #2

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions src/client/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <unistd.h>

extern bool is_exiting;

Expand Down Expand Up @@ -226,24 +228,34 @@ 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;
openAuctionRequest.assetFilename = asset_fname;
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) {
Expand Down Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions src/client/commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/utils/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
50 changes: 49 additions & 1 deletion src/utils/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 << " "
Expand Down Expand Up @@ -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();
Expand Down
25 changes: 25 additions & 0 deletions src/utils/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 1 addition & 2 deletions test files/test.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
logout
login 192837 eusofixe
open picasso32 picaso32.png 10000 1000
open picasso32 Renoir_01.jpg 10000 1000
exit
logout
exit