2016-08-16 16:51:22 +02:00
|
|
|
/*
|
2017-11-13 23:09:33 +01:00
|
|
|
* This file is part of the Flowee project
|
2019-03-17 22:44:21 +01:00
|
|
|
* Copyright (C) 2016-2017, 2019 Tom Zander <tomz@freedommail.ch>
|
2016-08-16 16:51:22 +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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-02-15 19:50:12 +01:00
|
|
|
#ifndef APIRPCBINDING_H
|
|
|
|
|
#define APIRPCBINDING_H
|
2016-08-16 16:51:22 +02:00
|
|
|
|
2020-09-29 17:06:57 +02:00
|
|
|
#include <networkmanager/NetworkConnection.h>
|
2020-10-26 15:11:28 +01:00
|
|
|
#include <Message.h>
|
2019-03-09 16:36:13 +01:00
|
|
|
|
2016-08-16 16:51:22 +02:00
|
|
|
#include <string>
|
2020-10-26 15:11:28 +01:00
|
|
|
#include <thread>
|
2020-10-26 22:38:28 +01:00
|
|
|
#include <mutex>
|
2016-08-16 16:51:22 +02:00
|
|
|
|
|
|
|
|
namespace Streaming {
|
|
|
|
|
class MessageBuilder;
|
|
|
|
|
}
|
|
|
|
|
class UniValue;
|
|
|
|
|
class Message;
|
|
|
|
|
|
2019-03-07 18:38:03 +01:00
|
|
|
namespace Api
|
2016-08-16 16:51:22 +02:00
|
|
|
{
|
2020-09-29 17:06:57 +02:00
|
|
|
class Server;
|
|
|
|
|
class SessionData
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
SessionData() {}
|
|
|
|
|
virtual ~SessionData();
|
|
|
|
|
};
|
|
|
|
|
|
2019-03-09 16:36:13 +01:00
|
|
|
class ParserException : public std::runtime_error {
|
|
|
|
|
public:
|
|
|
|
|
ParserException(const char *errorMessage) : std::runtime_error(errorMessage) {}
|
|
|
|
|
};
|
|
|
|
|
|
2017-10-06 19:12:30 +02:00
|
|
|
/**
|
2017-10-06 20:18:09 +02:00
|
|
|
* This class, and its subclasses RpcParser / DirectParser are the baseclasses for specific commands.
|
|
|
|
|
*
|
2018-02-15 19:50:12 +01:00
|
|
|
* In the API we have specific incoming messages which map to a Parser implementation.
|
2017-10-06 19:12:30 +02:00
|
|
|
* When a new request comes in from the network, the specific parser that can handle this
|
2018-02-15 19:50:12 +01:00
|
|
|
* is instantiated and then based on the api server finding either a RpcParser or a DirectParser
|
2017-10-06 20:18:09 +02:00
|
|
|
* its virtual methods will be called.
|
2017-10-06 19:12:30 +02:00
|
|
|
*/
|
2016-08-16 16:51:22 +02:00
|
|
|
class Parser {
|
2017-10-06 20:18:09 +02:00
|
|
|
public:
|
|
|
|
|
enum ParserType {
|
|
|
|
|
WrapsRPCCall,
|
2020-09-29 17:06:57 +02:00
|
|
|
IncludesHandler,
|
|
|
|
|
ASyncParser
|
2017-10-06 20:18:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Parser(ParserType type, int replyMessageId, int messageSize = -1);
|
|
|
|
|
virtual ~Parser() {}
|
|
|
|
|
|
|
|
|
|
inline ParserType type() const {
|
|
|
|
|
return m_type;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-15 19:50:12 +01:00
|
|
|
/// Returns the message-id we set in the answer message. Typically an enum from the APIProtocol.h
|
2017-10-06 20:18:09 +02:00
|
|
|
inline int replyMessageId() const {
|
|
|
|
|
return m_replyMessageId;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-09 16:36:13 +01:00
|
|
|
void setSessionData(SessionData **value);
|
|
|
|
|
|
2017-10-06 20:18:09 +02:00
|
|
|
protected:
|
|
|
|
|
int m_messageSize;
|
|
|
|
|
int m_replyMessageId;
|
|
|
|
|
ParserType m_type;
|
2019-03-09 16:36:13 +01:00
|
|
|
SessionData **data;
|
2017-10-06 20:18:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2019-03-07 18:38:03 +01:00
|
|
|
* A parser that binds to an existing (legacy) RPC call to execute the API call.
|
2017-10-06 20:18:09 +02:00
|
|
|
* When a new request comes in from the network, the specific parser that can handle this
|
|
|
|
|
* is instantiated and then createReqeust() is called with the message we received from the network,
|
|
|
|
|
* followed by a call to buildReply() which is meant to build the answer.
|
|
|
|
|
*/
|
|
|
|
|
class RpcParser : public Parser {
|
2016-08-16 16:51:22 +02:00
|
|
|
public:
|
2017-10-06 19:12:30 +02:00
|
|
|
/**
|
|
|
|
|
* Constructor, meant for overloading. Subclasses should pass in the parameters.
|
|
|
|
|
* @param method this parameter is the name of the RPC method we map to.
|
|
|
|
|
* @param replyMessageId the enum value of the answer message that the network client expects.
|
|
|
|
|
* @param messageSize when passed in, this will be the hardcoded size and calculateMessageSize() will not be called.
|
|
|
|
|
*/
|
2017-10-06 20:18:09 +02:00
|
|
|
RpcParser(const std::string &method, int replyMessageId, int messageSize = -1);
|
2016-08-16 16:51:22 +02:00
|
|
|
|
2017-10-06 19:12:30 +02:00
|
|
|
/**
|
|
|
|
|
* @brief messageSize returns the amount of bytes we should reserve for the reply.
|
|
|
|
|
* @param result is the result we received from the RPC layer.
|
|
|
|
|
* @return either the messageSize passed into the constructor, or the output of calculateMessageSize()
|
|
|
|
|
*/
|
2016-08-16 16:51:22 +02:00
|
|
|
inline int messageSize(const UniValue &result) const {
|
2017-10-06 19:12:30 +02:00
|
|
|
if (m_messageSize > 0)
|
|
|
|
|
return m_messageSize;
|
|
|
|
|
return calculateMessageSize(result);
|
2016-08-16 16:51:22 +02:00
|
|
|
}
|
2017-10-06 19:12:30 +02:00
|
|
|
/// Returns the name of the RPC method we map
|
2016-08-16 16:51:22 +02:00
|
|
|
inline const std::string &method() const {
|
|
|
|
|
return m_method;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-06 19:12:30 +02:00
|
|
|
/**
|
|
|
|
|
* @brief createRequest takes the incoming \a message and creates a univalue request to pass to the RPC layer.
|
|
|
|
|
* @param message the incoming message.
|
|
|
|
|
* @param output the data that the RPC method() we map to will understand.
|
|
|
|
|
*/
|
2016-08-16 16:51:22 +02:00
|
|
|
virtual void createRequest(const Message &message, UniValue &output);
|
2017-10-06 19:12:30 +02:00
|
|
|
/**
|
|
|
|
|
* @brief The buildReply method takes the results from the RPC method we map to and builds the reply to be sent over the network.
|
|
|
|
|
*/
|
|
|
|
|
virtual void buildReply(Streaming::MessageBuilder &builder, const UniValue &result);
|
|
|
|
|
/// Return the size we shall reserve for the message to be created in buildReply.
|
|
|
|
|
/// This size CAN NOT be smaller than what is actually consumed in buildReply.
|
|
|
|
|
virtual int calculateMessageSize(const UniValue&) const;
|
2016-08-16 16:51:22 +02:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
std::string m_method;
|
|
|
|
|
};
|
|
|
|
|
|
2017-10-06 20:18:09 +02:00
|
|
|
/**
|
2019-03-07 18:38:03 +01:00
|
|
|
* A parser that calls directly to the libraries to execute the API call.
|
2017-10-06 20:18:09 +02:00
|
|
|
* When a new request comes in from the network, the specific parser that can handle this
|
|
|
|
|
* is instantiated and then buildReply() is called with the message we received from the network,
|
|
|
|
|
* expecting a reply to be created to be send back to the caller.
|
|
|
|
|
*/
|
|
|
|
|
class DirectParser : public Parser {
|
|
|
|
|
public:
|
|
|
|
|
DirectParser(int replyMessageId, int messageSize = -1);
|
|
|
|
|
|
|
|
|
|
/// Return the size we shall reserve for the message to be created in buildReply.
|
|
|
|
|
/// This size CAN NOT be smaller than what is actually consumed in buildReply.
|
2019-03-08 17:58:19 +01:00
|
|
|
virtual int calculateMessageSize(const Message &request) { return m_messageSize; }
|
2017-10-06 20:18:09 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief The buildReply method takes the request and builds the reply to be sent over the network.
|
|
|
|
|
*/
|
|
|
|
|
virtual void buildReply(const Message &request, Streaming::MessageBuilder &builder) = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-26 15:11:28 +01:00
|
|
|
/**
|
|
|
|
|
* This is a parser that creates a thread, calls its methods and then shuts down.
|
|
|
|
|
* Inherting classes will want to reimplement run() and buildReply(), and all
|
|
|
|
|
* other details are taken care off.
|
|
|
|
|
*
|
|
|
|
|
* We use the fact that modern Linux can create many thousands of threads a second
|
|
|
|
|
* without issues.
|
|
|
|
|
*/
|
|
|
|
|
class ASyncParser : public Parser {
|
2020-09-29 17:06:57 +02:00
|
|
|
public:
|
|
|
|
|
ASyncParser(const Message &request, int replyMessageId, int messageSize = -1);
|
|
|
|
|
|
2020-10-26 15:11:28 +01:00
|
|
|
/// Called by the ApiServer.
|
|
|
|
|
/// \param token a boolean that we are expected to set to false as this parser completes.
|
|
|
|
|
/// \param connection this is the network connection we operate on.
|
|
|
|
|
/// \param server is the 'parent' environment we operate in.
|
|
|
|
|
void start(std::atomic_bool *token, NetworkConnection && connection, Server *server);
|
2020-09-29 17:06:57 +02:00
|
|
|
|
2020-10-26 15:11:28 +01:00
|
|
|
/// Create the returning message, called if error() is empty.
|
|
|
|
|
/// Is allowed to throw Api::ParseException
|
2020-09-29 17:06:57 +02:00
|
|
|
virtual void buildReply(Streaming::MessageBuilder &builder) = 0;
|
|
|
|
|
|
|
|
|
|
protected:
|
2020-10-26 15:11:28 +01:00
|
|
|
/// This is where you reimplement the handling of m_request, you are expected
|
|
|
|
|
/// to make sure that m_messageSize is properly set as you exit
|
|
|
|
|
/// Is allowed to throw Api::ParseException
|
2020-09-29 17:06:57 +02:00
|
|
|
virtual void run() = 0;
|
|
|
|
|
|
2020-10-26 15:11:28 +01:00
|
|
|
const Message m_request;
|
2020-09-29 17:06:57 +02:00
|
|
|
private:
|
|
|
|
|
void run_priv();
|
2020-10-26 15:11:28 +01:00
|
|
|
// called in the application-wide threadpool to join the private thread and delete me.
|
|
|
|
|
void deleteMe();
|
2020-09-29 17:06:57 +02:00
|
|
|
|
2020-10-26 15:11:28 +01:00
|
|
|
// Use this to have a mem-sync barrier for all standard members.
|
|
|
|
|
std::mutex m_lock;
|
2020-09-29 17:06:57 +02:00
|
|
|
NetworkConnection m_con;
|
2020-10-26 15:11:28 +01:00
|
|
|
std::atomic_bool *m_token = nullptr; // Used to tell the caller we are finished.
|
|
|
|
|
const Server* m_server = nullptr;
|
|
|
|
|
std::thread m_thread;
|
2020-09-29 17:06:57 +02:00
|
|
|
};
|
|
|
|
|
|
2017-10-06 19:12:30 +02:00
|
|
|
/// maps an input message to a Parser implementation.
|
2016-08-16 16:51:22 +02:00
|
|
|
Parser* createParser(const Message &message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|