network engine: move transaction ownership to Node

This commit is contained in:
Adrien Béraud committed 2017-10-11 17:36:20 +02:00
1 parent 8c529b7d7f
commit 3bb5e7ee12
6 files changed
+148 -146

No files matched your search

+35 -23
View File
@@ -41,11 +41,9 @@ Node::isGood(time_point now) const
bool
Node::isPendingMessage() const
{
for (auto w : requests_) {
if (auto r = w.lock()) {
if (r->pending())
return true;
}
for (auto r : requests_) {
if (r.second->pending())
return true;
}
return false;
}
@@ -54,11 +52,9 @@ size_t
Node::getPendingMessageCount() const
{
size_t count {0};
for (auto w : requests_) {
if (auto r = w.lock()) {
if (r->pending())
count++;
}
for (auto r : requests_) {
if (r.second->pending())
count++;
}
return count;
}
@@ -71,27 +67,44 @@ Node::update(const SockAddr& new_addr)
/** To be called when a message was sent to the node */
void
Node::requested(std::shared_ptr<net::Request>& req)
Node::requested(const Sp<net::Request>& req)
{
requests_.emplace_back(req);
auto e = requests_.emplace(req->getTid(), req);
if (not e.second) {
// Should not happen !
// Try to handle this scenario as well as we can
e.first->second->setExpired();
e.first->second = req;
}
}
/** To be called when a message was received from the node.
Req should be true if the message was an aswer to a request we made*/
void
Node::received(time_point now, std::shared_ptr<net::Request> req)
Node::received(time_point now, const Sp<net::Request>& req)
{
time = now;
expired_ = false;
if (req) {
reply_time = now;
for (auto it = requests_.begin(); it != requests_.end();) {
auto r = it->lock();
if (not r or r == req)
it = requests_.erase(it);
else
++it;
}
requests_.erase(req->getTid());
}
}
Sp<net::Request>
Node::getRequest(const net::TransId& tid)
{
auto it = requests_.find(tid);
return it != requests_.end() ? it->second : nullptr;
}
void
Node::cancelRequest(const Sp<net::Request>& req)
{
if (req) {
req->cancel();
closeSocket(req->getSocket());
requests_.erase(req->getTid());
}
}
@@ -99,9 +112,8 @@ void
Node::setExpired()
{
expired_ = true;
for (auto w : requests_) {
if (auto r = w.lock())
r->setExpired();
for (auto r : requests_) {
r.second->setExpired();
}
requests_.clear();
sockets_.clear();