Files

105 lines
3.1 KiB
C++
Raw Permalink Normal View History

2019-03-30 14:26:00 +01:00
/*
* This file is part of the Flowee project
2021-06-20 22:44:44 +02:00
* Copyright (C) 2019 Tom Zander <tom@flowee.org>
2019-03-30 14:26:00 +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/>.
*/
#ifndef INDEXER_H
#define INDEXER_H
2019-06-26 21:39:07 +02:00
#include <QString>
2019-03-30 14:26:00 +01:00
2019-06-26 21:39:07 +02:00
#include <Message.h>
2019-03-30 14:26:00 +01:00
#include <NetworkManager.h>
2019-04-09 19:34:55 +02:00
#include <NetworkService.h>
2019-06-26 21:39:07 +02:00
#include <QMutex>
#include <QWaitCondition>
2019-03-30 14:26:00 +01:00
#include <WorkerThreads.h>
2019-04-10 22:17:13 +02:00
#include <qtimer.h>
2023-12-21 15:13:56 +01:00
#include <memory>
2019-03-30 14:26:00 +01:00
2019-06-26 21:39:07 +02:00
class AddressIndexer;
class TxIndexer;
class SpentOutputIndexer;
2019-04-09 19:34:55 +02:00
class Indexer : public QObject, public NetworkService
2019-03-30 14:26:00 +01:00
{
2019-04-06 18:19:57 +02:00
Q_OBJECT
2019-03-30 14:26:00 +01:00
public:
Indexer(const boost::filesystem::path &basedir);
2019-09-12 15:21:48 +02:00
~Indexer() override;
2019-03-30 14:26:00 +01:00
/// connect to server
void tryConnectHub(const EndPoint &ep);
2019-04-09 19:34:55 +02:00
/// listen to incoming requests
void bind(const boost::asio::ip::tcp::endpoint &endpoint);
2019-03-30 14:26:00 +01:00
/// load config, if prioHubLocation is valid prefer that one.
void loadConfig(const QString &filename, const EndPoint &prioHubLocation);
2019-04-06 15:16:37 +02:00
2019-04-09 19:34:55 +02:00
// network service API
void onIncomingMessage(Remote *con, const Message &message, const EndPoint &ep) override;
2019-06-26 21:39:07 +02:00
/// called by the workerthreads to get a block-message. Blocking.
2019-08-09 17:18:08 +02:00
/// \param height is the requested blockheight of the next block to process
2019-12-12 16:02:08 +01:00
Message nextBlock(int height, int *knownTip, unsigned long timeout = ULONG_MAX);
2019-06-26 21:39:07 +02:00
2019-04-06 18:19:57 +02:00
private slots:
2019-08-13 16:19:31 +02:00
void requestBlock(int newBlockHeight = -1);
2019-04-11 14:59:14 +02:00
void checkBlockArrived();
2019-04-06 18:19:57 +02:00
void onFindAddressRequest(const Message &message);
signals:
void requestFindAddress(const Message &message);
2019-03-30 14:26:00 +01:00
private:
void hubConnected(const EndPoint &ep);
void hubDisconnected();
void hubSentMessage(const Message &message);
2019-04-10 14:30:30 +02:00
void clientConnected(NetworkConnection &con);
2019-04-09 19:34:55 +02:00
2019-03-30 14:26:00 +01:00
private:
2019-04-10 22:17:13 +02:00
QTimer m_pollingTimer;
2023-12-21 15:13:56 +01:00
std::shared_ptr<Streaming::BufferPool> m_pool;
std::shared_ptr<Streaming::BufferPool> m_poolAddressAnswers; // FIXME use thread local instead
2019-06-26 21:39:07 +02:00
boost::filesystem::path m_basedir;
TxIndexer *m_txdb = nullptr;
SpentOutputIndexer *m_spentOutputDb = nullptr;
AddressIndexer *m_addressdb = nullptr;
2019-03-30 14:26:00 +01:00
WorkerThreads m_workers;
NetworkManager m_network;
NetworkConnection m_serverConnection;
2019-04-06 15:16:37 +02:00
bool m_isServer = false; /// remembers if we (successfully) called m_network::bind() once.
2019-04-11 14:59:14 +02:00
int m_lastRequestedBlock = 0;
2019-09-12 15:21:48 +02:00
qint64 m_timeLastRequest = 0;
qint64 m_timeLastLogLine = 0;
2019-08-18 20:24:13 +02:00
2019-06-26 21:39:07 +02:00
// data to process blocks in different workers.
Message m_nextBlock;
2019-12-12 14:49:46 +01:00
mutable std::atomic_int m_bestBlockHeight;
2019-06-26 21:39:07 +02:00
mutable QMutex m_nextBlockLock;
mutable QWaitCondition m_waitForBlock;
2019-03-30 14:26:00 +01:00
};
#endif