Skip to content

Commit

Permalink
Ack pending add and move on if server sends 400 Bad Request or 404 No…
Browse files Browse the repository at this point in the history
…t Found.
  • Loading branch information
ebarlas committed May 3, 2024
1 parent af25368 commit a32898e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 71 deletions.
23 changes: 7 additions & 16 deletions src/game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,18 @@ void trippin::Game::initDbSynchronizer() {
m->addScore(s);
};
auto scoreDispatchFn = [t = transport.get()](const Score &s) {
auto id = s.id.c_str();
auto name = s.name.c_str();
if (t->addScore(s)) {
SDL_Log("added score, id=%s, game=%d, name=%s, score=%d", id, s.game, name, s.score);
return true;
} else {
SDL_Log("failed to add score, id=%s, game=%d, name=%s, score=%d", id, s.game, name, s.score);
return false;
}
auto result = t->addScore(s);
SDL_Log("add score attempted, id=%s, game=%d, name=%s, score=%d, result=%s",
s.id.c_str(), s.game, s.name.c_str(), s.score, toString(result));
return result == AddResult::success || result == AddResult::clientError;
};
scoreDb = std::make_unique<Db<Score>>("scores", configuration.version.major, scoreAddCallback, scoreDispatchFn);
scoreDb->start();
auto logAddCallback = [](const LogEvent &e) {};
auto logDispatchFn = [t = transport.get()](const LogEvent &e) {
if (t->addLogEvent(e)) {
SDL_Log("sent log event, index=%d", e.index);
return true;
} else {
SDL_Log("failed to send log event, index=%d", e.index);
return false;
}
auto result = t->addLogEvent(e);
SDL_Log("add log event attempted, index=%d, result=%s", e.index, toString(result));
return result == AddResult::success || result == AddResult::clientError;
};
logDb = std::make_unique<Db<LogEvent>>("logs", configuration.version.major, logAddCallback, logDispatchFn);
logDb->start();
Expand Down
23 changes: 23 additions & 0 deletions src/net/AddResult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef TRIPPIN_ADDRESULT_H
#define TRIPPIN_ADDRESULT_H

namespace trippin {
enum class AddResult {
success,
clientError,
other
};

inline const char *toString(AddResult r) {
switch (r) {
case AddResult::success:
return "success";
case AddResult::clientError:
return "clientError";
default:
return "other";
}
}
}

#endif
96 changes: 48 additions & 48 deletions src/net/Transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

trippin::Transport::Transport(std::string host, int port, int version, int limit)
: host(std::move(host)), port(port), version(version), limit(limit) {

}

trippin::Transport::Scores trippin::Transport::topScores() const {
Expand All @@ -23,19 +22,15 @@ trippin::Transport::Scores trippin::Transport::sendRequest(const std::string &ur
return {};
}

std::string msg = "GET ";
msg += uri;
msg += "?version=";
msg += std::to_string(version);
msg += "&limit=";
msg += std::to_string(limit);
msg += " HTTP/1.0\r\n";
msg += "Host: ";
msg += host;
msg += "\r\n";
msg += "Accept: */*\r\n\r\n";

tcp.send(msg);
std::stringstream req;
req << "GET " << uri
<< "?version=" << version
<< "&limit=" << limit
<< " HTTP/1.0\r\n"
<< "Host: " << host << "\r\n"
<< "Accept: */*\r\n\r\n";

tcp.send(req.str());
std::string response = tcp.receive(1'024 * 64);

auto f = response.find("\r\n\r\n");
Expand All @@ -58,11 +53,11 @@ trippin::Transport::Scores trippin::Transport::sendRequest(const std::string &ur
return scores;
}

bool trippin::Transport::addScore(const Score &score) const {
trippin::AddResult trippin::Transport::addScore(const Score &score) const {
Tcp tcp(host, port);
auto sock = tcp.get();
if (sock == nullptr) {
return false;
return AddResult::other;
}

nlohmann::json j = score.to_json();
Expand All @@ -76,29 +71,25 @@ bool trippin::Transport::addScore(const Score &score) const {

auto json = j.dump();

std::string msg = "POST /scores?h=";
msg += std::to_string(hash);
msg += " HTTP/1.0\r\n";
msg += "Host: ";
msg += host;
msg += "\r\n";
msg += "Content-Type: application/json\r\n";
msg += "Content-Length: ";
msg += std::to_string(json.size());
msg += "\r\n";
msg += "Accept: */*\r\n\r\n";
msg += json;

tcp.send(msg);
std::stringstream req;
req << "POST /scores?h=" << hash
<< " HTTP/1.0\r\n"
<< "Host: " << host << "\r\n"
<< "Content-Type: application/json\r\n"
<< "Content-Length: " << json.size() << "\r\n"
<< "Accept: */*\r\n\r\n"
<< json;

tcp.send(req.str());
std::string response = tcp.receive(4'096);
return response.find(" 200 OK") != std::string::npos;
return classifyResponse(response);
}

bool trippin::Transport::addLogEvent(const LogEvent &event) const {
trippin::AddResult trippin::Transport::addLogEvent(const LogEvent &event) const {
Tcp tcp(host, port);
auto sock = tcp.get();
if (sock == nullptr) {
return false;
return AddResult::other;
}

nlohmann::json j;
Expand All @@ -114,20 +105,29 @@ bool trippin::Transport::addLogEvent(const LogEvent &event) const {

auto json = j.dump();

std::string msg = "POST /logs?h=";
msg += std::to_string(hash);
msg += " HTTP/1.0\r\n";
msg += "Host: ";
msg += host;
msg += "\r\n";
msg += "Content-Type: application/json\r\n";
msg += "Content-Length: ";
msg += std::to_string(json.size());
msg += "\r\n";
msg += "Accept: */*\r\n\r\n";
msg += json;

tcp.send(msg);
std::stringstream req;
req << "POST /logs?h=" << hash
<< " HTTP/1.0\r\n"
<< "Host: " << host << "\r\n"
<< "Content-Type: application/json\r\n"
<< "Content-Length: " << json.size() << "\r\n"
<< "Accept: */*\r\n\r\n"
<< json;

tcp.send(req.str());
std::string response = tcp.receive(4'096);
return response.find(" 200 OK") != std::string::npos;
return classifyResponse(response);
}

trippin::AddResult trippin::Transport::classifyResponse(const std::string &response) {
auto findFn = [&response](const char *str) {
return response.find(str) != std::string::npos;
};
if (findFn(" 200 OK")) {
return AddResult::success;
}
if (findFn(" 400 Bad Request") || findFn(" 404 Not Found")) {
return AddResult::clientError;
}
return AddResult::other;
}
14 changes: 7 additions & 7 deletions src/net/Transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vector>
#include "Score.h"
#include "LogEvent.h"
#include "AddResult.h"

namespace trippin {
class Transport {
Expand All @@ -15,18 +16,17 @@ namespace trippin {
};

Transport(std::string host, int port, int version, int limit);
bool addScore(const Score &score) const;
bool addLogEvent(const LogEvent &event) const;
Scores topScores() const;
Scores todayScores() const;
[[nodiscard]] AddResult addScore(const Score &score) const;
[[nodiscard]] AddResult addLogEvent(const LogEvent &event) const;
[[nodiscard]] Scores topScores() const;
[[nodiscard]] Scores todayScores() const;
private:
const std::string host;
const int port;
const int version;
const int limit;
static std::vector<int> compress(const std::vector<Score::InputEvent> &events);
static std::vector<std::vector<int>> compress(const std::vector<std::vector<Score::InputEvent>> &vecs);
Scores sendRequest(const std::string &uri) const;
static AddResult classifyResponse(const std::string &response);
[[nodiscard]] Scores sendRequest(const std::string &uri) const;
};
}

Expand Down

0 comments on commit a32898e

Please sign in to comment.