diff options
author | Peter Powell <petpow@saberuk.com> | 2019-05-13 17:24:25 +0100 |
---|---|---|
committer | Peter Powell <petpow@saberuk.com> | 2019-10-14 11:03:03 +0100 |
commit | d0f802e30c492cb1b7e55f51063bfd38b29931c6 (patch) | |
tree | a11cbb5c48fae151d285e0536d4f51b2b09659da /include | |
parent | 51b9b4c9b404bd801be194644133be47bd035b58 (diff) |
Implement serialisation of users.
This allows for various things which will be coming in the future.
e.g. Transferring users to another server on upgrade.
Diffstat (limited to 'include')
-rw-r--r-- | include/extensible.h | 10 | ||||
-rw-r--r-- | include/inspircd.h | 1 | ||||
-rw-r--r-- | include/serialize.h | 117 | ||||
-rw-r--r-- | include/users.h | 14 |
4 files changed, 141 insertions, 1 deletions
diff --git a/include/extensible.h b/include/extensible.h index c24984f26..f3eeabdcf 100644 --- a/include/extensible.h +++ b/include/extensible.h @@ -133,7 +133,9 @@ class CoreExport ExtensionItem : public ServiceProvider, public usecountbase * a flags variable, and each module defining bits within the flag as 'theirs' as it is less prone to conflict and * supports arbitary data storage). */ -class CoreExport Extensible : public classbase +class CoreExport Extensible + : public classbase + , public Serializable { public: typedef insp::flat_map<reference<ExtensionItem>, void*> ExtensibleStore; @@ -165,6 +167,12 @@ class CoreExport Extensible : public classbase * Free all extension items attached to this Extensible */ void FreeAllExtItems(); + + /** @copydoc Serializable::Deserialize. */ + bool Deserialize(Data& data) CXX11_OVERRIDE; + + /** @copydoc Serializable::Deserialize. */ + bool Serialize(Serializable::Data& data) CXX11_OVERRIDE; }; class CoreExport ExtensionManager diff --git a/include/inspircd.h b/include/inspircd.h index 35183c3bf..775c201f9 100644 --- a/include/inspircd.h +++ b/include/inspircd.h @@ -74,6 +74,7 @@ struct fakederef #include "dynref.h" #include "consolecolors.h" #include "cull_list.h" +#include "serialize.h" #include "extensible.h" #include "fileutils.h" #include "ctables.h" diff --git a/include/serialize.h b/include/serialize.h new file mode 100644 index 000000000..b3783a48f --- /dev/null +++ b/include/serialize.h @@ -0,0 +1,117 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2019 Peter Powell <petpow@saberuk.com> + * + * This file is part of InspIRCd. InspIRCd 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, version 2. + * + * 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/>. + */ + + +#pragma once + +/** Base class for serializable elements. */ +class CoreExport Serializable +{ + protected: + Serializable() { } + + public: + /** Encapsulates a chunk of serialised data. */ + class CoreExport Data + { + public: + /** Maps keys to serialised data. */ + typedef TR1NS::unordered_map<std::string, Data> ChildMap; + + /** Maps keys to simple values. */ + typedef TR1NS::unordered_map<std::string, std::string> EntryMap; + + private: + /** A mapping of keys to serialised data. */ + ChildMap children; + + /** A mapping of keys to values. */ + EntryMap entries; + + public: + /** Retrieves the child elements. */ + const ChildMap& GetChildren() const { return children; } + ChildMap& GetChildren() { return children; } + + /** Retrieves the key/value map. */ + const EntryMap& GetEntries() const { return entries; } + EntryMap& GetEntries() { return entries; } + + /** Loads the serialised data with the specified key. + * @param key The key by which this serialised data is identified. + * @param out The location to store the serialised data for this key. + */ + Data& Load(const std::string& key, Data& out); + + /** Loads the value with the specified key. + * @param key The key by which this data is identified. + * @param out The location to store the value for this keu + */ + Data& Load(const std::string& key, std::string& out); + + /** Loads the value with the specified key. The value will be converted to the specified type. + * @param key The key by which this data is identified. + * @param out The location to store the value for this key. + */ + template <typename T> + Data& Load(const std::string& key, T& out) + { + // Attempt to load as a string. + std::string str; + Load(key, str); + + std::stringstream ss(str); + ss >> out; + return *this; + } + + /** Stores the serialised data against the specified key. + * @param key The key by which this serialised data should be stored against. + * @param out The serialised data to store. + */ + Data& Store(const std::string& key, const Data& value); + + /** Stores the value against the specified key. + * @param key The key by which this value should be stored against. + * @param out The value to store. + */ + Data& Store(const std::string& key, const std::string& value); + + /** Stores the value against the specified key. The value will be converted to a string using ConvToStr. + * @param key The key by which this value should be stored against. + * @param out The value to store. + */ + template <typename T> + Data& Store(const std::string& key, const T& value) + { + return Store(key, ConvToStr(value)); + } + }; + + /** Deserializes the specified Data instance into this object. + * @param data The Data object to deserialize from. + * @return True if the deserialisation succeeded; otherwise, false. + */ + virtual bool Deserialize(Data& data) = 0; + + /** Serializes the this object into the specified Data obect. + * @param data The Data object to serialize to. + * @return True if the serialisation succeeded; otherwise, false. + */ + virtual bool Serialize(Data& data) = 0; +}; diff --git a/include/users.h b/include/users.h index e58e8e316..6cc7f423b 100644 --- a/include/users.h +++ b/include/users.h @@ -676,6 +676,12 @@ class CoreExport User : public Extensible */ virtual ~User(); CullResult cull() CXX11_OVERRIDE; + + /** @copydoc Serializable::Deserialize. */ + bool Deserialize(Data& data) CXX11_OVERRIDE; + + /** @copydoc Serializable::Deserialize. */ + bool Serialize(Serializable::Data& data) CXX11_OVERRIDE; }; class CoreExport UserIOHandler : public StreamSocket @@ -729,6 +735,8 @@ class CoreExport LocalUser : public User, public insp::intrusive_list_node<Local public: LocalUser(int fd, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server); + LocalUser(int fd, const std::string& uuid, Serializable::Data& data); + CullResult cull() CXX11_OVERRIDE; UserIOHandler eh; @@ -872,6 +880,12 @@ class CoreExport LocalUser : public User, public insp::intrusive_list_node<Local * @param msg Message to send. */ void Send(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg); + + /** @copydoc Serializable::Deserialize. */ + bool Deserialize(Data& data) CXX11_OVERRIDE; + + /** @copydoc Serializable::Deserialize. */ + bool Serialize(Serializable::Data& data) CXX11_OVERRIDE; }; class RemoteUser : public User |