Files

135 lines
4.3 KiB
C++
Raw Permalink Normal View History

2018-02-15 14:47:17 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2019-2021 Tom Zander <tom@flowee.org>
2018-02-15 14:47:17 +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/>.
*/
2024-01-22 19:32:15 +01:00
#ifndef FLOWEE_NETWORKSERVICE_H
#define FLOWEE_NETWORKSERVICE_H
2018-02-15 14:47:17 +01:00
2019-04-09 17:24:38 +02:00
#include "NetworkConnection.h"
#include "NetworkServiceBase.h"
#include <streaming/BufferPool.h>
2019-05-18 15:56:34 +02:00
#include <deque>
#include <mutex>
class RemoteContainer;
2019-04-09 17:24:38 +02:00
/**
* Implement a handler of service messages.
*
* In the NetworkManager system messages are optionally routed to handlers
* using a service ID. Handling messages for a specific service, as they come
2022-01-21 19:12:31 +01:00
* from the network can be done by inheriting from the NetworkService class
2019-08-24 13:17:30 +02:00
* and reimplementing the onIncomingMessage() call.
2019-04-09 17:24:38 +02:00
*
* Please note that this class adds to the very basic NetworkServiceBase class
* a safe way to reply to your incoming messages.
*/
class NetworkService : public NetworkServiceBase
2018-02-15 14:47:17 +01:00
{
public:
~NetworkService();
2023-07-15 20:01:26 +02:00
void onIncomingMessage(const Message &message, const EndPoint &ep) final override;
2018-02-15 14:47:17 +01:00
2019-04-09 17:24:38 +02:00
class Remote {
public:
Remote() {}
virtual ~Remote();
NetworkConnection connection;
};
/**
* To implement a common subscription method where the only
* thing a remote tells us if they are enabled or disabled, we provide this
* standard pattern of finding remotes.
*
* To use this in a subclass you provide as your createRemote() method code like this:
* @code
Remote *createRemote() override {
return new RemoteSubscriptionInfo();
}
* @endcode
2023-07-15 20:01:26 +02:00
* In any method that is interested in remotes in order to send messages to, you can simply
* write:
* @code
const auto list = remotes<RemoteWithBool>(&NetworkService::filterRemoteWithBool);
if (list.empty())
return;
* @endcode
*/
class RemoteWithBool : public Remote {
public:
bool enabled = false;
};
/// Helper method to pass in to remotes<RemoteWithBool*>()
/// This method only returns items in the list that are the right type and have the 'enabled' bool set to true
static RemoteWithBool* filterRemoteWithBool(Remote *r);
2019-04-09 17:24:38 +02:00
/// pure virtual handler method that attaches a remote
virtual void onIncomingMessage(Remote *con, const Message &message, const EndPoint &ep) = 0;
2018-02-15 14:47:17 +01:00
protected:
2019-04-09 17:24:38 +02:00
/// constructor
NetworkService(int serviceId);
2018-02-15 14:47:17 +01:00
2023-07-15 20:01:26 +02:00
/// factory method, you can return a subclass of Remote with your own data in it
2019-04-09 17:24:38 +02:00
virtual Remote *createRemote();
2019-05-18 15:56:34 +02:00
std::deque<Remote*> remotes() const;
2019-04-09 17:24:38 +02:00
2023-08-02 11:54:05 +02:00
/**
* Filter remotes on a condition.
* The simplest usage is using the existing filter:
* @code
* auto list = remotes<MyRemoteType>(&NetworkService::filterRemoteWithBool);
* @endcode
*
* A more fun example is using a lambda
* @code
uint256 id = parser.uint256Data();
for (auto *remote : remotes<MyRemote>([id](Remote *r) -> MyRemote* {
auto remote = static_cast<MyRemote*>(r);
if (remote->filter.contains(id))
return remote;
return nullptr;
})) {
// something that uses connection, for instance
// remote->connection->send();
}
* @endcode
*/
template <class T>
std::deque<T*> remotes(const std::function<T*(Remote*)> &filter) const {
std::deque<T*> answer;
for (const auto r : remotes()) {
T* r_ = filter(r);
if (r_)
answer.push_back(r_);
}
return answer;
}
2019-04-09 17:24:38 +02:00
private:
2019-05-18 15:56:34 +02:00
void addRemote(Remote *remote);
void removeRemote(Remote *remote);
mutable std::atomic<RemoteContainer*> m_remotes;
2019-04-09 17:24:38 +02:00
void onDisconnected(const EndPoint &endPoint);
2018-02-15 14:47:17 +01:00
};
#endif