Browse Source

C++ has no coroutines (yet), and the ones of boost are so ugly

synchronous
Volker Birk 5 years ago
parent
commit
de3189fc0d
  1. 21
      passphrase_cache.cc
  2. 11
      passphrase_cache.hh
  3. 11
      test/test_passphrase_cache.cc

21
passphrase_cache.cc

@ -43,5 +43,26 @@ namespace pEp {
entry->tp = clock::now();
_cache.splice(_cache.end(), _cache, entry);
}
std::string PassphraseCache::latest_passphrase()
{
std::lock_guard<std::mutex> lock(_mtx);
cleanup();
_which = _cache.end();
return "";
}
std::string PassphraseCache::next_passphrase()
{
std::lock_guard<std::mutex> lock(_mtx);
if (_cache.empty() || _which == _cache.begin()) {
return "";
}
else {
--_which;
return _which->passphrase;
}
}
};

11
passphrase_cache.hh

@ -28,6 +28,8 @@ namespace pEp {
int _max_size;
duration _timeout;
cache::iterator _which;
public:
PassphraseCache(int max_size=20, duration timeout =
std::chrono::minutes(10)) : _max_size(max_size),
@ -49,6 +51,15 @@ namespace pEp {
using passphrase_callee = std::function<bool(std::string)>;
bool for_each_passphrase(const passphrase_callee& callee);
// get all passphrases in cache from latest to oldest
// always returns ""
std::string latest_passphrase();
// get next passphrase; returns "" when no passphrases are left
std::string next_passphrase();
// convenience functions
// i.e.
// status = cache.api(::encrypt_message, session, src, extra, dst, enc_format, flags)

11
test/test_passphrase_cache.cc

@ -49,6 +49,15 @@ int main()
status = cache.api(api_test2, session, n, str, bytes, sl);
assert(status == PEP_STATUS_OK);
std::cout << "expected: two passphrases in order\n";
pEp::PassphraseCache _cache = cache;
_cache.latest_passphrase();
for (std::string passphrase = _cache.next_passphrase(); passphrase != "" ;
passphrase = _cache.next_passphrase()) {
std::cout << passphrase << "\n";
}
sleep(2);
std::cout << "expected: no passphrase\n";
@ -59,8 +68,6 @@ int main()
status = cache.api(api_test2, session, 23, str, bytes, sl);
assert(status == PEP_STATUS_OK);
pEp::PassphraseCache _cache = cache;
::release(session);
return 0;
}

Loading…
Cancel
Save