Line data Source code
1 : /** 2 : Copyright (c) 2024 Stappler LLC <admin@stappler.dev> 3 : 4 : Permission is hereby granted, free of charge, to any person obtaining a copy 5 : of this software and associated documentation files (the "Software"), to deal 6 : in the Software without restriction, including without limitation the rights 7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 : copies of the Software, and to permit persons to whom the Software is 9 : furnished to do so, subject to the following conditions: 10 : 11 : The above copyright notice and this permission notice shall be included in 12 : all copies or substantial portions of the Software. 13 : 14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 : THE SOFTWARE. 21 : **/ 22 : 23 : #ifndef EXTRA_WEBSERVER_WEBSERVER_SERVER_SPWEBHOSTCOMPONENT_H_ 24 : #define EXTRA_WEBSERVER_WEBSERVER_SERVER_SPWEBHOSTCOMPONENT_H_ 25 : 26 : #include "SPWebHost.h" 27 : 28 : namespace STAPPLER_VERSIONIZED stappler::web { 29 : 30 : class HostComponent : public AllocBase { 31 : public: 32 : using Symbol = HostComponent * (*) (const Host &serv, const HostComponentInfo &dict); 33 : using Scheme = db::Scheme; 34 : 35 : using CommandCallback = Function<bool(StringView, const Callback<void(const Value &)> &)>; 36 : 37 : struct Loader { 38 : StringView name; 39 : Symbol loader; 40 : 41 : Loader(const StringView &, Symbol); 42 : }; 43 : 44 : struct Command { 45 : String name; 46 : String desc; 47 : String help; 48 : CommandCallback callback; 49 : }; 50 : 51 : HostComponent(const Host &serv, const HostComponentInfo &); 52 0 : virtual ~HostComponent() { } 53 : 54 : virtual void handleChildInit(const Host &); 55 : virtual void handleStorageInit(const Host &, const db::Adapter &); 56 : virtual void initTransaction(db::Transaction &); 57 : virtual void handleHeartbeat(const Host &); 58 : 59 : const Value & getConfig() const { return _config; } 60 375 : StringView getName() const { return _name; } 61 300 : StringView getVersion() const { return _version; } 62 : 63 : const db::Scheme * exportScheme(const db::Scheme &); 64 : 65 : void addCommand(const StringView &name, Function<Value(const StringView &)> &&, 66 : const StringView &desc = StringView(), const StringView &help = StringView()); 67 : void addOutputCommand(const StringView &name, CommandCallback &&, 68 : const StringView &desc = StringView(), const StringView &help = StringView()); 69 : const Command *getCommand(const StringView &name) const; 70 : 71 : const Map<String, Command> &getCommands() const; 72 : 73 : template <typename Value> 74 200 : void exportValues(Value &&val) { 75 200 : exportScheme(val); 76 200 : } 77 : 78 : template <typename Value, typename ... Args> 79 175 : void exportValues(Value &&val, Args && ... args) { 80 175 : exportValues(std::forward<Value>(val)); 81 175 : exportValues(std::forward<Args>(args)...); 82 175 : } 83 : 84 : Host getHost() const { return _host; } 85 : 86 : protected: 87 : Map<String, Command> _commands; 88 : 89 : Host _host; 90 : String _name; 91 : String _version; 92 : Value _config; 93 : }; 94 : 95 : } 96 : 97 : #endif /* EXTRA_WEBSERVER_WEBSERVER_SERVER_SPWEBHOSTCOMPONENT_H_ */