diff --git a/build b/build index 978b4e8..a5c750f 100644 --- a/build +++ b/build @@ -1 +1 @@ -26 \ No newline at end of file +27 \ No newline at end of file diff --git a/owfms.properties b/owfms.properties index 625561d..0cb21fb 100644 --- a/owfms.properties +++ b/owfms.properties @@ -35,6 +35,7 @@ openwifi.system.uri.private = https://localhost:17004 openwifi.system.uri.public = https://ucentral.dpaas.arilia.com:16004 openwifi.system.commandchannel = /tmp/app.owfms openwifi.system.uri.ui = ucentral-ui.arilia.com + firmwaredb.refresh = 1800 firmwaredb.maxage = 90 diff --git a/src/AutoUpdater.cpp b/src/AutoUpdater.cpp index 3428db3..2ad1f85 100644 --- a/src/AutoUpdater.cpp +++ b/src/AutoUpdater.cpp @@ -11,19 +11,22 @@ namespace OpenWifi { int AutoUpdater::Start() { - Running_ = true; - AutoUpdaterFrequency_ = MicroService::instance().ConfigGetInt("autoupdater.frequency",600); AutoUpdaterEnabled_ = MicroService::instance().ConfigGetBool("autoupdater.enabled", false); - if(AutoUpdaterEnabled_) - Thr_.start(*this); + if(AutoUpdaterEnabled_) { + Running_ = false; + AutoUpdaterFrequency_ = MicroService::instance().ConfigGetInt("autoupdater.frequency",600); + AutoUpdaterCallBack_ = std::make_unique>(*this, &AutoUpdater::onTimer); + Timer_.setStartInterval(5 * 60 * 1000); // first run in 5 minutes + Timer_.setPeriodicInterval(AutoUpdaterFrequency_ * 1000); + Timer_.start(*AutoUpdaterCallBack_); + } return 0; } void AutoUpdater::Stop() { Running_ = false; if(AutoUpdaterEnabled_) { - Thr_.wakeUp(); - Thr_.join(); + Timer_.stop(); } } @@ -34,65 +37,61 @@ namespace OpenWifi { Queue_.emplace_back(std::make_pair(std::move(serialNumber),std::move(DeviceType))); } - void AutoUpdater::run() { - while(Running_) { - Poco::Thread::trySleep(2000); - if(!Running_) - break; - std::unique_lock L(Mutex_); - while(!Queue_.empty() && Running_) { - auto Entry = Queue_.front(); - Queue_.pop_front(); - try { - Logger().debug(Poco::format("Preparing to upgrade %s",Entry.first)); - auto CacheEntry = Cache_.find(Entry.first); - uint64_t Now = std::time(nullptr); - std::string firmwareUpgrade; + void AutoUpdater::onTimer(Poco::Timer & timer) { + Running_ = true; + std::unique_lock L(Mutex_); + while(!Queue_.empty() && Running_) { + auto Entry = Queue_.front(); + Queue_.pop_front(); + try { + Logger().debug(Poco::format("Preparing to upgrade %s",Entry.first)); + auto CacheEntry = Cache_.find(Entry.first); + uint64_t Now = std::time(nullptr); + std::string firmwareUpgrade; + if(CacheEntry == Cache_.end() || (CacheEntry->second.LastCheck-Now)>300) { + // get the firmware settings for that device. + SerialCache C; + C.LastCheck = Now; bool firmwareRCOnly; - if(CacheEntry == Cache_.end() || (CacheEntry->second.LastCheck-Now)>300) { - // get the firmware settings for that device. - SerialCache C; - C.LastCheck = Now; - if(OpenWifi::SDK::Prov::GetFirmwareOptions(Entry.first, firmwareUpgrade, firmwareRCOnly)) { - Logger().debug(Poco::format("Found firmware options for %s",Entry.first)); - C.firmwareRCOnly = firmwareRCOnly; - C.firmwareUpgrade = firmwareUpgrade; - } else { - Logger().debug(Poco::format("Found no firmware options for %s",Entry.first)); - C.firmwareRCOnly = firmwareRCOnly; - C.firmwareUpgrade = firmwareUpgrade; - } - Cache_[Entry.first] = C; + if(OpenWifi::SDK::Prov::GetFirmwareOptions(Entry.first, firmwareUpgrade, firmwareRCOnly)) { + Logger().debug(Poco::format("Found firmware options for %s",Entry.first)); + C.firmwareRCOnly = firmwareRCOnly; + C.firmwareUpgrade = firmwareUpgrade; } else { - + Logger().debug(Poco::format("Found no firmware options for %s",Entry.first)); + C.firmwareRCOnly = firmwareRCOnly; + C.firmwareUpgrade = firmwareUpgrade; } + Cache_[Entry.first] = C; + } else { - if(firmwareUpgrade=="no") { - Logger().information(Poco::format("Device %s not upgradable. Provisioning service settings.",Entry.first)); - continue; - } + } - LatestFirmwareCacheEntry fwEntry; - FMSObjects::Firmware fwDetails; - auto LF = LatestFirmwareCache()->FindLatestFirmware(Entry.second, fwEntry ); - if(LF) { - if(StorageService()->FirmwaresDB().GetFirmware(fwEntry.Id,fwDetails)) { - // send the command to upgrade this device... - Logger().information(Poco::format("Upgrading %s to version %s", Entry.first, fwEntry.Revision)); - if(OpenWifi::SDK::GW::SendFirmwareUpgradeCommand(Entry.first,fwDetails.uri)) { - Logger().information(Poco::format("Upgrade command sent for %s",Entry.first)); - } else { - Logger().information(Poco::format("Upgrade command not sent for %s",Entry.first)); - } + if(firmwareUpgrade=="no") { + Logger().information(Poco::format("Device %s not upgradable. Provisioning service settings.",Entry.first)); + continue; + } + + LatestFirmwareCacheEntry fwEntry; + FMSObjects::Firmware fwDetails; + auto LF = LatestFirmwareCache()->FindLatestFirmware(Entry.second, fwEntry ); + if(LF) { + if(StorageService()->FirmwaresDB().GetFirmware(fwEntry.Id,fwDetails)) { + // send the command to upgrade this device... + Logger().information(Poco::format("Upgrading %s to version %s", Entry.first, fwEntry.Revision)); + if(OpenWifi::SDK::GW::SendFirmwareUpgradeCommand(Entry.first,fwDetails.uri)) { + Logger().information(Poco::format("Upgrade command sent for %s",Entry.first)); } else { - Logger().information(Poco::format("Firmware for device %s (%s) cannot be found.", Entry.first, Entry.second )); + Logger().information(Poco::format("Upgrade command not sent for %s",Entry.first)); } } else { Logger().information(Poco::format("Firmware for device %s (%s) cannot be found.", Entry.first, Entry.second )); } - } catch (...) { - Logger().information(Poco::format("Exception during auto update for device %s.", Entry.first )); + } else { + Logger().information(Poco::format("Firmware for device %s (%s) cannot be found.", Entry.first, Entry.second )); } + } catch (...) { + Logger().information(Poco::format("Exception during auto update for device %s.", Entry.first )); } } } diff --git a/src/AutoUpdater.h b/src/AutoUpdater.h index 94d6502..7c8ac8d 100644 --- a/src/AutoUpdater.h +++ b/src/AutoUpdater.h @@ -8,9 +8,11 @@ #include "framework/MicroService.h" #include #include "Poco/Util/Application.h" +#include "Poco/Timer.h" namespace OpenWifi { -class AutoUpdater : public SubSystemServer, Poco::Runnable { + + class AutoUpdater : public SubSystemServer { // };, Poco::Runnable { public: struct SerialCache { @@ -19,14 +21,13 @@ class AutoUpdater : public SubSystemServer, Poco::Runnable { bool firmwareRCOnly=false; }; - static AutoUpdater *instance() { - static AutoUpdater *instance_ = new AutoUpdater; + static auto instance() { + static auto instance_ = new AutoUpdater; return instance_; } int Start() override; void Stop() override; - void run() final; void ToBeUpgraded(std::string serialNumber, std::string DeviceType); inline void Reset() { std::lock_guard G(Mutex_); @@ -34,21 +35,24 @@ class AutoUpdater : public SubSystemServer, Poco::Runnable { Queue_.clear(); } void reinitialize(Poco::Util::Application &self) final; + void onTimer(Poco::Timer & timer); private: - std::atomic_bool Running_=false; - Poco::Thread Thr_; - std::map Cache_; - std::deque> Queue_; - uint64_t AutoUpdaterFrequency_=600; - bool AutoUpdaterEnabled_=true; + std::atomic_bool Running_=false; + std::map Cache_; + std::deque> Queue_; + uint64_t AutoUpdaterFrequency_=600; + bool AutoUpdaterEnabled_=true; + Poco::Timer Timer_; + std::unique_ptr> AutoUpdaterCallBack_; + explicit AutoUpdater() noexcept: SubSystemServer("AutoUpdater", "AUTO-UPDATER", "autoupdater") { } }; - inline AutoUpdater * AutoUpdater() { return AutoUpdater::instance(); } + inline auto AutoUpdater() { return AutoUpdater::instance(); } } #endif //OWFMS_AUTOUPDATER_H diff --git a/src/DeviceCache.h b/src/DeviceCache.h index 79c00a7..92219a7 100644 --- a/src/DeviceCache.h +++ b/src/DeviceCache.h @@ -19,8 +19,8 @@ namespace OpenWifi { class DeviceCache : public SubSystemServer { public: - static DeviceCache *instance() { - static DeviceCache *instance_ = new DeviceCache; + static auto instance() { + static auto instance_ = new DeviceCache; return instance_; } @@ -40,7 +40,7 @@ namespace OpenWifi { } }; - inline DeviceCache * DeviceCache() { return DeviceCache::instance(); } + inline auto DeviceCache() { return DeviceCache::instance(); } } diff --git a/src/FirmwareCache.h b/src/FirmwareCache.h index 64b2425..75387be 100644 --- a/src/FirmwareCache.h +++ b/src/FirmwareCache.h @@ -17,8 +17,8 @@ namespace OpenWifi { class FirmwareCache: public SubSystemServer { public: - static FirmwareCache *instance() { - static FirmwareCache *instance_= new FirmwareCache; + static auto instance() { + static auto instance_= new FirmwareCache; return instance_; } @@ -38,7 +38,7 @@ namespace OpenWifi { } }; - inline FirmwareCache * FirmwareCache() { return FirmwareCache::instance(); } + inline auto FirmwareCache() { return FirmwareCache::instance(); } } diff --git a/src/LatestFirmwareCache.h b/src/LatestFirmwareCache.h index 24e8a2a..f145408 100644 --- a/src/LatestFirmwareCache.h +++ b/src/LatestFirmwareCache.h @@ -24,8 +24,8 @@ namespace OpenWifi { class LatestFirmwareCache : public SubSystemServer { public: - static LatestFirmwareCache *instance() { - static LatestFirmwareCache *instance_ = new LatestFirmwareCache; + static auto instance() { + static auto instance_ = new LatestFirmwareCache; return instance_; } @@ -49,7 +49,7 @@ namespace OpenWifi { } }; - inline LatestFirmwareCache * LatestFirmwareCache() { return LatestFirmwareCache::instance(); } + inline auto LatestFirmwareCache() { return LatestFirmwareCache::instance(); } } diff --git a/src/ManifestCreator.cpp b/src/ManifestCreator.cpp index 1126ac5..a3b43ad 100644 --- a/src/ManifestCreator.cpp +++ b/src/ManifestCreator.cpp @@ -16,25 +16,14 @@ namespace OpenWifi { - void ManifestCreator::run() { - Running_ = true; - bool FirstRun = true; - - while(Running_) { - Poco::Thread::trySleep(FirstRun ? 10000 : DBRefresh_*1000); - if(!Running_) - break; - FirstRun = false; - Logger().information("Performing DB refresh"); - S3BucketContent BucketList; - StorageService()->FirmwaresDB().RemoveOldFirmware(); - ReadBucket(BucketList); - if(!Running_) - break; - Logger().information(Poco::format("Found %Lu firmware entries in S3 repository.",(uint64_t)BucketList.size())); - ComputeManifest(BucketList); - AddManifestToDB(BucketList); - } + void ManifestCreator::onTimer(Poco::Timer &timer) { + Logger().information("Performing DB refresh"); + S3BucketContent BucketList; + StorageService()->FirmwaresDB().RemoveOldFirmware(); + ReadBucket(BucketList); + Logger().information(Poco::format("Found %Lu firmware entries in S3 repository.",(uint64_t)BucketList.size())); + ComputeManifest(BucketList); + AddManifestToDB(BucketList); } bool ManifestCreator::ComputeManifest(S3BucketContent &BucketContent) { @@ -118,6 +107,7 @@ namespace OpenWifi { } int ManifestCreator::Start() { + Running_ = true; S3BucketName_ = MicroService::instance().ConfigGetString("s3.bucketname"); S3Region_ = MicroService::instance().ConfigGetString("s3.region"); S3Secret_ = MicroService::instance().ConfigGetString("s3.secret"); @@ -135,23 +125,21 @@ namespace OpenWifi { AwsCreds_.SetAWSAccessKeyId(S3Key_); AwsCreds_.SetAWSSecretKey(S3Secret_); - Worker_.start(*this); + ManifestCreatorCallBack_ = std::make_unique>(*this, &ManifestCreator::onTimer); + Timer_.setStartInterval(5 * 60 * 1000); // first run in 5 minutes + Timer_.setPeriodicInterval(DBRefresh_ * 1000); + Timer_.start(*ManifestCreatorCallBack_); + return 0; } void ManifestCreator::Stop() { if(Running_) { Running_ = false; - Worker_.wakeUp(); - Worker_.join(); + Timer_.stop(); } } - bool ManifestCreator::Update() { - Worker_.wakeUp(); - return true; - } - void ManifestCreator::CloseBucket() { } diff --git a/src/ManifestCreator.h b/src/ManifestCreator.h index 3512aae..76cf1de 100644 --- a/src/ManifestCreator.h +++ b/src/ManifestCreator.h @@ -10,6 +10,7 @@ #include #include "framework/MicroService.h" +#include "Poco/Timer.h" namespace OpenWifi { @@ -28,17 +29,15 @@ namespace OpenWifi { }; typedef std::map S3BucketContent; - class ManifestCreator : public SubSystemServer, Poco::Runnable { + class ManifestCreator : public SubSystemServer { public: - static ManifestCreator *instance() { - static ManifestCreator *instance_ = new ManifestCreator; + static auto instance() { + static auto instance_ = new ManifestCreator; return instance_; } - void run() override; int Start() override; void Stop() override; - bool Update(); bool ComputeManifest(S3BucketContent & BucketContent); bool AddManifestToDB(S3BucketContent & BucketContent); @@ -48,10 +47,9 @@ namespace OpenWifi { void CloseBucket(); void Print(const S3BucketContent &B); uint64_t MaxAge() const { return MaxAge_; } + void onTimer(Poco::Timer & timer); private: - static ManifestCreator *instance_; - Poco::Thread Worker_; std::atomic_bool Running_ = false; Aws::String S3BucketName_; Aws::String S3Region_; @@ -62,13 +60,15 @@ namespace OpenWifi { Aws::Auth::AWSCredentials AwsCreds_; uint64_t DBRefresh_ = 30 * 60; uint64_t MaxAge_ = 0 ; + Poco::Timer Timer_; + std::unique_ptr> ManifestCreatorCallBack_; ManifestCreator() noexcept: SubSystemServer("ManifestCreator", "MANIFEST-MGR", "manifestcreator") { } }; - inline ManifestCreator * ManifestCreator() { return ManifestCreator::instance(); }; + inline auto ManifestCreator() { return ManifestCreator::instance(); }; } diff --git a/src/NewCommandHandler.h b/src/NewCommandHandler.h index cb31f74..dc50e32 100644 --- a/src/NewCommandHandler.h +++ b/src/NewCommandHandler.h @@ -12,8 +12,8 @@ namespace OpenWifi { class NewCommandHandler : public SubSystemServer, Poco::Runnable { public: - static NewCommandHandler *instance() { - static NewCommandHandler *instance_ = new NewCommandHandler; + static auto instance() { + static auto instance_ = new NewCommandHandler; return instance_; } @@ -34,7 +34,7 @@ namespace OpenWifi { } }; - inline NewCommandHandler * NewCommandHandler() { return NewCommandHandler::instance(); }; + inline auto NewCommandHandler() { return NewCommandHandler::instance(); }; } diff --git a/src/NewConnectionHandler.h b/src/NewConnectionHandler.h index 3029408..14fe32d 100644 --- a/src/NewConnectionHandler.h +++ b/src/NewConnectionHandler.h @@ -14,8 +14,8 @@ namespace OpenWifi { class NewConnectionHandler : public SubSystemServer, Poco::Runnable { public: - static NewConnectionHandler *instance() { - static NewConnectionHandler *instance_ = new NewConnectionHandler; + static auto instance() { + static auto instance_ = new NewConnectionHandler; return instance_; } @@ -38,7 +38,7 @@ namespace OpenWifi { } }; - inline NewConnectionHandler * NewConnectionHandler() { return NewConnectionHandler::instance(); }; + inline auto NewConnectionHandler() { return NewConnectionHandler::instance(); }; } #endif //UCENTRALFMS_NEWCONNECTIONHANDLER_H diff --git a/src/RESTObjects/RESTAPI_SecurityObjects.h b/src/RESTObjects/RESTAPI_SecurityObjects.h index fd01baa..dc8695b 100644 --- a/src/RESTObjects/RESTAPI_SecurityObjects.h +++ b/src/RESTObjects/RESTAPI_SecurityObjects.h @@ -11,10 +11,14 @@ #include #include "framework/OpenWifiTypes.h" #include "Poco/JSON/Object.h" +#include "Poco/Data/LOB.h" +#include "Poco/Data/LOBStream.h" namespace OpenWifi { namespace SecurityObjects { + typedef std::string USER_ID_TYPE; + struct AclTemplate { bool Read_ = true; bool ReadWrite_ = true; @@ -280,5 +284,13 @@ namespace OpenWifi { bool from_json(Poco::JSON::Object::Ptr &Obj); }; + struct Avatar { + std::string id; + std::string type; + uint64_t created=0; + std::string name; + Poco::Data::LOB avatar; + }; + } } diff --git a/src/framework/MicroService.h b/src/framework/MicroService.h index c0aaff2..bc25689 100644 --- a/src/framework/MicroService.h +++ b/src/framework/MicroService.h @@ -68,6 +68,7 @@ using namespace std::chrono_literals; #include "Poco/PatternFormatter.h" #include "Poco/FileChannel.h" #include "Poco/SimpleFileChannel.h" +#include "Poco/Util/PropertyFileConfiguration.h" #include "cppkafka/cppkafka.h" @@ -1035,8 +1036,29 @@ namespace OpenWifi { static const std::string uSERVICE_SUBCRIBER{ "owsub"}; static const std::string uSERVICE_INSTALLER{ "owinst"}; + template class RecordCache { + public: + explicit RecordCache( KeyType Record::* Q) : + MemberOffset(Q){ + }; + inline auto update(const Record &R) { + return Cache_.update(R.*MemberOffset, R); + } + inline auto get(const KeyType &K) { + return Cache_.get(K); + } + inline auto remove(const KeyType &K) { + return Cache_.remove(K); + } + inline auto remove(const Record &R) { + return Cache_.remove(R.*MemberOffset); + } + private: + KeyType Record::* MemberOffset; + Poco::ExpireLRUCache Cache_{Size,Expiry}; + }; - class MyErrorHandler : public Poco::ErrorHandler { + class MyErrorHandler : public Poco::ErrorHandler { public: explicit MyErrorHandler(Poco::Util::Application &App) : App_(App) {} inline void exception(const Poco::Exception & E) { @@ -1473,7 +1495,7 @@ namespace OpenWifi { int Count=0; }; - static RESTAPI_RateLimiter *instance() { + static auto instance() { static auto instance_ = new RESTAPI_RateLimiter; return instance_; } @@ -1520,7 +1542,7 @@ namespace OpenWifi { }; - inline RESTAPI_RateLimiter * RESTAPI_RateLimiter() { return RESTAPI_RateLimiter::instance(); } + inline auto RESTAPI_RateLimiter() { return RESTAPI_RateLimiter::instance(); } class RESTAPIHandler : public Poco::Net::HTTPRequestHandler { public: @@ -2224,7 +2246,7 @@ namespace OpenWifi { inline void initialize(Poco::Util::Application & self) override; - static KafkaManager *instance() { + static auto instance() { static auto instance_ = new KafkaManager; return instance_; } @@ -2318,7 +2340,7 @@ namespace OpenWifi { } }; - inline KafkaManager * KafkaManager() { return KafkaManager::instance(); } + inline auto KafkaManager() { return KafkaManager::instance(); } class AuthClient : public SubSystemServer { public: @@ -2327,7 +2349,7 @@ namespace OpenWifi { { } - static AuthClient *instance() { + static auto instance() { static auto instance_ = new AuthClient; return instance_; } @@ -2337,6 +2359,7 @@ namespace OpenWifi { } inline void Stop() override { + std::lock_guard G(Mutex_); Cache_.clear(); } @@ -2366,6 +2389,7 @@ namespace OpenWifi { return false; } Expired = false; + std::lock_guard G(Mutex_); Cache_.update(SessionToken, UInfo); return true; } @@ -2378,6 +2402,7 @@ namespace OpenWifi { } inline bool IsAuthorized(const std::string &SessionToken, SecurityObjects::UserInfoAndPolicy & UInfo, bool & Expired, bool Sub = false) { + std::lock_guard G(Mutex_); auto User = Cache_.get(SessionToken); if(!User.isNull()) { if(IsTokenExpired(User->webtoken)) { @@ -2392,10 +2417,10 @@ namespace OpenWifi { } private: - Poco::ExpireLRUCache Cache_{1024,1200000 }; + Poco::ExpireLRUCache Cache_{512,1200000 }; }; - inline AuthClient * AuthClient() { return AuthClient::instance(); } + inline auto AuthClient() { return AuthClient::instance(); } class ALBRequestHandler: public Poco::Net::HTTPRequestHandler /// Return a HTML document with the current date and time. @@ -2406,7 +2431,7 @@ namespace OpenWifi { { } - void handleRequest(Poco::Net::HTTPServerRequest& Request, Poco::Net::HTTPServerResponse& Response) + void handleRequest(Poco::Net::HTTPServerRequest& Request, Poco::Net::HTTPServerResponse& Response) override { Logger_.information(Poco::format("ALB-REQUEST(%s): New ALB request.",Request.clientAddress().toString())); Response.setChunkedTransferEncoding(true); @@ -2451,8 +2476,8 @@ namespace OpenWifi { { } - static ALBHealthCheckServer *instance() { - static ALBHealthCheckServer * instance = new ALBHealthCheckServer; + static auto instance() { + static auto instance = new ALBHealthCheckServer; return instance; } @@ -2470,7 +2495,7 @@ namespace OpenWifi { std::atomic_bool Running_=false; }; - inline ALBHealthCheckServer * ALBHealthCheckServer() { return ALBHealthCheckServer::instance(); } + inline auto ALBHealthCheckServer() { return ALBHealthCheckServer::instance(); } Poco::Net::HTTPRequestHandler * RESTAPI_ExtRouter(const char *Path, RESTAPIHandler::BindingMap &Bindings, Poco::Logger & L, RESTAPI_GenericServer & S, uint64_t Id); @@ -2481,7 +2506,7 @@ namespace OpenWifi { class RESTAPI_ExtServer : public SubSystemServer { public: - static RESTAPI_ExtServer *instance() { + static auto instance() { static auto instance_ = new RESTAPI_ExtServer; return instance_; } @@ -2512,7 +2537,7 @@ namespace OpenWifi { } }; - inline RESTAPI_ExtServer * RESTAPI_ExtServer() { return RESTAPI_ExtServer::instance(); }; + inline auto RESTAPI_ExtServer() { return RESTAPI_ExtServer::instance(); }; class ExtRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { public: @@ -2564,7 +2589,7 @@ namespace OpenWifi { class RESTAPI_IntServer : public SubSystemServer { public: - static RESTAPI_IntServer *instance() { + static auto instance() { static auto instance_ = new RESTAPI_IntServer; return instance_; } @@ -2593,7 +2618,7 @@ namespace OpenWifi { } }; - inline RESTAPI_IntServer * RESTAPI_IntServer() { return RESTAPI_IntServer::instance(); }; + inline auto RESTAPI_IntServer() { return RESTAPI_IntServer::instance(); }; class IntRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { public: @@ -2648,8 +2673,6 @@ namespace OpenWifi { uint64_t LastUpdate=0; }; - - class SubSystemServer; typedef std::map MicroServiceMetaMap; typedef std::vector MicroServiceMetaVec; @@ -2707,7 +2730,7 @@ namespace OpenWifi { return Poco::Logger::get(Name); } - inline void Exit(int Reason); + static inline void Exit(int Reason); inline void BusMessageReceived(const std::string &Key, const std::string & Message); inline MicroServiceMetaVec GetServices(const std::string & Type); inline MicroServiceMetaVec GetServices(); @@ -2750,7 +2773,8 @@ namespace OpenWifi { inline int main(const ArgVec &args) override; static MicroService & instance() { return *instance_; } inline void InitializeLoggingSystem(); - + inline void SaveConfig() { PropConfigurationFile_->save(ConfigFileName_); } + inline auto UpdateConfig() { return PropConfigurationFile_; } private: static MicroService * instance_; bool HelpRequested_ = false; @@ -2775,7 +2799,7 @@ namespace OpenWifi { BusEventManager BusEventManager_; std::mutex InfraMutex_; std::default_random_engine RandomEngine_; - + Poco::Util::PropertyFileConfiguration * PropConfigurationFile_ = nullptr; std::string DAEMON_PROPERTIES_FILENAME; std::string DAEMON_ROOT_ENV_VAR; std::string DAEMON_CONFIG_ENV_VAR; @@ -2882,9 +2906,8 @@ namespace OpenWifi { inline void MicroService::LoadConfigurationFile() { std::string Location = Poco::Environment::get(DAEMON_CONFIG_ENV_VAR,"."); - Poco::Path ConfigFile; - - ConfigFile = ConfigFileName_.empty() ? Location + "/" + DAEMON_PROPERTIES_FILENAME : ConfigFileName_; + ConfigFileName_ = ConfigFileName_.empty() ? Location + "/" + DAEMON_PROPERTIES_FILENAME : ConfigFileName_; + Poco::Path ConfigFile(ConfigFileName_); if(!ConfigFile.isFile()) { @@ -2894,7 +2917,9 @@ namespace OpenWifi { std::exit(Poco::Util::Application::EXIT_CONFIG); } - loadConfiguration(ConfigFile.toString()); + // loadConfiguration(ConfigFile.toString()); + PropConfigurationFile_ = new Poco::Util::PropertyFileConfiguration(ConfigFile.toString()); + configPtr()->addWriteable(PropConfigurationFile_, PRIO_DEFAULT); } inline void MicroService::Reload() { diff --git a/src/framework/orm.h b/src/framework/orm.h index e283b92..3b3d38b 100644 --- a/src/framework/orm.h +++ b/src/framework/orm.h @@ -155,6 +155,20 @@ namespace ORM { return S; } + template class DBCache { + public: + DBCache(unsigned Size, unsigned Timeout) + { + + } + virtual void Create(const RecordType &R)=0; + virtual bool GetFromCache(const std::string &FieldName, const std::string &Value, RecordType &R)=0; + virtual void UpdateCache(const RecordType &R)=0; + virtual void Delete(const std::string &FieldName, const std::string &Value)=0; + private: + + }; + template class DB { public: DB( OpenWifi::DBType dbtype, @@ -162,21 +176,22 @@ namespace ORM { const FieldVec & Fields, const IndexVec & Indexes, Poco::Data::SessionPool & Pool, - Poco::Logger &L, - const char *Prefix): + Poco::Logger &L, + const char *Prefix, + DBCache * Cache=nullptr): Type_(dbtype), DBName_(TableName), Pool_(Pool), Logger_(L), - Prefix_(Prefix) + Prefix_(Prefix), + Cache_(Cache) { + assert(RecordTuple::length == Fields.size()); + bool first = true; int Place=0; - assert( RecordTuple::length == Fields.size()); - for(const auto &i:Fields) { - FieldNames_[i.Name] = Place; if(!first) { CreateFields_ += ", "; @@ -374,7 +389,11 @@ namespace ORM { Insert << ConvertParams(St) , Poco::Data::Keywords::use(RT); Insert.execute(); + + if(Cache_) + Cache_->Create(R); return true; + } catch (const Poco::Exception &E) { Logger_.log(E); } @@ -383,9 +402,13 @@ namespace ORM { template bool GetRecord( const char * FieldName, T Value, RecordType & R) { try { - assert( FieldNames_.find(FieldName) != FieldNames_.end() ); + if(Cache_) { + if(Cache_->GetFromCache(FieldName, Value, R)) + return true; + } + Poco::Data::Session Session = Pool_.get(); Poco::Data::Statement Select(Session); RecordTuple RT; @@ -397,6 +420,8 @@ namespace ORM { Poco::Data::Keywords::use(Value); if(Select.execute()==1) { Convert(RT,R); + if(Cache_) + Cache_->UpdateCache(R); return true; } return false; @@ -481,6 +506,8 @@ namespace ORM { Poco::Data::Keywords::use(RT), Poco::Data::Keywords::use(Value); Update.execute(); + if(Cache_) + Cache_->UpdateCache(R); return true; } catch (const Poco::Exception &E) { Logger_.log(E); @@ -536,6 +563,8 @@ namespace ORM { Delete << ConvertParams(St) , Poco::Data::Keywords::use(Value); Delete.execute(); + if(Cache_) + Cache_->Delete(FieldName, Value); return true; } catch (const Poco::Exception &E) { Logger_.log(E); @@ -785,10 +814,17 @@ namespace ORM { Poco::Logger & Logger() { return Logger_; } + bool DeleteRecordsFromCache(const char *FieldName, const std::string &Value ) { + if(Cache_) + Cache_->Delete(FieldName, Value); + return true; + } + protected: Poco::Data::SessionPool &Pool_; Poco::Logger &Logger_; std::string DBName_; + DBCache *Cache_= nullptr; private: OpenWifi::DBType Type_; std::string CreateFields_; @@ -797,7 +833,6 @@ namespace ORM { std::string UpdateFields_; std::vector IndexCreation_; std::map FieldNames_; - // Poco::Data::SessionPool &Pool_; std::string Prefix_; }; }