Skip to content

Commit

Permalink
[UDP] - server: list auctions that user has bidded done
Browse files Browse the repository at this point in the history
  • Loading branch information
simaosanguinho committed Dec 14, 2023
1 parent 3e08f2c commit 6f7df1d
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 5 deletions.
39 changes: 37 additions & 2 deletions src/server/handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,44 @@ void handleListUserBids(AuctionServerState &state, std::stringstream &buf,
SocketAddress &addressFrom) {
std::cout << "Handling list user bids request" << std::endl;

(void)state;
(void)buf;
(void)addressFrom;

ListUserBidsRequest request;
ListUserBidsResponse response;

try {
request.deserialize(buf);
state.cdebug << "[ListUserBids] User "
<< " requested to list bids" << std::endl;

if (state.usersManager.isUserLoggedIn(request.userID) != INVALID) {
response.auctions =
state.auctionManager.getAuctionsBiddedByUser(request.userID);

response.status = ListUserBidsResponse::OK;
state.cdebug << "[ListUserBids] Bids listed successfully" << std::endl;
} else {
response.status = ListUserBidsResponse::NLG;
state.cdebug << "[ListUserBids] User " << request.userID
<< " is not logged in" << std::endl;
}

} catch (NoOngoingBidsException &e) {
state.cdebug << "[ListUserBids] User " << request.userID
<< " has not bidded on any auction" << std::endl;
response.status = ListUserBidsResponse::NOK;
} catch (InvalidPacketException &e) {
state.cdebug << "[ListUserBids] Invalid packet received" << std::endl;
response.status = ListUserBidsResponse::ERR;
} catch (std::exception &e) {
std::cerr << "[ListUserBids] There was an unhandled exception that "
"prevented the user from listing bids: "
<< e.what() << std::endl;
return;
}

send_packet(response, addressFrom.socket,
(struct sockaddr *)&addressFrom.addr, addressFrom.size);
}

void handleListAuctions(AuctionServerState &state, std::stringstream &buf,
Expand Down
4 changes: 3 additions & 1 deletion src/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ void setupDB() {
std::string nextAuctionFile = AUCTIONDIR;
nextAuctionFile += "/next_auction.txt";
create_new_file(nextAuctionFile);
write_to_file(nextAuctionFile, "1\n");

// CHANGE THISSSSS!!!!! HARD CODED
write_to_file(nextAuctionFile, "3\n");
};

void wait_for_udp_packet(AuctionServerState &state) {
Expand Down
69 changes: 68 additions & 1 deletion src/server/server_auction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ uint32_t AuctionManager::openAuction(std::string userID,
int randomInteger = distribution(gen);
// to sr
std::string auctionID = std::to_string(randomInteger);
std::cout << "Creating auction for user " << userID << std::endl;
// std::cout << "Creating auction for user " << userID << std::endl;
std::string auctionPath = AUCTIONDIR;
auctionPath += "/" + auctionID;
create_new_directory(auctionPath);
Expand Down Expand Up @@ -146,4 +146,71 @@ std::string AuctionManager::getAuctionInfo(std::string auctionID) {
} catch (std::exception &e) {
throw;
}
}

std::vector<std::string>
AuctionManager::getAuctionBidders(std::string auctionID) {
try {
std::string auctionPath = AUCTIONDIR;
auctionPath += "/" + auctionID;
std::string auctionBidsPath = auctionPath + "/BIDS";

// iterate over all files in BIDS directory
std::vector<std::string> auctionsBidders;

int i = 0;
for (const auto &entry :
std::filesystem::directory_iterator(auctionBidsPath)) {
if (entry.is_regular_file() && entry.path().extension() == ".txt") {

std::string auctionBidFile = entry.path();

std::string bidder;
bidder = getFirstWord(auctionBidFile);
auctionsBidders.push_back(bidder);
}
i++;
}

if (auctionsBidders.size() == 0) {
throw NoOngoingBidsException();
}

return auctionsBidders;
} catch (std::exception &e) {
throw;
}
}

std::vector<std::pair<std::string, uint8_t>>
AuctionManager::getAuctionsBiddedByUser(std::string userID) {

std::vector<std::pair<std::string, uint8_t>> auctions;
std::vector<std::pair<std::string, uint8_t>> auctionsBiddedByUser;
try {
AuctionManager auctionManager;

auctions = auctionManager.listAuctions();
for (auto auction : auctions) {
std::vector<std::string> auctionBidders =
auctionManager.getAuctionBidders(auction.first);
for (auto bidder : auctionBidders) {
if (bidder == userID) {
auctionsBiddedByUser.push_back(auction);
}
}
}

if (auctionsBiddedByUser.size() == 0) {
throw NoOngoingBidsException();
}

return auctionsBiddedByUser;
} catch (NoOngoingBidsException &e) {
throw;
} catch (NoAuctionsException &e) {
throw NoOngoingBidsException();
} catch (std::exception &e) {
throw;
}
}
14 changes: 14 additions & 0 deletions src/server/server_auction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ class AuctionManager {

std::string getAuctionInfo(std::string auctionID);

std::vector<std::string> getAuctionBidders(std::string auctionID);

std::vector<std::pair<std::string, uint8_t>>
getAuctionsBiddedByUser(std::string userID);

/* lists auctions in which an user has bidded*/
std::vector<std::pair<std::string, uint8_t>> listUserBids(std::string userID);

// constructor
AuctionManager() = default;
// destructor
Expand All @@ -37,4 +45,10 @@ class NoAuctionsException : public std::runtime_error {
: std::runtime_error("No auctions have been opened yet") {}
};

class NoOngoingBidsException : public std::runtime_error {
public:
NoOngoingBidsException()
: std::runtime_error("The user has not yet bidded") {}
};

#endif
2 changes: 1 addition & 1 deletion src/utils/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ std::stringstream ListUserBidsResponse::serialize() {
if (status == OK) {
buffer << "OK";
for (auto auction : auctions) {
buffer << " " << auction.first << " " << auction.second;
buffer << " " << auction.first << " " << std::to_string(auction.second);
}
} else if (status == NOK) {
buffer << "NOK";
Expand Down
12 changes: 12 additions & 0 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,15 @@ std::string intToStringWithZeros(int number) {

return oss.str();
}

std::string getFirstWord(std::string path) {
std::string firstWord;
read_from_file(path, firstWord);
firstWord = firstWord.substr(0, firstWord.find(" "));

// remove the last character if it is a newline
if (firstWord[firstWord.length() - 1] == '\n') {
firstWord = firstWord.substr(0, firstWord.length() - 1);
}
return firstWord;
}
3 changes: 3 additions & 0 deletions src/utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,7 @@ std::string getCurrentTimeFormated();
std::string getStartFullTime();

std::string intToStringWithZeros(int number);

std::string getFirstWord(std::string path);

#endif

0 comments on commit 6f7df1d

Please sign in to comment.