mirror of
https://github.com/savoirfairelinux/opendht.git
synced 2026-11-11 22:07:10 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce8744862a | ||
|
|
d14759eadf |
No files matched your search
@@ -437,8 +437,10 @@ private:
|
||||
/**
|
||||
* Refresh a listen via a token
|
||||
* @param token
|
||||
* @param fetchValues ask the server to send the values it stores, for a listener
|
||||
* that may have missed some (its previous subscription failed).
|
||||
*/
|
||||
void resubscribe(const InfoHash& key, const size_t token, Listener& listener);
|
||||
void resubscribe(const InfoHash& key, const size_t token, Listener& listener, bool fetchValues = false);
|
||||
|
||||
/**
|
||||
* If we want to use push notifications by default.
|
||||
|
||||
@@ -1213,7 +1213,9 @@ DhtProxyClient::restartListeners(const asio::error_code& ec)
|
||||
for (auto& search : searches_)
|
||||
for (auto& listener : search.second.listeners)
|
||||
if (!listener.second.opstate->ok)
|
||||
resubscribe(search.first, listener.first, listener.second);
|
||||
// The subscription failed: values stored on the key since then were never
|
||||
// received (e.g. a connection request sent while the device had no network).
|
||||
resubscribe(search.first, listener.first, listener.second, true);
|
||||
return;
|
||||
}
|
||||
if (logger_)
|
||||
@@ -1376,7 +1378,8 @@ DhtProxyClient::pushNotificationReceived([[maybe_unused]] const std::map<std::st
|
||||
void
|
||||
DhtProxyClient::resubscribe([[maybe_unused]] const InfoHash& key,
|
||||
[[maybe_unused]] const size_t token,
|
||||
[[maybe_unused]] Listener& listener)
|
||||
[[maybe_unused]] Listener& listener,
|
||||
[[maybe_unused]] bool fetchValues)
|
||||
{
|
||||
#ifdef OPENDHT_PUSH_NOTIFICATIONS
|
||||
if (deviceKey_.empty())
|
||||
@@ -1398,7 +1401,7 @@ DhtProxyClient::resubscribe([[maybe_unused]] const InfoHash& key,
|
||||
listener.refreshSubscriberTimer->async_wait(
|
||||
std::bind(&DhtProxyClient::handleResubscribe, this, std::placeholders::_1, key, token, opstate));
|
||||
auto vcb = listener.cb;
|
||||
sendListen(header, vcb, opstate, listener, ListenMethod::RESUBSCRIBE);
|
||||
sendListen(header, vcb, opstate, listener, fetchValues ? ListenMethod::SUBSCRIBE : ListenMethod::RESUBSCRIBE);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1423,7 +1426,7 @@ DhtProxyClient::fillBody(bool resubscribe)
|
||||
Json::Value body;
|
||||
getPushRequest(body);
|
||||
if (resubscribe) {
|
||||
// This is the first listen, we want to retrieve previous values.
|
||||
// Refreshing a subscription: the server does not send the values it stores again.
|
||||
body["refresh"] = true;
|
||||
}
|
||||
auto content = Json::writeString(jsonBuilder_, body) + "\n";
|
||||
|
||||
@@ -90,4 +90,48 @@ DhtProxyClientTester::testSetPushNotificationTokenResubscribesWithNewToken()
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
DhtProxyClientTester::testRestartListenersFetchesValuesAfterFailedSubscribe()
|
||||
{
|
||||
#ifdef OPENDHT_PUSH_NOTIFICATIONS
|
||||
DhtProxyClient client({}, {}, [] {}, "http://127.0.0.1:8080", "OpenDHT-Test", "client-id", "push-token");
|
||||
|
||||
auto addListener = [&](const InfoHash& key, bool ok) -> DhtProxyClient::Listener& {
|
||||
auto& search = client.searches_[key];
|
||||
auto [it, inserted] = search.listeners.emplace(std::piecewise_construct,
|
||||
std::forward_as_tuple(1),
|
||||
std::forward_as_tuple(ValueCallback(
|
||||
[](const std::vector<Sp<Value>>&, bool) { return true; })));
|
||||
CPPUNIT_ASSERT(inserted);
|
||||
auto& listener = it->second;
|
||||
listener.opstate = std::make_shared<DhtProxyClient::OperationState>();
|
||||
listener.opstate->ok = ok;
|
||||
listener.cb = [](const std::vector<Sp<Value>>&, bool, system_clock::time_point) {
|
||||
return true;
|
||||
};
|
||||
return listener;
|
||||
};
|
||||
|
||||
// The subscription of this listener failed (e.g. no network): values stored on the
|
||||
// key since then were never received.
|
||||
auto failedKey = InfoHash::get("proxy-client-failed-subscribe");
|
||||
auto& failed = addListener(failedKey, false);
|
||||
auto healthyKey = InfoHash::get("proxy-client-healthy-subscribe");
|
||||
auto& healthy = addListener(healthyKey, true);
|
||||
|
||||
client.restartListeners({});
|
||||
|
||||
CPPUNIT_ASSERT(failed.request);
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("/key/") + failedKey.toString(),
|
||||
std::string(failed.request->header_.request_target()));
|
||||
CPPUNIT_ASSERT(failed.request->body_.find("push-token") != std::string::npos);
|
||||
// Without "refresh", the server answers with the values it stores for the key.
|
||||
CPPUNIT_ASSERT(failed.request->body_.find("\"refresh\"") == std::string::npos);
|
||||
|
||||
CPPUNIT_ASSERT(!healthy.request);
|
||||
#else
|
||||
CPPUNIT_ASSERT(true);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
@@ -12,11 +12,13 @@ class DhtProxyClientTester : public CppUnit::TestFixture
|
||||
CPPUNIT_TEST_SUITE(DhtProxyClientTester);
|
||||
CPPUNIT_TEST(testResubscribeUsesKeyRoute);
|
||||
CPPUNIT_TEST(testSetPushNotificationTokenResubscribesWithNewToken);
|
||||
CPPUNIT_TEST(testRestartListenersFetchesValuesAfterFailedSubscribe);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
void testResubscribeUsesKeyRoute();
|
||||
void testSetPushNotificationTokenResubscribesWithNewToken();
|
||||
void testRestartListenersFetchesValuesAfterFailedSubscribe();
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
Reference in new issue
Block a user