repeat HTTP requests once

This commit is contained in:
John
2023-06-17 10:24:08 +02:00
parent 333ae3b82f
commit ea87515ef5
4 changed files with 58 additions and 15 deletions
+4 -2
View File
@@ -379,8 +379,10 @@ void MainLoop::run() {
m_busHandler->formatUpdateInfo(&ostr); m_busHandler->formatUpdateInfo(&ostr);
ostr << "}"; ostr << "}";
string response; string response;
if (!m_httpClient.post("/", ostr.str(), &response)) { bool repeat = false;
if (!m_httpClient.post("/", ostr.str(), &response, &repeat)) {
logError(lf_main, "update check error: %s", response.c_str()); logError(lf_main, "update check error: %s", response.c_str());
nextCheckRun = now + (repeat ? CHECK_INITIAL_DELAY : CHECK_DELAY);
} else { } else {
m_updateCheck = response.empty() ? "unknown" : response; m_updateCheck = response.empty() ? "unknown" : response;
logNotice(lf_main, "update check: %s", response.c_str()); logNotice(lf_main, "update check: %s", response.c_str());
@@ -389,8 +391,8 @@ void MainLoop::run() {
dataSink->notifyUpdateCheckResult(response == "OK" ? "" : m_updateCheck); dataSink->notifyUpdateCheckResult(response == "OK" ? "" : m_updateCheck);
} }
} }
nextCheckRun = now + CHECK_DELAY;
} }
nextCheckRun = now + CHECK_DELAY;
} }
} }
time(&lastTaskRun); time(&lastTaskRun);
+31 -5
View File
@@ -57,6 +57,9 @@ ScanHelper::~ScanHelper() {
} }
} }
// the time slice to sleep when repeating an HTTP request
#define REPEAT_NANOS 1000000
result_t ScanHelper::collectConfigFiles(const string& relPath, const string& prefix, const string& extension, result_t ScanHelper::collectConfigFiles(const string& relPath, const string& prefix, const string& extension,
vector<string>* files, vector<string>* files,
bool ignoreAddressPrefix, const string& query, bool ignoreAddressPrefix, const string& query,
@@ -66,8 +69,19 @@ result_t ScanHelper::collectConfigFiles(const string& relPath, const string& pre
string uri = m_configUriPrefix + relPathWithSlash + m_configLangQuery + (m_configLangQuery.empty() ? "?" : "&") string uri = m_configUriPrefix + relPathWithSlash + m_configLangQuery + (m_configLangQuery.empty() ? "?" : "&")
+ "t=" + extension.substr(1) + query; + "t=" + extension.substr(1) + query;
string names; string names;
if (!m_configHttpClient->get(uri, "", &names)) { bool repeat = false;
return RESULT_ERR_NOTFOUND; if (!m_configHttpClient->get(uri, "", &names, &repeat)) {
if (!names.empty()) {
logError(lf_main, "HTTP failure%s: %s", repeat ? ", repeating" : "", names.c_str());
names = "";
}
if (!repeat) {
return RESULT_ERR_NOTFOUND;
}
usleep(REPEAT_NANOS);
if (!m_configHttpClient->get(uri, "", &names)) {
return RESULT_ERR_NOTFOUND;
}
} }
istringstream stream(names); istringstream stream(names);
string name; string name;
@@ -261,11 +275,23 @@ result_t ScanHelper::loadDefinitionsFromConfigPath(FileReader* reader, const str
time_t mtime = 0; time_t mtime = 0;
if (m_configUriPrefix.empty()) { if (m_configUriPrefix.empty()) {
stream = FileReader::openFile(m_configLocalPrefix + filename, errorDescription, &mtime); stream = FileReader::openFile(m_configLocalPrefix + filename, errorDescription, &mtime);
} else { } else if (m_configHttpClient) {
string uri = m_configUriPrefix + filename + m_configLangQuery;
string content; string content;
if (m_configHttpClient bool repeat = false;
&& m_configHttpClient->get(m_configUriPrefix + filename + m_configLangQuery, "", &content, &mtime)) { if (m_configHttpClient->get(uri, "", &content, &repeat, &mtime)) {
stream = new istringstream(content); stream = new istringstream(content);
} else {
if (!content.empty()) {
logError(lf_main, "HTTP failure%s: %s", repeat ? ", repeating" : "", content.c_str());
content = "";
}
if (repeat) {
usleep(REPEAT_NANOS);
if (m_configHttpClient->get(uri, "", &content, nullptr, &mtime)) {
stream = new istringstream(content);
}
}
} }
} }
result_t result; result_t result;
+12 -5
View File
@@ -431,12 +431,12 @@ void HttpClient::disconnect() {
} }
} }
bool HttpClient::get(const string& uri, const string& body, string* response, time_t* time) { bool HttpClient::get(const string& uri, const string& body, string* response, bool* repeatable, time_t* time) {
return request("GET", uri, body, response, time); return request("GET", uri, body, response, repeatable, time);
} }
bool HttpClient::post(const string& uri, const string& body, string* response) { bool HttpClient::post(const string& uri, const string& body, string* response, bool* repeatable) {
return request("POST", uri, body, response); return request("POST", uri, body, response, repeatable);
} }
const int indexToMonth[] = { const int indexToMonth[] = {
@@ -446,9 +446,13 @@ const int indexToMonth[] = {
-1, -1, 4, -1, -1, -1, -1, -1, // 24-31 -1, -1, 4, -1, -1, -1, -1, -1, // 24-31
}; };
bool HttpClient::request(const string& method, const string& uri, const string& body, string* response, time_t* time) { bool HttpClient::request(const string& method, const string& uri, const string& body, string* response,
bool* repeatable, time_t* time) {
if (!ensureConnected()) { if (!ensureConnected()) {
*response = "not connected"; *response = "not connected";
if (repeatable) {
*repeatable = true;
}
return false; return false;
} }
ostringstream ostr; ostringstream ostr;
@@ -473,6 +477,9 @@ bool HttpClient::request(const string& method, const string& uri, const string&
if (sent < 0) { if (sent < 0) {
disconnect(); disconnect();
*response = "send error"; *response = "send error";
if (repeatable) {
*repeatable = true;
}
return false; return false;
} }
pos += sent; pos += sent;
+11 -3
View File
@@ -188,19 +188,24 @@ class HttpClient {
* @param uri the URI string. * @param uri the URI string.
* @param body the optional body to send. * @param body the optional body to send.
* @param response the response body from the server (or the HTTP header on error). * @param response the response body from the server (or the HTTP header on error).
* @param repeatable optional pointer to a bool in which to store whether the request should be repeated later on
* (e.g. due to temporary connectivity issues).
* @param time optional pointer to a @a time_t value for storing the modification time of the file, or nullptr. * @param time optional pointer to a @a time_t value for storing the modification time of the file, or nullptr.
* @return true on success, false on error. * @return true on success, false on error.
*/ */
bool get(const string& uri, const string& body, string* response, time_t* time = nullptr); bool get(const string& uri, const string& body, string* response, bool* repeatable = nullptr,
time_t* time = nullptr);
/** /**
* Execute a POST request. * Execute a POST request.
* @param uri the URI string. * @param uri the URI string.
* @param body the optional body to send. * @param body the optional body to send.
* @param response the response body from the server (or the HTTP header on error). * @param response the response body from the server (or the HTTP header on error).
* @param repeatable optional pointer to a bool in which to store whether the request should be repeated later on
* (e.g. due to temporary connectivity issues).
* @return true on success, false on error. * @return true on success, false on error.
*/ */
bool post(const string& uri, const string& body, string* response); bool post(const string& uri, const string& body, string* response, bool* repeatable = nullptr);
/** /**
* Execute an arbitrary request. * Execute an arbitrary request.
@@ -208,10 +213,13 @@ class HttpClient {
* @param uri the URI string. * @param uri the URI string.
* @param body the optional body to send. * @param body the optional body to send.
* @param response the response body from the server (or the HTTP header on error). * @param response the response body from the server (or the HTTP header on error).
* @param repeatable optional pointer to a bool in which to store whether the request should be repeated later on
* (e.g. due to temporary connectivity issues).
* @param time optional pointer to a @a time_t value for storing the modification time of the file, or nullptr. * @param time optional pointer to a @a time_t value for storing the modification time of the file, or nullptr.
* @return true on success, false on error. * @return true on success, false on error.
*/ */
bool request(const string& method, const string& uri, const string& body, string* response, time_t* time = nullptr); bool request(const string& method, const string& uri, const string& body, string* response,
bool* repeatable = nullptr, time_t* time = nullptr);
private: private:
/** /**