Skip to content

Commit

Permalink
Merge pull request #7 from pearsang/fix-open
Browse files Browse the repository at this point in the history
Fix open
  • Loading branch information
simaosanguinho authored Dec 14, 2023
2 parents 78b54dc + 9934e9b commit 4115848
Show file tree
Hide file tree
Showing 21 changed files with 165 additions and 54 deletions.
Binary file added 9509810195416236015/vangogh.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ fmt: $(SOURCES) $(HEADERS)
fmt-check: $(SOURCES) $(HEADERS)
clang-format -n --Werror $^

src/server/server: $(SERVER_OBJECTS) $(UTILS_OBJECTS) #$(SERVER_HEADERS) #$(UTILS_HEADERS)
src/client/user: $(CLIENT_OBJECTS) $(UTILS_OBJECTS) #$(CLIENT_HEADERS) #$(UTILS_HEADERS)
src/server/server: $(SERVER_OBJECTS) $(UTILS_OBJECTS) $(SERVER_HEADERS) $(UTILS_HEADERS)
src/client/user: $(CLIENT_OBJECTS) $(UTILS_OBJECTS) $(CLIENT_HEADERS) $(UTILS_HEADERS)

AS: src/server/server
cp src/server/server AS
Expand Down
File renamed without changes
2 changes: 1 addition & 1 deletion src/client/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void LoginCommand::handleCommand(std::string args, UserState &state) {
} else if (loginResponse.status == LoginResponse::status::NOK) {
std::cout << "Login failed: Incorrect password" << std::endl;
} else if (loginResponse.status == LoginResponse::status::REG) {
std::cout << "Login successful: You were registred" << std::endl;
std::cout << "Login successful: You were registered" << std::endl;
state.setUserID(user_id);
state.setPassword(password);
} else if (loginResponse.status == LoginResponse::status::ERR) {
Expand Down
1 change: 0 additions & 1 deletion src/client/commands.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#pragma once
#ifndef COMMANDS_H
#define COMMANDS_H

Expand Down
1 change: 0 additions & 1 deletion src/client/user.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#pragma once
#ifndef USER_H
#define USER_H

Expand Down
1 change: 0 additions & 1 deletion src/client/user_state.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#pragma once
#ifndef USER_STATE_H
#define USER_STATE_H

Expand Down
4 changes: 2 additions & 2 deletions src/server/handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ void handleOpenAuction(AuctionServerState &state, int fd) {
request.receive(fd);
state.cdebug << "[OpenAuction] User " << request.userID
<< " requested to open an auction" << std::endl;

if (state.usersManager.isUserLoggedIn(request.userID) == 0) {

uint32_t auctionID = state.auctionManager.openAuction(
request.userID, request.auctionName, request.startValue,
request.timeActive, request.assetFilename);
request.timeActive, request.assetFilename, request.assetFilePath);
if (auctionID == (uint32_t)INVALID) {
response.status = OpenAuctionResponse::NOK;
state.cdebug << "[OpenAuction] Auction "
Expand Down
1 change: 0 additions & 1 deletion src/server/handlers.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#pragma once
#ifndef HANDLERS_H
#define HANDLERS_H

Expand Down
19 changes: 17 additions & 2 deletions src/server/server.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "server.hpp"

#include <arpa/inet.h>
#include <filesystem>
#include <iostream>
#include <unistd.h>

#include <iostream>
Expand Down Expand Up @@ -120,8 +122,21 @@ void setupDB() {
nextAuctionFile += "/next_auction.txt";
create_new_file(nextAuctionFile);

// CHANGE THISSSSS!!!!! HARD CODED
write_to_file(nextAuctionFile, "3\n");
// count the number of directories inside AUctions
int count = 0;

for (const auto &entry : std::filesystem::directory_iterator(AUCTIONDIR)) {
if (std::filesystem::is_directory(entry.path())) {
count++;
}
}

count++;

std::string nAuctions = std::to_string(count);
nAuctions += "\n";
write_to_file(nextAuctionFile, nAuctions);

};

void wait_for_udp_packet(AuctionServerState &state) {
Expand Down
2 changes: 1 addition & 1 deletion src/server/server.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once
#ifndef SERVER_H
#define SERVER_H

#include <csignal>
#include <filesystem>

#include "../utils/constants.hpp"
#include "server_state.hpp"
Expand Down
73 changes: 56 additions & 17 deletions src/server/server_auction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,16 @@
uint32_t AuctionManager::openAuction(std::string userID,
std::string auctionName,
uint32_t startValue, uint32_t timeActive,
std::string assetFilename) {
if (validateUserID(userID) == INVALID ||
validateAuctionName(auctionName) == INVALID ||
validateAssetFilename(assetFilename) == INVALID) {
// add more validations !!!! TODO
throw InvalidPacketException();
}
std::string assetFilename,
std::string assetFilePath) {
// validations of arguments already performed

try {
std::random_device rd;
std::mt19937 gen(rd());
std::string auctionID = getnextAuctionID();
std::cout << "Auction ID: " << auctionID << std::endl;

// Define the distribution for integers between 0 and 999
std::uniform_int_distribution<int> distribution(0, 999);
std::cout << "Creating auction for user " << userID << std::endl;

// Generate a random integer
int randomInteger = distribution(gen);
// to sr
std::string auctionID = std::to_string(randomInteger);
// std::cout << "Creating auction for user " << userID << std::endl;
std::string auctionPath = AUCTIONDIR;
auctionPath += "/" + auctionID;
create_new_directory(auctionPath);
Expand All @@ -36,10 +26,59 @@ uint32_t AuctionManager::openAuction(std::string userID,
" " + std::to_string(startValue) + " " +
std::to_string(timeActive) + " " + start_datetime +
" " + end_sec_time);
std::string assetFilenamePath = assetFilePath.substr(
assetFilePath.find_last_of("/") + 1, assetFilePath.size());

// create ASSET directory
std::string assetPath = auctionPath + "/" + "ASSET" + "/";
create_new_directory(assetPath);
rename_file(assetFilePath, assetPath + assetFilenamePath);

std::string assetFilenamePathSubstr =
assetFilePath.substr(0, assetFilePath.find_first_of("/"));
delete_directory(assetFilenamePathSubstr);

/// FALTA ATUALZAR O NEXT AUCTION FILE?????
return (uint32_t)std::stoi(auctionID);
} catch (std::exception &e) {
return 1;
throw;
}
return (uint32_t)INVALID;
}

std::string AuctionManager::getnextAuctionID() {
try {
std::string nextAuctionID;
std::string nextAuctionPath = AUCTIONDIR;
nextAuctionPath += "/next_auction.txt";
read_from_file(nextAuctionPath, nextAuctionID);

// update next auction id
int nextAuctionID_int = std::stoi(nextAuctionID);
nextAuctionID_int++;
write_to_file(nextAuctionPath, std::to_string(nextAuctionID_int));
nextAuctionID = nextAuctionID.substr(0, nextAuctionID.size() - 1);
return nextAuctionID;
} catch (std::exception &e) {
throw;
}
}


void validateOpenAuctionArgs(std::string userID, std::string password,
std::string auctionName, uint32_t startValue,
uint32_t timeActive, std::string assetFilename,
uint32_t assetSize) {
if (validateUserID(userID) == INVALID ||
validatePassword(password) == INVALID ||
validateAuctionName(auctionName) == INVALID ||
validateAssetFilename(assetFilename) == INVALID ||
validateStartValue(std::to_string(startValue)) == INVALID ||
validateAuctionDuration(std::to_string(timeActive)) == INVALID ||
validateAssetFileSize(assetSize) == INVALID) {
throw InvalidPacketException();
}
return;
}

std::vector<std::pair<std::string, uint8_t>> AuctionManager::listAuctions() {
Expand Down
9 changes: 8 additions & 1 deletion src/server/server_auction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class AuctionManager {
public:
uint32_t openAuction(std::string userID, std::string auctionName,
uint32_t startValue, uint32_t timeActive,
std::string assetFilename);
std::string assetFilename, std::string assetFilePath);
std::string getnextAuctionID();

std::string getNextAuctionID();

Expand All @@ -39,6 +40,12 @@ class AuctionManager {
~AuctionManager() = default;
};


void validateOpenAuctionArgs(std::string userID, std::string password,
std::string auctionName, uint32_t startValue,
uint32_t timeActive, std::string assetFilename,
uint32_t assetSize);

class NoAuctionsException : public std::runtime_error {
public:
NoAuctionsException()
Expand Down
1 change: 0 additions & 1 deletion src/server/server_state.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#pragma once
#ifndef SERVER_STATE_H
#define SERVER_STATE_H

Expand Down
1 change: 0 additions & 1 deletion src/server/server_user.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#pragma once
#ifndef SERVER_USER_H
#define SERVER_USER_H

Expand Down
1 change: 0 additions & 1 deletion src/utils/constants.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#pragma once
#ifndef CONSTANTS_H
#define CONSTANTS_H

Expand Down
53 changes: 39 additions & 14 deletions src/utils/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
#include <sys/types.h>
#include <unistd.h>

#include <chrono>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <random>
#include <string>
#include <sys/stat.h>

#include "../utils/utils.hpp"
#include "constants.hpp"
#include <sys/socket.h>

extern bool is_exiting;
Expand Down Expand Up @@ -650,10 +650,33 @@ void ErrorTcpPacket::send(int fd) {

void ErrorTcpPacket::receive(int fd) { (void)fd; }

void TcpPacket::readAndSaveToFile(const int fd, const std::string &file_name,
const size_t file_size) {
std::ofstream file(file_name);
std::string generateUniqueIdentifier() {
// Use current time as a seed for the random number generator
auto seed =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::mt19937 generator(static_cast<unsigned int>(seed));

// Generate a random number and convert it to a string
std::uniform_int_distribution<unsigned long long> distribution;
unsigned long long random_number = distribution(generator);

// Convert the random number to a string
std::string unique_identifier = std::to_string(random_number);

return unique_identifier;
}

std::string TcpPacket::readAndSaveToFile(const int fd,
std::string &file_name,
const size_t file_size, bool flag) {
if (flag) {
std::string filepath = generateUniqueIdentifier();
create_new_directory(filepath);
filepath += "/" + file_name;
std::cout << "Saving file to " << filepath << std::endl;
file_name = filepath;
}
std::ofstream file(file_name);
if (!file.good()) {
throw IOException();
}
Expand Down Expand Up @@ -710,6 +733,9 @@ void TcpPacket::readAndSaveToFile(const int fd, const std::string &file_name,
}

file.close();
// print filepath
std::cout << "File saved to " << file_name << std::endl;
return file_name;
}

void ShowAssetRequest::send(int fd) {
Expand Down Expand Up @@ -758,7 +784,7 @@ void ShowAssetResponse::receive(int fd) {
readSpace(fd);
assetSize = readInt(fd);
readSpace(fd);
readAndSaveToFile(fd, assetFilename, assetSize);
assetFilePath = readAndSaveToFile(fd, assetFilename, assetSize, false);
} else if (status_str == "NOK") {
this->status = NOK;
} else {
Expand Down Expand Up @@ -788,7 +814,9 @@ void OpenAuctionRequest::receive(int fd) {
userID = readString(fd);
readSpace(fd);
password = readString(fd);
if (password != getUserPassword(userID)) {

// must confirm the user password is correct
if (this->password != getUserPassword(userID)) {
throw InvalidPacketException();
}

Expand All @@ -803,15 +831,12 @@ void OpenAuctionRequest::receive(int fd) {
readSpace(fd);
assetSize = readInt(fd);

// convert assetSize to str
std::stringstream ss;
ss << assetSize;
std::string assetSizeStr = ss.str();
if (assetSizeStr.length() > FILESIZE_MAX) {
if (assetSize > 99999999) {
throw InvalidPacketException();
}

readSpace(fd);
readAndSaveToFile(fd, assetFilename, assetSize);
assetFilePath = readAndSaveToFile(fd, assetFilename, assetSize, true);
readPacketDelimiter(fd);
}

Expand Down Expand Up @@ -1056,4 +1081,4 @@ uint32_t getFileSize(std::filesystem::path file_path) {
} catch (...) {
throw PacketSerializationException();
}
}
}
13 changes: 10 additions & 3 deletions src/utils/protocol.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#pragma once
#ifndef PROTOCOL_HPP
#define PROTOCOL_HPP

Expand All @@ -16,6 +15,10 @@
#include <tuple>
#include <vector>

#include "../server/server_auction.hpp"
#include "constants.hpp"
#include "utils.hpp"

/**
* @class UnexpectedPacketException
*
Expand Down Expand Up @@ -570,8 +573,8 @@ class TcpPacket {
* @param file_name The name of the file to save the asset to.
* @param file_size The size of the file to read.
*/
void readAndSaveToFile(const int fd, const std::string &file_name,
const size_t file_size);
std::string readAndSaveToFile(const int fd, std::string &file_name,
const size_t file_size, bool flag);

public:
/**
Expand Down Expand Up @@ -625,6 +628,7 @@ class ShowAssetResponse : public TcpPacket {
status status;
std::string assetFilename;
uint32_t assetSize;
std::string assetFilePath;

void send(int fd);
void receive(int fd);
Expand All @@ -650,6 +654,7 @@ class OpenAuctionRequest : public TcpPacket {
uint32_t assetSize;
uint32_t startValue;
uint32_t timeActive;
std::string assetFilePath;

void send(int fd);
void receive(int fd);
Expand Down Expand Up @@ -804,4 +809,6 @@ void sendFile(int fd, std::filesystem::path image_path);
* @param image_path The path to the image file.
*/
uint32_t getFileSize(std::filesystem::path file_path);

std::string generateUniqueIdentifier();
#endif
Loading

0 comments on commit 4115848

Please sign in to comment.