mirror of
https://github.com/RetroShare/libretroshare.git
synced 2026-09-12 19:50:03 +05:00
341 lines
12 KiB
C++
341 lines
12 KiB
C++
/*******************************************************************************
|
|
* Retroshare events service *
|
|
* *
|
|
* libretroshare: retroshare core library *
|
|
* *
|
|
* Copyright (C) 2019-2020 Retroshare Team <contact@retroshare.cc> *
|
|
* Copyright (C) 2019-2020 Gioacchino Mazzurco <gio@retroshare.cc> *
|
|
* *
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. *
|
|
* *
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
* *
|
|
*******************************************************************************/
|
|
#pragma once
|
|
|
|
#include <memory>
|
|
#include <cstdint>
|
|
#include <chrono>
|
|
#include <functional>
|
|
|
|
#include "retroshare/rstypes.h"
|
|
#include "util/rsmemory.h"
|
|
#include "util/rsurl.h"
|
|
#include "serialiser/rsserializable.h"
|
|
#include "serialiser/rstypeserializer.h"
|
|
#include "util/rstime.h"
|
|
#include "util/rsdebug.h"
|
|
|
|
class RsEvents;
|
|
|
|
/**
|
|
* Pointer to global instance of RsEvents service implementation
|
|
* @jsonapi{development}
|
|
*
|
|
* TODO: this should become std::weak_ptr once we have a reasonable services
|
|
* management.
|
|
*/
|
|
extern RsEvents* rsEvents;
|
|
|
|
/**
|
|
* @brief Events types.
|
|
* When creating a new type of event, add a new type here and use that to
|
|
* initialize mType in the constructor of your derivative of @see RsEvent
|
|
*/
|
|
enum class RsEventType : uint32_t
|
|
{
|
|
__NONE = 0, /// Used internally to detect invalid event type passed
|
|
|
|
/// @see RsBroadcastDiscovery
|
|
BROADCAST_DISCOVERY = 1,
|
|
|
|
/// @see RsDiscPendingPgpReceivedEvent
|
|
GOSSIP_DISCOVERY = 2,
|
|
|
|
/// @see AuthSSL
|
|
AUTHSSL_CONNECTION_AUTENTICATION = 3,
|
|
|
|
/// @see pqissl
|
|
FRIEND_LIST = 4, // former PEER_STATUS
|
|
|
|
/// @see RsGxsChanges, used also in @see RsGxsBroadcast
|
|
GXS_CHANGES = 5,
|
|
|
|
/// Emitted when a peer state changes, @see RsPeers
|
|
_________UNUSED___001_ = 6, // former PEER_STATE_CHANGED
|
|
|
|
/// @see RsMailStatusEvent
|
|
MAIL_STATUS = 7,
|
|
|
|
/// @see RsGxsCircleEvent
|
|
GXS_CIRCLES = 8,
|
|
|
|
/// @see RsGxsChannelEvent
|
|
GXS_CHANNELS = 9,
|
|
|
|
/// @see RsGxsForumEvent
|
|
GXS_FORUMS = 10,
|
|
|
|
/// @see RsGxsPostedEvent
|
|
GXS_POSTED = 11,
|
|
|
|
/// @see RsGxsPostedEvent
|
|
GXS_IDENTITY = 12,
|
|
|
|
/// @see RsFiles @deprecated
|
|
SHARED_DIRECTORIES = 13,
|
|
|
|
/// @see RsFiles
|
|
FILE_TRANSFER = 14,
|
|
|
|
/// @see RsChats
|
|
CHAT_SERVICE = 15,
|
|
|
|
/// @see rspeers.h
|
|
NETWORK = 16,
|
|
|
|
/// @see RsMailTagEvent
|
|
MAIL_TAG = 17,
|
|
|
|
/// @see RsJsonApiEvent
|
|
JSON_API = 18,
|
|
|
|
/** Emitted to update library clients about file hashing being completed */
|
|
_________UNUSED___002_ = 20, // former FILE_HASING_COMPLETE
|
|
|
|
/// @see rspeers.h
|
|
TOR_MANAGER = 21,
|
|
|
|
/// @see rsfriendserver.h
|
|
FRIEND_SERVER = 22,
|
|
|
|
/// @see RsWireEvent
|
|
WIRE = 23,
|
|
|
|
/// @see RsWireEvent
|
|
SYSTEM = 24, // general system notifications
|
|
|
|
__MAX = 25 // Used internally, keep last.
|
|
};
|
|
|
|
enum class RsEventsErrorNum : int32_t
|
|
{
|
|
EVENT_TYPE_UNDEFINED = 3004,
|
|
EVENT_TYPE_OUT_OF_RANGE = 3005,
|
|
INVALID_HANDLER_ID = 3006,
|
|
NULL_EVENT_POINTER = 3007
|
|
};
|
|
|
|
struct RsEventsErrorCategory: std::error_category
|
|
{
|
|
const char* name() const noexcept override
|
|
{ return "RetroShare Events"; }
|
|
|
|
std::string message(int ev) const override
|
|
{
|
|
switch (static_cast<RsEventsErrorNum>(ev))
|
|
{
|
|
case RsEventsErrorNum::EVENT_TYPE_UNDEFINED:
|
|
return "Undefined event type";
|
|
case RsEventsErrorNum::EVENT_TYPE_OUT_OF_RANGE:
|
|
return "Event type out of range";
|
|
case RsEventsErrorNum::INVALID_HANDLER_ID:
|
|
return "Invalid handler id";
|
|
default:
|
|
return rsErrorNotInCategory(ev, name());
|
|
}
|
|
}
|
|
|
|
std::error_condition default_error_condition(int ev) const noexcept override;
|
|
|
|
const static RsEventsErrorCategory instance;
|
|
};
|
|
|
|
|
|
namespace std
|
|
{
|
|
/** Register RsJsonApiErrorNum as an error condition enum, must be in std
|
|
* namespace */
|
|
template<> struct is_error_condition_enum<RsEventsErrorNum> : true_type {};
|
|
}
|
|
|
|
/** Provide RsEventsErrorNum conversion to std::error_condition, must be in
|
|
* same namespace of RsJsonApiErrorNum */
|
|
inline std::error_condition make_error_condition(RsEventsErrorNum e) noexcept
|
|
{
|
|
return std::error_condition(
|
|
static_cast<int>(e), RsEventsErrorCategory::instance );
|
|
};
|
|
|
|
|
|
/**
|
|
* This struct is not meant to be used directly, you should create events type
|
|
* deriving from it.
|
|
*/
|
|
struct RsEvent : RsSerializable
|
|
{
|
|
protected:
|
|
explicit RsEvent(RsEventType type) :
|
|
mType(type), mTimePoint(std::chrono::system_clock::now()) {}
|
|
|
|
RsEvent() = delete;
|
|
|
|
public:
|
|
RsEventType mType;
|
|
std::chrono::system_clock::time_point mTimePoint;
|
|
|
|
/**
|
|
* Derived types must call this method at beginning of their implementation
|
|
* of serial_process
|
|
* @see RsSerializable
|
|
*/
|
|
void serial_process( RsGenericSerializer::SerializeJob j,
|
|
RsGenericSerializer::SerializeContext& ctx) override
|
|
{
|
|
RS_SERIAL_PROCESS(mType);
|
|
|
|
rstime_t mTime = std::chrono::system_clock::to_time_t(mTimePoint);
|
|
RS_SERIAL_PROCESS(mTime);
|
|
mTimePoint = std::chrono::system_clock::from_time_t(mTime);
|
|
}
|
|
|
|
~RsEvent() override;
|
|
};
|
|
|
|
typedef uint32_t RsEventsHandlerId_t;
|
|
|
|
class RsEvents
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Post event to the event queue.
|
|
* @param[in] event
|
|
* @return Success or error details.
|
|
*/
|
|
virtual std::error_condition postEvent( std::shared_ptr<const RsEvent> event ) = 0;
|
|
|
|
/**
|
|
* @brief Send event directly to handlers. Blocking API
|
|
* The handlers get exectuded on the caller thread.
|
|
* @param[in] event
|
|
* @return Success or error details.
|
|
*/
|
|
virtual std::error_condition sendEvent( std::shared_ptr<const RsEvent> event ) = 0;
|
|
|
|
/**
|
|
* @brief Generate unique handler identifier
|
|
* @return generate Id
|
|
*/
|
|
virtual RsEventsHandlerId_t generateUniqueHandlerId() = 0;
|
|
|
|
/**
|
|
* @brief getDynamicEventType
|
|
* This function can be used to generate event types on the fly when not already defined in rseventids.h. This is
|
|
* for instance useful when plugins need to generate their own event type. Simply calling
|
|
* getDynamicEventType("SOME_STRING_SPECIFIC_TO_THE_PLUGIN") will return the given event type, possibly generating it
|
|
* on the fly if needed.
|
|
* Other event handlers that use EventType values that are already in rseventids.h do not need this function.
|
|
* The result is only valid for the current RS session and may change after restart.
|
|
*
|
|
* @param unique_service_identifier simple string that is unique to the plugin.
|
|
*
|
|
* @return returns the event type associated to the string identifier.
|
|
*
|
|
*/
|
|
virtual RsEventType getDynamicEventType(const std::string& unique_service_identifier) =0;
|
|
|
|
/**
|
|
* @brief Register events handler
|
|
* Every time an event is dispatced the registered events handlers will get
|
|
* their method handleEvent called with the event passed as paramether.
|
|
* @attention Callbacks must not fiddle internally with methods of this
|
|
* class otherwise a deadlock will happen.
|
|
* @jsonapi{development,manualwrapper}
|
|
* @param multiCallback Function that will be called each time an event
|
|
* is dispatched.
|
|
* @param[inout] hId Optional storage for handler id, useful to
|
|
* eventually unregister the handler later. The
|
|
* value may be provided to the function call but
|
|
* must habe been generated with
|
|
* @see generateUniqueHandlerId()
|
|
* @param[in] eventType Optional type of event for which the callback is
|
|
* called, if __NONE is passed multiCallback is
|
|
* called for every events without filtering.
|
|
* @return Success or error details.
|
|
*/
|
|
virtual std::error_condition registerEventsHandler(
|
|
std::function<void(std::shared_ptr<const RsEvent>)> multiCallback,
|
|
RsEventsHandlerId_t& hId = RS_DEFAULT_STORAGE_PARAM(RsEventsHandlerId_t, 0),
|
|
RsEventType eventType = RsEventType::__NONE ) = 0;
|
|
|
|
/**
|
|
* @brief Unregister event handler
|
|
* @param[in] hId Id of the event handler to unregister
|
|
* @return Success or error details.
|
|
*/
|
|
virtual std::error_condition unregisterEventsHandler(
|
|
RsEventsHandlerId_t hId ) = 0;
|
|
|
|
virtual ~RsEvents();
|
|
};
|
|
|
|
enum class RsSystemEventCode: uint8_t
|
|
{
|
|
UNKNOWN = 0x00, // Computer universal time is different from friends
|
|
TIME_SHIFT_PROBLEM = 0x01, // Computer universal time is different from friends
|
|
DISK_SPACE_ERROR = 0x02, // Disk full
|
|
GENERAL_ERROR = 0x03, // general error, see string.
|
|
DATA_STREAMING_ERROR = 0x04, // streaming error, mostly from the pqistreamer components
|
|
PASSWORD_REQUESTED = 0x05, // ask the user for a passwd. This may be sent using the synchroneous sendEvent() if needed
|
|
NEW_PLUGIN_FOUND = 0x06, // ask the user to validate a new plugin. This may be sent synchroneously is needed.
|
|
};
|
|
|
|
struct RsSystemEvent : RsEvent
|
|
{
|
|
RsSystemEvent() : RsEvent(RsEventType::SYSTEM) {}
|
|
virtual ~RsSystemEvent() override = default;
|
|
|
|
RsSystemEventCode mEventCode;
|
|
|
|
float mTimeShift;
|
|
int mDiskErrorLocation; // { RS_PARTIALS_DIRECTORY, RS_CONFIG_DIRECTORY, RS_DOWNLOAD_DIRECTORY }
|
|
int mDiskErrorSizeLimit;// size limit in MB
|
|
std::string mErrorMsg; // generic error message info string. Can be anything.
|
|
|
|
std::string passwd_request_title;
|
|
std::string passwd_request_key_details;
|
|
bool passwd_request_prev_is_bad;
|
|
|
|
std::string plugin_file_name;
|
|
RsFileHash plugin_file_hash;
|
|
bool plugin_first_time;
|
|
|
|
///* @see RsEvent @see RsSerializable
|
|
void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) override
|
|
{
|
|
RsEvent::serial_process(j, ctx);
|
|
RS_SERIAL_PROCESS(mEventCode);
|
|
RS_SERIAL_PROCESS(mDiskErrorLocation);
|
|
RS_SERIAL_PROCESS(mDiskErrorSizeLimit);
|
|
RS_SERIAL_PROCESS(mErrorMsg);
|
|
|
|
RS_SERIAL_PROCESS(passwd_request_title);
|
|
RS_SERIAL_PROCESS(passwd_request_key_details);
|
|
RS_SERIAL_PROCESS(passwd_request_prev_is_bad);
|
|
|
|
RS_SERIAL_PROCESS(plugin_file_name);
|
|
RS_SERIAL_PROCESS(plugin_file_hash);
|
|
RS_SERIAL_PROCESS(plugin_first_time);
|
|
}
|
|
};
|
|
|