Skip to content

Commit

Permalink
change setupDB - count sub-directories in AUCTIONS directory
Browse files Browse the repository at this point in the history
  • Loading branch information
simaosanguinho committed Dec 14, 2023
1 parent a85f18a commit 8c9155e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 7 deletions.
18 changes: 17 additions & 1 deletion 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 @@ -119,7 +121,21 @@ void setupDB() {
std::string nextAuctionFile = AUCTIONDIR;
nextAuctionFile += "/next_auction.txt";
create_new_file(nextAuctionFile);
write_to_file(nextAuctionFile, "1\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
1 change: 1 addition & 0 deletions src/server/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SERVER_H

#include <csignal>
#include <filesystem>

#include "../utils/constants.hpp"
#include "server_state.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/server/server_auction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ uint32_t AuctionManager::openAuction(std::string userID,
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) {
throw;
Expand Down
2 changes: 1 addition & 1 deletion src/server/server_auction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AuctionManager {
public:
uint32_t openAuction(std::string userID, std::string auctionName,
uint32_t startValue, uint32_t timeActive,
std::string assetFilename,std::string assetFilePath);
std::string assetFilename, std::string assetFilePath);
std::string getnextAuctionID();

// constructor
Expand Down
9 changes: 6 additions & 3 deletions src/utils/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,11 @@ std::string generateUniqueIdentifier() {
return unique_identifier;
}

std::string TcpPacket::readAndSaveToFile(const int fd, const std::string &file_name,
const size_t file_size, bool flag) {
std::string TcpPacket::readAndSaveToFile(const int fd,
const std::string &file_name,
const size_t file_size, bool flag) {

(void)flag;
// set filepath
std::string filepath = generateUniqueIdentifier();
create_new_directory(filepath);
Expand Down Expand Up @@ -779,7 +782,7 @@ void ShowAssetResponse::receive(int fd) {
readSpace(fd);
assetSize = readInt(fd);
readSpace(fd);
assetFilePath = readAndSaveToFile(fd, assetFilename, assetSize , false);
assetFilePath = readAndSaveToFile(fd, assetFilename, assetSize, false);
} else if (status_str == "NOK") {
this->status = NOK;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ class TcpPacket {
* @param file_size The size of the file to read.
*/
std::string readAndSaveToFile(const int fd, const std::string &file_name,
const size_t file_size, bool flag);
const size_t file_size, bool flag);

public:
/**
Expand Down
1 change: 0 additions & 1 deletion src/utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,4 @@ std::string getCurrentTimeFormated();

std::string getStartFullTime();


#endif

0 comments on commit 8c9155e

Please sign in to comment.