2021-02-05 17:22:52 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
2025-02-10 17:00:41 +01:00
|
|
|
* Copyright (C) 2021-2025 Tom Zander <tom@flowee.org>
|
2021-02-05 17:22:52 +01:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
#include "NotificationCenter.h"
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
|
|
NotificationCenter::NotificationCenter()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NotificationCenter::notifyNewBlock(int height)
|
|
|
|
|
{
|
|
|
|
|
P2PNet::Notification n;
|
|
|
|
|
n.blockHeight = height;
|
2022-08-15 20:10:05 +02:00
|
|
|
std::vector<NotificationListener*> listeners;
|
|
|
|
|
{
|
2025-02-10 17:00:41 +01:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(m_lock);
|
2022-08-15 20:10:05 +02:00
|
|
|
listeners = m_listeners;
|
|
|
|
|
}
|
|
|
|
|
if (listeners.empty())
|
2021-02-05 17:22:52 +01:00
|
|
|
return;
|
2022-08-15 20:10:05 +02:00
|
|
|
size_t i = 0;
|
|
|
|
|
while (i < listeners.size()) {
|
|
|
|
|
auto copy = listeners.at(i);
|
2021-02-05 17:22:52 +01:00
|
|
|
copy->notifyNewBlock(n);
|
2022-08-15 20:10:05 +02:00
|
|
|
if (listeners.at(i) == copy) // it didn't remove itself.
|
2021-02-05 17:22:52 +01:00
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NotificationCenter::notifyNewTransaction(const P2PNet::Notification &n)
|
|
|
|
|
{
|
|
|
|
|
size_t i = 0;
|
2025-02-10 17:00:41 +01:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(m_lock);
|
2021-02-05 17:22:52 +01:00
|
|
|
if (m_listeners.empty())
|
|
|
|
|
return;
|
|
|
|
|
while (i < m_listeners.size()) {
|
|
|
|
|
auto copy = m_listeners.at(i);
|
|
|
|
|
copy->notifyNewTransaction(n);
|
|
|
|
|
if (copy->isCollating())
|
|
|
|
|
copy->updateSegment(n);
|
|
|
|
|
|
|
|
|
|
if (m_listeners.at(i) == copy) // it didn't remove itself.
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NotificationCenter::addListener(NotificationListener *nl)
|
|
|
|
|
{
|
|
|
|
|
assert(nl);
|
2025-02-10 17:00:41 +01:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(m_lock);
|
2021-02-05 17:22:52 +01:00
|
|
|
m_listeners.push_back(nl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NotificationCenter::removeListener(NotificationListener *nl)
|
|
|
|
|
{
|
2025-02-10 17:00:41 +01:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(m_lock);
|
2021-02-05 17:22:52 +01:00
|
|
|
auto iter = m_listeners.begin();
|
|
|
|
|
while (iter != m_listeners.end()) {
|
|
|
|
|
if (*iter == nl)
|
|
|
|
|
iter = m_listeners.erase(iter);
|
|
|
|
|
else
|
|
|
|
|
++iter;
|
|
|
|
|
}
|
|
|
|
|
}
|