Files
thehub/libs/p2p/Action.h
T

78 lines
2.3 KiB
C++
Raw Permalink Normal View History

2020-04-17 19:33:06 +02:00
/*
* This file is part of the Flowee project
* Copyright (C) 2020-2025 Tom Zander <tom@flowee.org>
2020-04-17 19:33:06 +02: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/>.
*/
2024-01-22 19:32:15 +01:00
#ifndef FLOWEE_ACTION_H
#define FLOWEE_ACTION_H
2020-04-17 19:33:06 +02:00
#include <boost/asio/deadline_timer.hpp>
2021-02-02 13:05:19 +01:00
class DownloadManager;
2021-02-03 14:33:38 +01:00
/***
* The Action is a baseclass for the P2PNet async maintainance actions.
*
* Most of the design of the p2p lib is based on events. A peer sends something,
* we respond.
* This design makes it really hard to do monitoring like actions, for instance
* there is no clean way to use events to respond to a peer NOT doing something.
*
* This is where actions come in, they are owned by the DownloadManager and
* run every couple of seconds in order to do things.
*
* Any user action can be created and you can reimplement execute() which will
* get called periodically.
*
* To start:
* @code
* DownloadManager::addAction<MyAction>();
* @endcode
*
* To stop call `DownloadManager::done(this);`
*
* And please be sure to call 'again()' every single iteration of execute() as long
* as the action is not done yet.
*/
2020-04-17 19:33:06 +02:00
class Action
{
public:
virtual ~Action();
2021-02-03 14:33:38 +01:00
/// This is called by the DownloadManager to call execute() async
2020-04-17 19:33:06 +02:00
void start();
2021-02-03 14:33:38 +01:00
/// This is called on system shutdown
2020-04-17 19:33:06 +02:00
virtual void cancel();
protected:
explicit Action(DownloadManager *parent);
virtual void execute(const boost::system::error_code &error) = 0;
2021-02-03 14:33:38 +01:00
/// Makes the execute() method be called again after an interval.
2020-04-17 19:33:06 +02:00
void again();
2021-02-03 14:33:38 +01:00
DownloadManager * const m_dlm;
/// Set the amount of milliseconds that 'again()' waits
void setInterval(int milliseconds);
2020-04-17 19:33:06 +02:00
private:
boost::asio::deadline_timer m_timer;
2021-02-03 14:33:38 +01:00
int m_interval = 1500;
bool m_cancelled = false;
2020-04-17 19:33:06 +02:00
};
#endif