From 16cc44378684e62085029f0957caa54eed4216f1 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 27 Nov 2023 13:05:52 -0800 Subject: [PATCH 001/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Connection.cpp | 47 ++++++++------- src/AP_WS_Connection.h | 8 ++- src/AP_WS_Process_connect.cpp | 6 +- src/AP_WS_Process_crashlog.cpp | 2 +- src/AP_WS_Process_deviceupdate.cpp | 2 +- src/AP_WS_Process_healthcheck.cpp | 2 +- src/AP_WS_Process_log.cpp | 2 +- src/AP_WS_Process_rebootLog.cpp | 2 +- src/AP_WS_Process_recovery.cpp | 2 +- src/AP_WS_Process_state.cpp | 2 +- src/AP_WS_ReactorPool.h | 11 +++- src/AP_WS_Server.h | 2 +- src/RESTAPI/RESTAPI_blacklist.cpp | 2 +- src/StorageService.h | 17 ++++-- src/framework/StorageClass.h | 2 + src/framework/utils.h | 85 ++++++++++++++++++++++++++++ src/storage/storage_blacklist.cpp | 18 +++--- src/storage/storage_capabilities.cpp | 15 ++++- src/storage/storage_device.cpp | 40 ++++++++++--- src/storage/storage_healthcheck.cpp | 10 ++++ src/storage/storage_logs.cpp | 4 +- src/storage/storage_statistics.cpp | 15 ++++- 23 files changed, 228 insertions(+), 70 deletions(-) diff --git a/build b/build index 9a037142a..9d607966b 100644 --- a/build +++ b/build @@ -1 +1 @@ -10 \ No newline at end of file +11 \ No newline at end of file diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 216cf75bf..06c89fea3 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -36,12 +36,6 @@ namespace OpenWifi { -#define DBL \ - { \ - std::cout << __LINE__ << " ID: " << ConnectionId_ << " Ser: " << SerialNumber_ \ - << std::endl; \ - } - void AP_WS_Connection::LogException(const Poco::Exception &E) { poco_information(Logger_, fmt::format("EXCEPTION({}): {}", CId_, E.displayText())); } @@ -49,9 +43,12 @@ namespace OpenWifi { AP_WS_Connection::AP_WS_Connection(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response, uint64_t connection_id, Poco::Logger &L, - Poco::Net::SocketReactor &R) - : Logger_(L), Reactor_(R) { + std::pair R) + : Logger_(L) { + Reactor_ = R.first; + DbSession_ = R.second; State_.sessionId = connection_id; + WS_ = std::make_unique(request, response); auto TS = Poco::Timespan(360, 0); @@ -62,13 +59,13 @@ namespace OpenWifi { WS_->setKeepAlive(true); WS_->setBlocking(false); - Reactor_.addEventHandler(*WS_, + Reactor_->addEventHandler(*WS_, Poco::NObserver( *this, &AP_WS_Connection::OnSocketReadable)); - Reactor_.addEventHandler(*WS_, + Reactor_->addEventHandler(*WS_, Poco::NObserver( *this, &AP_WS_Connection::OnSocketShutdown)); - Reactor_.addEventHandler(*WS_, + Reactor_->addEventHandler(*WS_, Poco::NObserver( *this, &AP_WS_Connection::OnSocketError)); Registered_ = true; @@ -127,6 +124,16 @@ namespace OpenWifi { } CN_ = Poco::trim(Poco::toLower(PeerCert.commonName())); + if(!Utils::ValidSerialNumber(CN_)) { + poco_trace(Logger_, + fmt::format("TLS-CONNECTION({}): Session={} Invalid serial number: CN={}", CId_, + State_.sessionId, CN_)); + EndConnection(); + return false; + } + SerialNumber_ = CN_; + SerialNumberInt_ = Utils::SerialNumberToInt(SerialNumber_); + State_.VerifiedCertificate = GWObjects::VALID_CERTIFICATE; poco_trace(Logger_, fmt::format("TLS-CONNECTION({}): Session={} Valid certificate: CN={}", CId_, @@ -140,14 +147,14 @@ namespace OpenWifi { return false; } - if(AP_WS_Server::IsSim(CN_)) { + if(AP_WS_Server::IsSim(SerialNumber_)) { State_.VerifiedCertificate = GWObjects::SIMULATED; Simulated_ = true; } std::string reason, author; std::uint64_t created; - if (!CN_.empty() && StorageService()->IsBlackListed(CN_, reason, author, created)) { + if (!CN_.empty() && StorageService()->IsBlackListed(SerialNumberInt_, reason, author, created)) { DeviceBlacklistedKafkaEvent KE(Utils::SerialNumberToInt(CN_), Utils::Now(), reason, author, created, CId_); poco_warning( Logger_, @@ -159,8 +166,6 @@ namespace OpenWifi { } State_.certificateExpiryDate = PeerCert.expiresOn().timestamp().epochTime(); - SerialNumber_ = CN_; - SerialNumberInt_ = Utils::SerialNumberToInt(SerialNumber_); poco_trace(Logger_, fmt::format("TLS-CONNECTION({}): Session={} CN={} Completed. (t={})", CId_, @@ -254,18 +259,18 @@ namespace OpenWifi { if (!Dead_.test_and_set()) { if(!SerialNumber_.empty() && State_.LastContact!=0) { - StorageService()->SetDeviceLastRecordedContact(SerialNumber_, State_.LastContact); + StorageService()->SetDeviceLastRecordedContact(*DbSession_, SerialNumber_, State_.LastContact); } if (Registered_) { Registered_ = false; - Reactor_.removeEventHandler( + Reactor_->removeEventHandler( *WS_, Poco::NObserver( *this, &AP_WS_Connection::OnSocketReadable)); - Reactor_.removeEventHandler( + Reactor_->removeEventHandler( *WS_, Poco::NObserver( *this, &AP_WS_Connection::OnSocketShutdown)); - Reactor_.removeEventHandler( + Reactor_->removeEventHandler( *WS_, Poco::NObserver( *this, &AP_WS_Connection::OnSocketError)); } @@ -301,7 +306,7 @@ namespace OpenWifi { } GWObjects::Device D; - if (StorageService()->GetDevice(SerialNumber_, D)) { + if (StorageService()->GetDevice(*DbSession_,SerialNumber_, D)) { if(D.pendingUUID!=0 && UUID==D.pendingUUID) { // so we sent an upgrade to a device, and now it is completing now... @@ -447,7 +452,7 @@ namespace OpenWifi { std::string reason, author; std::uint64_t created; - if (StorageService()->IsBlackListed(Serial, reason, author, created)) { + if (StorageService()->IsBlackListed(SerialNumberInt_, reason, author, created)) { DeviceBlacklistedKafkaEvent KE(Utils::SerialNumberToInt(CN_), Utils::Now(), reason, author, created, CId_); Poco::Exception E( fmt::format("BLACKLIST({}): device is blacklisted and not allowed to connect.", diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index e56a9d99a..857891f0a 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -14,6 +14,7 @@ #include "Poco/Net/SocketReactor.h" #include "Poco/Net/StreamSocket.h" #include "Poco/Net/WebSocket.h" +#include #include "RESTObjects/RESTAPI_GWobjects.h" @@ -25,7 +26,7 @@ namespace OpenWifi { public: explicit AP_WS_Connection(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response, uint64_t connection_id, - Poco::Logger &L, Poco::Net::SocketReactor &R); + Poco::Logger &L, std::pair R); ~AP_WS_Connection(); void EndConnection(bool DeleteSession=true); @@ -164,7 +165,8 @@ namespace OpenWifi { mutable std::mutex ConnectionMutex_; std::mutex TelemetryMutex_; Poco::Logger &Logger_; - Poco::Net::SocketReactor &Reactor_; + Poco::Net::SocketReactor *Reactor_{nullptr}; + Poco::Data::Session *DbSession_{nullptr}; std::unique_ptr WS_; std::string SerialNumber_; uint64_t SerialNumberInt_ = 0; @@ -183,7 +185,7 @@ namespace OpenWifi { volatile uint64_t TelemetryWebSocketPackets_ = 0; volatile uint64_t TelemetryKafkaPackets_ = 0; GWObjects::ConnectionState State_; - std::string RawLastStats_; + Utils::CompressedString RawLastStats_; GWObjects::HealthCheck RawLastHealthcheck_; std::chrono::time_point ConnectionStart_ = std::chrono::high_resolution_clock::now(); diff --git a/src/AP_WS_Process_connect.cpp b/src/AP_WS_Process_connect.cpp index e5af14096..f9b1fe6a3 100644 --- a/src/AP_WS_Process_connect.cpp +++ b/src/AP_WS_Process_connect.cpp @@ -110,7 +110,7 @@ namespace OpenWifi { State_.locale = FindCountryFromIP()->Get(IP); GWObjects::Device DeviceInfo; - auto DeviceExists = StorageService()->GetDevice(SerialNumber_, DeviceInfo); + auto DeviceExists = StorageService()->GetDevice(*DbSession_,SerialNumber_, DeviceInfo); if (Daemon()->AutoProvisioning() && !DeviceExists) { // check the firmware version. if this is too old, we cannot let that device connect yet, we must // force a firmware upgrade @@ -166,7 +166,7 @@ namespace OpenWifi { poco_warning(Logger(),fmt::format("Device {} is a {} from {} and cannot be provisioned.",SerialNumber_,Compatible_, CId_)); return EndConnection(); } else if (DeviceExists) { - StorageService()->UpdateDeviceCapabilities(SerialNumber_, Caps); + StorageService()->UpdateDeviceCapabilities(*DbSession_, SerialNumber_, Caps); int Updated{0}; if (!Firmware.empty()) { if (Firmware != DeviceInfo.Firmware) { @@ -238,7 +238,7 @@ namespace OpenWifi { } if (Updated) { - StorageService()->UpdateDevice(DeviceInfo); + StorageService()->UpdateDevice(*DbSession_, DeviceInfo); } if(!Simulated_) { diff --git a/src/AP_WS_Process_crashlog.cpp b/src/AP_WS_Process_crashlog.cpp index 9b8773c31..c56ea198b 100644 --- a/src/AP_WS_Process_crashlog.cpp +++ b/src/AP_WS_Process_crashlog.cpp @@ -29,7 +29,7 @@ namespace OpenWifi { .Recorded = Utils::Now(), .LogType = 1, .UUID = ParamsObj->get(uCentralProtocol::UUID)}; - StorageService()->AddLog(DeviceLog); + StorageService()->AddLog(*DbSession_, DeviceLog); DeviceLogKafkaEvent E(DeviceLog); } else { poco_warning(Logger_, fmt::format("LOG({}): Missing parameters.", CId_)); diff --git a/src/AP_WS_Process_deviceupdate.cpp b/src/AP_WS_Process_deviceupdate.cpp index b48c3cea7..c1056d290 100644 --- a/src/AP_WS_Process_deviceupdate.cpp +++ b/src/AP_WS_Process_deviceupdate.cpp @@ -21,7 +21,7 @@ namespace OpenWifi { if (ParamsObj->has("currentPassword")) { auto Password = ParamsObj->get("currentPassword").toString(); - StorageService()->SetDevicePassword(Serial, Password); + StorageService()->SetDevicePassword(*DbSession_,Serial, Password); poco_trace( Logger_, fmt::format("DEVICE-UPDATE({}): Device is updating its login password.", Serial)); diff --git a/src/AP_WS_Process_healthcheck.cpp b/src/AP_WS_Process_healthcheck.cpp index 14b2cf308..a6aab8721 100644 --- a/src/AP_WS_Process_healthcheck.cpp +++ b/src/AP_WS_Process_healthcheck.cpp @@ -48,7 +48,7 @@ namespace OpenWifi { Check.Data = CheckData; Check.Sanity = Sanity; - StorageService()->AddHealthCheckData(Check); + StorageService()->AddHealthCheckData(*DbSession_, Check); if (!request_uuid.empty()) { StorageService()->SetCommandResult(request_uuid, CheckData); diff --git a/src/AP_WS_Process_log.cpp b/src/AP_WS_Process_log.cpp index 20fe77dfe..64c7f265f 100644 --- a/src/AP_WS_Process_log.cpp +++ b/src/AP_WS_Process_log.cpp @@ -36,7 +36,7 @@ namespace OpenWifi { .Recorded = (uint64_t)time(nullptr), .LogType = 0, .UUID = State_.UUID}; - StorageService()->AddLog(DeviceLog); + StorageService()->AddLog(*DbSession_, DeviceLog); DeviceLogKafkaEvent E(DeviceLog); } else { poco_warning(Logger_, fmt::format("LOG({}): Missing parameters.", CId_)); diff --git a/src/AP_WS_Process_rebootLog.cpp b/src/AP_WS_Process_rebootLog.cpp index 070aafd9d..871d44227 100644 --- a/src/AP_WS_Process_rebootLog.cpp +++ b/src/AP_WS_Process_rebootLog.cpp @@ -35,7 +35,7 @@ namespace OpenWifi { .Recorded = ParamsObj->get(uCentralProtocol::DATE), .LogType = 2, .UUID = ParamsObj->get(uCentralProtocol::UUID)}; - StorageService()->AddLog(DeviceLog); + StorageService()->AddLog(*DbSession_, DeviceLog); DeviceLogKafkaEvent E(DeviceLog); } else { poco_warning(Logger_, fmt::format("REBOOT-LOG({}): Missing parameters.", CId_)); diff --git a/src/AP_WS_Process_recovery.cpp b/src/AP_WS_Process_recovery.cpp index a9100b2c0..8871c6548 100644 --- a/src/AP_WS_Process_recovery.cpp +++ b/src/AP_WS_Process_recovery.cpp @@ -35,7 +35,7 @@ namespace OpenWifi { .LogType = 1, .UUID = 0}; - StorageService()->AddLog(DeviceLog); + StorageService()->AddLog(*DbSession_, DeviceLog); if (ParamsObj->get(uCentralProtocol::REBOOT).toString() == "true") { GWObjects::CommandDetails Cmd; diff --git a/src/AP_WS_Process_state.cpp b/src/AP_WS_Process_state.cpp index 646b37de9..7d99685b8 100644 --- a/src/AP_WS_Process_state.cpp +++ b/src/AP_WS_Process_state.cpp @@ -49,7 +49,7 @@ namespace OpenWifi { GWObjects::Statistics Stats{ .SerialNumber = SerialNumber_, .UUID = UUID, .Data = StateStr}; Stats.Recorded = Utils::Now(); - StorageService()->AddStatisticsData(Stats); + StorageService()->AddStatisticsData(*DbSession_,Stats); if (!request_uuid.empty()) { StorageService()->SetCommandResult(request_uuid, StateStr); } diff --git a/src/AP_WS_ReactorPool.h b/src/AP_WS_ReactorPool.h index 205c29dcb..5f168a3d2 100644 --- a/src/AP_WS_ReactorPool.h +++ b/src/AP_WS_ReactorPool.h @@ -9,8 +9,9 @@ #include "Poco/Environment.h" #include "Poco/Net/SocketAcceptor.h" - +#include #include "framework/utils.h" +#include "framework/StorageClass.h" namespace OpenWifi { class AP_WS_ReactorThreadPool { @@ -32,6 +33,7 @@ namespace OpenWifi { Utils::SetThreadName(*NewThread, ThreadName.c_str()); Reactors_.emplace_back(std::move(NewReactor)); Threads_.emplace_back(std::move(NewThread)); + DbSessions_.emplace_back(std::make_unique(StorageClass().Pool().get())); } } @@ -43,13 +45,14 @@ namespace OpenWifi { } Reactors_.clear(); Threads_.clear(); + DbSessions_.clear(); } - Poco::Net::SocketReactor &NextReactor() { + std::pair NextReactor() { std::lock_guard Lock(Mutex_); NextReactor_++; NextReactor_ %= NumberOfThreads_; - return *Reactors_[NextReactor_]; + return std::make_pair(Reactors_[NextReactor_].get(), DbSessions_[NextReactor_].get()); } private: @@ -58,5 +61,7 @@ namespace OpenWifi { uint64_t NextReactor_ = 0; std::vector> Reactors_; std::vector> Threads_; + std::vector> DbSessions_; + }; } // namespace OpenWifi \ No newline at end of file diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index 37dc5b10c..c4db15187 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -94,7 +94,7 @@ namespace OpenWifi { inline bool UseProvisioning() const { return LookAtProvisioning_; } inline bool UseDefaults() const { return UseDefaultConfig_; } - [[nodiscard]] inline Poco::Net::SocketReactor &NextReactor() { + [[nodiscard]] inline std::pair NextReactor() { return Reactor_pool_->NextReactor(); } [[nodiscard]] inline bool Running() const { return Running_; } diff --git a/src/RESTAPI/RESTAPI_blacklist.cpp b/src/RESTAPI/RESTAPI_blacklist.cpp index 42259b889..b4c96c584 100644 --- a/src/RESTAPI/RESTAPI_blacklist.cpp +++ b/src/RESTAPI/RESTAPI_blacklist.cpp @@ -63,7 +63,7 @@ namespace OpenWifi { poco_debug(Logger(), fmt::format("BLACKLIST-POST: {}", D.serialNumber)); Poco::toLowerInPlace(D.serialNumber); - if (StorageService()->IsBlackListed(D.serialNumber)) { + if (StorageService()->IsBlackListed(Utils::MACToInt(D.serialNumber))) { return BadRequest(RESTAPI::Errors::SerialNumberExists); } diff --git a/src/StorageService.h b/src/StorageService.h index a20a9f266..628828df9 100644 --- a/src/StorageService.h +++ b/src/StorageService.h @@ -90,7 +90,8 @@ namespace OpenWifi { // typedef std::map DeviceCapabilitiesCache; - bool AddLog(const GWObjects::DeviceLog &Log); + bool AddLog(Poco::Data::Session &DbSession, const GWObjects::DeviceLog &Log); + bool AddStatisticsData(Poco::Data::Session &DbSession, const GWObjects::Statistics &Stats); bool AddStatisticsData(const GWObjects::Statistics &Stats); bool GetStatisticsData(std::string &SerialNumber, uint64_t FromDate, uint64_t ToDate, uint64_t Offset, uint64_t HowMany, @@ -102,6 +103,7 @@ namespace OpenWifi { std::vector &Stats); bool AddHealthCheckData(const GWObjects::HealthCheck &Check); + bool AddHealthCheckData(Poco::Data::Session &DbSession, const GWObjects::HealthCheck &Check); bool GetHealthCheckData(std::string &SerialNumber, uint64_t FromDate, uint64_t ToDate, uint64_t Offset, uint64_t HowMany, std::vector &Checks); @@ -117,11 +119,13 @@ namespace OpenWifi { bool RollbackDeviceConfigurationChange(std::string & SerialNumber); bool CompleteDeviceConfigurationChange(std::string & SerialNumber); + bool CreateDevice(Poco::Data::Session &DbSession, GWObjects::Device &); bool CreateDevice(GWObjects::Device &); bool CreateDefaultDevice(std::string &SerialNumber, const Config::Capabilities &Caps, std::string &Firmware, const Poco::Net::IPAddress &IPAddress, bool simulated); + bool GetDevice(Poco::Data::Session &DbSession, std::string &SerialNumber, GWObjects::Device &); bool GetDevice(std::string &SerialNumber, GWObjects::Device &); bool GetDevices(uint64_t From, uint64_t HowMany, std::vector &Devices, const std::string &orderBy = ""); @@ -132,6 +136,7 @@ namespace OpenWifi { bool DeleteDevices(std::uint64_t OlderContact, bool SimulatedOnly); bool UpdateDevice(GWObjects::Device &); + bool UpdateDevice(Poco::Data::Session &Session, GWObjects::Device &); bool DeviceExists(std::string &SerialNumber); bool SetConnectInfo(std::string &SerialNumber, std::string &Firmware); bool GetDeviceCount(uint64_t &Count); @@ -139,7 +144,7 @@ namespace OpenWifi { std::vector &SerialNumbers, const std::string &orderBy = ""); bool GetDeviceFWUpdatePolicy(std::string &SerialNumber, std::string &Policy); - bool SetDevicePassword(std::string &SerialNumber, std::string &Password); + bool SetDevicePassword(Poco::Data::Session &Session, std::string &SerialNumber, std::string &Password); bool UpdateSerialNumberCache(); static void GetDeviceDbFieldList(Types::StringVec &Fields); @@ -148,6 +153,8 @@ namespace OpenWifi { bool UpdateDeviceCapabilities(std::string &SerialNumber, const Config::Capabilities &Capabilities); + bool UpdateDeviceCapabilities(Poco::Data::Session &Session, std::string &SerialNumber, + const Config::Capabilities &Capabilities); bool GetDeviceCapabilities(std::string &SerialNumber, GWObjects::Capabilities &); bool DeleteDeviceCapabilities(std::string &SerialNumber); bool CreateDeviceCapabilities(std::string &SerialNumber, @@ -228,9 +235,9 @@ namespace OpenWifi { bool AddBlackListDevice(GWObjects::BlackListedDevice &Device); bool GetBlackListDevice(std::string &SerialNumber, GWObjects::BlackListedDevice &Device); bool DeleteBlackListDevice(std::string &SerialNumber); - bool IsBlackListed(const std::string &SerialNumber, std::string &reason, + bool IsBlackListed(std::uint64_t SerialNumber, std::string &reason, std::string &author, std::uint64_t &created); - bool IsBlackListed(const std::string &SerialNumber); + bool IsBlackListed(std::uint64_t SerialNumber); bool InitializeBlackListCache(); bool GetBlackListDevices(uint64_t Offset, uint64_t HowMany, std::vector &Devices); @@ -245,7 +252,7 @@ namespace OpenWifi { bool RemoveCommandListRecordsOlderThan(uint64_t Date); bool RemoveUploadedFilesRecordsOlderThan(uint64_t Date); - bool SetDeviceLastRecordedContact(std::string & SeialNumber, std::uint64_t lastRecordedContact); + bool SetDeviceLastRecordedContact(Poco::Data::Session &Session, std::string & SeialNumber, std::uint64_t lastRecordedContact); int Create_Tables(); int Create_Statistics(); diff --git a/src/framework/StorageClass.h b/src/framework/StorageClass.h index cfb55304a..36751bc6e 100644 --- a/src/framework/StorageClass.h +++ b/src/framework/StorageClass.h @@ -47,6 +47,8 @@ namespace OpenWifi { } + Poco::Data::SessionPool &Pool() { return *Pool_; } + private: inline int Setup_SQLite(); inline int Setup_MySQL(); diff --git a/src/framework/utils.h b/src/framework/utils.h index d5357de7f..cf36bcb49 100644 --- a/src/framework/utils.h +++ b/src/framework/utils.h @@ -330,5 +330,90 @@ namespace OpenWifi::Utils { uint32_t Port; }; + class CompressedString { + public: + CompressedString() { + DecompressedSize_ = 0; + }; + + explicit CompressedString(const std::string &Data) : DecompressedSize_(Data.size()) { + CompressIt(Data); + } + + CompressedString(const CompressedString &Data) { + this->DecompressedSize_ = Data.DecompressedSize_; + this->CompressedData_ = Data.CompressedData_; + } + + CompressedString& operator=(const CompressedString& rhs) { + if (this != &rhs) { + this->DecompressedSize_ = rhs.DecompressedSize_; + this->CompressedData_ = rhs.CompressedData_; + } + return *this; + } + + CompressedString& operator=(CompressedString&& rhs) { + if (this != &rhs) { + this->DecompressedSize_ = rhs.DecompressedSize_; + this->CompressedData_ = rhs.CompressedData_; + } + return *this; + } + + ~CompressedString() = default; + + operator std::string() const { + return DecompressIt(); + } + + CompressedString &operator=(const std::string &Data) { + DecompressedSize_ = Data.size(); + CompressIt(Data); + return *this; + } + + auto CompressedSize() const { return CompressedData_.size(); } + auto DecompressedSize() const { return DecompressedSize_; } + + private: + std::string CompressedData_; + std::size_t DecompressedSize_; + + inline void CompressIt(const std::string &Data) { + z_stream strm; // = {0}; + CompressedData_.resize(Data.size()); + strm.next_in = (Bytef *)Data.data(); + strm.avail_in = Data.size(); + strm.next_out = (Bytef *)CompressedData_.data(); + strm.avail_out = Data.size(); + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY); + deflate(&strm, Z_FINISH); + deflateEnd(&strm); + CompressedData_.resize(strm.total_out); + } + + [[nodiscard]] std::string DecompressIt() const { + std::string Result; + if(DecompressedSize_!=0) { + Result.resize(DecompressedSize_); + z_stream strm ; //= {0}; + strm.next_in = (Bytef *)CompressedData_.data(); + strm.avail_in = CompressedData_.size(); + strm.next_out = (Bytef *)Result.data(); + strm.avail_out = Result.size(); + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + inflateInit2(&strm, 15 + 32); + inflate(&strm, Z_FINISH); + inflateEnd(&strm); + } + return Result; + } + }; } // namespace OpenWifi::Utils diff --git a/src/storage/storage_blacklist.cpp b/src/storage/storage_blacklist.cpp index e7b2242da..9018c1e8d 100644 --- a/src/storage/storage_blacklist.cpp +++ b/src/storage/storage_blacklist.cpp @@ -59,7 +59,7 @@ namespace OpenWifi { std::uint64_t created; }; - static std::map BlackListDevices; + static std::map BlackListDevices; static std::recursive_mutex BlackListMutex; bool Storage::InitializeBlackListCache() { @@ -78,7 +78,7 @@ namespace OpenWifi { auto Reason = RSet[1].convert(); auto Author = RSet[2].convert(); auto Created = RSet[3].convert(); - BlackListDevices[SerialNumber] = + BlackListDevices[Utils::MACToInt(SerialNumber)] = DeviceDetails{.reason = Reason, .author = Author, .created = Created}; More = RSet.moveNext(); } @@ -104,7 +104,7 @@ namespace OpenWifi { Insert.execute(); std::lock_guard G(BlackListMutex); - BlackListDevices[Device.serialNumber] = DeviceDetails{ + BlackListDevices[Utils::MACToInt(Device.serialNumber)] = DeviceDetails{ .reason = Device.reason, .author = Device.author, .created = Device.created}; return true; } catch (const Poco::Exception &E) { @@ -139,7 +139,7 @@ namespace OpenWifi { Delete.execute(); std::lock_guard G(BlackListMutex); - BlackListDevices.erase(SerialNumber); + BlackListDevices.erase(Utils::MACToInt(SerialNumber)); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), @@ -189,7 +189,7 @@ namespace OpenWifi { Update.execute(); std::lock_guard G(BlackListMutex); - BlackListDevices[Device.serialNumber] = DeviceDetails{ + BlackListDevices[Utils::MACToInt(Device.serialNumber)] = DeviceDetails{ .reason = Device.reason, .author = Device.author, .created = Device.created}; return true; @@ -233,10 +233,10 @@ namespace OpenWifi { return BlackListDevices.size(); } - bool Storage::IsBlackListed(const std::string &SerialNumber, std::string &reason, + bool Storage::IsBlackListed(std::uint64_t SerialNumber, std::string &reason, std::string &author, std::uint64_t &created) { std::lock_guard G(BlackListMutex); - auto DeviceHint = BlackListDevices.find(Poco::toLower(SerialNumber)); + auto DeviceHint = BlackListDevices.find(SerialNumber); if (DeviceHint == end(BlackListDevices)) return false; reason = DeviceHint->second.reason; @@ -245,9 +245,9 @@ namespace OpenWifi { return true; } - bool Storage::IsBlackListed(const std::string &SerialNumber) { + bool Storage::IsBlackListed(std::uint64_t SerialNumber) { std::lock_guard G(BlackListMutex); - auto DeviceHint = BlackListDevices.find(Poco::toLower(SerialNumber)); + auto DeviceHint = BlackListDevices.find(SerialNumber); return DeviceHint != end(BlackListDevices); } } // namespace OpenWifi \ No newline at end of file diff --git a/src/storage/storage_capabilities.cpp b/src/storage/storage_capabilities.cpp index 72d054e4e..4febd667e 100644 --- a/src/storage/storage_capabilities.cpp +++ b/src/storage/storage_capabilities.cpp @@ -41,10 +41,9 @@ namespace OpenWifi { return false; } - bool Storage::UpdateDeviceCapabilities(std::string &SerialNumber, + bool Storage::UpdateDeviceCapabilities(Poco::Data::Session &Sess, std::string &SerialNumber, const Config::Capabilities &Caps) { try { - Poco::Data::Session Sess = Pool_->get(); Poco::Data::Statement UpSert(Sess); uint64_t Now = Utils::Now(); @@ -69,6 +68,18 @@ namespace OpenWifi { return false; } + bool Storage::UpdateDeviceCapabilities(std::string &SerialNumber, + const Config::Capabilities &Caps) { + try { + Poco::Data::Session Sess = Pool_->get(); + return UpdateDeviceCapabilities(Sess, SerialNumber, Caps); + } catch (const Poco::Exception &E) { + poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), + E.displayText())); + } + return false; + } + bool Storage::GetDeviceCapabilities(std::string &SerialNumber, GWObjects::Capabilities &Caps) { try { Poco::Data::Session Sess = Pool_->get(); diff --git a/src/storage/storage_device.cpp b/src/storage/storage_device.cpp index 4e1a0d8a3..80ac91e02 100644 --- a/src/storage/storage_device.cpp +++ b/src/storage/storage_device.cpp @@ -366,9 +366,8 @@ namespace OpenWifi { return false; } - bool Storage::SetDeviceLastRecordedContact(std::string &SerialNumber, std::uint64_t lastRecordedContact) { + bool Storage::SetDeviceLastRecordedContact(Poco::Data::Session &Sess, std::string &SerialNumber, std::uint64_t lastRecordedContact) { try { - Poco::Data::Session Sess = Pool_->get(); Poco::Data::Statement Update(Sess); std::string St{"UPDATE Devices SET lastRecordedContact=? WHERE SerialNumber=?"}; @@ -383,11 +382,9 @@ namespace OpenWifi { return false; } - bool Storage::CreateDevice(GWObjects::Device &DeviceDetails) { + bool Storage::CreateDevice(Poco::Data::Session &Sess, GWObjects::Device &DeviceDetails) { std::string SerialNumber; try { - - Poco::Data::Session Sess = Pool_->get(); Poco::Data::Statement Select(Sess); std::string St{"SELECT SerialNumber FROM Devices WHERE SerialNumber=?"}; @@ -435,6 +432,16 @@ namespace OpenWifi { return false; } + bool Storage::CreateDevice(GWObjects::Device &DeviceDetails) { + try { + auto Session = StorageClass().Pool().get(); + return CreateDevice(Session, DeviceDetails); + } catch (const Poco::Exception &E) { + Logger().log(E); + } + return false; + } + static std::string InsertRadiosCountyRegulation(std::string &Config, const Poco::Net::IPAddress &IPAddress) { std::string FoundCountry; @@ -560,9 +567,8 @@ namespace OpenWifi { return false; } - bool Storage::SetDevicePassword(std::string &SerialNumber, std::string &Password) { + bool Storage::SetDevicePassword(Poco::Data::Session &Sess, std::string &SerialNumber, std::string &Password) { try { - Poco::Data::Session Sess = Pool_->get(); Poco::Data::Statement Update(Sess); std::string St{"UPDATE Devices SET DevicePassword=? WHERE SerialNumber=?"}; @@ -694,7 +700,16 @@ namespace OpenWifi { bool Storage::GetDevice(std::string &SerialNumber, GWObjects::Device &DeviceDetails) { try { - Poco::Data::Session Sess = Pool_->get(); + auto Sess = Pool_->get(); + return GetDevice(Sess, SerialNumber, DeviceDetails); + } catch (const Poco::Exception &E) { + Logger().log(E); + } + return false; + } + + bool Storage::GetDevice(Poco::Data::Session &Sess, std::string &SerialNumber, GWObjects::Device &DeviceDetails) { + try { Poco::Data::Statement Select(Sess); std::string St{"SELECT " + DB_DeviceSelectFields + @@ -741,6 +756,15 @@ namespace OpenWifi { bool Storage::UpdateDevice(GWObjects::Device &NewDeviceDetails) { try { Poco::Data::Session Sess = Pool_->get(); + return UpdateDevice(Sess, NewDeviceDetails); + } catch (const Poco::Exception &E) { + Logger().log(E); + } + return false; + } + + bool Storage::UpdateDevice(Poco::Data::Session &Sess, GWObjects::Device &NewDeviceDetails) { + try { Poco::Data::Statement Update(Sess); DeviceRecordTuple R; diff --git a/src/storage/storage_healthcheck.cpp b/src/storage/storage_healthcheck.cpp index 971bbe7e4..c88dcf4d0 100644 --- a/src/storage/storage_healthcheck.cpp +++ b/src/storage/storage_healthcheck.cpp @@ -38,6 +38,16 @@ namespace OpenWifi { bool Storage::AddHealthCheckData(const GWObjects::HealthCheck &Check) { try { Poco::Data::Session Sess = Pool_->get(); + return Storage::AddHealthCheckData(Sess, Check); + } catch (const Poco::Exception &E) { + poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), + E.displayText())); + } + return false; + } + + bool Storage::AddHealthCheckData(Poco::Data::Session &Sess, const GWObjects::HealthCheck &Check) { + try { Poco::Data::Statement Insert(Sess); std::string St{"INSERT INTO HealthChecks ( " + DB_HealthCheckSelectFields + diff --git a/src/storage/storage_logs.cpp b/src/storage/storage_logs.cpp index df8f54ae4..a1b217f9b 100644 --- a/src/storage/storage_logs.cpp +++ b/src/storage/storage_logs.cpp @@ -39,10 +39,8 @@ namespace OpenWifi { R.set<6>(Log.UUID); } - bool Storage::AddLog(const GWObjects::DeviceLog &Log) { + bool Storage::AddLog(Poco::Data::Session &Sess, const GWObjects::DeviceLog &Log) { try { - - Poco::Data::Session Sess = Pool_->get(); Poco::Data::Statement Insert(Sess); std::string St{"INSERT INTO DeviceLogs (" + DB_LogsSelectFields + ") values( " + diff --git a/src/storage/storage_statistics.cpp b/src/storage/storage_statistics.cpp index 7b3592c9c..9833f902a 100644 --- a/src/storage/storage_statistics.cpp +++ b/src/storage/storage_statistics.cpp @@ -8,7 +8,6 @@ #include "AP_WS_Server.h" #include "StorageService.h" - #include "fmt/format.h" namespace OpenWifi { @@ -33,9 +32,8 @@ namespace OpenWifi { R.set<3>(Stats.Recorded); } - bool Storage::AddStatisticsData(const GWObjects::Statistics &Stats) { + bool Storage::AddStatisticsData(Poco::Data::Session &Sess, const GWObjects::Statistics &Stats) { try { - Poco::Data::Session Sess(Pool_->get()); Poco::Data::Statement Insert(Sess); poco_trace(Logger(), fmt::format("{}: Adding stats. Size={}", Stats.SerialNumber, @@ -54,6 +52,17 @@ namespace OpenWifi { return false; } + bool Storage::AddStatisticsData(const GWObjects::Statistics &Stats) { + try { + auto Session = StorageClass().Pool().get(); + return Storage::AddStatisticsData(Session, Stats); + } catch (const Poco::Exception &E) { + poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), + E.displayText())); + } + return false; + } + bool Storage::GetNumberOfStatisticsDataRecords(std::string &SerialNumber, uint64_t FromDate, uint64_t ToDate, std::uint64_t &Count) { try { From 97547068d76d22032edd77b9083fccb5cb0c4983 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 27 Nov 2023 13:30:25 -0800 Subject: [PATCH 002/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- CMakeLists.txt | 2 +- build | 2 +- src/{AP_WS_ReactorPool.h => AP_WS_Reactor_Pool.h} | 13 +++++++++---- src/AP_WS_Server.cpp | 2 +- src/AP_WS_Server.h | 2 +- src/TelemetryStream.h | 2 +- 6 files changed, 14 insertions(+), 9 deletions(-) rename src/{AP_WS_ReactorPool.h => AP_WS_Reactor_Pool.h} (78%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f0c29479..a4b75c95b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -175,7 +175,7 @@ add_executable( owgw src/SDKcalls.cpp src/SDKcalls.h src/StateUtils.cpp src/StateUtils.h - src/AP_WS_ReactorPool.h + src/AP_WS_Reactor_Pool.h src/AP_WS_Connection.h src/AP_WS_Connection.cpp src/TelemetryClient.h src/TelemetryClient.cpp diff --git a/build b/build index 9d607966b..3cacc0b93 100644 --- a/build +++ b/build @@ -1 +1 @@ -11 \ No newline at end of file +12 \ No newline at end of file diff --git a/src/AP_WS_ReactorPool.h b/src/AP_WS_Reactor_Pool.h similarity index 78% rename from src/AP_WS_ReactorPool.h rename to src/AP_WS_Reactor_Pool.h index 5f168a3d2..c775455ac 100644 --- a/src/AP_WS_ReactorPool.h +++ b/src/AP_WS_Reactor_Pool.h @@ -11,13 +11,13 @@ #include "Poco/Net/SocketAcceptor.h" #include #include "framework/utils.h" -#include "framework/StorageClass.h" +#include namespace OpenWifi { class AP_WS_ReactorThreadPool { public: - explicit AP_WS_ReactorThreadPool() { - NumberOfThreads_ = Poco::Environment::processorCount() * 4; + explicit AP_WS_ReactorThreadPool(Poco::Logger &Logger) : Logger_(Logger) { + NumberOfThreads_ = Poco::Environment::processorCount() * 2; if (NumberOfThreads_ == 0) NumberOfThreads_ = 4; } @@ -25,6 +25,9 @@ namespace OpenWifi { ~AP_WS_ReactorThreadPool() { Stop(); } void Start() { + Reactors_.reserve(NumberOfThreads_); + DbSessions_.reserve(NumberOfThreads_); + Threads_.reserve(NumberOfThreads_); for (uint64_t i = 0; i < NumberOfThreads_; ++i) { auto NewReactor = std::make_unique(); auto NewThread = std::make_unique(); @@ -33,8 +36,9 @@ namespace OpenWifi { Utils::SetThreadName(*NewThread, ThreadName.c_str()); Reactors_.emplace_back(std::move(NewReactor)); Threads_.emplace_back(std::move(NewThread)); - DbSessions_.emplace_back(std::make_unique(StorageClass().Pool().get())); + DbSessions_.emplace_back(std::make_unique(StorageService()->Pool().get())); } + Logger_.information(fmt::format("WebSocket Processor: {} threads started.", NumberOfThreads_)); } void Stop() { @@ -62,6 +66,7 @@ namespace OpenWifi { std::vector> Reactors_; std::vector> Threads_; std::vector> DbSessions_; + Poco::Logger &Logger_; }; } // namespace OpenWifi \ No newline at end of file diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index a99b7a574..fcc9411d2 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -57,7 +57,7 @@ namespace OpenWifi { SessionTimeOut_ = MicroServiceConfigGetInt("openwifi.session.timeout", 10*60); - Reactor_pool_ = std::make_unique(); + Reactor_pool_ = std::make_unique(Logger()); Reactor_pool_->Start(); for (const auto &Svr : ConfigServersList_) { diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index c4db15187..341a2d6dc 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -24,7 +24,7 @@ #include "Poco/Timer.h" #include "AP_WS_Connection.h" -#include "AP_WS_ReactorPool.h" +#include "AP_WS_Reactor_Pool.h" #include "framework/SubSystemServer.h" #include "framework/utils.h" diff --git a/src/TelemetryStream.h b/src/TelemetryStream.h index e4fbbdcba..d9364e9f3 100644 --- a/src/TelemetryStream.h +++ b/src/TelemetryStream.h @@ -23,7 +23,7 @@ #include "framework/SubSystemServer.h" -#include "AP_WS_ReactorPool.h" +#include "AP_WS_Reactor_Pool.h" #include "TelemetryClient.h" namespace OpenWifi { From c981ae14eefc4f10691702d1556ee6f7eb757575 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 27 Nov 2023 13:43:07 -0800 Subject: [PATCH 003/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Reactor_Pool.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/AP_WS_Reactor_Pool.h b/src/AP_WS_Reactor_Pool.h index c775455ac..640602e2c 100644 --- a/src/AP_WS_Reactor_Pool.h +++ b/src/AP_WS_Reactor_Pool.h @@ -28,6 +28,7 @@ namespace OpenWifi { Reactors_.reserve(NumberOfThreads_); DbSessions_.reserve(NumberOfThreads_); Threads_.reserve(NumberOfThreads_); + Logger_.information(fmt::format("WebSocket Processor: starting {} threads.", NumberOfThreads_)); for (uint64_t i = 0; i < NumberOfThreads_; ++i) { auto NewReactor = std::make_unique(); auto NewThread = std::make_unique(); From 8dbbfc329846763963fb65ecd39460fd80daa199 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 27 Nov 2023 14:56:48 -0800 Subject: [PATCH 004/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Process_connect.cpp | 2 +- src/StorageService.h | 5 +++-- src/storage/storage_capabilities.cpp | 3 +-- src/storage/storage_device.cpp | 7 +++---- 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/build b/build index 3cacc0b93..ca7bf83ac 100644 --- a/build +++ b/build @@ -1 +1 @@ -12 \ No newline at end of file +13 \ No newline at end of file diff --git a/src/AP_WS_Process_connect.cpp b/src/AP_WS_Process_connect.cpp index f9b1fe6a3..1d44b5f86 100644 --- a/src/AP_WS_Process_connect.cpp +++ b/src/AP_WS_Process_connect.cpp @@ -157,7 +157,7 @@ namespace OpenWifi { } return; } else { - StorageService()->CreateDefaultDevice( + StorageService()->CreateDefaultDevice( *DbSession_, SerialNumber_, Caps, Firmware, PeerAddress_, State_.VerifiedCertificate == GWObjects::SIMULATED); } diff --git a/src/StorageService.h b/src/StorageService.h index 628828df9..fb292e5a9 100644 --- a/src/StorageService.h +++ b/src/StorageService.h @@ -121,7 +121,8 @@ namespace OpenWifi { bool CreateDevice(Poco::Data::Session &DbSession, GWObjects::Device &); bool CreateDevice(GWObjects::Device &); - bool CreateDefaultDevice(std::string &SerialNumber, const Config::Capabilities &Caps, + bool CreateDefaultDevice(Poco::Data::Session &DbSession,std::string &SerialNumber, + const Config::Capabilities &Caps, std::string &Firmware, const Poco::Net::IPAddress &IPAddress, bool simulated); @@ -157,7 +158,7 @@ namespace OpenWifi { const Config::Capabilities &Capabilities); bool GetDeviceCapabilities(std::string &SerialNumber, GWObjects::Capabilities &); bool DeleteDeviceCapabilities(std::string &SerialNumber); - bool CreateDeviceCapabilities(std::string &SerialNumber, + bool CreateDeviceCapabilities(Poco::Data::Session &Session, std::string &SerialNumber, const Config::Capabilities &Capabilities); bool InitCapabilitiesCache(); diff --git a/src/storage/storage_capabilities.cpp b/src/storage/storage_capabilities.cpp index 4febd667e..a5089ff2e 100644 --- a/src/storage/storage_capabilities.cpp +++ b/src/storage/storage_capabilities.cpp @@ -17,10 +17,9 @@ namespace OpenWifi { - bool Storage::CreateDeviceCapabilities(std::string &SerialNumber, + bool Storage::CreateDeviceCapabilities(Poco::Data::Session &Sess, std::string &SerialNumber, const Config::Capabilities &Capabilities) { try { - Poco::Data::Session Sess = Pool_->get(); Poco::Data::Statement UpSert(Sess); std::string TCaps{Capabilities.AsString()}; diff --git a/src/storage/storage_device.cpp b/src/storage/storage_device.cpp index 80ac91e02..77b5ef700 100644 --- a/src/storage/storage_device.cpp +++ b/src/storage/storage_device.cpp @@ -496,7 +496,7 @@ namespace OpenWifi { #define __DBGLOG__ std::cout << __LINE__ << std::endl; - bool Storage::CreateDefaultDevice(std::string &SerialNumber, const Config::Capabilities &Caps, + bool Storage::CreateDefaultDevice(Poco::Data::Session &Sess, std::string &SerialNumber, const Config::Capabilities &Caps, std::string &Firmware, const Poco::Net::IPAddress &IPAddress, bool simulated) { @@ -546,9 +546,8 @@ namespace OpenWifi { D.Notes = SecurityObjects::NoteInfoVec{ SecurityObjects::NoteInfo{(uint64_t)Utils::Now(), "", "Auto-provisioned."}}; - CreateDeviceCapabilities(SerialNumber, Caps); - - return CreateDevice(D); + CreateDeviceCapabilities(Sess, SerialNumber, Caps); + return CreateDevice(Sess, D); } bool Storage::GetDeviceFWUpdatePolicy(std::string &SerialNumber, std::string &Policy) { From 0974abd510c4e3cbd9622eca454b7068e41dd55a Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 27 Nov 2023 22:27:50 -0800 Subject: [PATCH 005/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/storage/storage_capabilities.cpp | 4 ++++ src/storage/storage_device.cpp | 14 +++++++++++--- src/storage/storage_healthcheck.cpp | 2 ++ src/storage/storage_statistics.cpp | 3 +++ 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/build b/build index ca7bf83ac..3f10ffe7a 100644 --- a/build +++ b/build @@ -1 +1 @@ -13 \ No newline at end of file +15 \ No newline at end of file diff --git a/src/storage/storage_capabilities.cpp b/src/storage/storage_capabilities.cpp index a5089ff2e..2070ace43 100644 --- a/src/storage/storage_capabilities.cpp +++ b/src/storage/storage_capabilities.cpp @@ -20,6 +20,7 @@ namespace OpenWifi { bool Storage::CreateDeviceCapabilities(Poco::Data::Session &Sess, std::string &SerialNumber, const Config::Capabilities &Capabilities) { try { + Sess.begin(); Poco::Data::Statement UpSert(Sess); std::string TCaps{Capabilities.AsString()}; @@ -32,6 +33,7 @@ namespace OpenWifi { Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(TCaps), Poco::Data::Keywords::use(Now); UpSert.execute(); + Sess.commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), @@ -43,6 +45,7 @@ namespace OpenWifi { bool Storage::UpdateDeviceCapabilities(Poco::Data::Session &Sess, std::string &SerialNumber, const Config::Capabilities &Caps) { try { + Sess.begin(); Poco::Data::Statement UpSert(Sess); uint64_t Now = Utils::Now(); @@ -59,6 +62,7 @@ namespace OpenWifi { Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(TCaps), Poco::Data::Keywords::use(Now); UpSert.execute(); + Sess.commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), diff --git a/src/storage/storage_device.cpp b/src/storage/storage_device.cpp index 77b5ef700..db363478d 100644 --- a/src/storage/storage_device.cpp +++ b/src/storage/storage_device.cpp @@ -368,12 +368,14 @@ namespace OpenWifi { bool Storage::SetDeviceLastRecordedContact(Poco::Data::Session &Sess, std::string &SerialNumber, std::uint64_t lastRecordedContact) { try { + Sess.begin(); Poco::Data::Statement Update(Sess); std::string St{"UPDATE Devices SET lastRecordedContact=? WHERE SerialNumber=?"}; Update << ConvertParams(St), Poco::Data::Keywords::use(lastRecordedContact), Poco::Data::Keywords::use(SerialNumber); Update.execute(); + Sess.commit(); return true; } catch (const Poco::Exception &E) { @@ -385,9 +387,9 @@ namespace OpenWifi { bool Storage::CreateDevice(Poco::Data::Session &Sess, GWObjects::Device &DeviceDetails) { std::string SerialNumber; try { - Poco::Data::Statement Select(Sess); +// Poco::Data::Statement Select(Sess); - std::string St{"SELECT SerialNumber FROM Devices WHERE SerialNumber=?"}; +// std::string St{"SELECT SerialNumber FROM Devices WHERE SerialNumber=?"}; // Select << ConvertParams(St), Poco::Data::Keywords::into(SerialNumber), // Poco::Data::Keywords::use(DeviceDetails.SerialNumber); @@ -404,6 +406,7 @@ namespace OpenWifi { if (Cfg.Valid() && Cfg.SetUUID(DeviceDetails.UUID)) { DeviceDetails.Configuration = Cfg.get(); + Sess.begin(); Poco::Data::Statement Insert(Sess); std::string St2{"INSERT INTO Devices ( " + DB_DeviceSelectFields + " ) " + @@ -414,6 +417,7 @@ namespace OpenWifi { ConvertDeviceRecord(DeviceDetails, R); Insert << ConvertParams(St2), Poco::Data::Keywords::use(R); Insert.execute(); + Sess.commit(); SetCurrentConfigurationID(DeviceDetails.SerialNumber, DeviceDetails.UUID); SerialNumberCache()->AddSerialNumber(DeviceDetails.SerialNumber); return true; @@ -434,7 +438,7 @@ namespace OpenWifi { bool Storage::CreateDevice(GWObjects::Device &DeviceDetails) { try { - auto Session = StorageClass().Pool().get(); + auto Session = Pool_->get(); return CreateDevice(Session, DeviceDetails); } catch (const Poco::Exception &E) { Logger().log(E); @@ -568,12 +572,14 @@ namespace OpenWifi { bool Storage::SetDevicePassword(Poco::Data::Session &Sess, std::string &SerialNumber, std::string &Password) { try { + Sess.begin(); Poco::Data::Statement Update(Sess); std::string St{"UPDATE Devices SET DevicePassword=? WHERE SerialNumber=?"}; Update << ConvertParams(St), Poco::Data::Keywords::use(Password), Poco::Data::Keywords::use(SerialNumber); Update.execute(); + Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -764,6 +770,7 @@ namespace OpenWifi { bool Storage::UpdateDevice(Poco::Data::Session &Sess, GWObjects::Device &NewDeviceDetails) { try { + Sess.begin(); Poco::Data::Statement Update(Sess); DeviceRecordTuple R; @@ -776,6 +783,7 @@ namespace OpenWifi { Update << ConvertParams(St2), Poco::Data::Keywords::use(R), Poco::Data::Keywords::use(NewDeviceDetails.SerialNumber); Update.execute(); + Sess.commit(); // GetDevice(NewDeviceDetails.SerialNumber,NewDeviceDetails); return true; } catch (const Poco::Exception &E) { diff --git a/src/storage/storage_healthcheck.cpp b/src/storage/storage_healthcheck.cpp index c88dcf4d0..1baf62f61 100644 --- a/src/storage/storage_healthcheck.cpp +++ b/src/storage/storage_healthcheck.cpp @@ -48,6 +48,7 @@ namespace OpenWifi { bool Storage::AddHealthCheckData(Poco::Data::Session &Sess, const GWObjects::HealthCheck &Check) { try { + Sess.begin(); Poco::Data::Statement Insert(Sess); std::string St{"INSERT INTO HealthChecks ( " + DB_HealthCheckSelectFields + @@ -57,6 +58,7 @@ namespace OpenWifi { ConvertHealthCheckRecord(Check, R); Insert << ConvertParams(St), Poco::Data::Keywords::use(R); Insert.execute(); + Sess.commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), diff --git a/src/storage/storage_statistics.cpp b/src/storage/storage_statistics.cpp index 9833f902a..55720482f 100644 --- a/src/storage/storage_statistics.cpp +++ b/src/storage/storage_statistics.cpp @@ -34,6 +34,7 @@ namespace OpenWifi { bool Storage::AddStatisticsData(Poco::Data::Session &Sess, const GWObjects::Statistics &Stats) { try { + Sess.begin(); Poco::Data::Statement Insert(Sess); poco_trace(Logger(), fmt::format("{}: Adding stats. Size={}", Stats.SerialNumber, @@ -44,6 +45,8 @@ namespace OpenWifi { ConvertStatsRecord(Stats, R); Insert << ConvertParams(St), Poco::Data::Keywords::use(R); Insert.execute(); + Sess.commit(); + return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), From 10c890a196df766b6200eda50ea56c3c2337c179 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 28 Nov 2023 08:19:26 -0800 Subject: [PATCH 006/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/storage/storage_blacklist.cpp | 11 +++--- src/storage/storage_capabilities.cpp | 15 ++------ src/storage/storage_command.cpp | 52 ++++++++++++++++++++-------- src/storage/storage_def_firmware.cpp | 11 +++--- src/storage/storage_defconfig.cpp | 10 ++++-- src/storage/storage_device.cpp | 28 ++++++++++----- src/storage/storage_healthcheck.cpp | 17 +++------ src/storage/storage_logs.cpp | 8 +++-- src/storage/storage_statistics.cpp | 15 ++------ 10 files changed, 93 insertions(+), 76 deletions(-) diff --git a/build b/build index 3f10ffe7a..19c7bdba7 100644 --- a/build +++ b/build @@ -1 +1 @@ -15 \ No newline at end of file +16 \ No newline at end of file diff --git a/src/storage/storage_blacklist.cpp b/src/storage/storage_blacklist.cpp index 9018c1e8d..00662415c 100644 --- a/src/storage/storage_blacklist.cpp +++ b/src/storage/storage_blacklist.cpp @@ -56,7 +56,7 @@ namespace OpenWifi { struct DeviceDetails { std::string reason; std::string author; - std::uint64_t created; + std::uint64_t created=Utils::Now(); }; static std::map BlackListDevices; @@ -93,6 +93,7 @@ namespace OpenWifi { bool Storage::AddBlackListDevice(GWObjects::BlackListedDevice &Device) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Insert(Sess); std::string St{"INSERT INTO BlackList (" + DB_BlackListDeviceSelectFields + ") " + @@ -102,7 +103,7 @@ namespace OpenWifi { ConvertBlackListDeviceRecord(Device, T); Insert << ConvertParams(St), Poco::Data::Keywords::use(T); Insert.execute(); - + Sess.commit(); std::lock_guard G(BlackListMutex); BlackListDevices[Utils::MACToInt(Device.serialNumber)] = DeviceDetails{ .reason = Device.reason, .author = Device.author, .created = Device.created}; @@ -130,6 +131,7 @@ namespace OpenWifi { bool Storage::DeleteBlackListDevice(std::string &SerialNumber) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Delete(Sess); std::string St{"DELETE FROM BlackList WHERE SerialNumber=?"}; @@ -137,7 +139,7 @@ namespace OpenWifi { Poco::toLowerInPlace(SerialNumber); Delete << ConvertParams(St), Poco::Data::Keywords::use(SerialNumber); Delete.execute(); - + Sess.commit(); std::lock_guard G(BlackListMutex); BlackListDevices.erase(Utils::MACToInt(SerialNumber)); return true; @@ -177,6 +179,7 @@ namespace OpenWifi { GWObjects::BlackListedDevice &Device) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Update(Sess); std::string St{"UPDATE BlackList SET " + DB_BlackListDeviceUpdateFields + @@ -187,7 +190,7 @@ namespace OpenWifi { Update << ConvertParams(St), Poco::Data::Keywords::use(T), Poco::Data::Keywords::use(SerialNumber); Update.execute(); - + Sess.commit(); std::lock_guard G(BlackListMutex); BlackListDevices[Utils::MACToInt(Device.serialNumber)] = DeviceDetails{ .reason = Device.reason, .author = Device.author, .created = Device.created}; diff --git a/src/storage/storage_capabilities.cpp b/src/storage/storage_capabilities.cpp index 2070ace43..c0952121a 100644 --- a/src/storage/storage_capabilities.cpp +++ b/src/storage/storage_capabilities.cpp @@ -71,18 +71,6 @@ namespace OpenWifi { return false; } - bool Storage::UpdateDeviceCapabilities(std::string &SerialNumber, - const Config::Capabilities &Caps) { - try { - Poco::Data::Session Sess = Pool_->get(); - return UpdateDeviceCapabilities(Sess, SerialNumber, Caps); - } catch (const Poco::Exception &E) { - poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), - E.displayText())); - } - return false; - } - bool Storage::GetDeviceCapabilities(std::string &SerialNumber, GWObjects::Capabilities &Caps) { try { Poco::Data::Session Sess = Pool_->get(); @@ -113,13 +101,14 @@ namespace OpenWifi { bool Storage::DeleteDeviceCapabilities(std::string &SerialNumber) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Delete(Sess); std::string St{"DELETE FROM Capabilities WHERE SerialNumber=?"}; Delete << ConvertParams(St), Poco::Data::Keywords::use(SerialNumber); Delete.execute(); - + Sess.commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), diff --git a/src/storage/storage_command.cpp b/src/storage/storage_command.cpp index b4664a59a..13d5bcbb0 100644 --- a/src/storage/storage_command.cpp +++ b/src/storage/storage_command.cpp @@ -105,6 +105,7 @@ namespace OpenWifi { bool Storage::RemoveOldCommands(std::string &SerialNumber, std::string &Command) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Delete(Sess); std::string St{ @@ -112,8 +113,7 @@ namespace OpenWifi { Delete << ConvertParams(St), Poco::Data::Keywords::use(SerialNumber), Poco::Data::Keywords::use(Command); Delete.execute(); - Delete.reset(Sess); - + Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -146,6 +146,7 @@ namespace OpenWifi { RemoveOldCommands(SerialNumber, Command.Command); Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Insert(Sess); std::string St{"INSERT INTO CommandList ( " + DB_Command_SelectFields + " ) VALUES( " + @@ -156,7 +157,7 @@ namespace OpenWifi { Insert << ConvertParams(St), Poco::Data::Keywords::use(R); Insert.execute(); - + Sess.commit(); return true; } catch (const Poco::Exception &E) { @@ -215,6 +216,7 @@ namespace OpenWifi { bool Storage::DeleteCommands(std::string &SerialNumber, uint64_t FromDate, uint64_t ToDate) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Delete(Sess); bool DatesIncluded = (FromDate != 0 || ToDate != 0); @@ -237,8 +239,7 @@ namespace OpenWifi { Delete << IntroStatement + DateSelector; Delete.execute(); - Delete.reset(Sess); - + Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -274,7 +275,6 @@ namespace OpenWifi { if (Records.size() < HowMany) Done = true; } - Select.reset(Sess); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -286,6 +286,7 @@ namespace OpenWifi { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Update(Sess); std::string St{"UPDATE CommandList SET Status=?, Executed=?, Completed=?, " @@ -299,7 +300,7 @@ namespace OpenWifi { Poco::Data::Keywords::use(Command.ErrorCode), Poco::Data::Keywords::use(UUID); Update.execute(); - + Sess.commit(); return true; } catch (const Poco::Exception &E) { @@ -311,6 +312,7 @@ namespace OpenWifi { bool Storage::SetCommandExecuted(std::string &CommandUUID) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Update(Sess); auto Now = Utils::Now(); @@ -321,6 +323,7 @@ namespace OpenWifi { Update << ConvertParams(St), Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(Status), Poco::Data::Keywords::use(CommandUUID); Update.execute(); + Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -331,6 +334,7 @@ namespace OpenWifi { void Storage::RemovedExpiredCommands() { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Update(Sess); auto Now = Utils::Now(), Window = Now - CommandManager()->CommandTimeout(); @@ -341,8 +345,7 @@ namespace OpenWifi { Update << ConvertParams(St), Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(Status), Poco::Data::Keywords::use(Window); Update.execute(); - Update.reset(Sess); - + Sess.commit(); } catch (const Poco::Exception &E) { Logger().log(E); } @@ -351,6 +354,7 @@ namespace OpenWifi { bool Storage::SetCommandLastTry(std::string &CommandUUID) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Update(Sess); auto Now = Utils::Now(); @@ -359,6 +363,7 @@ namespace OpenWifi { Update << ConvertParams(St), Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(CommandUUID); Update.execute(); + Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -369,6 +374,7 @@ namespace OpenWifi { void Storage::RemoveTimedOutCommands() { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Update(Sess); auto Now = Utils::Now(), Window = Now - CommandManager()->CommandTimeout(); @@ -377,7 +383,7 @@ namespace OpenWifi { Update << ConvertParams(St), Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(Window); Update.execute(); - Update.reset(Sess); + Sess.commit(); } catch (const Poco::Exception &E) { Logger().log(E); } @@ -386,6 +392,7 @@ namespace OpenWifi { bool Storage::SetCommandTimedOut(std::string &CommandUUID) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Update(Sess); auto Now = Utils::Now(); @@ -395,6 +402,7 @@ namespace OpenWifi { Update << ConvertParams(St), Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(Status), Poco::Data::Keywords::use(CommandUUID); Update.execute(); + Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -425,6 +433,7 @@ namespace OpenWifi { bool Storage::DeleteCommand(std::string &UUID) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Delete(Sess); std::string St{"DELETE FROM CommandList WHERE UUID=?"}; @@ -435,8 +444,7 @@ namespace OpenWifi { St = "DELETE FROM FileUploads WHERE UUID=?"; Delete << ConvertParams(St), Poco::Data::Keywords::use(UUID); Delete.execute(); - Delete.reset(Sess); - + Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -510,6 +518,7 @@ namespace OpenWifi { auto Now = Utils::Now(); Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Update(Sess); std::string St{"UPDATE CommandList SET Executed=? WHERE UUID=?"}; @@ -518,7 +527,7 @@ namespace OpenWifi { Poco::Data::Keywords::use(UUID); Update.execute(); - + Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -555,6 +564,7 @@ namespace OpenWifi { } Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Update(Sess); auto Status = to_string(Storage::CommandExecutionType::COMMAND_COMPLETED); @@ -566,6 +576,7 @@ namespace OpenWifi { Poco::Data::Keywords::use(ResultStr), Poco::Data::Keywords::use(Status), Poco::Data::Keywords::use(tET), Poco::Data::Keywords::use(UUID); Update.execute(); + Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -603,6 +614,7 @@ namespace OpenWifi { bool Storage::CancelWaitFile(std::string &UUID, std::string &ErrorText) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); auto Now = Utils::Now(); uint64_t Size = 0, WaitForFile = 0; @@ -616,6 +628,7 @@ namespace OpenWifi { Poco::Data::Keywords::use(ErrorText), Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(UUID); Update.execute(); + Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -631,6 +644,7 @@ namespace OpenWifi { uint64_t Size = FileContent.str().size(); Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Statement(Sess); std::string StatementStr; @@ -662,10 +676,11 @@ namespace OpenWifi { Poco::Data::Keywords::use(FileType), Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(TheBlob); Insert.execute(); - return true; } else { poco_warning(Logger(), fmt::format("File {} is too large.", UUID)); } + Sess.commit(); + return true; } catch (const Poco::Exception &E) { Logger().log(E); } @@ -712,6 +727,7 @@ namespace OpenWifi { bool Storage::SetCommandResult(std::string &UUID, std::string &Result) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Update(Sess); auto Now = Utils::Now(); @@ -722,6 +738,7 @@ namespace OpenWifi { Poco::Data::Keywords::use(Result), Poco::Data::Keywords::use(Status), Poco::Data::Keywords::use(UUID); Update.execute(); + Sess.commit(); return true; } catch (const Poco::Exception &E) { @@ -733,13 +750,14 @@ namespace OpenWifi { bool Storage::RemoveAttachedFile(std::string &UUID) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Delete(Sess); std::string St{"DELETE FROM FileUploads WHERE UUID=?"}; Delete << ConvertParams(St), Poco::Data::Keywords::use(UUID); Delete.execute(); - + Sess.commit(); return true; } catch (const Poco::Exception &E) { @@ -751,11 +769,13 @@ namespace OpenWifi { bool Storage::RemoveUploadedFilesRecordsOlderThan(uint64_t Date) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Delete(Sess); std::string St1{"delete from FileUploads where Createdget(); + Sess.begin(); Poco::Data::Statement Delete(Sess); std::string St1{"delete from CommandList where Submittedget(); + Sess.begin(); Poco::Data::Statement Delete(Sess); Poco::toLowerInPlace(deviceType); @@ -114,7 +117,7 @@ namespace OpenWifi { Delete << ConvertParams(St), Poco::Data::Keywords::use(deviceType); Delete.execute(); - + Sess.commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), @@ -125,9 +128,9 @@ namespace OpenWifi { bool Storage::UpdateDefaultFirmware(GWObjects::DefaultFirmware &DefFirmware) { try { + uint64_t Now = Utils::Now(); Poco::Data::Session Sess = Pool_->get(); - - uint64_t Now = time(nullptr); + Sess.begin(); Poco::Data::Statement Update(Sess); DefFirmware.LastModified = Now; Poco::toLowerInPlace(DefFirmware.deviceType); @@ -143,7 +146,7 @@ namespace OpenWifi { Poco::Data::Keywords::use(R), Poco::Data::Keywords::use(DefFirmware.deviceType); Update.execute(); - + Sess.commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), diff --git a/src/storage/storage_defconfig.cpp b/src/storage/storage_defconfig.cpp index 7b0f2d398..36f8cc755 100644 --- a/src/storage/storage_defconfig.cpp +++ b/src/storage/storage_defconfig.cpp @@ -72,6 +72,7 @@ namespace OpenWifi { Config::Config Cfg(DefConfig.Configuration); if (Cfg.Valid()) { + Sess.begin(); Poco::Data::Statement Insert(Sess); std::string St{"INSERT INTO DefaultConfigs ( " + DB_DefConfig_SelectFields + @@ -83,6 +84,7 @@ namespace OpenWifi { Convert(DefConfig, R); Insert << ConvertParams(St), Poco::Data::Keywords::use(R); Insert.execute(); + Sess.commit(); return true; } else { poco_warning(Logger(), "Cannot create device: invalid configuration."); @@ -99,13 +101,14 @@ namespace OpenWifi { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Delete(Sess); std::string St{"DELETE FROM DefaultConfigs WHERE Name=?"}; Delete << ConvertParams(St), Poco::Data::Keywords::use(Name); Delete.execute(); - + Sess.commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), @@ -117,9 +120,9 @@ namespace OpenWifi { bool Storage::UpdateDefaultConfiguration(std::string &Name, GWObjects::DefaultConfiguration &DefConfig) { try { + uint64_t Now = Utils::Now(); Poco::Data::Session Sess = Pool_->get(); - - uint64_t Now = time(nullptr); + Sess.begin(); Poco::Data::Statement Update(Sess); DefConfig.LastModified = Now; @@ -132,6 +135,7 @@ namespace OpenWifi { Update << ConvertParams(St), Poco::Data::Keywords::use(R), Poco::Data::Keywords::use(Name); Update.execute(); + Sess.commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), diff --git a/src/storage/storage_device.cpp b/src/storage/storage_device.cpp index db363478d..219ab2925 100644 --- a/src/storage/storage_device.cpp +++ b/src/storage/storage_device.cpp @@ -210,7 +210,7 @@ namespace OpenWifi { return false; } - bool Storage::UpdateDeviceConfiguration(std::string &SerialNumber, std::string &Configuration, +/* bool Storage::UpdateDeviceConfiguration(std::string &SerialNumber, std::string &Configuration, uint64_t &NewUUID) { try { @@ -255,7 +255,7 @@ namespace OpenWifi { } return false; } - +*/ bool Storage::RollbackDeviceConfigurationChange(std::string & SerialNumber) { try { GWObjects::Device D; @@ -268,6 +268,7 @@ namespace OpenWifi { ConfigurationCache().Add(Utils::SerialNumberToInt(SerialNumber), D.UUID); Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Update(Sess); DeviceRecordTuple R; @@ -277,6 +278,7 @@ namespace OpenWifi { Update << ConvertParams(St2), Poco::Data::Keywords::use(R), Poco::Data::Keywords::use(SerialNumber); Update.execute(); + Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -301,6 +303,7 @@ namespace OpenWifi { ConfigurationCache().Add(Utils::SerialNumberToInt(SerialNumber), D.UUID); Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Update(Sess); DeviceRecordTuple R; @@ -310,6 +313,7 @@ namespace OpenWifi { Update << ConvertParams(St2), Poco::Data::Keywords::use(R), Poco::Data::Keywords::use(SerialNumber); Update.execute(); + Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -328,13 +332,12 @@ namespace OpenWifi { return false; } - Poco::Data::Session Sess = Pool_->get(); - Poco::Data::Statement Select(Sess); GWObjects::Device D; if (!GetDevice(SerialNumber, D)) return false; + uint64_t Now = time(nullptr); if(NewUUID==0) { D.pendingUUID = NewUUID = (D.LastConfigurationChange == Now ? Now + 1 : Now); @@ -343,6 +346,8 @@ namespace OpenWifi { } if (Cfg.SetUUID(NewUUID)) { + Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Update(Sess); D.pendingConfiguration = Cfg.get(); @@ -353,6 +358,7 @@ namespace OpenWifi { Update << ConvertParams(St2), Poco::Data::Keywords::use(R), Poco::Data::Keywords::use(SerialNumber); Update.execute(); + Sess.commit(); poco_information(Logger(), fmt::format("DEVICE-PENDING-CONFIGURATION-UPDATED({}): New UUID is {}", SerialNumber, NewUUID)); @@ -554,7 +560,7 @@ namespace OpenWifi { return CreateDevice(Sess, D); } - bool Storage::GetDeviceFWUpdatePolicy(std::string &SerialNumber, std::string &Policy) { +/* bool Storage::GetDeviceFWUpdatePolicy(std::string &SerialNumber, std::string &Policy) { try { Poco::Data::Session Sess = Pool_->get(); Poco::Data::Statement Select(Sess); @@ -569,7 +575,7 @@ namespace OpenWifi { } return false; } - +*/ bool Storage::SetDevicePassword(Poco::Data::Session &Sess, std::string &SerialNumber, std::string &Password) { try { Sess.begin(); @@ -587,7 +593,7 @@ namespace OpenWifi { return false; } - bool Storage::SetConnectInfo(std::string &SerialNumber, std::string &Firmware) { +/* bool Storage::SetConnectInfo(std::string &SerialNumber, std::string &Firmware) { try { Poco::Data::Session Sess = Pool_->get(); Poco::Data::Statement Select(Sess); @@ -616,7 +622,7 @@ namespace OpenWifi { } return false; } - +*/ bool Storage::DeleteDevice(std::string &SerialNumber) { try { std::vector TableNames{"Devices", "Statistics", "CommandList", @@ -625,12 +631,14 @@ namespace OpenWifi { for (const auto &tableName : TableNames) { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Delete(Sess); std::string St = fmt::format("DELETE FROM {} WHERE SerialNumber='{}'", tableName, SerialNumber); try { Delete << St; Delete.execute(); + Sess.commit(); } catch (...) { } } @@ -662,6 +670,7 @@ namespace OpenWifi { try { std::vector SerialNumbers; Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement GetSerialNumbers(Sess); std::string SelectStatement = SimulatedOnly ? @@ -674,6 +683,7 @@ namespace OpenWifi { poco_information(Logger(),fmt::format("BATCH-DEVICE_DELETE: Found {} devices that match the criteria {} to delete.", SerialNumbers.size(), SerialPattern)); DeleteDeviceList(SerialNumbers, Logger()); + Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -685,6 +695,7 @@ namespace OpenWifi { try { std::vector SerialNumbers; Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement GetSerialNumbers(Sess); std::string SelectStatement = SimulatedOnly ? @@ -696,6 +707,7 @@ namespace OpenWifi { poco_information(Logger(),fmt::format("BATCH-DEVICE_DELETE: Found {} devices that match with lastRecordedContact older than {} to delete.", SerialNumbers.size(), OlderContact)); DeleteDeviceList(SerialNumbers, Logger()); + Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); diff --git a/src/storage/storage_healthcheck.cpp b/src/storage/storage_healthcheck.cpp index 1baf62f61..2fc348f89 100644 --- a/src/storage/storage_healthcheck.cpp +++ b/src/storage/storage_healthcheck.cpp @@ -35,17 +35,6 @@ namespace OpenWifi { R.set<4>(H.Recorded); } - bool Storage::AddHealthCheckData(const GWObjects::HealthCheck &Check) { - try { - Poco::Data::Session Sess = Pool_->get(); - return Storage::AddHealthCheckData(Sess, Check); - } catch (const Poco::Exception &E) { - poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), - E.displayText())); - } - return false; - } - bool Storage::AddHealthCheckData(Poco::Data::Session &Sess, const GWObjects::HealthCheck &Check) { try { Sess.begin(); @@ -146,7 +135,7 @@ namespace OpenWifi { uint64_t ToDate) { try { Poco::Data::Session Sess = Pool_->get(); - + Sess.begin(); bool DatesIncluded = (FromDate != 0 || ToDate != 0); std::string Prefix{"DELETE FROM HealthChecks "}; @@ -170,7 +159,7 @@ namespace OpenWifi { Delete << Statement + DateSelector; Delete.execute(); - + Sess.commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), @@ -182,11 +171,13 @@ namespace OpenWifi { bool Storage::RemoveHealthChecksRecordsOlderThan(uint64_t Date) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Delete(Sess); std::string St1{"delete from HealthChecks where recordedget(); - + Sess.begin(); bool DatesIncluded = (FromDate != 0 || ToDate != 0); bool HasWhere = DatesIncluded || !SerialNumber.empty(); @@ -139,7 +141,7 @@ namespace OpenWifi { Delete << StatementStr + DateSelector + TypeSelector; Delete.execute(); - Delete.reset(Sess); + Sess.commit(); return true; } catch (const Poco::Exception &E) { @@ -181,11 +183,13 @@ namespace OpenWifi { bool Storage::RemoveDeviceLogsRecordsOlderThan(uint64_t Date) { try { Poco::Data::Session Sess = Pool_->get(); + Sess.begin(); Poco::Data::Statement Delete(Sess); std::string St1{"delete from DeviceLogs where recordedget(); - + Sess.begin(); bool DatesIncluded = (FromDate != 0 || ToDate != 0); std::string Prefix{"DELETE FROM Statistics "}; @@ -201,7 +190,7 @@ namespace OpenWifi { Poco::Data::Statement Select(Sess); Select << Statement + DateSelector; Select.execute(); - + Sess.commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), (fmt::format("{}: Failed with: {}", std::string(__func__), From 5be884ef9876b1c17d40807eec0b5c752dee9c34 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 28 Nov 2023 13:30:26 -0800 Subject: [PATCH 007/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Server.cpp | 116 ++++++++++++++++++++++++++++++++++++++++--- src/AP_WS_Server.h | 13 +++-- 3 files changed, 119 insertions(+), 12 deletions(-) diff --git a/build b/build index 19c7bdba7..8e2afd342 100644 --- a/build +++ b/build @@ -1 +1 @@ -16 \ No newline at end of file +17 \ No newline at end of file diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index fcc9411d2..be0ed013d 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -160,18 +160,119 @@ namespace OpenWifi { SimulatorEnabled_ = !SimulatorId_.empty(); Utils::SetThreadName(ReactorThread_, "dev:react:head"); +/* GarbageCollectorCallback_ = std::make_unique>( *this, &AP_WS_Server::onGarbageCollecting); Timer_.setStartInterval(10 * 1000); Timer_.setPeriodicInterval(10 * 1000); // every minute Timer_.start(*GarbageCollectorCallback_, MicroServiceTimerPool()); - +*/ Running_ = true; + GarbageCollector_.setName("ws:garbage"); + GarbageCollector_.start(*this); return 0; } - void AP_WS_Server::onGarbageCollecting([[maybe_unused]] Poco::Timer &timer) { - static uint64_t last_log = Utils::Now(), last_zombie_run = 0; + void AP_WS_Server::run() { + uint64_t last_log = Utils::Now(), + last_zombie_run = 0; + + while(Running_) { + if(Poco::Thread::trySleep(30000)) { + break; + } + + { + std::lock_guard SessionLock(SessionMutex_); + if (!GarbageSessions_.empty()) { + GarbageSessions_.clear(); + } + } + + uint64_t total_connected_time = 0, now = Utils::Now(); + + if(now-last_zombie_run > 20) { + poco_information(Logger(), fmt::format("Garbage collecting...")); + std::vector SessionsToRemove; + NumberOfConnectedDevices_ = 0; + NumberOfConnectingDevices_ = 0; + AverageDeviceConnectionTime_ = 0; + last_zombie_run = now; + for(int hashIndex=0;hashIndex<256;hashIndex++) { + std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); + auto hint = SerialNumbers_[hashIndex].begin(); + while (hint != end(SerialNumbers_[hashIndex])) { + if (hint->second.second == nullptr) { + hint = SerialNumbers_[hashIndex].erase(hint); + } else if ((now - hint->second.second->State_.LastContact) > + SessionTimeOut_) { + hint->second.second->EndConnection(false); + poco_information( + Logger(), + fmt::format( + "{}: Session seems idle. Controller disconnecting device.", + hint->second.second->SerialNumber_)); + SessionsToRemove.emplace_back(hint->second.first); + GarbageSessions_.push_back(hint->second.second); + hint = SerialNumbers_[hashIndex].erase(hint); + } else if (hint->second.second->State_.Connected) { + NumberOfConnectedDevices_++; + total_connected_time += (now - hint->second.second->State_.started); + hint++; + } else { + NumberOfConnectingDevices_++; + hint++; + } + } + } + + if(SessionsToRemove.empty()) { + poco_information(Logger(), fmt::format("Removing {} sessions.", SessionsToRemove.size())); + std::lock_guard Lock(SessionMutex_); + for (const auto &Session : SessionsToRemove) { + Sessions_.erase(Session); + } + } + + AverageDeviceConnectionTime_ = + NumberOfConnectedDevices_ > 0 ? total_connected_time / NumberOfConnectedDevices_ + : 0; + + poco_information(Logger(), fmt::format("Garbage collecting done...")); + } else { + std::lock_guard SessionLock(SessionMutex_); + NumberOfConnectedDevices_ = Sessions_.size(); + AverageDeviceConnectionTime_ += 10; + } + + if ((now - last_log) > 120) { + last_log = now; + poco_information(Logger(), + fmt::format("Active AP connections: {} Connecting: {} Average connection time: {} seconds", + NumberOfConnectedDevices_, NumberOfConnectingDevices_, + AverageDeviceConnectionTime_)); + } + + GWWebSocketNotifications::NumberOfConnection_t Notification; + Notification.content.numberOfConnectingDevices = NumberOfConnectingDevices_; + Notification.content.numberOfDevices = NumberOfConnectedDevices_; + Notification.content.averageConnectedTime = AverageDeviceConnectionTime_; + GetTotalDataStatistics(Notification.content.tx,Notification.content.rx); + GWWebSocketNotifications::NumberOfConnections(Notification); + + Poco::JSON::Object KafkaNotification; + Notification.to_json(KafkaNotification); + + Poco::JSON::Object FullEvent; + FullEvent.set("type", "load-update"); + FullEvent.set("timestamp", now); + FullEvent.set("payload", KafkaNotification); + + KafkaManager()->PostMessage(KafkaTopics::DEVICE_EVENT_QUEUE, "system", FullEvent); + } + } + +/* void AP_WS_Server::onGarbageCollecting([[maybe_unused]] Poco::Timer &timer) { auto now = Utils::Now(); { @@ -264,12 +365,15 @@ namespace OpenWifi { KafkaManager()->PostMessage(KafkaTopics::DEVICE_EVENT_QUEUE, "system", FullEvent); } - +*/ void AP_WS_Server::Stop() { poco_information(Logger(), "Stopping..."); Running_ = false; - Timer_.stop(); + GarbageCollector_.wakeUp(); + GarbageCollector_.join(); + + // Timer_.stop(); for (auto &server : WebServers_) { server->stopAll(); @@ -340,7 +444,7 @@ namespace OpenWifi { if (Session == end(Sessions_)) return false; - Garbage_.push_back(Session->second); + GarbageSessions_.push_back(Session->second); auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index 341a2d6dc..685209156 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -63,7 +63,7 @@ namespace OpenWifi { inline static uint64_t id_ = 1; }; - class AP_WS_Server : public SubSystemServer { + class AP_WS_Server : public SubSystemServer, public Poco::Runnable { public: static auto instance() { static auto instance_ = new AP_WS_Server; @@ -85,6 +85,8 @@ namespace OpenWifi { return SerialNumber.substr(0, 6) == "53494d"; } + void run() override; + inline bool IsSimEnabled() const { return SimulatorEnabled_; } inline bool AllowSerialNumberMismatch() const { return AllowSerialNumberMismatch_; } @@ -259,11 +261,12 @@ namespace OpenWifi { std::atomic_uint64_t TX_=0,RX_=0; - std::vector> Garbage_; + std::vector> GarbageSessions_; + +// std::unique_ptr> GarbageCollectorCallback_; +// Poco::Timer Timer_; - std::unique_ptr> GarbageCollectorCallback_; - Poco::Timer Timer_; - Poco::Thread GarbageCollector_; + Poco::Thread GarbageCollector_; AP_WS_Server() noexcept : SubSystemServer("WebSocketServer", "WS-SVR", "ucentral.websocket") {} From 0aba8462774e568261b724355ad18ad654c3e6d7 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 28 Nov 2023 13:40:35 -0800 Subject: [PATCH 008/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index be0ed013d..709191299 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -182,17 +182,20 @@ namespace OpenWifi { break; } + Logger().information(fmt::format("Garbage collecting starting run." )); { std::lock_guard SessionLock(SessionMutex_); if (!GarbageSessions_.empty()) { + Logger().information(fmt::format("Garbage collecting removing {} stale connections.", GarbageSessions_.size())); GarbageSessions_.clear(); + Logger().information(fmt::format("Garbage collecting removed stale connections.")); } } uint64_t total_connected_time = 0, now = Utils::Now(); - if(now-last_zombie_run > 20) { - poco_information(Logger(), fmt::format("Garbage collecting...")); + if(now-last_zombie_run > 30) { + poco_information(Logger(), fmt::format("Garbage collecting zombies...")); std::vector SessionsToRemove; NumberOfConnectedDevices_ = 0; NumberOfConnectingDevices_ = 0; @@ -238,7 +241,7 @@ namespace OpenWifi { NumberOfConnectedDevices_ > 0 ? total_connected_time / NumberOfConnectedDevices_ : 0; - poco_information(Logger(), fmt::format("Garbage collecting done...")); + poco_information(Logger(), fmt::format("Garbage collecting zombies done...")); } else { std::lock_guard SessionLock(SessionMutex_); NumberOfConnectedDevices_ = Sessions_.size(); From ecb6312f0f2f244d541353f2b1a8cc1b8aa65010 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 28 Nov 2023 13:43:48 -0800 Subject: [PATCH 009/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 709191299..cd410c1d0 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -178,7 +178,8 @@ namespace OpenWifi { last_zombie_run = 0; while(Running_) { - if(Poco::Thread::trySleep(30000)) { + + if(!Poco::Thread::trySleep(30000)) { break; } From 4b472fd112d83b3e3029ee47d39e56ea66fe4fa4 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 28 Nov 2023 13:44:06 -0800 Subject: [PATCH 010/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build b/build index 8e2afd342..25bf17fc5 100644 --- a/build +++ b/build @@ -1 +1 @@ -17 \ No newline at end of file +18 \ No newline at end of file From 82897f5b76d2f4ef35e0a7eacadc0b45aa2cd903 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 28 Nov 2023 13:55:49 -0800 Subject: [PATCH 011/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index cd410c1d0..5bf7e47af 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -230,7 +230,7 @@ namespace OpenWifi { } } - if(SessionsToRemove.empty()) { + if(!SessionsToRemove.empty()) { poco_information(Logger(), fmt::format("Removing {} sessions.", SessionsToRemove.size())); std::lock_guard Lock(SessionMutex_); for (const auto &Session : SessionsToRemove) { @@ -273,7 +273,10 @@ namespace OpenWifi { FullEvent.set("payload", KafkaNotification); KafkaManager()->PostMessage(KafkaTopics::DEVICE_EVENT_QUEUE, "system", FullEvent); + Logger().information(fmt::format("Garbage collection run finished run." )); } + + Logger().information(fmt::format("Garbage collector done for the day." )); } /* void AP_WS_Server::onGarbageCollecting([[maybe_unused]] Poco::Timer &timer) { From 8400b8cfcbbcacbe05d3fbca1a88a1cdb96ffbac Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 28 Nov 2023 14:14:29 -0800 Subject: [PATCH 012/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 10 +++++++--- src/AP_WS_Server.h | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 5bf7e47af..35d1573f5 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -185,7 +185,7 @@ namespace OpenWifi { Logger().information(fmt::format("Garbage collecting starting run." )); { - std::lock_guard SessionLock(SessionMutex_); + std::lock_guard SessionLock(GarbageMutex_); if (!GarbageSessions_.empty()) { Logger().information(fmt::format("Garbage collecting removing {} stale connections.", GarbageSessions_.size())); GarbageSessions_.clear(); @@ -217,6 +217,7 @@ namespace OpenWifi { "{}: Session seems idle. Controller disconnecting device.", hint->second.second->SerialNumber_)); SessionsToRemove.emplace_back(hint->second.first); + std::lock_guard GarbageLock(GarbageMutex_); GarbageSessions_.push_back(hint->second.second); hint = SerialNumbers_[hashIndex].erase(hint); } else if (hint->second.second->State_.Connected) { @@ -273,7 +274,7 @@ namespace OpenWifi { FullEvent.set("payload", KafkaNotification); KafkaManager()->PostMessage(KafkaTopics::DEVICE_EVENT_QUEUE, "system", FullEvent); - Logger().information(fmt::format("Garbage collection run finished run." )); + Logger().information(fmt::format("Garbage collection finished run." )); } Logger().information(fmt::format("Garbage collector done for the day." )); @@ -451,7 +452,10 @@ namespace OpenWifi { if (Session == end(Sessions_)) return false; - GarbageSessions_.push_back(Session->second); + { + std::lock_guard Lock(GarbageMutex_); + GarbageSessions_.push_back(Session->second); + } auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index 685209156..1117dd93f 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -232,6 +232,7 @@ namespace OpenWifi { private: mutable std::mutex SessionMutex_; mutable std::mutex StatsMutex_; + mutable std::mutex GarbageMutex_; std::unique_ptr IssuerCert_; std::list> WebServers_; Poco::Net::SocketReactor Reactor_; From 9560f908a6d69a0bd77b7667c7b2bb1266eb1c22 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 28 Nov 2023 14:25:06 -0800 Subject: [PATCH 013/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 35d1573f5..869fb1c6b 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -195,7 +195,7 @@ namespace OpenWifi { uint64_t total_connected_time = 0, now = Utils::Now(); - if(now-last_zombie_run > 30) { + if(now-last_zombie_run > 60) { poco_information(Logger(), fmt::format("Garbage collecting zombies...")); std::vector SessionsToRemove; NumberOfConnectedDevices_ = 0; From a8a33013be6ecb2c72ebe51130ccec6dedfdeb0c Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 28 Nov 2023 14:59:11 -0800 Subject: [PATCH 014/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Server.cpp | 6 ++++-- src/AP_WS_Server.h | 1 - src/RADIUSSessionTracker.cpp | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/build b/build index 25bf17fc5..dec2bf5d6 100644 --- a/build +++ b/build @@ -1 +1 @@ -18 \ No newline at end of file +19 \ No newline at end of file diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 869fb1c6b..804074e59 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -217,8 +217,10 @@ namespace OpenWifi { "{}: Session seems idle. Controller disconnecting device.", hint->second.second->SerialNumber_)); SessionsToRemove.emplace_back(hint->second.first); - std::lock_guard GarbageLock(GarbageMutex_); - GarbageSessions_.push_back(hint->second.second); + { + std::lock_guard GarbageLock(GarbageMutex_); + GarbageSessions_.push_back(hint->second.second); + } hint = SerialNumbers_[hashIndex].erase(hint); } else if (hint->second.second->State_.Connected) { NumberOfConnectedDevices_++; diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index 1117dd93f..59275da35 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -152,7 +152,6 @@ namespace OpenWifi { void SetSessionDetails(uint64_t connection_id, uint64_t SerialNumber); bool EndSession(uint64_t connection_id, uint64_t serial_number); - bool EndSessionUnSafe(uint64_t session_id, uint64_t serial_number); void SetWebSocketTelemetryReporting(uint64_t RPCID, uint64_t SerialNumber, uint64_t Interval, uint64_t Lifetime, const std::vector &TelemetryTypes); diff --git a/src/RADIUSSessionTracker.cpp b/src/RADIUSSessionTracker.cpp index e70208741..2c4fe52f6 100644 --- a/src/RADIUSSessionTracker.cpp +++ b/src/RADIUSSessionTracker.cpp @@ -423,15 +423,15 @@ namespace OpenWifi { } void RADIUSSessionTracker::DisconnectSession(const std::string &SerialNumber) { - poco_information(Logger(),fmt::format("{}: Disconnecting.", SerialNumber)); std::lock_guard Guard(Mutex_); - auto hint = AccountingSessions_.find(SerialNumber); if(hint==end(AccountingSessions_)) { return; } + poco_information(Logger(),fmt::format("{}: Disconnecting.", SerialNumber)); + // we need to go through all sessions and send an accounting stop for(const auto &session:hint->second) { poco_debug(Logger(), fmt::format("Stopping accounting for {}:{}", SerialNumber, session.first )); From a8fc823b9456b220f1b6f38e4cf9a892579fc286 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 28 Nov 2023 21:23:58 -0800 Subject: [PATCH 015/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- CMakeLists.txt | 3 +- build | 2 +- src/AP_WS_Connection.cpp | 2 +- src/AP_WS_Connection.h | 7 +- src/AP_WS_Locked_Session.h | 19 ++++ src/AP_WS_Reactor_Pool.h | 16 +-- src/AP_WS_Server.h | 2 +- src/StorageService.h | 28 ++--- src/storage/storage_capabilities.cpp | 18 ++-- src/storage/storage_command.cpp | 5 +- src/storage/storage_device.cpp | 148 +++++++++++++++------------ src/storage/storage_healthcheck.cpp | 9 +- src/storage/storage_logs.cpp | 9 +- src/storage/storage_statistics.cpp | 10 +- 14 files changed, 163 insertions(+), 115 deletions(-) create mode 100644 src/AP_WS_Locked_Session.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a4b75c95b..70cc7fcf2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -211,7 +211,8 @@ add_executable( owgw src/RegulatoryInfo.cpp src/RegulatoryInfo.h src/RADIUSSessionTracker.cpp src/RADIUSSessionTracker.h src/libs/Scheduler.h src/libs/InterruptableSleep.h src/libs/ctpl_stl.h src/libs/Cron.h - src/GenericScheduler.cpp src/GenericScheduler.h src/framework/default_device_types.h src/AP_WS_Process_rebootLog.cpp src/AP_WS_ConfigAutoUpgrader.cpp src/AP_WS_ConfigAutoUpgrader.h src/RESTAPI/RESTAPI_default_firmwares.cpp src/RESTAPI/RESTAPI_default_firmwares.h src/RESTAPI/RESTAPI_default_firmware.cpp src/RESTAPI/RESTAPI_default_firmware.h src/storage/storage_def_firmware.cpp src/firmware_revision_cache.h src/sdks/sdk_fms.h) + src/GenericScheduler.cpp src/GenericScheduler.h src/framework/default_device_types.h src/AP_WS_Process_rebootLog.cpp src/AP_WS_ConfigAutoUpgrader.cpp src/AP_WS_ConfigAutoUpgrader.h src/RESTAPI/RESTAPI_default_firmwares.cpp src/RESTAPI/RESTAPI_default_firmwares.h src/RESTAPI/RESTAPI_default_firmware.cpp src/RESTAPI/RESTAPI_default_firmware.h src/storage/storage_def_firmware.cpp src/firmware_revision_cache.h src/sdks/sdk_fms.h + src/AP_WS_Locked_Session.h) if(NOT SMALL_BUILD) diff --git a/build b/build index dec2bf5d6..8fdd954df 100644 --- a/build +++ b/build @@ -1 +1 @@ -19 \ No newline at end of file +22 \ No newline at end of file diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 06c89fea3..532e73ddc 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -43,7 +43,7 @@ namespace OpenWifi { AP_WS_Connection::AP_WS_Connection(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response, uint64_t connection_id, Poco::Logger &L, - std::pair R) + std::pair R) : Logger_(L) { Reactor_ = R.first; DbSession_ = R.second; diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index 857891f0a..2767ab57f 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -17,6 +17,7 @@ #include #include "RESTObjects/RESTAPI_GWobjects.h" +#include namespace OpenWifi { @@ -26,7 +27,7 @@ namespace OpenWifi { public: explicit AP_WS_Connection(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response, uint64_t connection_id, - Poco::Logger &L, std::pair R); + Poco::Logger &L, std::pair R); ~AP_WS_Connection(); void EndConnection(bool DeleteSession=true); @@ -165,8 +166,8 @@ namespace OpenWifi { mutable std::mutex ConnectionMutex_; std::mutex TelemetryMutex_; Poco::Logger &Logger_; - Poco::Net::SocketReactor *Reactor_{nullptr}; - Poco::Data::Session *DbSession_{nullptr}; + Poco::Net::SocketReactor *Reactor_{nullptr}; + LockedDbSession *DbSession_{nullptr}; std::unique_ptr WS_; std::string SerialNumber_; uint64_t SerialNumberInt_ = 0; diff --git a/src/AP_WS_Locked_Session.h b/src/AP_WS_Locked_Session.h new file mode 100644 index 000000000..245f30a2c --- /dev/null +++ b/src/AP_WS_Locked_Session.h @@ -0,0 +1,19 @@ +// +// Created by stephane bourque on 2023-11-28. +// + +#pragma once + +#include +#include + +namespace OpenWifi { + + struct LockedDbSession { + std::unique_ptr Session; + std::unique_ptr Mutex; + + inline Poco::Data::Session & operator *() { return *Session; } + }; + +} \ No newline at end of file diff --git a/src/AP_WS_Reactor_Pool.h b/src/AP_WS_Reactor_Pool.h index 640602e2c..8a46dff1d 100644 --- a/src/AP_WS_Reactor_Pool.h +++ b/src/AP_WS_Reactor_Pool.h @@ -12,8 +12,10 @@ #include #include "framework/utils.h" #include +#include namespace OpenWifi { + class AP_WS_ReactorThreadPool { public: explicit AP_WS_ReactorThreadPool(Poco::Logger &Logger) : Logger_(Logger) { @@ -37,7 +39,9 @@ namespace OpenWifi { Utils::SetThreadName(*NewThread, ThreadName.c_str()); Reactors_.emplace_back(std::move(NewReactor)); Threads_.emplace_back(std::move(NewThread)); - DbSessions_.emplace_back(std::make_unique(StorageService()->Pool().get())); + LockedDbSession S{ .Session = std::make_unique(Poco::Data::Session(StorageService()->Pool().get())), + .Mutex = std::make_unique() }; + DbSessions_.emplace_back(std::move(S)); } Logger_.information(fmt::format("WebSocket Processor: {} threads started.", NumberOfThreads_)); } @@ -53,20 +57,20 @@ namespace OpenWifi { DbSessions_.clear(); } - std::pair NextReactor() { + std::pair NextReactor() { std::lock_guard Lock(Mutex_); NextReactor_++; NextReactor_ %= NumberOfThreads_; - return std::make_pair(Reactors_[NextReactor_].get(), DbSessions_[NextReactor_].get()); + return std::make_pair(Reactors_[NextReactor_].get(), &DbSessions_[NextReactor_]); } private: std::mutex Mutex_; uint64_t NumberOfThreads_; uint64_t NextReactor_ = 0; - std::vector> Reactors_; - std::vector> Threads_; - std::vector> DbSessions_; + std::vector> Reactors_; + std::vector> Threads_; + std::vector DbSessions_; Poco::Logger &Logger_; }; diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index 59275da35..aecc50e4a 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -96,7 +96,7 @@ namespace OpenWifi { inline bool UseProvisioning() const { return LookAtProvisioning_; } inline bool UseDefaults() const { return UseDefaultConfig_; } - [[nodiscard]] inline std::pair NextReactor() { + [[nodiscard]] inline std::pair NextReactor() { return Reactor_pool_->NextReactor(); } [[nodiscard]] inline bool Running() const { return Running_; } diff --git a/src/StorageService.h b/src/StorageService.h index fb292e5a9..848f30c53 100644 --- a/src/StorageService.h +++ b/src/StorageService.h @@ -13,6 +13,7 @@ #include "RESTObjects//RESTAPI_GWobjects.h" #include "framework/StorageClass.h" #include "storage/storage_scripts.h" +#include namespace OpenWifi { @@ -90,8 +91,8 @@ namespace OpenWifi { // typedef std::map DeviceCapabilitiesCache; - bool AddLog(Poco::Data::Session &DbSession, const GWObjects::DeviceLog &Log); - bool AddStatisticsData(Poco::Data::Session &DbSession, const GWObjects::Statistics &Stats); + bool AddLog(LockedDbSession &Session, const GWObjects::DeviceLog &Log); + bool AddStatisticsData(LockedDbSession &Session, const GWObjects::Statistics &Stats); bool AddStatisticsData(const GWObjects::Statistics &Stats); bool GetStatisticsData(std::string &SerialNumber, uint64_t FromDate, uint64_t ToDate, uint64_t Offset, uint64_t HowMany, @@ -103,7 +104,7 @@ namespace OpenWifi { std::vector &Stats); bool AddHealthCheckData(const GWObjects::HealthCheck &Check); - bool AddHealthCheckData(Poco::Data::Session &DbSession, const GWObjects::HealthCheck &Check); + bool AddHealthCheckData(LockedDbSession &Session, const GWObjects::HealthCheck &Check); bool GetHealthCheckData(std::string &SerialNumber, uint64_t FromDate, uint64_t ToDate, uint64_t Offset, uint64_t HowMany, std::vector &Checks); @@ -119,14 +120,16 @@ namespace OpenWifi { bool RollbackDeviceConfigurationChange(std::string & SerialNumber); bool CompleteDeviceConfigurationChange(std::string & SerialNumber); - bool CreateDevice(Poco::Data::Session &DbSession, GWObjects::Device &); + bool CreateDevice(LockedDbSession &Session, GWObjects::Device &); bool CreateDevice(GWObjects::Device &); - bool CreateDefaultDevice(Poco::Data::Session &DbSession,std::string &SerialNumber, + bool CreateDefaultDevice(LockedDbSession &Session,std::string &SerialNumber, const Config::Capabilities &Caps, std::string &Firmware, const Poco::Net::IPAddress &IPAddress, bool simulated); + bool CreateDevice(Poco::Data::Session &Sess, GWObjects::Device &DeviceDetails); - bool GetDevice(Poco::Data::Session &DbSession, std::string &SerialNumber, GWObjects::Device &); + bool GetDevice(LockedDbSession &Session, std::string &SerialNumber, GWObjects::Device &); + bool GetDevice(Poco::Data::Session &Session, std::string &SerialNumber, GWObjects::Device &DeviceDetails); bool GetDevice(std::string &SerialNumber, GWObjects::Device &); bool GetDevices(uint64_t From, uint64_t HowMany, std::vector &Devices, const std::string &orderBy = ""); @@ -137,7 +140,8 @@ namespace OpenWifi { bool DeleteDevices(std::uint64_t OlderContact, bool SimulatedOnly); bool UpdateDevice(GWObjects::Device &); - bool UpdateDevice(Poco::Data::Session &Session, GWObjects::Device &); + bool UpdateDevice(LockedDbSession &Session, GWObjects::Device &); + bool UpdateDevice(Poco::Data::Session &Sess, GWObjects::Device &NewDeviceDetails); bool DeviceExists(std::string &SerialNumber); bool SetConnectInfo(std::string &SerialNumber, std::string &Firmware); bool GetDeviceCount(uint64_t &Count); @@ -145,7 +149,7 @@ namespace OpenWifi { std::vector &SerialNumbers, const std::string &orderBy = ""); bool GetDeviceFWUpdatePolicy(std::string &SerialNumber, std::string &Policy); - bool SetDevicePassword(Poco::Data::Session &Session, std::string &SerialNumber, std::string &Password); + bool SetDevicePassword(LockedDbSession &Session, std::string &SerialNumber, std::string &Password); bool UpdateSerialNumberCache(); static void GetDeviceDbFieldList(Types::StringVec &Fields); @@ -154,11 +158,11 @@ namespace OpenWifi { bool UpdateDeviceCapabilities(std::string &SerialNumber, const Config::Capabilities &Capabilities); - bool UpdateDeviceCapabilities(Poco::Data::Session &Session, std::string &SerialNumber, + bool UpdateDeviceCapabilities(LockedDbSession &Session, std::string &SerialNumber, const Config::Capabilities &Capabilities); bool GetDeviceCapabilities(std::string &SerialNumber, GWObjects::Capabilities &); bool DeleteDeviceCapabilities(std::string &SerialNumber); - bool CreateDeviceCapabilities(Poco::Data::Session &Session, std::string &SerialNumber, + bool CreateDeviceCapabilities(LockedDbSession &Session, std::string &SerialNumber, const Config::Capabilities &Capabilities); bool InitCapabilitiesCache(); @@ -230,7 +234,7 @@ namespace OpenWifi { void RemovedExpiredCommands(); void RemoveTimedOutCommands(); - bool RemoveOldCommands(std::string &SerilNumber, std::string &Command); + bool RemoveOldCommands(std::string &SerialNumber, std::string &Command); bool AddBlackListDevices(std::vector &Devices); bool AddBlackListDevice(GWObjects::BlackListedDevice &Device); @@ -253,7 +257,7 @@ namespace OpenWifi { bool RemoveCommandListRecordsOlderThan(uint64_t Date); bool RemoveUploadedFilesRecordsOlderThan(uint64_t Date); - bool SetDeviceLastRecordedContact(Poco::Data::Session &Session, std::string & SeialNumber, std::uint64_t lastRecordedContact); + bool SetDeviceLastRecordedContact(LockedDbSession &Session, std::string & SeialNumber, std::uint64_t lastRecordedContact); int Create_Tables(); int Create_Statistics(); diff --git a/src/storage/storage_capabilities.cpp b/src/storage/storage_capabilities.cpp index c0952121a..1e2b57e65 100644 --- a/src/storage/storage_capabilities.cpp +++ b/src/storage/storage_capabilities.cpp @@ -17,11 +17,12 @@ namespace OpenWifi { - bool Storage::CreateDeviceCapabilities(Poco::Data::Session &Sess, std::string &SerialNumber, + bool Storage::CreateDeviceCapabilities(LockedDbSession &Session, std::string &SerialNumber, const Config::Capabilities &Capabilities) { try { - Sess.begin(); - Poco::Data::Statement UpSert(Sess); + std::lock_guard Guard(*Session.Mutex); + Session.Session->begin(); + Poco::Data::Statement UpSert(*Session.Session); std::string TCaps{Capabilities.AsString()}; uint64_t Now = Utils::Now(); @@ -33,7 +34,7 @@ namespace OpenWifi { Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(TCaps), Poco::Data::Keywords::use(Now); UpSert.execute(); - Sess.commit(); + Session.Session->commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), @@ -42,11 +43,12 @@ namespace OpenWifi { return false; } - bool Storage::UpdateDeviceCapabilities(Poco::Data::Session &Sess, std::string &SerialNumber, + bool Storage::UpdateDeviceCapabilities(LockedDbSession &Session, std::string &SerialNumber, const Config::Capabilities &Caps) { try { - Sess.begin(); - Poco::Data::Statement UpSert(Sess); + std::lock_guard Guard(*Session.Mutex); + Session.Session->begin(); + Poco::Data::Statement UpSert(*Session.Session); uint64_t Now = Utils::Now(); if (!Caps.Compatible().empty() && !Caps.Platform().empty()) @@ -62,7 +64,7 @@ namespace OpenWifi { Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(TCaps), Poco::Data::Keywords::use(Now); UpSert.execute(); - Sess.commit(); + Session.Session->commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), diff --git a/src/storage/storage_command.cpp b/src/storage/storage_command.cpp index 13d5bcbb0..d021f16d1 100644 --- a/src/storage/storage_command.cpp +++ b/src/storage/storage_command.cpp @@ -658,14 +658,14 @@ namespace OpenWifi { Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(Size), Poco::Data::Keywords::use(UUID); Statement.execute(); - + Sess.commit(); if (Size < FileUploader()->MaxSize()) { Poco::Data::BLOB TheBlob; TheBlob.appendRaw((const unsigned char *)FileContent.str().c_str(), FileContent.str().size()); - + Sess.begin(); Poco::Data::Statement Insert(Sess); std::string FileType{Type}; @@ -676,6 +676,7 @@ namespace OpenWifi { Poco::Data::Keywords::use(FileType), Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(TheBlob); Insert.execute(); + Sess.commit(); } else { poco_warning(Logger(), fmt::format("File {} is too large.", UUID)); } diff --git a/src/storage/storage_device.cpp b/src/storage/storage_device.cpp index 219ab2925..c5965bfb8 100644 --- a/src/storage/storage_device.cpp +++ b/src/storage/storage_device.cpp @@ -372,16 +372,17 @@ namespace OpenWifi { return false; } - bool Storage::SetDeviceLastRecordedContact(Poco::Data::Session &Sess, std::string &SerialNumber, std::uint64_t lastRecordedContact) { + bool Storage::SetDeviceLastRecordedContact(LockedDbSession &Session, std::string &SerialNumber, std::uint64_t lastRecordedContact) { try { - Sess.begin(); - Poco::Data::Statement Update(Sess); + std::lock_guard Lock(*Session.Mutex); + Session.Session->begin(); + Poco::Data::Statement Update(*Session.Session); std::string St{"UPDATE Devices SET lastRecordedContact=? WHERE SerialNumber=?"}; Update << ConvertParams(St), Poco::Data::Keywords::use(lastRecordedContact), Poco::Data::Keywords::use(SerialNumber); Update.execute(); - Sess.commit(); + Session.Session->commit(); return true; } catch (const Poco::Exception &E) { @@ -393,48 +394,34 @@ namespace OpenWifi { bool Storage::CreateDevice(Poco::Data::Session &Sess, GWObjects::Device &DeviceDetails) { std::string SerialNumber; try { -// Poco::Data::Statement Select(Sess); - -// std::string St{"SELECT SerialNumber FROM Devices WHERE SerialNumber=?"}; + Config::Config Cfg(DeviceDetails.Configuration); + uint64_t Now = Utils::Now(); -// Select << ConvertParams(St), Poco::Data::Keywords::into(SerialNumber), -// Poco::Data::Keywords::use(DeviceDetails.SerialNumber); -// Select.execute(); - -// if (Select.rowsExtracted() == 0) { - Config::Config Cfg(DeviceDetails.Configuration); - uint64_t Now = Utils::Now(); + DeviceDetails.modified = Utils::Now(); + DeviceDetails.CreationTimestamp = DeviceDetails.LastConfigurationDownload = + DeviceDetails.UUID = DeviceDetails.LastConfigurationChange = Now; - DeviceDetails.modified = Utils::Now(); - DeviceDetails.CreationTimestamp = DeviceDetails.LastConfigurationDownload = - DeviceDetails.UUID = DeviceDetails.LastConfigurationChange = Now; + if (Cfg.Valid() && Cfg.SetUUID(DeviceDetails.UUID)) { - if (Cfg.Valid() && Cfg.SetUUID(DeviceDetails.UUID)) { - - DeviceDetails.Configuration = Cfg.get(); - Sess.begin(); - Poco::Data::Statement Insert(Sess); + DeviceDetails.Configuration = Cfg.get(); + Sess.begin(); + Poco::Data::Statement Insert(Sess); - std::string St2{"INSERT INTO Devices ( " + DB_DeviceSelectFields + " ) " + - DB_DeviceInsertValues + " ON CONFLICT (SerialNumber) DO NOTHING"}; + std::string St2{"INSERT INTO Devices ( " + DB_DeviceSelectFields + " ) " + + DB_DeviceInsertValues + " ON CONFLICT (SerialNumber) DO NOTHING"}; - SetCurrentConfigurationID(DeviceDetails.SerialNumber, DeviceDetails.UUID); - DeviceRecordTuple R; - ConvertDeviceRecord(DeviceDetails, R); - Insert << ConvertParams(St2), Poco::Data::Keywords::use(R); - Insert.execute(); - Sess.commit(); - SetCurrentConfigurationID(DeviceDetails.SerialNumber, DeviceDetails.UUID); - SerialNumberCache()->AddSerialNumber(DeviceDetails.SerialNumber); - return true; - } else { - poco_warning(Logger(), "Cannot create device: invalid configuration."); - return false; - } -// } else { -// poco_warning(Logger(), fmt::format("Device {} already exists.", SerialNumber)); -// return false; -// } + SetCurrentConfigurationID(DeviceDetails.SerialNumber, DeviceDetails.UUID); + DeviceRecordTuple R; + ConvertDeviceRecord(DeviceDetails, R); + Insert << ConvertParams(St2), Poco::Data::Keywords::use(R); + Insert.execute(); + Sess.commit(); + SetCurrentConfigurationID(DeviceDetails.SerialNumber, DeviceDetails.UUID); + SerialNumberCache()->AddSerialNumber(DeviceDetails.SerialNumber); + } else { + poco_warning(Logger(), "Cannot create device: invalid configuration."); + return false; + } return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -442,7 +429,17 @@ namespace OpenWifi { return false; } - bool Storage::CreateDevice(GWObjects::Device &DeviceDetails) { + bool Storage::CreateDevice(LockedDbSession &Session, GWObjects::Device &DeviceDetails) { + try { + std::lock_guard Lock(*Session.Mutex); + return CreateDevice(*Session.Session, DeviceDetails); + } catch (const Poco::Exception &E) { + Logger().log(E); + } + return false; + } + + bool Storage::CreateDevice(GWObjects::Device &DeviceDetails) { try { auto Session = Pool_->get(); return CreateDevice(Session, DeviceDetails); @@ -504,9 +501,7 @@ namespace OpenWifi { return true; } -#define __DBGLOG__ std::cout << __LINE__ << std::endl; - - bool Storage::CreateDefaultDevice(Poco::Data::Session &Sess, std::string &SerialNumber, const Config::Capabilities &Caps, + bool Storage::CreateDefaultDevice(LockedDbSession &Session, std::string &SerialNumber, const Config::Capabilities &Caps, std::string &Firmware, const Poco::Net::IPAddress &IPAddress, bool simulated) { @@ -556,8 +551,9 @@ namespace OpenWifi { D.Notes = SecurityObjects::NoteInfoVec{ SecurityObjects::NoteInfo{(uint64_t)Utils::Now(), "", "Auto-provisioned."}}; - CreateDeviceCapabilities(Sess, SerialNumber, Caps); - return CreateDevice(Sess, D); + CreateDeviceCapabilities(Session, SerialNumber, Caps); + std::lock_guard Lock(*Session.Mutex); + return CreateDevice(*Session.Session, D); } /* bool Storage::GetDeviceFWUpdatePolicy(std::string &SerialNumber, std::string &Policy) { @@ -576,16 +572,18 @@ namespace OpenWifi { return false; } */ - bool Storage::SetDevicePassword(Poco::Data::Session &Sess, std::string &SerialNumber, std::string &Password) { + bool Storage::SetDevicePassword(LockedDbSession &Sess, std::string &SerialNumber, std::string &Password) { try { - Sess.begin(); - Poco::Data::Statement Update(Sess); + std::lock_guard Lock(*Sess.Mutex); + Sess.Session->begin(); + + Poco::Data::Statement Update(*Sess.Session); std::string St{"UPDATE Devices SET DevicePassword=? WHERE SerialNumber=?"}; Update << ConvertParams(St), Poco::Data::Keywords::use(Password), Poco::Data::Keywords::use(SerialNumber); Update.execute(); - Sess.commit(); + Sess.Session->commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -670,7 +668,6 @@ namespace OpenWifi { try { std::vector SerialNumbers; Poco::Data::Session Sess = Pool_->get(); - Sess.begin(); Poco::Data::Statement GetSerialNumbers(Sess); std::string SelectStatement = SimulatedOnly ? @@ -683,7 +680,6 @@ namespace OpenWifi { poco_information(Logger(),fmt::format("BATCH-DEVICE_DELETE: Found {} devices that match the criteria {} to delete.", SerialNumbers.size(), SerialPattern)); DeleteDeviceList(SerialNumbers, Logger()); - Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -695,7 +691,6 @@ namespace OpenWifi { try { std::vector SerialNumbers; Poco::Data::Session Sess = Pool_->get(); - Sess.begin(); Poco::Data::Statement GetSerialNumbers(Sess); std::string SelectStatement = SimulatedOnly ? @@ -707,7 +702,6 @@ namespace OpenWifi { poco_information(Logger(),fmt::format("BATCH-DEVICE_DELETE: Found {} devices that match with lastRecordedContact older than {} to delete.", SerialNumbers.size(), OlderContact)); DeleteDeviceList(SerialNumbers, Logger()); - Sess.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -715,19 +709,9 @@ namespace OpenWifi { return false; } - bool Storage::GetDevice(std::string &SerialNumber, GWObjects::Device &DeviceDetails) { - try { - auto Sess = Pool_->get(); - return GetDevice(Sess, SerialNumber, DeviceDetails); - } catch (const Poco::Exception &E) { - Logger().log(E); - } - return false; - } - - bool Storage::GetDevice(Poco::Data::Session &Sess, std::string &SerialNumber, GWObjects::Device &DeviceDetails) { + bool Storage::GetDevice(Poco::Data::Session &Session, std::string &SerialNumber, GWObjects::Device &DeviceDetails) { try { - Poco::Data::Statement Select(Sess); + Poco::Data::Statement Select(Session); std::string St{"SELECT " + DB_DeviceSelectFields + " FROM Devices WHERE SerialNumber=?"}; @@ -747,6 +731,26 @@ namespace OpenWifi { return false; } + bool Storage::GetDevice(std::string &SerialNumber, GWObjects::Device &DeviceDetails) { + try { + auto Sess = Pool_->get(); + return GetDevice(Sess, SerialNumber, DeviceDetails); + } catch (const Poco::Exception &E) { + Logger().log(E); + } + return false; + } + + bool Storage::GetDevice(LockedDbSession &Session, std::string &SerialNumber, GWObjects::Device &DeviceDetails) { + try { + std::lock_guard Lock(*Session.Mutex); + return GetDevice(*Session.Session, SerialNumber, DeviceDetails); + } catch (const Poco::Exception &E) { + Logger().log(E); + } + return false; + } + bool Storage::DeviceExists(std::string &SerialNumber) { try { Poco::Data::Session Sess = Pool_->get(); @@ -780,6 +784,16 @@ namespace OpenWifi { return false; } + bool Storage::UpdateDevice(LockedDbSession &Session, GWObjects::Device &NewDeviceDetails) { + try { + std::lock_guard Lock(*Session.Mutex); + return UpdateDevice(*Session.Session, NewDeviceDetails); + } catch (const Poco::Exception &E) { + Logger().log(E); + } + return false; + } + bool Storage::UpdateDevice(Poco::Data::Session &Sess, GWObjects::Device &NewDeviceDetails) { try { Sess.begin(); diff --git a/src/storage/storage_healthcheck.cpp b/src/storage/storage_healthcheck.cpp index 2fc348f89..ca487d5a7 100644 --- a/src/storage/storage_healthcheck.cpp +++ b/src/storage/storage_healthcheck.cpp @@ -35,10 +35,11 @@ namespace OpenWifi { R.set<4>(H.Recorded); } - bool Storage::AddHealthCheckData(Poco::Data::Session &Sess, const GWObjects::HealthCheck &Check) { + bool Storage::AddHealthCheckData(LockedDbSession &Session, const GWObjects::HealthCheck &Check) { try { - Sess.begin(); - Poco::Data::Statement Insert(Sess); + std::lock_guard Guard(*Session.Mutex); + Session.Session->begin(); + Poco::Data::Statement Insert(*Session.Session); std::string St{"INSERT INTO HealthChecks ( " + DB_HealthCheckSelectFields + " ) VALUES( " + DB_HealthCheckInsertValues + " )"}; @@ -47,7 +48,7 @@ namespace OpenWifi { ConvertHealthCheckRecord(Check, R); Insert << ConvertParams(St), Poco::Data::Keywords::use(R); Insert.execute(); - Sess.commit(); + Session.Session->commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), diff --git a/src/storage/storage_logs.cpp b/src/storage/storage_logs.cpp index caea14b63..7046c576c 100644 --- a/src/storage/storage_logs.cpp +++ b/src/storage/storage_logs.cpp @@ -39,10 +39,11 @@ namespace OpenWifi { R.set<6>(Log.UUID); } - bool Storage::AddLog(Poco::Data::Session &Sess, const GWObjects::DeviceLog &Log) { + bool Storage::AddLog(LockedDbSession &Session, const GWObjects::DeviceLog &Log) { try { - Sess.begin(); - Poco::Data::Statement Insert(Sess); + std::lock_guard Guard(*Session.Mutex); + Session.Session->begin(); + Poco::Data::Statement Insert(*Session.Session); std::string St{"INSERT INTO DeviceLogs (" + DB_LogsSelectFields + ") values( " + DB_LogsInsertValues + " )"}; @@ -52,7 +53,7 @@ namespace OpenWifi { Insert << ConvertParams(St), Poco::Data::Keywords::use(R); Insert.execute(); - Sess.commit(); + Session.Session->commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), diff --git a/src/storage/storage_statistics.cpp b/src/storage/storage_statistics.cpp index b0a4c8548..48b75bf61 100644 --- a/src/storage/storage_statistics.cpp +++ b/src/storage/storage_statistics.cpp @@ -32,10 +32,11 @@ namespace OpenWifi { R.set<3>(Stats.Recorded); } - bool Storage::AddStatisticsData(Poco::Data::Session &Sess, const GWObjects::Statistics &Stats) { + bool Storage::AddStatisticsData(LockedDbSession &Session, const GWObjects::Statistics &Stats) { try { - Sess.begin(); - Poco::Data::Statement Insert(Sess); + std::lock_guard Guard(*Session.Mutex); + Session.Session->begin(); + Poco::Data::Statement Insert(*Session.Session); poco_trace(Logger(), fmt::format("{}: Adding stats. Size={}", Stats.SerialNumber, std::to_string(Stats.Data.size()))); @@ -45,8 +46,7 @@ namespace OpenWifi { ConvertStatsRecord(Stats, R); Insert << ConvertParams(St), Poco::Data::Keywords::use(R); Insert.execute(); - Sess.commit(); - + Session.Session->commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), From 3619be1832cca635f4003691d7e253f53f98f6db Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 28 Nov 2023 21:57:48 -0800 Subject: [PATCH 016/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Server.cpp | 40 +++++++++++++++++++++++++++------------- src/AP_WS_Server.h | 24 +++++++----------------- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/build b/build index 8fdd954df..b39356075 100644 --- a/build +++ b/build @@ -1 +1 @@ -22 \ No newline at end of file +23 \ No newline at end of file diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 804074e59..6e3bf944b 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -235,9 +235,9 @@ namespace OpenWifi { if(!SessionsToRemove.empty()) { poco_information(Logger(), fmt::format("Removing {} sessions.", SessionsToRemove.size())); - std::lock_guard Lock(SessionMutex_); for (const auto &Session : SessionsToRemove) { - Sessions_.erase(Session); + std::lock_guard Lock(SessionMutex_[Session % 256]); + Sessions_[Session % 256].erase(Session); } } @@ -247,8 +247,8 @@ namespace OpenWifi { poco_information(Logger(), fmt::format("Garbage collecting zombies done...")); } else { - std::lock_guard SessionLock(SessionMutex_); - NumberOfConnectedDevices_ = Sessions_.size(); + // std::lock_guard SessionLock(SessionMutex_); + // NumberOfConnectedDevices_ = Sessions_.size(); AverageDeviceConnectionTime_ += 10; } @@ -394,6 +394,20 @@ namespace OpenWifi { poco_information(Logger(), "Stopped..."); } + bool AP_WS_Server::GetHealthDevices(std::uint64_t lowLimit, std::uint64_t highLimit, std::vector & SerialNumbers) { + SerialNumbers.clear(); + for(int i=0;i<256;i++) { + std::lock_guard Lock(SessionMutex_[i]); + for (const auto &connection : Sessions_[i]) { + if (connection.second->RawLastHealthcheck_.Sanity >= lowLimit && + connection.second->RawLastHealthcheck_.Sanity <= highLimit) { + SerialNumbers.push_back(connection.second->SerialNumber_); + } + } + } + return true; + } + bool AP_WS_Server::GetStatistics(uint64_t SerialNumber, std::string &Statistics) const { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); @@ -433,9 +447,9 @@ namespace OpenWifi { } void AP_WS_Server::SetSessionDetails(uint64_t connection_id, uint64_t SerialNumber) { - std::lock_guard SessionLock(SessionMutex_); - auto Conn = Sessions_.find(connection_id); - if (Conn == end(Sessions_)) + std::lock_guard SessionLock(SessionMutex_[connection_id % 256]); + auto Conn = Sessions_[connection_id %256].find(connection_id); + if (Conn == end(Sessions_[connection_id % 256])) return; auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); @@ -449,9 +463,9 @@ namespace OpenWifi { } bool AP_WS_Server::EndSession(uint64_t session_id, uint64_t SerialNumber) { - std::lock_guard SessionLock(SessionMutex_); - auto Session = Sessions_.find(session_id); - if (Session == end(Sessions_)) + std::lock_guard SessionLock(SessionMutex_[session_id % 256]); + auto Session = Sessions_[session_id % 256].find(session_id); + if (Session == end(Sessions_[session_id % 256])) return false; { @@ -463,17 +477,17 @@ namespace OpenWifi { std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); if (Device == end(SerialNumbers_[hashIndex])) { - Sessions_.erase(Session); + Sessions_[session_id % 256].erase(Session); return false; } if (Device->second.first == session_id) { - Sessions_.erase(Session); + Sessions_[session_id % 256].erase(Session); SerialNumbers_[hashIndex].erase(Device); return true; } - Sessions_.erase(Session); + Sessions_[session_id % 256].erase(Session); return false; } diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index aecc50e4a..a28c26cf7 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -103,8 +103,8 @@ namespace OpenWifi { inline void AddConnection(uint64_t session_id, std::shared_ptr Connection) { - std::lock_guard Lock(SessionMutex_); - Sessions_[session_id] = std::move(Connection); + std::lock_guard Lock(SessionMutex_[session_id % 256]); + Sessions_[session_id % 256][session_id] = std::move(Connection); } inline bool DeviceRequiresSecureRtty(uint64_t serialNumber) const { @@ -193,17 +193,7 @@ namespace OpenWifi { RX = RX_; } - // TOD: move to hash based map. - inline bool GetHealthDevices(std::uint64_t lowLimit, std::uint64_t highLimit, std::vector & SerialNumbers) { - std::lock_guard Lock(SessionMutex_); - for(const auto &connection:Sessions_) { - if( connection.second->RawLastHealthcheck_.Sanity>=lowLimit && - connection.second->RawLastHealthcheck_.Sanity<=highLimit) { - SerialNumbers.push_back(connection.second->SerialNumber_); - } - } - return true; - } + bool GetHealthDevices(std::uint64_t lowLimit, std::uint64_t highLimit, std::vector & SerialNumbers); inline bool ExtendedAttributes(const std::string &serialNumber, bool & hasGPS, @@ -229,9 +219,9 @@ namespace OpenWifi { } private: - mutable std::mutex SessionMutex_; - mutable std::mutex StatsMutex_; - mutable std::mutex GarbageMutex_; + mutable std::array SessionMutex_; + mutable std::mutex StatsMutex_; + mutable std::mutex GarbageMutex_; std::unique_ptr IssuerCert_; std::list> WebServers_; Poco::Net::SocketReactor Reactor_; @@ -243,7 +233,7 @@ namespace OpenWifi { bool SimulatorEnabled_ = false; std::unique_ptr Reactor_pool_; std::atomic_bool Running_ = false; - std::map> Sessions_; + std::array>,256> Sessions_; using SerialNumberMap = std::map>>; From 81721b4a61d0d4ae960bc2cad4a2bbe51aa8b777 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 28 Nov 2023 22:01:32 -0800 Subject: [PATCH 017/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 6e3bf944b..9b120acd4 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -211,7 +211,7 @@ namespace OpenWifi { } else if ((now - hint->second.second->State_.LastContact) > SessionTimeOut_) { hint->second.second->EndConnection(false); - poco_information( + poco_debug( Logger(), fmt::format( "{}: Session seems idle. Controller disconnecting device.", From ff53d4ba2edb8394e9d7ae7c77d08f108f79e3b7 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 28 Nov 2023 22:44:29 -0800 Subject: [PATCH 018/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/StorageService.h | 2 +- src/storage/storage_capabilities.cpp | 9 ++++----- src/storage/storage_device.cpp | 6 +++--- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/build b/build index b39356075..cabf43b5d 100644 --- a/build +++ b/build @@ -1 +1 @@ -23 \ No newline at end of file +24 \ No newline at end of file diff --git a/src/StorageService.h b/src/StorageService.h index 848f30c53..bb74241f8 100644 --- a/src/StorageService.h +++ b/src/StorageService.h @@ -162,7 +162,7 @@ namespace OpenWifi { const Config::Capabilities &Capabilities); bool GetDeviceCapabilities(std::string &SerialNumber, GWObjects::Capabilities &); bool DeleteDeviceCapabilities(std::string &SerialNumber); - bool CreateDeviceCapabilities(LockedDbSession &Session, std::string &SerialNumber, + bool CreateDeviceCapabilities(Poco::Data::Session &Session, std::string &SerialNumber, const Config::Capabilities &Capabilities); bool InitCapabilitiesCache(); diff --git a/src/storage/storage_capabilities.cpp b/src/storage/storage_capabilities.cpp index 1e2b57e65..82011e1ab 100644 --- a/src/storage/storage_capabilities.cpp +++ b/src/storage/storage_capabilities.cpp @@ -17,12 +17,11 @@ namespace OpenWifi { - bool Storage::CreateDeviceCapabilities(LockedDbSession &Session, std::string &SerialNumber, + bool Storage::CreateDeviceCapabilities(Poco::Data::Session &Session, std::string &SerialNumber, const Config::Capabilities &Capabilities) { try { - std::lock_guard Guard(*Session.Mutex); - Session.Session->begin(); - Poco::Data::Statement UpSert(*Session.Session); + Session.begin(); + Poco::Data::Statement UpSert(Session); std::string TCaps{Capabilities.AsString()}; uint64_t Now = Utils::Now(); @@ -34,7 +33,7 @@ namespace OpenWifi { Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(TCaps), Poco::Data::Keywords::use(Now); UpSert.execute(); - Session.Session->commit(); + Session.commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), diff --git a/src/storage/storage_device.cpp b/src/storage/storage_device.cpp index c5965bfb8..0c1c2649c 100644 --- a/src/storage/storage_device.cpp +++ b/src/storage/storage_device.cpp @@ -508,7 +508,7 @@ namespace OpenWifi { GWObjects::Device D; poco_information(Logger(), fmt::format("AUTO-CREATION({})", SerialNumber)); - uint64_t Now = time(nullptr); + uint64_t Now = Utils::Now(); GWObjects::DefaultConfiguration DefConfig; if (!Caps.Platform().empty() && !Caps.Compatible().empty()) { @@ -551,8 +551,8 @@ namespace OpenWifi { D.Notes = SecurityObjects::NoteInfoVec{ SecurityObjects::NoteInfo{(uint64_t)Utils::Now(), "", "Auto-provisioned."}}; - CreateDeviceCapabilities(Session, SerialNumber, Caps); - std::lock_guard Lock(*Session.Mutex); + std::lock_guard Lock(*Session.Mutex); + CreateDeviceCapabilities(*Session.Session, SerialNumber, Caps); return CreateDevice(*Session.Session, D); } From 72173ed4b5dc275d40b9c8cdc1a5b140d50f7da8 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 28 Nov 2023 23:11:14 -0800 Subject: [PATCH 019/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Process_connect.cpp | 16 +--------------- src/AP_WS_Server.cpp | 7 +++++-- src/storage/storage_device.cpp | 2 +- 4 files changed, 8 insertions(+), 19 deletions(-) diff --git a/build b/build index cabf43b5d..410b14d2c 100644 --- a/build +++ b/build @@ -1 +1 @@ -24 \ No newline at end of file +25 \ No newline at end of file diff --git a/src/AP_WS_Process_connect.cpp b/src/AP_WS_Process_connect.cpp index 1d44b5f86..ff133e132 100644 --- a/src/AP_WS_Process_connect.cpp +++ b/src/AP_WS_Process_connect.cpp @@ -110,26 +110,12 @@ namespace OpenWifi { State_.locale = FindCountryFromIP()->Get(IP); GWObjects::Device DeviceInfo; - auto DeviceExists = StorageService()->GetDevice(*DbSession_,SerialNumber_, DeviceInfo); + auto DeviceExists = StorageService()->GetDevice(SerialNumber_, DeviceInfo); if (Daemon()->AutoProvisioning() && !DeviceExists) { // check the firmware version. if this is too old, we cannot let that device connect yet, we must // force a firmware upgrade GWObjects::DefaultFirmware MinimumFirmware; if(FirmwareRevisionCache()->DeviceMustUpgrade(Compatible_, Firmware, MinimumFirmware)) { -/* - - { "jsonrpc" : "2.0" , - "method" : "upgrade" , - "params" : { - "serial" : , - "when" : Optional - , - "uri" : , - "FWsignature" : (optional) - }, - "id" : - } - - */ Poco::JSON::Object UpgradeCommand, Params; UpgradeCommand.set(uCentralProtocol::JSONRPC,uCentralProtocol::JSONRPC_VERSION); UpgradeCommand.set(uCentralProtocol::METHOD,uCentralProtocol::UPGRADE); diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 9b120acd4..b8009afe8 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -247,8 +247,11 @@ namespace OpenWifi { poco_information(Logger(), fmt::format("Garbage collecting zombies done...")); } else { - // std::lock_guard SessionLock(SessionMutex_); - // NumberOfConnectedDevices_ = Sessions_.size(); + NumberOfConnectedDevices_=0; + for(int i=0;i<256;i++) { + std::lock_guard Lock(SerialNumbersMutex_[i]); + NumberOfConnectedDevices_ += SerialNumbers_[i].size(); + } AverageDeviceConnectionTime_ += 10; } diff --git a/src/storage/storage_device.cpp b/src/storage/storage_device.cpp index 0c1c2649c..18a517542 100644 --- a/src/storage/storage_device.cpp +++ b/src/storage/storage_device.cpp @@ -507,6 +507,7 @@ namespace OpenWifi { bool simulated) { GWObjects::Device D; + poco_information(Logger(), fmt::format("AUTO-CREATION({})", SerialNumber)); uint64_t Now = Utils::Now(); GWObjects::DefaultConfiguration DefConfig; @@ -712,7 +713,6 @@ namespace OpenWifi { bool Storage::GetDevice(Poco::Data::Session &Session, std::string &SerialNumber, GWObjects::Device &DeviceDetails) { try { Poco::Data::Statement Select(Session); - std::string St{"SELECT " + DB_DeviceSelectFields + " FROM Devices WHERE SerialNumber=?"}; From b072f1e2ab01a531d9951deed473879d91ce5658 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 29 Nov 2023 07:46:06 -0800 Subject: [PATCH 020/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/storage/storage_device.cpp | 36 ++++------------------------------ 1 file changed, 4 insertions(+), 32 deletions(-) diff --git a/src/storage/storage_device.cpp b/src/storage/storage_device.cpp index 18a517542..7d9ce3560 100644 --- a/src/storage/storage_device.cpp +++ b/src/storage/storage_device.cpp @@ -508,7 +508,7 @@ namespace OpenWifi { GWObjects::Device D; - poco_information(Logger(), fmt::format("AUTO-CREATION({})", SerialNumber)); + poco_information(Logger(), fmt::format("AUTO-CREATION({}): Start.", SerialNumber)); uint64_t Now = Utils::Now(); GWObjects::DefaultConfiguration DefConfig; @@ -554,7 +554,9 @@ namespace OpenWifi { std::lock_guard Lock(*Session.Mutex); CreateDeviceCapabilities(*Session.Session, SerialNumber, Caps); - return CreateDevice(*Session.Session, D); + auto Result = CreateDevice(*Session.Session, D); + poco_information(Logger(), fmt::format("AUTO-CREATION({}): Done, Result={}", SerialNumber, Result)); + return Result; } /* bool Storage::GetDeviceFWUpdatePolicy(std::string &SerialNumber, std::string &Policy) { @@ -592,36 +594,6 @@ namespace OpenWifi { return false; } -/* bool Storage::SetConnectInfo(std::string &SerialNumber, std::string &Firmware) { - try { - Poco::Data::Session Sess = Pool_->get(); - Poco::Data::Statement Select(Sess); - - // Get the old version and if they do not match, set the last date - std::string St{"SELECT Firmware FROM Devices WHERE SerialNumber=?"}; - std::string TmpFirmware; - Select << ConvertParams(St), Poco::Data::Keywords::into(TmpFirmware), - Poco::Data::Keywords::use(SerialNumber); - Select.execute(); - - if (TmpFirmware != Firmware) { - Poco::Data::Statement Update(Sess); - std::string St2{ - "UPDATE Devices SET Firmware=?, LastFWUpdate=? WHERE SerialNumber=?"}; - uint64_t Now = Utils::Now(); - - Update << ConvertParams(St2), Poco::Data::Keywords::use(Firmware), - Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(SerialNumber); - Update.execute(); - return true; - } - return true; - } catch (const Poco::Exception &E) { - Logger().log(E); - } - return false; - } -*/ bool Storage::DeleteDevice(std::string &SerialNumber) { try { std::vector TableNames{"Devices", "Statistics", "CommandList", From 3560871f44430ca86441a0c591e1859c0ab86caa Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 29 Nov 2023 08:21:09 -0800 Subject: [PATCH 021/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Process_connect.cpp | 10 ++++++---- src/AP_WS_Server.cpp | 14 +++++++++----- src/StorageService.h | 4 ++-- src/storage/storage_capabilities.cpp | 9 ++++----- src/storage/storage_device.cpp | 7 +++---- 6 files changed, 25 insertions(+), 21 deletions(-) diff --git a/build b/build index 410b14d2c..978b4e8e5 100644 --- a/build +++ b/build @@ -1 +1 @@ -25 \ No newline at end of file +26 \ No newline at end of file diff --git a/src/AP_WS_Process_connect.cpp b/src/AP_WS_Process_connect.cpp index ff133e132..b67c07675 100644 --- a/src/AP_WS_Process_connect.cpp +++ b/src/AP_WS_Process_connect.cpp @@ -73,7 +73,9 @@ namespace OpenWifi { AP_WS_Server()->SetSessionDetails(State_.sessionId, SerialNumberInt_); + std::lock_guard DbSessionLock(*DbSession_->Mutex); std::lock_guard Lock(ConnectionMutex_); + Config::Capabilities Caps(Capabilities); Compatible_ = Caps.Compatible(); @@ -110,7 +112,7 @@ namespace OpenWifi { State_.locale = FindCountryFromIP()->Get(IP); GWObjects::Device DeviceInfo; - auto DeviceExists = StorageService()->GetDevice(SerialNumber_, DeviceInfo); + auto DeviceExists = StorageService()->GetDevice(*DbSession_->Session, SerialNumber_, DeviceInfo); if (Daemon()->AutoProvisioning() && !DeviceExists) { // check the firmware version. if this is too old, we cannot let that device connect yet, we must // force a firmware upgrade @@ -143,7 +145,7 @@ namespace OpenWifi { } return; } else { - StorageService()->CreateDefaultDevice( *DbSession_, + StorageService()->CreateDefaultDevice( *DbSession_->Session, SerialNumber_, Caps, Firmware, PeerAddress_, State_.VerifiedCertificate == GWObjects::SIMULATED); } @@ -152,7 +154,7 @@ namespace OpenWifi { poco_warning(Logger(),fmt::format("Device {} is a {} from {} and cannot be provisioned.",SerialNumber_,Compatible_, CId_)); return EndConnection(); } else if (DeviceExists) { - StorageService()->UpdateDeviceCapabilities(*DbSession_, SerialNumber_, Caps); + StorageService()->UpdateDeviceCapabilities(*DbSession_->Session, SerialNumber_, Caps); int Updated{0}; if (!Firmware.empty()) { if (Firmware != DeviceInfo.Firmware) { @@ -224,7 +226,7 @@ namespace OpenWifi { } if (Updated) { - StorageService()->UpdateDevice(*DbSession_, DeviceInfo); + StorageService()->UpdateDevice(*DbSession_->Session, DeviceInfo); } if(!Simulated_) { diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index b8009afe8..d90bac5ce 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -450,17 +450,21 @@ namespace OpenWifi { } void AP_WS_Server::SetSessionDetails(uint64_t connection_id, uint64_t SerialNumber) { - std::lock_guard SessionLock(SessionMutex_[connection_id % 256]); - auto Conn = Sessions_[connection_id %256].find(connection_id); - if (Conn == end(Sessions_[connection_id % 256])) - return; + std::shared_ptr Connection; + { + std::lock_guard SessionLock(SessionMutex_[connection_id % 256]); + auto Conn = Sessions_[connection_id % 256].find(connection_id); + if (Conn == end(Sessions_[connection_id % 256])) + return; + Connection = Conn->second; + } auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto CurrentSerialNumber = SerialNumbers_[hashIndex].find(SerialNumber); if ((CurrentSerialNumber == SerialNumbers_[hashIndex].end()) || (CurrentSerialNumber->second.first < connection_id)) { - SerialNumbers_[hashIndex][SerialNumber] = std::make_pair(connection_id, Conn->second); + SerialNumbers_[hashIndex][SerialNumber] = std::make_pair(connection_id, Connection); return; } } diff --git a/src/StorageService.h b/src/StorageService.h index bb74241f8..857d7acd7 100644 --- a/src/StorageService.h +++ b/src/StorageService.h @@ -122,7 +122,7 @@ namespace OpenWifi { bool CreateDevice(LockedDbSession &Session, GWObjects::Device &); bool CreateDevice(GWObjects::Device &); - bool CreateDefaultDevice(LockedDbSession &Session,std::string &SerialNumber, + bool CreateDefaultDevice(Poco::Data::Session &Session,std::string &SerialNumber, const Config::Capabilities &Caps, std::string &Firmware, const Poco::Net::IPAddress &IPAddress, bool simulated); @@ -158,7 +158,7 @@ namespace OpenWifi { bool UpdateDeviceCapabilities(std::string &SerialNumber, const Config::Capabilities &Capabilities); - bool UpdateDeviceCapabilities(LockedDbSession &Session, std::string &SerialNumber, + bool UpdateDeviceCapabilities(Poco::Data::Session &Session, std::string &SerialNumber, const Config::Capabilities &Capabilities); bool GetDeviceCapabilities(std::string &SerialNumber, GWObjects::Capabilities &); bool DeleteDeviceCapabilities(std::string &SerialNumber); diff --git a/src/storage/storage_capabilities.cpp b/src/storage/storage_capabilities.cpp index 82011e1ab..6ae963305 100644 --- a/src/storage/storage_capabilities.cpp +++ b/src/storage/storage_capabilities.cpp @@ -42,12 +42,11 @@ namespace OpenWifi { return false; } - bool Storage::UpdateDeviceCapabilities(LockedDbSession &Session, std::string &SerialNumber, + bool Storage::UpdateDeviceCapabilities(Poco::Data::Session &Session, std::string &SerialNumber, const Config::Capabilities &Caps) { try { - std::lock_guard Guard(*Session.Mutex); - Session.Session->begin(); - Poco::Data::Statement UpSert(*Session.Session); + Session.begin(); + Poco::Data::Statement UpSert(Session); uint64_t Now = Utils::Now(); if (!Caps.Compatible().empty() && !Caps.Platform().empty()) @@ -63,7 +62,7 @@ namespace OpenWifi { Poco::Data::Keywords::use(Now), Poco::Data::Keywords::use(TCaps), Poco::Data::Keywords::use(Now); UpSert.execute(); - Session.Session->commit(); + Session.commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), diff --git a/src/storage/storage_device.cpp b/src/storage/storage_device.cpp index 7d9ce3560..b482df504 100644 --- a/src/storage/storage_device.cpp +++ b/src/storage/storage_device.cpp @@ -501,7 +501,7 @@ namespace OpenWifi { return true; } - bool Storage::CreateDefaultDevice(LockedDbSession &Session, std::string &SerialNumber, const Config::Capabilities &Caps, + bool Storage::CreateDefaultDevice(Poco::Data::Session &Session, std::string &SerialNumber, const Config::Capabilities &Caps, std::string &Firmware, const Poco::Net::IPAddress &IPAddress, bool simulated) { @@ -552,9 +552,8 @@ namespace OpenWifi { D.Notes = SecurityObjects::NoteInfoVec{ SecurityObjects::NoteInfo{(uint64_t)Utils::Now(), "", "Auto-provisioned."}}; - std::lock_guard Lock(*Session.Mutex); - CreateDeviceCapabilities(*Session.Session, SerialNumber, Caps); - auto Result = CreateDevice(*Session.Session, D); + CreateDeviceCapabilities(Session, SerialNumber, Caps); + auto Result = CreateDevice(Session, D); poco_information(Logger(), fmt::format("AUTO-CREATION({}): Done, Result={}", SerialNumber, Result)); return Result; } From 84281ec58e0a2478e921fdae8342403ffbc36437 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 29 Nov 2023 08:54:39 -0800 Subject: [PATCH 022/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index d90bac5ce..702f88967 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -210,12 +210,12 @@ namespace OpenWifi { hint = SerialNumbers_[hashIndex].erase(hint); } else if ((now - hint->second.second->State_.LastContact) > SessionTimeOut_) { - hint->second.second->EndConnection(false); - poco_debug( + poco_information( Logger(), fmt::format( "{}: Session seems idle. Controller disconnecting device.", hint->second.second->SerialNumber_)); + hint->second.second->EndConnection(false); SessionsToRemove.emplace_back(hint->second.first); { std::lock_guard GarbageLock(GarbageMutex_); From 2e361a41d72cda9411434b6b3fbded0edca0cd6b Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 29 Nov 2023 09:09:10 -0800 Subject: [PATCH 023/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 702f88967..8349687fd 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -175,7 +175,8 @@ namespace OpenWifi { void AP_WS_Server::run() { uint64_t last_log = Utils::Now(), - last_zombie_run = 0; + last_zombie_run = 0, + last_garbage_run = 0 while(Running_) { @@ -252,7 +253,8 @@ namespace OpenWifi { std::lock_guard Lock(SerialNumbersMutex_[i]); NumberOfConnectedDevices_ += SerialNumbers_[i].size(); } - AverageDeviceConnectionTime_ += 10; + if(last_garbage_run>0) + AverageDeviceConnectionTime_ += (now - last_garbage_run); } if ((now - last_log) > 120) { @@ -280,6 +282,7 @@ namespace OpenWifi { KafkaManager()->PostMessage(KafkaTopics::DEVICE_EVENT_QUEUE, "system", FullEvent); Logger().information(fmt::format("Garbage collection finished run." )); + last_garbage_run = now; } Logger().information(fmt::format("Garbage collector done for the day." )); From ef7cb883fb5ec8b55f94933b7c8efc39265fb72e Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 29 Nov 2023 09:10:38 -0800 Subject: [PATCH 024/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 8349687fd..d0872743c 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -176,7 +176,7 @@ namespace OpenWifi { void AP_WS_Server::run() { uint64_t last_log = Utils::Now(), last_zombie_run = 0, - last_garbage_run = 0 + last_garbage_run = 0; while(Running_) { From 135a1950817a553524cd9579e0354e1fcd80184f Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 29 Nov 2023 10:58:22 -0800 Subject: [PATCH 025/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Connection.cpp | 10 +++-- src/AP_WS_Connection.h | 76 +++++++++++++++++------------------ src/AP_WS_Process_connect.cpp | 1 - src/AP_WS_Server.cpp | 2 +- 5 files changed, 45 insertions(+), 46 deletions(-) diff --git a/build b/build index 978b4e8e5..a5c750fea 100644 --- a/build +++ b/build @@ -1 +1 @@ -26 \ No newline at end of file +27 \ No newline at end of file diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 532e73ddc..bb2e43294 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -80,7 +80,6 @@ namespace OpenWifi { if (!Valid_) return false; - std::lock_guard Lock(ConnectionMutex_); try { auto SockImpl = dynamic_cast(WS_->impl()); auto SS = @@ -243,8 +242,10 @@ namespace OpenWifi { } AP_WS_Connection::~AP_WS_Connection() { - Valid_ = false; - EndConnection(); + if(Valid_) { + Valid_ = false; + EndConnection(); + } } void DeviceDisconnectionCleanup(const std::string &SerialNumber, std::uint64_t uuid) { @@ -273,6 +274,7 @@ namespace OpenWifi { Reactor_->removeEventHandler( *WS_, Poco::NObserver( *this, &AP_WS_Connection::OnSocketError)); + Registered_=false; } WS_->close(); @@ -667,6 +669,8 @@ namespace OpenWifi { if (!AP_WS_Server()->Running()) return EndConnection(); + std::lock_guard DeviceLock(ConnectionMutex_); + if (!ValidatedDevice()) return; diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index 2767ab57f..05df503cc 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -46,9 +46,6 @@ namespace OpenWifi { void OnSocketShutdown(const Poco::AutoPtr &pNf); void OnSocketError(const Poco::AutoPtr &pNf); bool LookForUpgrade(uint64_t UUID, uint64_t &UpgradedUUID); - static bool ExtractBase64CompressedData(const std::string &CompressedData, - std::string &UnCompressedData, - uint64_t compress_sz); void LogException(const Poco::Exception &E); inline Poco::Logger &Logger() { return Logger_; } bool SetWebSocketTelemetryReporting(uint64_t RPCID, uint64_t interval, @@ -65,43 +62,6 @@ namespace OpenWifi { LastStats = RawLastStats_; } - inline void SetLastStats(const std::string &LastStats) { - std::lock_guard G(ConnectionMutex_); - RawLastStats_ = LastStats; - try { - Poco::JSON::Parser P; - auto Stats = P.parse(LastStats).extract(); - hasGPS = Stats->isObject("gps"); - auto Unit = Stats->getObject("unit"); - auto Memory = Unit->getObject("memory"); - std::uint64_t TotalMemory = Memory->get("total"); - std::uint64_t FreeMemory = Memory->get("free"); - if(TotalMemory>0) { - memory_used_ = - (100.0 * ((double)TotalMemory - (double)FreeMemory)) / (double)TotalMemory; - } - if(Unit->isArray("load")) { - Poco::JSON::Array::Ptr Load = Unit->getArray("load"); - if(Load->size()>1) { - cpu_load_ = Load->get(1); - } - } - if(Unit->isArray("temperature")) { - Poco::JSON::Array::Ptr Temperature = Unit->getArray("temperature"); - if(Temperature->size()>1) { - temperature_ = Temperature->get(0); - } - } - } catch (...) { - - } - } - - inline void SetLastHealthCheck(const GWObjects::HealthCheck &H) { - std::lock_guard G(ConnectionMutex_); - RawLastHealthcheck_ = H; - } - inline void GetLastHealthCheck(GWObjects::HealthCheck &H) { std::lock_guard G(ConnectionMutex_); H = RawLastHealthcheck_; @@ -206,6 +166,42 @@ namespace OpenWifi { std::double_t memory_used_=0.0, cpu_load_ = 0.0, temperature_ = 0.0; std::uint64_t uuid_=0; bool Simulated_=false; + + inline void SetLastStats(const std::string &LastStats) { + RawLastStats_ = LastStats; + try { + Poco::JSON::Parser P; + auto Stats = P.parse(LastStats).extract(); + hasGPS = Stats->isObject("gps"); + auto Unit = Stats->getObject("unit"); + auto Memory = Unit->getObject("memory"); + std::uint64_t TotalMemory = Memory->get("total"); + std::uint64_t FreeMemory = Memory->get("free"); + if(TotalMemory>0) { + memory_used_ = + (100.0 * ((double)TotalMemory - (double)FreeMemory)) / (double)TotalMemory; + } + if(Unit->isArray("load")) { + Poco::JSON::Array::Ptr Load = Unit->getArray("load"); + if(Load->size()>1) { + cpu_load_ = Load->get(1); + } + } + if(Unit->isArray("temperature")) { + Poco::JSON::Array::Ptr Temperature = Unit->getArray("temperature"); + if(Temperature->size()>1) { + temperature_ = Temperature->get(0); + } + } + } catch (const Poco::Exception &E) { + poco_error(Logger_, "Failed to parse last stats: " + E.displayText()); + } + } + + inline void SetLastHealthCheck(const GWObjects::HealthCheck &H) { + RawLastHealthcheck_ = H; + } + }; } // namespace OpenWifi \ No newline at end of file diff --git a/src/AP_WS_Process_connect.cpp b/src/AP_WS_Process_connect.cpp index b67c07675..f826f300b 100644 --- a/src/AP_WS_Process_connect.cpp +++ b/src/AP_WS_Process_connect.cpp @@ -74,7 +74,6 @@ namespace OpenWifi { AP_WS_Server()->SetSessionDetails(State_.sessionId, SerialNumberInt_); std::lock_guard DbSessionLock(*DbSession_->Mutex); - std::lock_guard Lock(ConnectionMutex_); Config::Capabilities Caps(Capabilities); diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index d0872743c..100add6d8 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -216,7 +216,7 @@ namespace OpenWifi { fmt::format( "{}: Session seems idle. Controller disconnecting device.", hint->second.second->SerialNumber_)); - hint->second.second->EndConnection(false); + // hint->second.second->EndConnection(false); SessionsToRemove.emplace_back(hint->second.first); { std::lock_guard GarbageLock(GarbageMutex_); From a01d006d4ebc61f12444972c17447a16dcaea495 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 29 Nov 2023 21:54:02 -0800 Subject: [PATCH 026/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Connection.cpp | 18 ++--- src/AP_WS_Connection.h | 3 +- src/AP_WS_Server.cpp | 165 +++++++-------------------------------- src/AP_WS_Server.h | 4 +- 5 files changed, 40 insertions(+), 152 deletions(-) diff --git a/build b/build index a5c750fea..368f89cee 100644 --- a/build +++ b/build @@ -1 +1 @@ -27 \ No newline at end of file +28 \ No newline at end of file diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index bb2e43294..d4b1f0353 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -71,6 +71,7 @@ namespace OpenWifi { Registered_ = true; Valid_ = true; uuid_ = MicroServiceRandom(std::numeric_limits::max()-1); + LastContact_ = Utils::Now(); } bool AP_WS_Connection::ValidatedDevice() { @@ -253,9 +254,12 @@ namespace OpenWifi { NotifyKafkaDisconnect(SerialNumber, uuid); } RADIUSSessionTracker()->DeviceDisconnect(SerialNumber); + GWWebSocketNotifications::SingleDevice_t N; + N.content.serialNumber = SerialNumber; + GWWebSocketNotifications::DeviceDisconnected(N); } - void AP_WS_Connection::EndConnection(bool DeleteSession) { + void AP_WS_Connection::EndConnection() { Valid_ = false; if (!Dead_.test_and_set()) { @@ -283,15 +287,7 @@ namespace OpenWifi { Cleanup.detach(); } - bool SessionDeleted = false; - if(DeleteSession) - SessionDeleted = AP_WS_Server()->EndSession(State_.sessionId, SerialNumberInt_); - - if (SessionDeleted || !DeleteSession) { - GWWebSocketNotifications::SingleDevice_t N; - N.content.serialNumber = SerialNumber_; - GWWebSocketNotifications::DeviceDisconnected(N); - } + AP_WS_Server()->EndSession(State_.sessionId); } } @@ -651,12 +647,14 @@ namespace OpenWifi { void AP_WS_Connection::OnSocketShutdown( [[maybe_unused]] const Poco::AutoPtr &pNf) { poco_trace(Logger_, fmt::format("SOCKET-SHUTDOWN({}): Closing.", CId_)); + std::lock_guard G(ConnectionMutex_); return EndConnection(); } void AP_WS_Connection::OnSocketError( [[maybe_unused]] const Poco::AutoPtr &pNf) { poco_trace(Logger_, fmt::format("SOCKET-ERROR({}): Closing.", CId_)); + std::lock_guard G(ConnectionMutex_); return EndConnection(); } diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index 05df503cc..52bc1676f 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -30,7 +30,7 @@ namespace OpenWifi { Poco::Logger &L, std::pair R); ~AP_WS_Connection(); - void EndConnection(bool DeleteSession=true); + void EndConnection(); void ProcessJSONRPCEvent(Poco::JSON::Object::Ptr &Doc); void ProcessJSONRPCResult(Poco::JSON::Object::Ptr Doc); void ProcessIncomingFrame(); @@ -166,6 +166,7 @@ namespace OpenWifi { std::double_t memory_used_=0.0, cpu_load_ = 0.0, temperature_ = 0.0; std::uint64_t uuid_=0; bool Simulated_=false; + std::uint64_t LastContact_=0; inline void SetLastStats(const std::string &LastStats) { RawLastStats_ = LastStats; diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 100add6d8..cc1c71d13 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -216,12 +216,8 @@ namespace OpenWifi { fmt::format( "{}: Session seems idle. Controller disconnecting device.", hint->second.second->SerialNumber_)); - // hint->second.second->EndConnection(false); - SessionsToRemove.emplace_back(hint->second.first); - { - std::lock_guard GarbageLock(GarbageMutex_); - GarbageSessions_.push_back(hint->second.second); - } + std::lock_guard ConnectionLock(hint->second.second->ConnectionMutex_); + hint->second.second->EndConnection(); hint = SerialNumbers_[hashIndex].erase(hint); } else if (hint->second.second->State_.Connected) { NumberOfConnectedDevices_++; @@ -234,11 +230,20 @@ namespace OpenWifi { } } - if(!SessionsToRemove.empty()) { - poco_information(Logger(), fmt::format("Removing {} sessions.", SessionsToRemove.size())); - for (const auto &Session : SessionsToRemove) { - std::lock_guard Lock(SessionMutex_[Session % 256]); - Sessions_[Session % 256].erase(Session); + for(int i=0;i<256;i++) { + std::lock_guard Lock(SessionMutex_[i]); + auto hint = Sessions_[i].begin(); + while (hint != end(Sessions_[i])) { + if ((now - hint->second->LastContact_) > SessionTimeOut_) { + poco_information( + Logger(), + fmt::format( + "{}: Session seems idle. Controller disconnecting device.", + hint->second->SerialNumber_)); + hint = Sessions_[i].erase(hint); + } else { + hint++; + } } } @@ -284,104 +289,9 @@ namespace OpenWifi { Logger().information(fmt::format("Garbage collection finished run." )); last_garbage_run = now; } - Logger().information(fmt::format("Garbage collector done for the day." )); } -/* void AP_WS_Server::onGarbageCollecting([[maybe_unused]] Poco::Timer &timer) { - auto now = Utils::Now(); - - { - { - std::lock_guard SessionLock(SessionMutex_); - if (!Garbage_.empty()) { - Garbage_.clear(); - } - } - - uint64_t total_connected_time = 0; - - if(now-last_zombie_run > 20) { - poco_information(Logger(), fmt::format("Garbage collecting...")); - std::vector SessionsToRemove; - NumberOfConnectedDevices_ = 0; - NumberOfConnectingDevices_ = 0; - AverageDeviceConnectionTime_ = 0; - last_zombie_run = now; - for(int hashIndex=0;hashIndex<256;hashIndex++) { - std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - auto hint = SerialNumbers_[hashIndex].begin(); - while (hint != end(SerialNumbers_[hashIndex])) { - if (hint->second.second == nullptr) { - hint = SerialNumbers_[hashIndex].erase(hint); - } else if ((now - hint->second.second->State_.LastContact) > - SessionTimeOut_) { - hint->second.second->EndConnection(false); - poco_information( - Logger(), - fmt::format( - "{}: Session seems idle. Controller disconnecting device.", - hint->second.second->SerialNumber_)); - SessionsToRemove.emplace_back(hint->second.first); - Garbage_.push_back(hint->second.second); - hint = SerialNumbers_[hashIndex].erase(hint); - } else if (hint->second.second->State_.Connected) { - NumberOfConnectedDevices_++; - total_connected_time += (now - hint->second.second->State_.started); - hint++; - } else { - NumberOfConnectingDevices_++; - hint++; - } - } - } - - if(SessionsToRemove.empty()) { - poco_information(Logger(), fmt::format("Removing {} sessions.", SessionsToRemove.size())); - std::lock_guard Lock(SessionMutex_); - for (const auto &Session : SessionsToRemove) { - Sessions_.erase(Session); - } - } - - AverageDeviceConnectionTime_ = - NumberOfConnectedDevices_ > 0 ? total_connected_time / NumberOfConnectedDevices_ - : 0; - - poco_information(Logger(), fmt::format("Garbage collecting done...")); - } else { - std::lock_guard SessionLock(SessionMutex_); - NumberOfConnectedDevices_ = Sessions_.size(); - AverageDeviceConnectionTime_ += 10; - } - - if ((now - last_log) > 120) { - last_log = now; - poco_information(Logger(), - fmt::format("Active AP connections: {} Connecting: {} Average connection time: {} seconds", - NumberOfConnectedDevices_, NumberOfConnectingDevices_, - AverageDeviceConnectionTime_)); - } - } - - GWWebSocketNotifications::NumberOfConnection_t Notification; - Notification.content.numberOfConnectingDevices = NumberOfConnectingDevices_; - Notification.content.numberOfDevices = NumberOfConnectedDevices_; - Notification.content.averageConnectedTime = AverageDeviceConnectionTime_; - GetTotalDataStatistics(Notification.content.tx,Notification.content.rx); - GWWebSocketNotifications::NumberOfConnections(Notification); - - Poco::JSON::Object KafkaNotification; - Notification.to_json(KafkaNotification); - - Poco::JSON::Object FullEvent; - FullEvent.set("type", "load-update"); - FullEvent.set("timestamp", now); - FullEvent.set("payload", KafkaNotification); - - KafkaManager()->PostMessage(KafkaTopics::DEVICE_EVENT_QUEUE, "system", FullEvent); - } -*/ void AP_WS_Server::Stop() { poco_information(Logger(), "Stopping..."); Running_ = false; @@ -389,11 +299,10 @@ namespace OpenWifi { GarbageCollector_.wakeUp(); GarbageCollector_.join(); - // Timer_.stop(); - for (auto &server : WebServers_) { server->stopAll(); } + Reactor_pool_->Stop(); Reactor_.stop(); ReactorThread_.join(); @@ -454,13 +363,12 @@ namespace OpenWifi { void AP_WS_Server::SetSessionDetails(uint64_t connection_id, uint64_t SerialNumber) { std::shared_ptr Connection; - { - std::lock_guard SessionLock(SessionMutex_[connection_id % 256]); - auto Conn = Sessions_[connection_id % 256].find(connection_id); - if (Conn == end(Sessions_[connection_id % 256])) - return; - Connection = Conn->second; - } + + std::lock_guard SessionLock(SessionMutex_[connection_id % 256]); + auto ConnHint = Sessions_[connection_id % 256].find(connection_id); + if (ConnHint == end(Sessions_[connection_id % 256])) + return; + Connection = ConnHint->second; auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); @@ -468,40 +376,23 @@ namespace OpenWifi { if ((CurrentSerialNumber == SerialNumbers_[hashIndex].end()) || (CurrentSerialNumber->second.first < connection_id)) { SerialNumbers_[hashIndex][SerialNumber] = std::make_pair(connection_id, Connection); + Sessions_[connection_id % 256].erase(ConnHint); return; } } - bool AP_WS_Server::EndSession(uint64_t session_id, uint64_t SerialNumber) { + bool AP_WS_Server::EndSession(uint64_t session_id) { + std::lock_guard SessionLock(SessionMutex_[session_id % 256]); auto Session = Sessions_[session_id % 256].find(session_id); if (Session == end(Sessions_[session_id % 256])) return false; - { - std::lock_guard Lock(GarbageMutex_); - GarbageSessions_.push_back(Session->second); - } - - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); - std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex])) { - Sessions_[session_id % 256].erase(Session); - return false; - } - - if (Device->second.first == session_id) { - Sessions_[session_id % 256].erase(Session); - SerialNumbers_[hashIndex].erase(Device); - return true; - } - Sessions_[session_id % 256].erase(Session); - - return false; + return true; } + bool AP_WS_Server::Connected(uint64_t SerialNumber, GWObjects::DeviceRestrictions &Restrictions) const { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index a28c26cf7..4dd1a114b 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -151,7 +151,7 @@ namespace OpenWifi { std::size_t size); void SetSessionDetails(uint64_t connection_id, uint64_t SerialNumber); - bool EndSession(uint64_t connection_id, uint64_t serial_number); + bool EndSession(uint64_t connection_id); void SetWebSocketTelemetryReporting(uint64_t RPCID, uint64_t SerialNumber, uint64_t Interval, uint64_t Lifetime, const std::vector &TelemetryTypes); @@ -168,8 +168,6 @@ namespace OpenWifi { uint64_t &TelemetryWebSocketPackets, uint64_t &TelemetryKafkaPackets); - void onGarbageCollecting(Poco::Timer &timer); - inline void AverageDeviceStatistics(uint64_t &Connections, uint64_t &AverageConnectionTime, uint64_t &NumberOfConnectingDevices) const { Connections = NumberOfConnectedDevices_; From 142bc4f2716e84bac8eda13aa2786dc122d5e648 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 29 Nov 2023 22:04:42 -0800 Subject: [PATCH 027/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 6 ++++-- src/AP_WS_Server.h | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index cc1c71d13..305ee053c 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -230,6 +230,7 @@ namespace OpenWifi { } } + LeftOverSessions_ = 0; for(int i=0;i<256;i++) { std::lock_guard Lock(SessionMutex_[i]); auto hint = Sessions_[i].begin(); @@ -242,6 +243,7 @@ namespace OpenWifi { hint->second->SerialNumber_)); hint = Sessions_[i].erase(hint); } else { + LeftOverSessions_++; hint++; } } @@ -265,9 +267,9 @@ namespace OpenWifi { if ((now - last_log) > 120) { last_log = now; poco_information(Logger(), - fmt::format("Active AP connections: {} Connecting: {} Average connection time: {} seconds", + fmt::format("Active AP connections: {} Connecting: {} Average connection time: {} seconds. Left Over Sessions: {}", NumberOfConnectedDevices_, NumberOfConnectingDevices_, - AverageDeviceConnectionTime_)); + AverageDeviceConnectionTime_, LeftOverSessions_)); } GWWebSocketNotifications::NumberOfConnection_t Notification; diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index 4dd1a114b..51c19b373 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -246,7 +246,7 @@ namespace OpenWifi { std::uint64_t AverageDeviceConnectionTime_ = 0; std::uint64_t NumberOfConnectingDevices_ = 0; std::uint64_t SessionTimeOut_ = 10*60; - + std::uint64_t LeftOverSessions_ = 0; std::atomic_uint64_t TX_=0,RX_=0; std::vector> GarbageSessions_; From 177891226469abbfec726dbb7c6f89fb390f81c2 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 29 Nov 2023 22:18:58 -0800 Subject: [PATCH 028/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/RESTAPI/RESTAPI_devices_handler.cpp | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/build b/build index 368f89cee..d99e90eb9 100644 --- a/build +++ b/build @@ -1 +1 @@ -28 \ No newline at end of file +29 \ No newline at end of file diff --git a/src/RESTAPI/RESTAPI_devices_handler.cpp b/src/RESTAPI/RESTAPI_devices_handler.cpp index 7d67f9908..beef733e3 100644 --- a/src/RESTAPI/RESTAPI_devices_handler.cpp +++ b/src/RESTAPI/RESTAPI_devices_handler.cpp @@ -175,10 +175,12 @@ namespace OpenWifi { } if(GetBoolParameter("simulatedDevices",false)) { - if(StorageService()->DeleteSimulatedDevice("")) { - return OK(); - } - return NotFound(); + auto F = []() ->void { + StorageService()->DeleteSimulatedDevice(""); + }; + std::thread T(F); + T.detach(); + return OK(); } if(!QB_.Select.empty() && !Utils::ValidSerialNumbers(QB_.Select)) { From aa472ed79ed1e189a66b36af6f22db0754dd1385 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 29 Nov 2023 22:50:45 -0800 Subject: [PATCH 029/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 305ee053c..e6746e0eb 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -197,7 +197,7 @@ namespace OpenWifi { uint64_t total_connected_time = 0, now = Utils::Now(); if(now-last_zombie_run > 60) { - poco_information(Logger(), fmt::format("Garbage collecting zombies...")); + poco_information(Logger(), fmt::format("Garbage collecting zombies... (step 1)")); std::vector SessionsToRemove; NumberOfConnectedDevices_ = 0; NumberOfConnectingDevices_ = 0; @@ -230,6 +230,7 @@ namespace OpenWifi { } } + poco_information(Logger(), fmt::format("Garbage collecting zombies... (step 2)")); LeftOverSessions_ = 0; for(int i=0;i<256;i++) { std::lock_guard Lock(SessionMutex_[i]); @@ -252,8 +253,8 @@ namespace OpenWifi { AverageDeviceConnectionTime_ = NumberOfConnectedDevices_ > 0 ? total_connected_time / NumberOfConnectedDevices_ : 0; - poco_information(Logger(), fmt::format("Garbage collecting zombies done...")); + } else { NumberOfConnectedDevices_=0; for(int i=0;i<256;i++) { @@ -264,7 +265,7 @@ namespace OpenWifi { AverageDeviceConnectionTime_ += (now - last_garbage_run); } - if ((now - last_log) > 120) { + if ((now - last_log) > 60) { last_log = now; poco_information(Logger(), fmt::format("Active AP connections: {} Connecting: {} Average connection time: {} seconds. Left Over Sessions: {}", From 7b899adb88c2246cb36e474e0bdcdeed5f1b15c3 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 29 Nov 2023 23:31:20 -0800 Subject: [PATCH 030/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Connection.cpp | 2 +- src/AP_WS_Server.cpp | 25 +++++++++++++++++++------ src/AP_WS_Server.h | 2 +- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/build b/build index d99e90eb9..8580e7b68 100644 --- a/build +++ b/build @@ -1 +1 @@ -29 \ No newline at end of file +30 \ No newline at end of file diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index d4b1f0353..454708e50 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -287,7 +287,7 @@ namespace OpenWifi { Cleanup.detach(); } - AP_WS_Server()->EndSession(State_.sessionId); + AP_WS_Server()->EndSession(State_.sessionId, SerialNumberInt_); } } diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index e6746e0eb..77436de5b 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -384,14 +384,27 @@ namespace OpenWifi { } } - bool AP_WS_Server::EndSession(uint64_t session_id) { + bool AP_WS_Server::EndSession(uint64_t session_id, uint64_t SerialNumber) { - std::lock_guard SessionLock(SessionMutex_[session_id % 256]); - auto Session = Sessions_[session_id % 256].find(session_id); - if (Session == end(Sessions_[session_id % 256])) - return false; + { + std::lock_guard SessionLock(SessionMutex_[session_id % 256]); + auto Session = Sessions_[session_id % 256].find(session_id); + if (Session != end(Sessions_[session_id % 256])) { + Sessions_[session_id % 256].erase(Session); + } + } - Sessions_[session_id % 256].erase(Session); + { + auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); + std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); + auto Device = SerialNumbers_[hashIndex].find(SerialNumber); + if (Device == SerialNumbers_[hashIndex].end() + || Device->second.second == nullptr + || Device->second.second->State_.sessionId != session_id) { + return false; + } + SerialNumbers_[hashIndex].erase(Device); + } return true; } diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index 51c19b373..ce32f01ba 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -151,7 +151,7 @@ namespace OpenWifi { std::size_t size); void SetSessionDetails(uint64_t connection_id, uint64_t SerialNumber); - bool EndSession(uint64_t connection_id); + bool EndSession(uint64_t connection_id, uint64_t SerialNumber); void SetWebSocketTelemetryReporting(uint64_t RPCID, uint64_t SerialNumber, uint64_t Interval, uint64_t Lifetime, const std::vector &TelemetryTypes); From c5c2dc1a1e73f66d507a9f441e25ef45080d72aa Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 08:38:50 -0800 Subject: [PATCH 031/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.h | 2 ++ src/AP_WS_Server.cpp | 9 ++++++--- src/storage/storage_device.cpp | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index 52bc1676f..a8a6588b2 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -68,8 +68,10 @@ namespace OpenWifi { } inline void GetState(GWObjects::ConnectionState &State) const { + std::cout << __LINE__ << ": GetState" << std::endl; std::lock_guard G(ConnectionMutex_); State = State_; + std::cout << __LINE__ << ": GetState" << std::endl; } inline bool HasGPS() { return hasGPS; } diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 77436de5b..d794994d5 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -342,11 +342,14 @@ namespace OpenWifi { bool AP_WS_Server::GetState(uint64_t SerialNumber, GWObjects::ConnectionState &State) const { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == SerialNumbers_[hashIndex].end() || Device->second.second == nullptr) { + auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); + if (DeviceHint == SerialNumbers_[hashIndex].end() || DeviceHint->second.second == nullptr) { return false; } - Device->second.second->GetState(State); + auto Device = DeviceHint->second.second; + std::cout << __LINE__ << " " << Device->State_.sessionId << std::endl; + Device->GetState(State); + std::cout << __LINE__ << " " << Device->State_.sessionId << std::endl; return true; } diff --git a/src/storage/storage_device.cpp b/src/storage/storage_device.cpp index b482df504..b0af94528 100644 --- a/src/storage/storage_device.cpp +++ b/src/storage/storage_device.cpp @@ -508,7 +508,7 @@ namespace OpenWifi { GWObjects::Device D; - poco_information(Logger(), fmt::format("AUTO-CREATION({}): Start.", SerialNumber)); + // poco_information(Logger(), fmt::format("AUTO-CREATION({}): Start.", SerialNumber)); uint64_t Now = Utils::Now(); GWObjects::DefaultConfiguration DefConfig; From 0318b475f5813c276b337e90a6692acf49ef95a1 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 08:57:02 -0800 Subject: [PATCH 032/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index d794994d5..5d1ab81ce 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -340,16 +340,20 @@ namespace OpenWifi { } bool AP_WS_Server::GetState(uint64_t SerialNumber, GWObjects::ConnectionState &State) const { - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); - std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); - if (DeviceHint == SerialNumbers_[hashIndex].end() || DeviceHint->second.second == nullptr) { - return false; + std::shared_ptr Connection; + { + auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); + std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); + auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); + if (DeviceHint == SerialNumbers_[hashIndex].end() || + DeviceHint->second.second == nullptr) { + return false; + } + Connection = DeviceHint->second.second; } - auto Device = DeviceHint->second.second; - std::cout << __LINE__ << " " << Device->State_.sessionId << std::endl; - Device->GetState(State); - std::cout << __LINE__ << " " << Device->State_.sessionId << std::endl; + std::cout << __LINE__ << " " << Connection->State_.sessionId << std::endl; + Connection->GetState(State); + std::cout << __LINE__ << " " << Connection->State_.sessionId << std::endl; return true; } From 2cfc6c30e9ad6d8cdb2a006104898dc5175bc9f6 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 08:58:44 -0800 Subject: [PATCH 033/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 5d1ab81ce..809e23a39 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -351,9 +351,9 @@ namespace OpenWifi { } Connection = DeviceHint->second.second; } - std::cout << __LINE__ << " " << Connection->State_.sessionId << std::endl; + std::cout << __LINE__ << " " << Connection->State_.sessionId << ":" << Connection->SerialNumber_ << std::endl; Connection->GetState(State); - std::cout << __LINE__ << " " << Connection->State_.sessionId << std::endl; + std::cout << __LINE__ << " " << Connection->State_.sessionId << ":" << Connection->SerialNumber_ << std::endl; return true; } From 2ddc0d311700d867739e52242b5aa2804fc4a6c3 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 09:13:25 -0800 Subject: [PATCH 034/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index a8a6588b2..03fd725cb 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -67,7 +67,7 @@ namespace OpenWifi { H = RawLastHealthcheck_; } - inline void GetState(GWObjects::ConnectionState &State) const { + inline void GetState(GWObjects::ConnectionState &State) { std::cout << __LINE__ << ": GetState" << std::endl; std::lock_guard G(ConnectionMutex_); State = State_; @@ -76,7 +76,7 @@ namespace OpenWifi { inline bool HasGPS() { return hasGPS; } - inline void GetRestrictions(GWObjects::DeviceRestrictions &R) const { + inline void GetRestrictions(GWObjects::DeviceRestrictions &R) { std::lock_guard G(ConnectionMutex_); R = Restrictions_; } @@ -117,7 +117,7 @@ namespace OpenWifi { friend class AP_WS_Server; - inline GWObjects::DeviceRestrictions Restrictions() const { + inline GWObjects::DeviceRestrictions Restrictions() { std::lock_guard G(ConnectionMutex_); return Restrictions_; } @@ -125,7 +125,7 @@ namespace OpenWifi { inline bool MustBeSecureRtty() const { return RttyMustBeSecure_; } private: - mutable std::mutex ConnectionMutex_; + std::mutex ConnectionMutex_; std::mutex TelemetryMutex_; Poco::Logger &Logger_; Poco::Net::SocketReactor *Reactor_{nullptr}; From 5b1a4fb9c05fd11b246cc93061f195c47b31a3d3 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 09:25:43 -0800 Subject: [PATCH 035/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 454708e50..e2915059a 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -287,7 +287,9 @@ namespace OpenWifi { Cleanup.detach(); } + std::cout << __LINE__ << ": EndConnection" << std::endl; AP_WS_Server()->EndSession(State_.sessionId, SerialNumberInt_); + std::cout << __LINE__ << ": EndConnection" << std::endl; } } From ceda1fc8f68cf5208d1c059f13ae00e62a6f51e2 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 09:31:57 -0800 Subject: [PATCH 036/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 6 ++++++ src/AP_WS_Connection.h | 8 ++++++++ src/AP_WS_Server.cpp | 2 ++ 3 files changed, 16 insertions(+) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index e2915059a..85f9effe6 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -649,14 +649,18 @@ namespace OpenWifi { void AP_WS_Connection::OnSocketShutdown( [[maybe_unused]] const Poco::AutoPtr &pNf) { poco_trace(Logger_, fmt::format("SOCKET-SHUTDOWN({}): Closing.", CId_)); + std::cout << __LINE__ << ": OnSocketShutdown" << std::endl; std::lock_guard G(ConnectionMutex_); + std::cout << __LINE__ << ": OnSocketShutdown" << std::endl; return EndConnection(); } void AP_WS_Connection::OnSocketError( [[maybe_unused]] const Poco::AutoPtr &pNf) { poco_trace(Logger_, fmt::format("SOCKET-ERROR({}): Closing.", CId_)); + std::cout << __LINE__ << ": OnSocketError" << std::endl; std::lock_guard G(ConnectionMutex_); + std::cout << __LINE__ << ": OnSocketError" << std::endl; return EndConnection(); } @@ -669,7 +673,9 @@ namespace OpenWifi { if (!AP_WS_Server()->Running()) return EndConnection(); + std::cout << __LINE__ << ": OnSocketReadable" << std::endl; std::lock_guard DeviceLock(ConnectionMutex_); + std::cout << __LINE__ << ": OnSocketReadable" << std::endl; if (!ValidatedDevice()) return; diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index 03fd725cb..6758f2c1d 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -58,12 +58,16 @@ namespace OpenWifi { bool StopKafkaTelemetry(uint64_t RPCID); inline void GetLastStats(std::string &LastStats) { + std::cout << __LINE__ << ": GetLastStats" << std::endl; std::lock_guard G(ConnectionMutex_); + std::cout << __LINE__ << ": GetLastStats" << std::endl; LastStats = RawLastStats_; } inline void GetLastHealthCheck(GWObjects::HealthCheck &H) { + std::cout << __LINE__ << ": GetLastHealthCheck" << std::endl; std::lock_guard G(ConnectionMutex_); + std::cout << __LINE__ << ": GetLastHealthCheck" << std::endl; H = RawLastHealthcheck_; } @@ -77,7 +81,9 @@ namespace OpenWifi { inline bool HasGPS() { return hasGPS; } inline void GetRestrictions(GWObjects::DeviceRestrictions &R) { + std::cout << __LINE__ << ": GetRestrictions" << std::endl; std::lock_guard G(ConnectionMutex_); + std::cout << __LINE__ << ": GetRestrictions" << std::endl; R = Restrictions_; } @@ -118,7 +124,9 @@ namespace OpenWifi { friend class AP_WS_Server; inline GWObjects::DeviceRestrictions Restrictions() { + std::cout << __LINE__ << ": Restrictions" << std::endl; std::lock_guard G(ConnectionMutex_); + std::cout << __LINE__ << ": Restrictions" << std::endl; return Restrictions_; } diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 809e23a39..4470d260b 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -216,7 +216,9 @@ namespace OpenWifi { fmt::format( "{}: Session seems idle. Controller disconnecting device.", hint->second.second->SerialNumber_)); + std::cout << __LINE__ << ": AP_WS_Server" << std::endl; std::lock_guard ConnectionLock(hint->second.second->ConnectionMutex_); + std::cout << __LINE__ << ": AP_WS_Server" << std::endl; hint->second.second->EndConnection(); hint = SerialNumbers_[hashIndex].erase(hint); } else if (hint->second.second->State_.Connected) { From 77f67fe5453785d4d584853f0981bf6837d64bfc Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 11:53:19 -0800 Subject: [PATCH 037/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Process_connect.cpp | 2 ++ src/AP_WS_Server.cpp | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/src/AP_WS_Process_connect.cpp b/src/AP_WS_Process_connect.cpp index f826f300b..49422ab7d 100644 --- a/src/AP_WS_Process_connect.cpp +++ b/src/AP_WS_Process_connect.cpp @@ -71,7 +71,9 @@ namespace OpenWifi { CommandManager()->ClearQueue(SerialNumberInt_); + std::cout << __LINE__ << ": set details" << std::endl; AP_WS_Server()->SetSessionDetails(State_.sessionId, SerialNumberInt_); + std::cout << __LINE__ << ": set details" << std::endl; std::lock_guard DbSessionLock(*DbSession_->Mutex); diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 4470d260b..2b110b548 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -376,6 +376,8 @@ namespace OpenWifi { void AP_WS_Server::SetSessionDetails(uint64_t connection_id, uint64_t SerialNumber) { std::shared_ptr Connection; + std::cout << __LINE__ << ": Attempting to set connection details" << std::endl; + std::lock_guard SessionLock(SessionMutex_[connection_id % 256]); auto ConnHint = Sessions_[connection_id % 256].find(connection_id); if (ConnHint == end(Sessions_[connection_id % 256])) @@ -389,8 +391,10 @@ namespace OpenWifi { (CurrentSerialNumber->second.first < connection_id)) { SerialNumbers_[hashIndex][SerialNumber] = std::make_pair(connection_id, Connection); Sessions_[connection_id % 256].erase(ConnHint); + std::cout << __LINE__ << ": Set connection details" << std::endl; return; } + std::cout << __LINE__ << ": Failed to set connection details" << std::endl; } bool AP_WS_Server::EndSession(uint64_t session_id, uint64_t SerialNumber) { From 46c632167431840707d3e10d58e1b8c115e2ae38 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 12:05:13 -0800 Subject: [PATCH 038/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 2b110b548..54ed4b46c 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -380,8 +380,10 @@ namespace OpenWifi { std::lock_guard SessionLock(SessionMutex_[connection_id % 256]); auto ConnHint = Sessions_[connection_id % 256].find(connection_id); - if (ConnHint == end(Sessions_[connection_id % 256])) + if (ConnHint == end(Sessions_[connection_id % 256])) { + std::cout << __LINE__ << ": Failed (1) to set connection details" << std::endl; return; + } Connection = ConnHint->second; auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); From 8e48d30d6ab996e84b5497920fe688ee0db4a1b0 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 12:17:06 -0800 Subject: [PATCH 039/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 80 ++++++++++++++++++++++---------------------- src/AP_WS_Server.h | 21 +++++++----- 2 files changed, 52 insertions(+), 49 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 54ed4b46c..43743ce63 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -207,23 +207,23 @@ namespace OpenWifi { std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto hint = SerialNumbers_[hashIndex].begin(); while (hint != end(SerialNumbers_[hashIndex])) { - if (hint->second.second == nullptr) { + if (hint->second == nullptr) { hint = SerialNumbers_[hashIndex].erase(hint); - } else if ((now - hint->second.second->State_.LastContact) > + } else if ((now - hint->second->State_.LastContact) > SessionTimeOut_) { poco_information( Logger(), fmt::format( "{}: Session seems idle. Controller disconnecting device.", - hint->second.second->SerialNumber_)); + hint->second->SerialNumber_)); std::cout << __LINE__ << ": AP_WS_Server" << std::endl; - std::lock_guard ConnectionLock(hint->second.second->ConnectionMutex_); + std::lock_guard ConnectionLock(hint->second->ConnectionMutex_); std::cout << __LINE__ << ": AP_WS_Server" << std::endl; - hint->second.second->EndConnection(); + hint->second->EndConnection(); hint = SerialNumbers_[hashIndex].erase(hint); - } else if (hint->second.second->State_.Connected) { + } else if (hint->second->State_.Connected) { NumberOfConnectedDevices_++; - total_connected_time += (now - hint->second.second->State_.started); + total_connected_time += (now - hint->second->State_.started); hint++; } else { NumberOfConnectingDevices_++; @@ -333,10 +333,10 @@ namespace OpenWifi { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == SerialNumbers_[hashIndex].end() || Device->second.second == nullptr) { + if (Device == SerialNumbers_[hashIndex].end() || Device->second == nullptr) { return false; } - Device->second.second->GetLastStats(Statistics); + Device->second->GetLastStats(Statistics); return true; } @@ -348,10 +348,10 @@ namespace OpenWifi { std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); if (DeviceHint == SerialNumbers_[hashIndex].end() || - DeviceHint->second.second == nullptr) { + DeviceHint->second == nullptr) { return false; } - Connection = DeviceHint->second.second; + Connection = DeviceHint->second; } std::cout << __LINE__ << " " << Connection->State_.sessionId << ":" << Connection->SerialNumber_ << std::endl; Connection->GetState(State); @@ -365,10 +365,10 @@ namespace OpenWifi { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == SerialNumbers_[hashIndex].end() || Device->second.second == nullptr) { + if (Device == SerialNumbers_[hashIndex].end() || Device->second == nullptr) { return false; } - Device->second.second->GetLastHealthCheck(CheckData); + Device->second->GetLastHealthCheck(CheckData); return true; } @@ -390,8 +390,8 @@ namespace OpenWifi { std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto CurrentSerialNumber = SerialNumbers_[hashIndex].find(SerialNumber); if ((CurrentSerialNumber == SerialNumbers_[hashIndex].end()) || - (CurrentSerialNumber->second.first < connection_id)) { - SerialNumbers_[hashIndex][SerialNumber] = std::make_pair(connection_id, Connection); + (CurrentSerialNumber->second != nullptr && CurrentSerialNumber->second->State_.sessionId < connection_id)) { + SerialNumbers_[hashIndex][SerialNumber] = Connection; Sessions_[connection_id % 256].erase(ConnHint); std::cout << __LINE__ << ": Set connection details" << std::endl; return; @@ -414,8 +414,8 @@ namespace OpenWifi { std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); if (Device == SerialNumbers_[hashIndex].end() - || Device->second.second == nullptr - || Device->second.second->State_.sessionId != session_id) { + || Device->second == nullptr + || Device->second->State_.sessionId != session_id) { return false; } SerialNumbers_[hashIndex].erase(Device); @@ -429,33 +429,33 @@ namespace OpenWifi { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second.second == nullptr) { + if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { return false; } - Device->second.second->GetRestrictions(Restrictions); - return Device->second.second->State_.Connected; + Device->second->GetRestrictions(Restrictions); + return Device->second->State_.Connected; } bool AP_WS_Server::Connected(uint64_t SerialNumber) const { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second.second == nullptr) { + if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { return false; } - return Device->second.second->State_.Connected; + return Device->second->State_.Connected; } bool AP_WS_Server::SendFrame(uint64_t SerialNumber, const std::string &Payload) const { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second.second == nullptr) { + if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { return false; } try { - return Device->second.second->Send(Payload); + return Device->second->Send(Payload); } catch (...) { poco_debug(Logger(), fmt::format(": SendFrame: Could not send data to device '{}'", Utils::IntToSerialNumber(SerialNumber))); @@ -467,10 +467,10 @@ namespace OpenWifi { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second.second == nullptr) { + if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { return; } - Device->second.second->StopWebSocketTelemetry(RPCID); + Device->second->StopWebSocketTelemetry(RPCID); } void @@ -480,10 +480,10 @@ namespace OpenWifi { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second.second == nullptr) { + if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { return; } - Device->second.second->SetWebSocketTelemetryReporting(RPCID, Interval, Lifetime, TelemetryTypes); + Device->second->SetWebSocketTelemetryReporting(RPCID, Interval, Lifetime, TelemetryTypes); } void AP_WS_Server::SetKafkaTelemetryReporting(uint64_t RPCID, uint64_t SerialNumber, @@ -492,20 +492,20 @@ namespace OpenWifi { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second.second == nullptr) { + if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { return; } - Device->second.second->SetKafkaTelemetryReporting(RPCID, Interval, Lifetime, TelemetryTypes); + Device->second->SetKafkaTelemetryReporting(RPCID, Interval, Lifetime, TelemetryTypes); } void AP_WS_Server::StopKafkaTelemetry(uint64_t RPCID, uint64_t SerialNumber) { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second.second == nullptr) { + if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { return; } - Device->second.second->StopKafkaTelemetry(RPCID); + Device->second->StopKafkaTelemetry(RPCID); } void AP_WS_Server::GetTelemetryParameters( @@ -517,11 +517,11 @@ namespace OpenWifi { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second.second == nullptr) { + if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { return; } - Device->second.second->GetTelemetryParameters(TelemetryRunning, TelemetryInterval, + Device->second->GetTelemetryParameters(TelemetryRunning, TelemetryInterval, TelemetryWebSocketTimer, TelemetryKafkaTimer, TelemetryWebSocketCount, TelemetryKafkaCount, TelemetryWebSocketPackets, TelemetryKafkaPackets); @@ -534,12 +534,12 @@ namespace OpenWifi { auto hashIndex = Utils::CalculateMacAddressHash(IntSerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(IntSerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second.second == nullptr) { + if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { return false; } try { - return Device->second.second->SendRadiusAccountingData(buffer, size); + return Device->second->SendRadiusAccountingData(buffer, size); } catch (...) { poco_debug( Logger(), @@ -555,12 +555,12 @@ namespace OpenWifi { auto hashIndex = Utils::CalculateMacAddressHash(IntSerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(IntSerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second.second == nullptr) { + if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { return false; } try { - return Device->second.second->SendRadiusAuthenticationData(buffer, size); + return Device->second->SendRadiusAuthenticationData(buffer, size); } catch (...) { poco_debug( Logger(), @@ -576,12 +576,12 @@ namespace OpenWifi { auto hashIndex = Utils::CalculateMacAddressHash(IntSerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(IntSerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second.second == nullptr) { + if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { return false; } try { - return Device->second.second->SendRadiusCoAData(buffer, size); + return Device->second->SendRadiusCoAData(buffer, size); } catch (...) { poco_debug(Logger(), fmt::format(": SendRadiusCoAData: Could not send data to device '{}'", diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index ce32f01ba..7f4e94425 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -112,9 +112,9 @@ namespace OpenWifi { std::lock_guard G(SerialNumbersMutex_[hashIndex]); auto Connection = SerialNumbers_[hashIndex].find(serialNumber); - if (Connection==end(SerialNumbers_[hashIndex]) || Connection->second.second==nullptr) + if (Connection==end(SerialNumbers_[hashIndex]) || Connection->second==nullptr) return false; - return Connection->second.second->RttyMustBeSecure_; + return Connection->second->RttyMustBeSecure_; } inline bool GetStatistics(const std::string &SerialNumber, std::string &Statistics) const { @@ -208,11 +208,11 @@ namespace OpenWifi { if(session_hint==end(SerialNumbers_[hashIndex])) { return false; } - hasGPS = session_hint->second.second->hasGPS; - Sanity = session_hint->second.second->RawLastHealthcheck_.Sanity; - MemoryUsed = session_hint->second.second->memory_used_; - Load = session_hint->second.second->cpu_load_; - Temperature = session_hint->second.second->temperature_; + hasGPS = session_hint->second->hasGPS; + Sanity = session_hint->second->RawLastHealthcheck_.Sanity; + MemoryUsed = session_hint->second->memory_used_; + Load = session_hint->second->cpu_load_; + Temperature = session_hint->second->temperature_; return true; } @@ -233,8 +233,11 @@ namespace OpenWifi { std::atomic_bool Running_ = false; std::array>,256> Sessions_; - using SerialNumberMap = std::map>>; +// using SerialNumberMap = std::map>>; + + using SerialNumberMap = std::map>; std::array SerialNumbers_; mutable std::array SerialNumbersMutex_; From 14ef6608d685cee100cc814832c954683dbd07ef Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 12:29:01 -0800 Subject: [PATCH 040/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 43743ce63..3576cb09b 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -353,9 +353,9 @@ namespace OpenWifi { } Connection = DeviceHint->second; } - std::cout << __LINE__ << " " << Connection->State_.sessionId << ":" << Connection->SerialNumber_ << std::endl; + std::cout << __LINE__ << " Session" << Connection->State_.sessionId << " " << Connection->SerialNumber_ << std::endl; Connection->GetState(State); - std::cout << __LINE__ << " " << Connection->State_.sessionId << ":" << Connection->SerialNumber_ << std::endl; + std::cout << __LINE__ << " Session" << Connection->State_.sessionId << " " << Connection->SerialNumber_ << std::endl; return true; } @@ -381,7 +381,7 @@ namespace OpenWifi { std::lock_guard SessionLock(SessionMutex_[connection_id % 256]); auto ConnHint = Sessions_[connection_id % 256].find(connection_id); if (ConnHint == end(Sessions_[connection_id % 256])) { - std::cout << __LINE__ << ": Failed (1) to set connection details" << std::endl; + std::cout << __LINE__ << ": " << connection_id << " " << Connection->SerialNumber_ << " FAIL Set connection details" << std::endl; return; } Connection = ConnHint->second; @@ -393,10 +393,10 @@ namespace OpenWifi { (CurrentSerialNumber->second != nullptr && CurrentSerialNumber->second->State_.sessionId < connection_id)) { SerialNumbers_[hashIndex][SerialNumber] = Connection; Sessions_[connection_id % 256].erase(ConnHint); - std::cout << __LINE__ << ": Set connection details" << std::endl; + std::cout << __LINE__ << ": " << connection_id << " " << Connection->SerialNumber_ << " SET Set connection details" << std::endl; return; } - std::cout << __LINE__ << ": Failed to set connection details" << std::endl; + std::cout << __LINE__ << ": " << connection_id << " " << Connection->SerialNumber_ << " FAIL(2) Set connection details" << std::endl; } bool AP_WS_Server::EndSession(uint64_t session_id, uint64_t SerialNumber) { From 50fa1de62cb42a9bdfcc5230aa047aeee5da15a6 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 12:40:18 -0800 Subject: [PATCH 041/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index 6758f2c1d..bf1eb06a9 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -74,6 +74,7 @@ namespace OpenWifi { inline void GetState(GWObjects::ConnectionState &State) { std::cout << __LINE__ << ": GetState" << std::endl; std::lock_guard G(ConnectionMutex_); + std::cout << __LINE__ << ": GetState" << std::endl; State = State_; std::cout << __LINE__ << ": GetState" << std::endl; } From cc9d4c5f68dbabbe4a462dc34df7b0a8ce31bc95 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 12:42:57 -0800 Subject: [PATCH 042/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index bf1eb06a9..1ce854da1 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -73,10 +73,12 @@ namespace OpenWifi { inline void GetState(GWObjects::ConnectionState &State) { std::cout << __LINE__ << ": GetState" << std::endl; - std::lock_guard G(ConnectionMutex_); +// std::lock_guard G(ConnectionMutex_); + ConnectionMutex_.lock(); std::cout << __LINE__ << ": GetState" << std::endl; State = State_; std::cout << __LINE__ << ": GetState" << std::endl; + ConnectionMutex_.unlock(); } inline bool HasGPS() { return hasGPS; } From 63993789caec58e5440a30432724738274c9de8e Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 12:51:13 -0800 Subject: [PATCH 043/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 9 +++++---- src/AP_WS_Connection.h | 4 +--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 85f9effe6..e9bad5b1c 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -59,6 +59,11 @@ namespace OpenWifi { WS_->setKeepAlive(true); WS_->setBlocking(false); + Registered_ = true; + Valid_ = true; + uuid_ = MicroServiceRandom(std::numeric_limits::max()-1); + LastContact_ = Utils::Now(); + Reactor_->addEventHandler(*WS_, Poco::NObserver( *this, &AP_WS_Connection::OnSocketReadable)); @@ -68,10 +73,6 @@ namespace OpenWifi { Reactor_->addEventHandler(*WS_, Poco::NObserver( *this, &AP_WS_Connection::OnSocketError)); - Registered_ = true; - Valid_ = true; - uuid_ = MicroServiceRandom(std::numeric_limits::max()-1); - LastContact_ = Utils::Now(); } bool AP_WS_Connection::ValidatedDevice() { diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index 1ce854da1..bf1eb06a9 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -73,12 +73,10 @@ namespace OpenWifi { inline void GetState(GWObjects::ConnectionState &State) { std::cout << __LINE__ << ": GetState" << std::endl; -// std::lock_guard G(ConnectionMutex_); - ConnectionMutex_.lock(); + std::lock_guard G(ConnectionMutex_); std::cout << __LINE__ << ": GetState" << std::endl; State = State_; std::cout << __LINE__ << ": GetState" << std::endl; - ConnectionMutex_.unlock(); } inline bool HasGPS() { return hasGPS; } From 4c6fb85542c9867102d25273a47b2092a448df14 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 12:53:28 -0800 Subject: [PATCH 044/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index e9bad5b1c..bed9b2861 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -45,6 +45,9 @@ namespace OpenWifi { uint64_t connection_id, Poco::Logger &L, std::pair R) : Logger_(L) { + + std::lock_guard Guard(ConnectionMutex_); + Reactor_ = R.first; DbSession_ = R.second; State_.sessionId = connection_id; From 5b199bc4f52f7ab10e6911e991be805c8a2a1858 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 12:55:25 -0800 Subject: [PATCH 045/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index bf1eb06a9..22777fa62 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -134,7 +134,7 @@ namespace OpenWifi { inline bool MustBeSecureRtty() const { return RttyMustBeSecure_; } private: - std::mutex ConnectionMutex_; + std::recursive_mutex ConnectionMutex_; std::mutex TelemetryMutex_; Poco::Logger &Logger_; Poco::Net::SocketReactor *Reactor_{nullptr}; From 0bcfb26579f98b18489ce27efe2da2b66a800147 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 13:08:17 -0800 Subject: [PATCH 046/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.h | 6 +++--- src/AP_WS_Process_connect.cpp | 2 +- src/AP_WS_Server.cpp | 8 ++++++-- src/AP_WS_Server.h | 4 ++-- src/RESTAPI/RESTAPI_device_commandHandler.cpp | 4 ++-- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index 22777fa62..af3e592a8 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -131,10 +131,10 @@ namespace OpenWifi { return Restrictions_; } - inline bool MustBeSecureRtty() const { return RttyMustBeSecure_; } + inline bool MustBeSecureRTTY() const { return RTTYMustBeSecure_; } private: - std::recursive_mutex ConnectionMutex_; + std::mutex ConnectionMutex_; std::mutex TelemetryMutex_; Poco::Logger &Logger_; Poco::Net::SocketReactor *Reactor_{nullptr}; @@ -166,7 +166,7 @@ namespace OpenWifi { std::atomic_bool DeviceValidated_ = false; std::atomic_bool Valid_ = false; OpenWifi::GWObjects::DeviceRestrictions Restrictions_; - bool RttyMustBeSecure_ = false; + bool RTTYMustBeSecure_ = false; static inline std::atomic_uint64_t ConcurrentStartingDevices_ = 0; diff --git a/src/AP_WS_Process_connect.cpp b/src/AP_WS_Process_connect.cpp index 49422ab7d..59e9b6437 100644 --- a/src/AP_WS_Process_connect.cpp +++ b/src/AP_WS_Process_connect.cpp @@ -108,7 +108,7 @@ namespace OpenWifi { } if(Capabilities->has("secure-rtty")) { - RttyMustBeSecure_ = Capabilities->getValue("secure-rtty"); + RTTYMustBeSecure_ = Capabilities->getValue("secure-rtty"); } State_.locale = FindCountryFromIP()->Get(IP); diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 3576cb09b..764ffcba3 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -353,9 +353,13 @@ namespace OpenWifi { } Connection = DeviceHint->second; } - std::cout << __LINE__ << " Session" << Connection->State_.sessionId << " " << Connection->SerialNumber_ << std::endl; + + std::cout << __LINE__ << " Session" << Connection->State_.sessionId << " STATS " << Connection->SerialNumber_ << std::endl; + std::string S; + Connection->GetLastStats(S); + std::cout << __LINE__ << " Session" << Connection->State_.sessionId << " STATE " << Connection->SerialNumber_ << std::endl; Connection->GetState(State); - std::cout << __LINE__ << " Session" << Connection->State_.sessionId << " " << Connection->SerialNumber_ << std::endl; + std::cout << __LINE__ << " Session" << Connection->State_.sessionId << " STATE " << Connection->SerialNumber_ << std::endl; return true; } diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index 7f4e94425..53297efe4 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -107,14 +107,14 @@ namespace OpenWifi { Sessions_[session_id % 256][session_id] = std::move(Connection); } - inline bool DeviceRequiresSecureRtty(uint64_t serialNumber) const { + inline bool DeviceRequiresSecureRTTY(uint64_t serialNumber) const { auto hashIndex = Utils::CalculateMacAddressHash(serialNumber); std::lock_guard G(SerialNumbersMutex_[hashIndex]); auto Connection = SerialNumbers_[hashIndex].find(serialNumber); if (Connection==end(SerialNumbers_[hashIndex]) || Connection->second==nullptr) return false; - return Connection->second->RttyMustBeSecure_; + return Connection->second->RTTYMustBeSecure_; } inline bool GetStatistics(const std::string &SerialNumber, std::string &Statistics) const { diff --git a/src/RESTAPI/RESTAPI_device_commandHandler.cpp b/src/RESTAPI/RESTAPI_device_commandHandler.cpp index bc23d40c7..84ce8f319 100644 --- a/src/RESTAPI/RESTAPI_device_commandHandler.cpp +++ b/src/RESTAPI/RESTAPI_device_commandHandler.cpp @@ -87,7 +87,7 @@ namespace OpenWifi { poco_debug( Logger_, fmt::format( - "Command rtty TID={} can proceed. Identified as {} and RPCID as {}. thr_id={}", + "Command RTTY TID={} can proceed. Identified as {} and RPCID as {}. thr_id={}", TransactionId_, UUID, RPC, Poco::Thread::current()->id())); return Rtty(UUID, RPC, 60000ms, Restrictions); }; @@ -1169,7 +1169,7 @@ namespace OpenWifi { if (RTTYS_server()->UseInternal()) { std::uint64_t SN = Utils::SerialNumberToInt(SerialNumber_); - bool mTLS = AP_WS_Server()->DeviceRequiresSecureRtty(SN); + bool mTLS = AP_WS_Server()->DeviceRequiresSecureRTTY(SN); auto Hash = Utils::ComputeHash(UserInfo_.webtoken.refresh_token_, Utils::Now()); Rtty.Token = Hash.substr(0, RTTY_DEVICE_TOKEN_LENGTH); if (!RTTYS_server()->CreateEndPoint(Rtty.ConnectionId, Rtty.Token, Requester(), From 5f6300bb171ad9edcbe276b1188b522d311a92c6 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 13:44:01 -0800 Subject: [PATCH 047/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index bed9b2861..c9d72d66c 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -46,7 +46,7 @@ namespace OpenWifi { std::pair R) : Logger_(L) { - std::lock_guard Guard(ConnectionMutex_); +// std::lock_guard Guard(ConnectionMutex_); Reactor_ = R.first; DbSession_ = R.second; @@ -677,9 +677,9 @@ namespace OpenWifi { if (!AP_WS_Server()->Running()) return EndConnection(); - std::cout << __LINE__ << ": OnSocketReadable" << std::endl; - std::lock_guard DeviceLock(ConnectionMutex_); - std::cout << __LINE__ << ": OnSocketReadable" << std::endl; +// std::cout << __LINE__ << ": OnSocketReadable" << std::endl; +// std::lock_guard DeviceLock(ConnectionMutex_); +// std::cout << __LINE__ << ": OnSocketReadable" << std::endl; if (!ValidatedDevice()) return; From fbb9f40529a5bb7d8d7600bd33e753193d5b4fc0 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 13:53:46 -0800 Subject: [PATCH 048/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 764ffcba3..053eb3049 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -395,9 +395,9 @@ namespace OpenWifi { auto CurrentSerialNumber = SerialNumbers_[hashIndex].find(SerialNumber); if ((CurrentSerialNumber == SerialNumbers_[hashIndex].end()) || (CurrentSerialNumber->second != nullptr && CurrentSerialNumber->second->State_.sessionId < connection_id)) { - SerialNumbers_[hashIndex][SerialNumber] = Connection; - Sessions_[connection_id % 256].erase(ConnHint); std::cout << __LINE__ << ": " << connection_id << " " << Connection->SerialNumber_ << " SET Set connection details" << std::endl; + SerialNumbers_[hashIndex][SerialNumber] = std::move(Connection); + Sessions_[connection_id % 256].erase(ConnHint); return; } std::cout << __LINE__ << ": " << connection_id << " " << Connection->SerialNumber_ << " FAIL(2) Set connection details" << std::endl; From c3ad34d84b93505450e19ef9e790d516fe2ddc3a Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 14:41:48 -0800 Subject: [PATCH 049/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 12 ++-- src/AP_WS_Process_connect.cpp | 27 ++++++++- src/AP_WS_Server.cpp | 106 +++++++++++++++------------------- src/AP_WS_Server.h | 12 ++-- 4 files changed, 85 insertions(+), 72 deletions(-) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index c9d72d66c..4584aada4 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -42,15 +42,15 @@ namespace OpenWifi { AP_WS_Connection::AP_WS_Connection(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response, - uint64_t connection_id, Poco::Logger &L, + uint64_t session_id, Poco::Logger &L, std::pair R) : Logger_(L) { -// std::lock_guard Guard(ConnectionMutex_); + std::lock_guard Guard(ConnectionMutex_); Reactor_ = R.first; DbSession_ = R.second; - State_.sessionId = connection_id; + State_.sessionId = session_id; WS_ = std::make_unique(request, response); @@ -677,9 +677,9 @@ namespace OpenWifi { if (!AP_WS_Server()->Running()) return EndConnection(); -// std::cout << __LINE__ << ": OnSocketReadable" << std::endl; -// std::lock_guard DeviceLock(ConnectionMutex_); -// std::cout << __LINE__ << ": OnSocketReadable" << std::endl; + std::cout << __LINE__ << ": OnSocketReadable" << std::endl; + std::lock_guard DeviceLock(ConnectionMutex_); + std::cout << __LINE__ << ": OnSocketReadable" << std::endl; if (!ValidatedDevice()) return; diff --git a/src/AP_WS_Process_connect.cpp b/src/AP_WS_Process_connect.cpp index 59e9b6437..170fe536f 100644 --- a/src/AP_WS_Process_connect.cpp +++ b/src/AP_WS_Process_connect.cpp @@ -75,27 +75,30 @@ namespace OpenWifi { AP_WS_Server()->SetSessionDetails(State_.sessionId, SerialNumberInt_); std::cout << __LINE__ << ": set details" << std::endl; - std::lock_guard DbSessionLock(*DbSession_->Mutex); - Config::Capabilities Caps(Capabilities); Compatible_ = Caps.Compatible(); + std::cout << __LINE__ << ": set details" << std::endl; + State_.UUID = UUID; State_.Firmware = Firmware; State_.PendingUUID = 0; State_.Address = Utils::FormatIPv6(WS_->peerAddress().toString()); CId_ = SerialNumber_ + "@" + CId_; + std::cout << __LINE__ << ": set details" << std::endl; if(ParamsObj->has("reason")) { State_.connectReason = ParamsObj->get("reason").toString(); } + std::cout << __LINE__ << ": set details" << std::endl; auto IP = PeerAddress_.toString(); if (IP.substr(0, 7) == "::ffff:") { IP = IP.substr(7); } + std::cout << __LINE__ << ": set details" << std::endl; bool RestrictedDevice = false; if (Capabilities->has("restrictions")) { RestrictedDevice = true; @@ -103,21 +106,29 @@ namespace OpenWifi { Restrictions_.from_json(RestrictionObject); } + std::cout << __LINE__ << ": set details" << std::endl; if (Capabilities->has("developer")) { Restrictions_.developer = Capabilities->getValue("developer"); } + std::cout << __LINE__ << ": set details" << std::endl; if(Capabilities->has("secure-rtty")) { RTTYMustBeSecure_ = Capabilities->getValue("secure-rtty"); } + std::cout << __LINE__ << ": set details" << std::endl; State_.locale = FindCountryFromIP()->Get(IP); GWObjects::Device DeviceInfo; + std::cout << __LINE__ << ": set details" << std::endl; + std::lock_guard DbSessionLock(*DbSession_->Mutex); + std::cout << __LINE__ << ": set details" << std::endl; auto DeviceExists = StorageService()->GetDevice(*DbSession_->Session, SerialNumber_, DeviceInfo); + std::cout << __LINE__ << ": set details" << std::endl; if (Daemon()->AutoProvisioning() && !DeviceExists) { // check the firmware version. if this is too old, we cannot let that device connect yet, we must // force a firmware upgrade GWObjects::DefaultFirmware MinimumFirmware; + std::cout << __LINE__ << ": set details" << std::endl; if(FirmwareRevisionCache()->DeviceMustUpgrade(Compatible_, Firmware, MinimumFirmware)) { Poco::JSON::Object UpgradeCommand, Params; UpgradeCommand.set(uCentralProtocol::JSONRPC,uCentralProtocol::JSONRPC_VERSION); @@ -151,10 +162,12 @@ namespace OpenWifi { State_.VerifiedCertificate == GWObjects::SIMULATED); } } else if (!Daemon()->AutoProvisioning() && !DeviceExists) { + std::cout << __LINE__ << ": set details" << std::endl; SendKafkaDeviceNotProvisioned(SerialNumber_, Firmware, Compatible_, CId_); poco_warning(Logger(),fmt::format("Device {} is a {} from {} and cannot be provisioned.",SerialNumber_,Compatible_, CId_)); return EndConnection(); } else if (DeviceExists) { + std::cout << __LINE__ << ": set details" << std::endl; StorageService()->UpdateDeviceCapabilities(*DbSession_->Session, SerialNumber_, Caps); int Updated{0}; if (!Firmware.empty()) { @@ -175,6 +188,7 @@ namespace OpenWifi { } } + std::cout << __LINE__ << ": set details" << std::endl; if(ParamsObj->has("reason")) { State_.connectReason = ParamsObj->get("reason").toString(); DeviceInfo.connectReason = State_.connectReason; @@ -226,23 +240,30 @@ namespace OpenWifi { ++Updated; } + std::cout << __LINE__ << ": set details" << std::endl; if (Updated) { + std::cout << __LINE__ << ": set details" << std::endl; StorageService()->UpdateDevice(*DbSession_->Session, DeviceInfo); + std::cout << __LINE__ << ": set details" << std::endl; } if(!Simulated_) { uint64_t UpgradedUUID = 0; + std::cout << __LINE__ << ": set details" << std::endl; LookForUpgrade(UUID, UpgradedUUID); + std::cout << __LINE__ << ": set details" << std::endl; State_.UUID = UpgradedUUID; } } + std::cout << __LINE__ << ": set details" << std::endl; State_.Compatible = Compatible_; State_.Connected = true; ConnectionCompletionTime_ = std::chrono::high_resolution_clock::now() - ConnectionStart_; State_.connectionCompletionTime = ConnectionCompletionTime_.count(); + std::cout << __LINE__ << ": set details" << std::endl; if (State_.VerifiedCertificate == GWObjects::VALID_CERTIFICATE) { if ((Utils::SerialNumberMatch(CN_, SerialNumber_, (int)AP_WS_Server()->MismatchDepth())) || @@ -278,10 +299,12 @@ namespace OpenWifi { State_.connectionCompletionTime)); } + std::cout << __LINE__ << ": set details" << std::endl; GWWebSocketNotifications::SingleDevice_t Notification; Notification.content.serialNumber = SerialNumber_; GWWebSocketNotifications::DeviceConnected(Notification); + std::cout << __LINE__ << ": set details" << std::endl; if (KafkaManager()->Enabled()) { ParamsObj->set(uCentralProtocol::CONNECTIONIP, CId_); ParamsObj->set("locale", State_.locale); diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 053eb3049..8114c11c8 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -27,7 +27,7 @@ namespace OpenWifi { Poco::Net::HTTPServerResponse &response) { try { AP_WS_Server()->AddConnection( - id_, std::make_shared(request, response, id_, Logger_, + session_id_, std::make_shared(request, response, session_id_, Logger_, AP_WS_Server()->NextReactor())); } catch (...) { poco_warning(Logger_, "Exception during WS creation"); @@ -160,13 +160,6 @@ namespace OpenWifi { SimulatorEnabled_ = !SimulatorId_.empty(); Utils::SetThreadName(ReactorThread_, "dev:react:head"); -/* - GarbageCollectorCallback_ = std::make_unique>( - *this, &AP_WS_Server::onGarbageCollecting); - Timer_.setStartInterval(10 * 1000); - Timer_.setPeriodicInterval(10 * 1000); // every minute - Timer_.start(*GarbageCollectorCallback_, MicroServiceTimerPool()); -*/ Running_ = true; GarbageCollector_.setName("ws:garbage"); GarbageCollector_.start(*this); @@ -345,7 +338,7 @@ namespace OpenWifi { std::shared_ptr Connection; { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); - std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); + std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); if (DeviceHint == SerialNumbers_[hashIndex].end() || DeviceHint->second == nullptr) { @@ -377,52 +370,49 @@ namespace OpenWifi { } - void AP_WS_Server::SetSessionDetails(uint64_t connection_id, uint64_t SerialNumber) { + void AP_WS_Server::SetSessionDetails(uint64_t session_id, uint64_t SerialNumber) { std::shared_ptr Connection; std::cout << __LINE__ << ": Attempting to set connection details" << std::endl; - std::lock_guard SessionLock(SessionMutex_[connection_id % 256]); - auto ConnHint = Sessions_[connection_id % 256].find(connection_id); - if (ConnHint == end(Sessions_[connection_id % 256])) { - std::cout << __LINE__ << ": " << connection_id << " " << Connection->SerialNumber_ << " FAIL Set connection details" << std::endl; + std::lock_guard SessionLock(SessionMutex_[session_id % 256]); + auto SessionHint = Sessions_[session_id % 256].find(session_id); + if (SessionHint == end(Sessions_[session_id % 256])) { + std::cout << __LINE__ << ": " << session_id << " " << Connection->SerialNumber_ << " FAIL Set connection details" << std::endl; return; } - Connection = ConnHint->second; + Connection = SessionHint->second; auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto CurrentSerialNumber = SerialNumbers_[hashIndex].find(SerialNumber); if ((CurrentSerialNumber == SerialNumbers_[hashIndex].end()) || - (CurrentSerialNumber->second != nullptr && CurrentSerialNumber->second->State_.sessionId < connection_id)) { - std::cout << __LINE__ << ": " << connection_id << " " << Connection->SerialNumber_ << " SET Set connection details" << std::endl; - SerialNumbers_[hashIndex][SerialNumber] = std::move(Connection); - Sessions_[connection_id % 256].erase(ConnHint); + (CurrentSerialNumber->second != nullptr && CurrentSerialNumber->second->State_.sessionId < session_id)) { + std::cout << __LINE__ << ": " << session_id << " " << Connection->SerialNumber_ << " SET Set connection details" << std::endl; + SerialNumbers_[hashIndex][SerialNumber] = Connection; + Sessions_[session_id % 256].erase(SessionHint); return; } - std::cout << __LINE__ << ": " << connection_id << " " << Connection->SerialNumber_ << " FAIL(2) Set connection details" << std::endl; + std::cout << __LINE__ << ": " << session_id << " " << Connection->SerialNumber_ << " FAIL(2) Set connection details" << std::endl; } bool AP_WS_Server::EndSession(uint64_t session_id, uint64_t SerialNumber) { { std::lock_guard SessionLock(SessionMutex_[session_id % 256]); - auto Session = Sessions_[session_id % 256].find(session_id); - if (Session != end(Sessions_[session_id % 256])) { - Sessions_[session_id % 256].erase(Session); - } + Sessions_[session_id % 256].erase(session_id); } { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); - std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == SerialNumbers_[hashIndex].end() - || Device->second == nullptr - || Device->second->State_.sessionId != session_id) { + std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); + auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); + if (DeviceHint == SerialNumbers_[hashIndex].end() + || DeviceHint->second == nullptr + || DeviceHint->second->State_.sessionId != session_id) { return false; } - SerialNumbers_[hashIndex].erase(Device); + SerialNumbers_[hashIndex].erase(DeviceHint); } return true; } @@ -431,7 +421,7 @@ namespace OpenWifi { bool AP_WS_Server::Connected(uint64_t SerialNumber, GWObjects::DeviceRestrictions &Restrictions) const { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); - std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); + std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { return false; @@ -443,11 +433,11 @@ namespace OpenWifi { bool AP_WS_Server::Connected(uint64_t SerialNumber) const { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { + auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); + if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return false; } - return Device->second->State_.Connected; + return DeviceHint->second->State_.Connected; } bool AP_WS_Server::SendFrame(uint64_t SerialNumber, const std::string &Payload) const { @@ -482,12 +472,12 @@ namespace OpenWifi { uint64_t Interval, uint64_t Lifetime, const std::vector &TelemetryTypes) { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); - std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { + std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); + auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); + if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return; } - Device->second->SetWebSocketTelemetryReporting(RPCID, Interval, Lifetime, TelemetryTypes); + DeviceHint->second->SetWebSocketTelemetryReporting(RPCID, Interval, Lifetime, TelemetryTypes); } void AP_WS_Server::SetKafkaTelemetryReporting(uint64_t RPCID, uint64_t SerialNumber, @@ -504,12 +494,12 @@ namespace OpenWifi { void AP_WS_Server::StopKafkaTelemetry(uint64_t RPCID, uint64_t SerialNumber) { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); - std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { + std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); + auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); + if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return; } - Device->second->StopKafkaTelemetry(RPCID); + DeviceHint->second->StopKafkaTelemetry(RPCID); } void AP_WS_Server::GetTelemetryParameters( @@ -519,13 +509,13 @@ namespace OpenWifi { uint64_t &TelemetryWebSocketPackets, uint64_t &TelemetryKafkaPackets) { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); - std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { + std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); + auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); + if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return; } - Device->second->GetTelemetryParameters(TelemetryRunning, TelemetryInterval, + DeviceHint->second->GetTelemetryParameters(TelemetryRunning, TelemetryInterval, TelemetryWebSocketTimer, TelemetryKafkaTimer, TelemetryWebSocketCount, TelemetryKafkaCount, TelemetryWebSocketPackets, TelemetryKafkaPackets); @@ -536,14 +526,14 @@ namespace OpenWifi { auto IntSerialNumber = Utils::SerialNumberToInt(SerialNumber); auto hashIndex = Utils::CalculateMacAddressHash(IntSerialNumber); - std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - auto Device = SerialNumbers_[hashIndex].find(IntSerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { + std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); + auto DeviceHint = SerialNumbers_[hashIndex].find(IntSerialNumber); + if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return false; } try { - return Device->second->SendRadiusAccountingData(buffer, size); + return DeviceHint->second->SendRadiusAccountingData(buffer, size); } catch (...) { poco_debug( Logger(), @@ -557,14 +547,14 @@ namespace OpenWifi { const unsigned char *buffer, std::size_t size) { auto IntSerialNumber = Utils::SerialNumberToInt(SerialNumber); auto hashIndex = Utils::CalculateMacAddressHash(IntSerialNumber); - std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - auto Device = SerialNumbers_[hashIndex].find(IntSerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { + std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); + auto DeviceHint = SerialNumbers_[hashIndex].find(IntSerialNumber); + if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return false; } try { - return Device->second->SendRadiusAuthenticationData(buffer, size); + return DeviceHint->second->SendRadiusAuthenticationData(buffer, size); } catch (...) { poco_debug( Logger(), @@ -578,14 +568,14 @@ namespace OpenWifi { const unsigned char *buffer, std::size_t size) { auto IntSerialNumber = Utils::SerialNumberToInt(SerialNumber); auto hashIndex = Utils::CalculateMacAddressHash(IntSerialNumber); - std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - auto Device = SerialNumbers_[hashIndex].find(IntSerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { + std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); + auto DeviceHint = SerialNumbers_[hashIndex].find(IntSerialNumber); + if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return false; } try { - return Device->second->SendRadiusCoAData(buffer, size); + return DeviceHint->second->SendRadiusCoAData(buffer, size); } catch (...) { poco_debug(Logger(), fmt::format(": SendRadiusCoAData: Could not send data to device '{}'", diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index 53297efe4..d1b37beb9 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -33,14 +33,14 @@ namespace OpenWifi { class AP_WS_RequestHandler : public Poco::Net::HTTPRequestHandler { public: - explicit AP_WS_RequestHandler(Poco::Logger &L, uint64_t id) : Logger_(L), id_(id){}; + explicit AP_WS_RequestHandler(Poco::Logger &L, std::uint64_t session_id) : Logger_(L), session_id_(session_id){}; void handleRequest(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response) override; private: Poco::Logger &Logger_; - uint64_t id_ = 0; + std::uint64_t session_id_; }; class AP_WS_RequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { @@ -52,7 +52,7 @@ namespace OpenWifi { if (request.find("Upgrade") != request.end() && Poco::icompare(request["Upgrade"], "websocket") == 0) { Utils::SetThreadName("ws:conn-init"); - return new AP_WS_RequestHandler(Logger_, id_++); + return new AP_WS_RequestHandler(Logger_, session_id_++); } else { return nullptr; } @@ -60,7 +60,7 @@ namespace OpenWifi { private: Poco::Logger &Logger_; - inline static uint64_t id_ = 1; + inline static uint64_t session_id_ = 1; }; class AP_WS_Server : public SubSystemServer, public Poco::Runnable { @@ -150,8 +150,8 @@ namespace OpenWifi { bool SendRadiusCoAData(const std::string &SerialNumber, const unsigned char *buffer, std::size_t size); - void SetSessionDetails(uint64_t connection_id, uint64_t SerialNumber); - bool EndSession(uint64_t connection_id, uint64_t SerialNumber); + void SetSessionDetails(uint64_t session_id, uint64_t SerialNumber); + bool EndSession(uint64_t session_id, uint64_t SerialNumber); void SetWebSocketTelemetryReporting(uint64_t RPCID, uint64_t SerialNumber, uint64_t Interval, uint64_t Lifetime, const std::vector &TelemetryTypes); From 909ee66ef051fe340d5d420a324da510ba031ca4 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 15:02:17 -0800 Subject: [PATCH 050/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 6 +++--- src/AP_WS_Connection.h | 2 +- src/AP_WS_Process_connect.cpp | 2 +- src/AP_WS_Process_state.cpp | 4 +++- src/StorageService.h | 2 +- src/storage/storage_device.cpp | 17 +++++++++++++---- 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 4584aada4..00dd2dc0d 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -297,7 +297,7 @@ namespace OpenWifi { } } - bool AP_WS_Connection::LookForUpgrade(const uint64_t UUID, uint64_t &UpgradedUUID) { + bool AP_WS_Connection::LookForUpgrade(Poco::Data::Session &Session, const uint64_t UUID, uint64_t &UpgradedUUID) { // A UUID of zero means ignore updates for that connection. if (UUID == 0) @@ -310,12 +310,12 @@ namespace OpenWifi { } GWObjects::Device D; - if (StorageService()->GetDevice(*DbSession_,SerialNumber_, D)) { + if (StorageService()->GetDevice(Session,SerialNumber_, D)) { if(D.pendingUUID!=0 && UUID==D.pendingUUID) { // so we sent an upgrade to a device, and now it is completing now... UpgradedUUID = D.pendingUUID; - StorageService()->CompleteDeviceConfigurationChange(SerialNumber_); + StorageService()->CompleteDeviceConfigurationChange(Session, SerialNumber_); return true; } diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index af3e592a8..1339a99b8 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -45,7 +45,7 @@ namespace OpenWifi { void OnSocketReadable(const Poco::AutoPtr &pNf); void OnSocketShutdown(const Poco::AutoPtr &pNf); void OnSocketError(const Poco::AutoPtr &pNf); - bool LookForUpgrade(uint64_t UUID, uint64_t &UpgradedUUID); + bool LookForUpgrade(Poco::Data::Session &Session, uint64_t UUID, uint64_t &UpgradedUUID); void LogException(const Poco::Exception &E); inline Poco::Logger &Logger() { return Logger_; } bool SetWebSocketTelemetryReporting(uint64_t RPCID, uint64_t interval, diff --git a/src/AP_WS_Process_connect.cpp b/src/AP_WS_Process_connect.cpp index 170fe536f..fe576ba1b 100644 --- a/src/AP_WS_Process_connect.cpp +++ b/src/AP_WS_Process_connect.cpp @@ -250,7 +250,7 @@ namespace OpenWifi { if(!Simulated_) { uint64_t UpgradedUUID = 0; std::cout << __LINE__ << ": set details" << std::endl; - LookForUpgrade(UUID, UpgradedUUID); + LookForUpgrade(*DbSession_->Session, UUID, UpgradedUUID); std::cout << __LINE__ << ": set details" << std::endl; State_.UUID = UpgradedUUID; } diff --git a/src/AP_WS_Process_state.cpp b/src/AP_WS_Process_state.cpp index 7d99685b8..5100d0cd5 100644 --- a/src/AP_WS_Process_state.cpp +++ b/src/AP_WS_Process_state.cpp @@ -41,9 +41,11 @@ namespace OpenWifi { if(!Simulated_) { uint64_t UpgradedUUID; - LookForUpgrade(UUID, UpgradedUUID); + std::lock_guard Guard(*DbSession_->Mutex); + LookForUpgrade(*DbSession_->Session, UUID, UpgradedUUID); State_.UUID = UpgradedUUID; } + SetLastStats(StateStr); GWObjects::Statistics Stats{ diff --git a/src/StorageService.h b/src/StorageService.h index 857d7acd7..227d794cf 100644 --- a/src/StorageService.h +++ b/src/StorageService.h @@ -118,8 +118,8 @@ namespace OpenWifi { uint64_t &NewUUID); bool RollbackDeviceConfigurationChange(std::string & SerialNumber); + bool CompleteDeviceConfigurationChange(Poco::Data::Session &Session, std::string & SerialNumber); bool CompleteDeviceConfigurationChange(std::string & SerialNumber); - bool CreateDevice(LockedDbSession &Session, GWObjects::Device &); bool CreateDevice(GWObjects::Device &); bool CreateDefaultDevice(Poco::Data::Session &Session,std::string &SerialNumber, diff --git a/src/storage/storage_device.cpp b/src/storage/storage_device.cpp index b0af94528..43691522b 100644 --- a/src/storage/storage_device.cpp +++ b/src/storage/storage_device.cpp @@ -287,6 +287,16 @@ namespace OpenWifi { } bool Storage::CompleteDeviceConfigurationChange(std::string & SerialNumber) { + try { + auto Session = Pool_->get(); + return CompleteDeviceConfigurationChange(Session, SerialNumber); + } catch (const Poco::Exception &E) { + Logger().log(E); + } + return false; + } + + bool Storage::CompleteDeviceConfigurationChange(Poco::Data::Session & Session, std::string & SerialNumber) { try { GWObjects::Device D; if (!GetDevice(SerialNumber, D)) @@ -302,9 +312,8 @@ namespace OpenWifi { ConfigurationCache().Add(Utils::SerialNumberToInt(SerialNumber), D.UUID); - Poco::Data::Session Sess = Pool_->get(); - Sess.begin(); - Poco::Data::Statement Update(Sess); + Session.begin(); + Poco::Data::Statement Update(Session); DeviceRecordTuple R; ConvertDeviceRecord(D, R); @@ -313,7 +322,7 @@ namespace OpenWifi { Update << ConvertParams(St2), Poco::Data::Keywords::use(R), Poco::Data::Keywords::use(SerialNumber); Update.execute(); - Sess.commit(); + Session.commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); From 4120aefcd46e9105fa6c4a1ab4bd2fae310768cf Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 15:11:00 -0800 Subject: [PATCH 051/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 8 -------- src/AP_WS_Connection.h | 11 ----------- src/AP_WS_Server.cpp | 13 ------------- 3 files changed, 32 deletions(-) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 00dd2dc0d..44f965f52 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -291,9 +291,7 @@ namespace OpenWifi { Cleanup.detach(); } - std::cout << __LINE__ << ": EndConnection" << std::endl; AP_WS_Server()->EndSession(State_.sessionId, SerialNumberInt_); - std::cout << __LINE__ << ": EndConnection" << std::endl; } } @@ -653,18 +651,14 @@ namespace OpenWifi { void AP_WS_Connection::OnSocketShutdown( [[maybe_unused]] const Poco::AutoPtr &pNf) { poco_trace(Logger_, fmt::format("SOCKET-SHUTDOWN({}): Closing.", CId_)); - std::cout << __LINE__ << ": OnSocketShutdown" << std::endl; std::lock_guard G(ConnectionMutex_); - std::cout << __LINE__ << ": OnSocketShutdown" << std::endl; return EndConnection(); } void AP_WS_Connection::OnSocketError( [[maybe_unused]] const Poco::AutoPtr &pNf) { poco_trace(Logger_, fmt::format("SOCKET-ERROR({}): Closing.", CId_)); - std::cout << __LINE__ << ": OnSocketError" << std::endl; std::lock_guard G(ConnectionMutex_); - std::cout << __LINE__ << ": OnSocketError" << std::endl; return EndConnection(); } @@ -677,9 +671,7 @@ namespace OpenWifi { if (!AP_WS_Server()->Running()) return EndConnection(); - std::cout << __LINE__ << ": OnSocketReadable" << std::endl; std::lock_guard DeviceLock(ConnectionMutex_); - std::cout << __LINE__ << ": OnSocketReadable" << std::endl; if (!ValidatedDevice()) return; diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index 1339a99b8..de3fc9d73 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -58,33 +58,24 @@ namespace OpenWifi { bool StopKafkaTelemetry(uint64_t RPCID); inline void GetLastStats(std::string &LastStats) { - std::cout << __LINE__ << ": GetLastStats" << std::endl; std::lock_guard G(ConnectionMutex_); - std::cout << __LINE__ << ": GetLastStats" << std::endl; LastStats = RawLastStats_; } inline void GetLastHealthCheck(GWObjects::HealthCheck &H) { - std::cout << __LINE__ << ": GetLastHealthCheck" << std::endl; std::lock_guard G(ConnectionMutex_); - std::cout << __LINE__ << ": GetLastHealthCheck" << std::endl; H = RawLastHealthcheck_; } inline void GetState(GWObjects::ConnectionState &State) { - std::cout << __LINE__ << ": GetState" << std::endl; std::lock_guard G(ConnectionMutex_); - std::cout << __LINE__ << ": GetState" << std::endl; State = State_; - std::cout << __LINE__ << ": GetState" << std::endl; } inline bool HasGPS() { return hasGPS; } inline void GetRestrictions(GWObjects::DeviceRestrictions &R) { - std::cout << __LINE__ << ": GetRestrictions" << std::endl; std::lock_guard G(ConnectionMutex_); - std::cout << __LINE__ << ": GetRestrictions" << std::endl; R = Restrictions_; } @@ -125,9 +116,7 @@ namespace OpenWifi { friend class AP_WS_Server; inline GWObjects::DeviceRestrictions Restrictions() { - std::cout << __LINE__ << ": Restrictions" << std::endl; std::lock_guard G(ConnectionMutex_); - std::cout << __LINE__ << ": Restrictions" << std::endl; return Restrictions_; } diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 8114c11c8..851a78861 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -209,9 +209,7 @@ namespace OpenWifi { fmt::format( "{}: Session seems idle. Controller disconnecting device.", hint->second->SerialNumber_)); - std::cout << __LINE__ << ": AP_WS_Server" << std::endl; std::lock_guard ConnectionLock(hint->second->ConnectionMutex_); - std::cout << __LINE__ << ": AP_WS_Server" << std::endl; hint->second->EndConnection(); hint = SerialNumbers_[hashIndex].erase(hint); } else if (hint->second->State_.Connected) { @@ -346,13 +344,7 @@ namespace OpenWifi { } Connection = DeviceHint->second; } - - std::cout << __LINE__ << " Session" << Connection->State_.sessionId << " STATS " << Connection->SerialNumber_ << std::endl; - std::string S; - Connection->GetLastStats(S); - std::cout << __LINE__ << " Session" << Connection->State_.sessionId << " STATE " << Connection->SerialNumber_ << std::endl; Connection->GetState(State); - std::cout << __LINE__ << " Session" << Connection->State_.sessionId << " STATE " << Connection->SerialNumber_ << std::endl; return true; } @@ -373,12 +365,9 @@ namespace OpenWifi { void AP_WS_Server::SetSessionDetails(uint64_t session_id, uint64_t SerialNumber) { std::shared_ptr Connection; - std::cout << __LINE__ << ": Attempting to set connection details" << std::endl; - std::lock_guard SessionLock(SessionMutex_[session_id % 256]); auto SessionHint = Sessions_[session_id % 256].find(session_id); if (SessionHint == end(Sessions_[session_id % 256])) { - std::cout << __LINE__ << ": " << session_id << " " << Connection->SerialNumber_ << " FAIL Set connection details" << std::endl; return; } Connection = SessionHint->second; @@ -388,12 +377,10 @@ namespace OpenWifi { auto CurrentSerialNumber = SerialNumbers_[hashIndex].find(SerialNumber); if ((CurrentSerialNumber == SerialNumbers_[hashIndex].end()) || (CurrentSerialNumber->second != nullptr && CurrentSerialNumber->second->State_.sessionId < session_id)) { - std::cout << __LINE__ << ": " << session_id << " " << Connection->SerialNumber_ << " SET Set connection details" << std::endl; SerialNumbers_[hashIndex][SerialNumber] = Connection; Sessions_[session_id % 256].erase(SessionHint); return; } - std::cout << __LINE__ << ": " << session_id << " " << Connection->SerialNumber_ << " FAIL(2) Set connection details" << std::endl; } bool AP_WS_Server::EndSession(uint64_t session_id, uint64_t SerialNumber) { From 3f834a967b793731a5be087aa6e56df2a15b27c6 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 15:15:40 -0800 Subject: [PATCH 052/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Process_connect.cpp | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/src/AP_WS_Process_connect.cpp b/src/AP_WS_Process_connect.cpp index fe576ba1b..c897b30ed 100644 --- a/src/AP_WS_Process_connect.cpp +++ b/src/AP_WS_Process_connect.cpp @@ -71,34 +71,27 @@ namespace OpenWifi { CommandManager()->ClearQueue(SerialNumberInt_); - std::cout << __LINE__ << ": set details" << std::endl; AP_WS_Server()->SetSessionDetails(State_.sessionId, SerialNumberInt_); - std::cout << __LINE__ << ": set details" << std::endl; Config::Capabilities Caps(Capabilities); Compatible_ = Caps.Compatible(); - std::cout << __LINE__ << ": set details" << std::endl; - State_.UUID = UUID; State_.Firmware = Firmware; State_.PendingUUID = 0; State_.Address = Utils::FormatIPv6(WS_->peerAddress().toString()); CId_ = SerialNumber_ + "@" + CId_; - std::cout << __LINE__ << ": set details" << std::endl; if(ParamsObj->has("reason")) { State_.connectReason = ParamsObj->get("reason").toString(); } - std::cout << __LINE__ << ": set details" << std::endl; auto IP = PeerAddress_.toString(); if (IP.substr(0, 7) == "::ffff:") { IP = IP.substr(7); } - std::cout << __LINE__ << ": set details" << std::endl; bool RestrictedDevice = false; if (Capabilities->has("restrictions")) { RestrictedDevice = true; @@ -106,29 +99,22 @@ namespace OpenWifi { Restrictions_.from_json(RestrictionObject); } - std::cout << __LINE__ << ": set details" << std::endl; if (Capabilities->has("developer")) { Restrictions_.developer = Capabilities->getValue("developer"); } - std::cout << __LINE__ << ": set details" << std::endl; if(Capabilities->has("secure-rtty")) { RTTYMustBeSecure_ = Capabilities->getValue("secure-rtty"); } - std::cout << __LINE__ << ": set details" << std::endl; State_.locale = FindCountryFromIP()->Get(IP); GWObjects::Device DeviceInfo; - std::cout << __LINE__ << ": set details" << std::endl; std::lock_guard DbSessionLock(*DbSession_->Mutex); - std::cout << __LINE__ << ": set details" << std::endl; auto DeviceExists = StorageService()->GetDevice(*DbSession_->Session, SerialNumber_, DeviceInfo); - std::cout << __LINE__ << ": set details" << std::endl; if (Daemon()->AutoProvisioning() && !DeviceExists) { // check the firmware version. if this is too old, we cannot let that device connect yet, we must // force a firmware upgrade GWObjects::DefaultFirmware MinimumFirmware; - std::cout << __LINE__ << ": set details" << std::endl; if(FirmwareRevisionCache()->DeviceMustUpgrade(Compatible_, Firmware, MinimumFirmware)) { Poco::JSON::Object UpgradeCommand, Params; UpgradeCommand.set(uCentralProtocol::JSONRPC,uCentralProtocol::JSONRPC_VERSION); @@ -162,12 +148,10 @@ namespace OpenWifi { State_.VerifiedCertificate == GWObjects::SIMULATED); } } else if (!Daemon()->AutoProvisioning() && !DeviceExists) { - std::cout << __LINE__ << ": set details" << std::endl; SendKafkaDeviceNotProvisioned(SerialNumber_, Firmware, Compatible_, CId_); poco_warning(Logger(),fmt::format("Device {} is a {} from {} and cannot be provisioned.",SerialNumber_,Compatible_, CId_)); return EndConnection(); } else if (DeviceExists) { - std::cout << __LINE__ << ": set details" << std::endl; StorageService()->UpdateDeviceCapabilities(*DbSession_->Session, SerialNumber_, Caps); int Updated{0}; if (!Firmware.empty()) { @@ -188,7 +172,6 @@ namespace OpenWifi { } } - std::cout << __LINE__ << ": set details" << std::endl; if(ParamsObj->has("reason")) { State_.connectReason = ParamsObj->get("reason").toString(); DeviceInfo.connectReason = State_.connectReason; @@ -240,30 +223,23 @@ namespace OpenWifi { ++Updated; } - std::cout << __LINE__ << ": set details" << std::endl; if (Updated) { - std::cout << __LINE__ << ": set details" << std::endl; StorageService()->UpdateDevice(*DbSession_->Session, DeviceInfo); - std::cout << __LINE__ << ": set details" << std::endl; } if(!Simulated_) { uint64_t UpgradedUUID = 0; - std::cout << __LINE__ << ": set details" << std::endl; LookForUpgrade(*DbSession_->Session, UUID, UpgradedUUID); - std::cout << __LINE__ << ": set details" << std::endl; State_.UUID = UpgradedUUID; } } - std::cout << __LINE__ << ": set details" << std::endl; State_.Compatible = Compatible_; State_.Connected = true; ConnectionCompletionTime_ = std::chrono::high_resolution_clock::now() - ConnectionStart_; State_.connectionCompletionTime = ConnectionCompletionTime_.count(); - std::cout << __LINE__ << ": set details" << std::endl; if (State_.VerifiedCertificate == GWObjects::VALID_CERTIFICATE) { if ((Utils::SerialNumberMatch(CN_, SerialNumber_, (int)AP_WS_Server()->MismatchDepth())) || @@ -299,12 +275,10 @@ namespace OpenWifi { State_.connectionCompletionTime)); } - std::cout << __LINE__ << ": set details" << std::endl; GWWebSocketNotifications::SingleDevice_t Notification; Notification.content.serialNumber = SerialNumber_; GWWebSocketNotifications::DeviceConnected(Notification); - std::cout << __LINE__ << ": set details" << std::endl; if (KafkaManager()->Enabled()) { ParamsObj->set(uCentralProtocol::CONNECTIONIP, CId_); ParamsObj->set("locale", State_.locale); From a3c424d8fe7cdfdae0347afa697245a5fa2f24c7 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 15:39:14 -0800 Subject: [PATCH 053/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Process_connect.cpp | 1 + src/AP_WS_Process_state.cpp | 4 ++-- src/StorageService.h | 2 +- src/storage/storage_statistics.cpp | 9 ++++----- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/AP_WS_Process_connect.cpp b/src/AP_WS_Process_connect.cpp index c897b30ed..fb5e1d04f 100644 --- a/src/AP_WS_Process_connect.cpp +++ b/src/AP_WS_Process_connect.cpp @@ -110,6 +110,7 @@ namespace OpenWifi { State_.locale = FindCountryFromIP()->Get(IP); GWObjects::Device DeviceInfo; std::lock_guard DbSessionLock(*DbSession_->Mutex); + auto DeviceExists = StorageService()->GetDevice(*DbSession_->Session, SerialNumber_, DeviceInfo); if (Daemon()->AutoProvisioning() && !DeviceExists) { // check the firmware version. if this is too old, we cannot let that device connect yet, we must diff --git a/src/AP_WS_Process_state.cpp b/src/AP_WS_Process_state.cpp index 5100d0cd5..567c7cddf 100644 --- a/src/AP_WS_Process_state.cpp +++ b/src/AP_WS_Process_state.cpp @@ -39,9 +39,9 @@ namespace OpenWifi { UUID, request_uuid)); } + std::lock_guard Guard(*DbSession_->Mutex); if(!Simulated_) { uint64_t UpgradedUUID; - std::lock_guard Guard(*DbSession_->Mutex); LookForUpgrade(*DbSession_->Session, UUID, UpgradedUUID); State_.UUID = UpgradedUUID; } @@ -51,7 +51,7 @@ namespace OpenWifi { GWObjects::Statistics Stats{ .SerialNumber = SerialNumber_, .UUID = UUID, .Data = StateStr}; Stats.Recorded = Utils::Now(); - StorageService()->AddStatisticsData(*DbSession_,Stats); + StorageService()->AddStatisticsData(*DbSession_->Session,Stats); if (!request_uuid.empty()) { StorageService()->SetCommandResult(request_uuid, StateStr); } diff --git a/src/StorageService.h b/src/StorageService.h index 227d794cf..b5fc34060 100644 --- a/src/StorageService.h +++ b/src/StorageService.h @@ -92,7 +92,7 @@ namespace OpenWifi { // typedef std::map DeviceCapabilitiesCache; bool AddLog(LockedDbSession &Session, const GWObjects::DeviceLog &Log); - bool AddStatisticsData(LockedDbSession &Session, const GWObjects::Statistics &Stats); + bool AddStatisticsData(Poco::Data::Session &Session, const GWObjects::Statistics &Stats); bool AddStatisticsData(const GWObjects::Statistics &Stats); bool GetStatisticsData(std::string &SerialNumber, uint64_t FromDate, uint64_t ToDate, uint64_t Offset, uint64_t HowMany, diff --git a/src/storage/storage_statistics.cpp b/src/storage/storage_statistics.cpp index 48b75bf61..135808628 100644 --- a/src/storage/storage_statistics.cpp +++ b/src/storage/storage_statistics.cpp @@ -32,11 +32,10 @@ namespace OpenWifi { R.set<3>(Stats.Recorded); } - bool Storage::AddStatisticsData(LockedDbSession &Session, const GWObjects::Statistics &Stats) { + bool Storage::AddStatisticsData(Poco::Data::Session &Session, const GWObjects::Statistics &Stats) { try { - std::lock_guard Guard(*Session.Mutex); - Session.Session->begin(); - Poco::Data::Statement Insert(*Session.Session); + Session.begin(); + Poco::Data::Statement Insert(Session); poco_trace(Logger(), fmt::format("{}: Adding stats. Size={}", Stats.SerialNumber, std::to_string(Stats.Data.size()))); @@ -46,7 +45,7 @@ namespace OpenWifi { ConvertStatsRecord(Stats, R); Insert << ConvertParams(St), Poco::Data::Keywords::use(R); Insert.execute(); - Session.Session->commit(); + Session.commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), From 77cc0b250b8d5c656914c57b4577348178547be1 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 19:44:54 -0800 Subject: [PATCH 054/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 44f965f52..ed7baf835 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -668,11 +668,12 @@ namespace OpenWifi { if (!Valid_) return; + std::lock_guard DeviceLock(ConnectionMutex_); + if (!AP_WS_Server()->Running()) return EndConnection(); - std::lock_guard DeviceLock(ConnectionMutex_); - + LastContact_ = Utils::Now(); if (!ValidatedDevice()) return; From 6c13d845f197f78cb73e81971ce254276148244c Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 20:08:10 -0800 Subject: [PATCH 055/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 6 ++---- src/StorageService.h | 4 +++- src/storage/storage_device.cpp | 24 +++++++++++++++++++++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index ed7baf835..a788b4910 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -268,7 +268,7 @@ namespace OpenWifi { if (!Dead_.test_and_set()) { if(!SerialNumber_.empty() && State_.LastContact!=0) { - StorageService()->SetDeviceLastRecordedContact(*DbSession_, SerialNumber_, State_.LastContact); + StorageService()->SetDeviceLastRecordedContact(SerialNumber_, State_.LastContact); } if (Registered_) { @@ -681,18 +681,16 @@ namespace OpenWifi { return ProcessIncomingFrame(); } catch (const Poco::Exception &E) { Logger_.log(E); - return EndConnection(); } catch (const std::exception &E) { std::string W = E.what(); poco_information( Logger_, fmt::format("std::exception caught: {}. Connection terminated with {}", W, CId_)); - return EndConnection(); } catch (...) { poco_information(Logger_, fmt::format("Unknown exception for {}. Connection terminated.", CId_)); - return EndConnection(); } + return EndConnection(); } void AP_WS_Connection::ProcessIncomingFrame() { diff --git a/src/StorageService.h b/src/StorageService.h index b5fc34060..3136616a8 100644 --- a/src/StorageService.h +++ b/src/StorageService.h @@ -257,7 +257,9 @@ namespace OpenWifi { bool RemoveCommandListRecordsOlderThan(uint64_t Date); bool RemoveUploadedFilesRecordsOlderThan(uint64_t Date); - bool SetDeviceLastRecordedContact(LockedDbSession &Session, std::string & SeialNumber, std::uint64_t lastRecordedContact); + bool SetDeviceLastRecordedContact(LockedDbSession &Session, std::string & SerialNumber, std::uint64_t lastRecordedContact); + bool SetDeviceLastRecordedContact(std::string & SerialNumber, std::uint64_t lastRecordedContact); + bool SetDeviceLastRecordedContact(Poco::Data::Session & Session, std::string & SerialNumber, std::uint64_t lastRecordedContact); int Create_Tables(); int Create_Statistics(); diff --git a/src/storage/storage_device.cpp b/src/storage/storage_device.cpp index 43691522b..f0a7aa9c7 100644 --- a/src/storage/storage_device.cpp +++ b/src/storage/storage_device.cpp @@ -384,16 +384,34 @@ namespace OpenWifi { bool Storage::SetDeviceLastRecordedContact(LockedDbSession &Session, std::string &SerialNumber, std::uint64_t lastRecordedContact) { try { std::lock_guard Lock(*Session.Mutex); - Session.Session->begin(); - Poco::Data::Statement Update(*Session.Session); + return SetDeviceLastRecordedContact(*Session.Session, SerialNumber, lastRecordedContact); + } catch (const Poco::Exception &E) { + Logger().log(E); + } + return false; + } + + bool Storage::SetDeviceLastRecordedContact(Poco::Data::Session &Session, std::string &SerialNumber, std::uint64_t lastRecordedContact) { + try { + Session.begin(); + Poco::Data::Statement Update(Session); std::string St{"UPDATE Devices SET lastRecordedContact=? WHERE SerialNumber=?"}; Update << ConvertParams(St), Poco::Data::Keywords::use(lastRecordedContact), Poco::Data::Keywords::use(SerialNumber); Update.execute(); - Session.Session->commit(); + Session.commit(); return true; + } catch (const Poco::Exception &E) { + Logger().log(E); + } + return false; + } + bool Storage::SetDeviceLastRecordedContact(std::string &SerialNumber, std::uint64_t lastRecordedContact) { + try { + auto Session = Pool_->get(); + return SetDeviceLastRecordedContact(Session, SerialNumber, lastRecordedContact); } catch (const Poco::Exception &E) { Logger().log(E); } From 8c70ec5280b402e4247c23a677dc1c847894eb4a Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 22:08:25 -0800 Subject: [PATCH 056/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 851a78861..0afc52236 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -209,8 +209,8 @@ namespace OpenWifi { fmt::format( "{}: Session seems idle. Controller disconnecting device.", hint->second->SerialNumber_)); - std::lock_guard ConnectionLock(hint->second->ConnectionMutex_); - hint->second->EndConnection(); +// std::lock_guard ConnectionLock(hint->second->ConnectionMutex_); +// hint->second->EndConnection(); hint = SerialNumbers_[hashIndex].erase(hint); } else if (hint->second->State_.Connected) { NumberOfConnectedDevices_++; From bd067229487a5f197cdad0c88b15dd0b468dcb8a Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 22:09:24 -0800 Subject: [PATCH 057/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 0afc52236..dd322b898 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -202,7 +202,7 @@ namespace OpenWifi { while (hint != end(SerialNumbers_[hashIndex])) { if (hint->second == nullptr) { hint = SerialNumbers_[hashIndex].erase(hint); - } else if ((now - hint->second->State_.LastContact) > + } else if ((now - hint->second->LastContact_) > SessionTimeOut_) { poco_information( Logger(), From 3ffa35bb142b00a5fc3b8cae78f0d5b19a5ca481 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 22:30:01 -0800 Subject: [PATCH 058/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index dd322b898..7eed5680a 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -215,10 +215,10 @@ namespace OpenWifi { } else if (hint->second->State_.Connected) { NumberOfConnectedDevices_++; total_connected_time += (now - hint->second->State_.started); - hint++; + ++hint; } else { - NumberOfConnectingDevices_++; - hint++; + ++NumberOfConnectingDevices_; + ++hint; } } } @@ -229,6 +229,16 @@ namespace OpenWifi { std::lock_guard Lock(SessionMutex_[i]); auto hint = Sessions_[i].begin(); while (hint != end(Sessions_[i])) { + if(hint->second == nullptr) { + hint = Sessions_[i].erase(hint); + } else if (hint->second->State_.Connected) { + NumberOfConnectedDevices_++; + total_connected_time += (now - hint->second->State_.started); + ++hint; + } else { + ++NumberOfConnectingDevices_; + ++hint; + } if ((now - hint->second->LastContact_) > SessionTimeOut_) { poco_information( Logger(), @@ -237,8 +247,8 @@ namespace OpenWifi { hint->second->SerialNumber_)); hint = Sessions_[i].erase(hint); } else { - LeftOverSessions_++; - hint++; + ++LeftOverSessions_; + ++hint; } } } From b1c6884d49177b95dff4b3732f42ee3ada7e1d0e Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 22:30:54 -0800 Subject: [PATCH 059/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build b/build index 8580e7b68..b74e882ae 100644 --- a/build +++ b/build @@ -1 +1 @@ -30 \ No newline at end of file +31 \ No newline at end of file From d4dbbeb54bf88d59e22162ffa71fdf9bc06a009e Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 30 Nov 2023 22:43:34 -0800 Subject: [PATCH 060/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 7eed5680a..3bf72e6c7 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -231,15 +231,7 @@ namespace OpenWifi { while (hint != end(Sessions_[i])) { if(hint->second == nullptr) { hint = Sessions_[i].erase(hint); - } else if (hint->second->State_.Connected) { - NumberOfConnectedDevices_++; - total_connected_time += (now - hint->second->State_.started); - ++hint; - } else { - ++NumberOfConnectingDevices_; - ++hint; - } - if ((now - hint->second->LastContact_) > SessionTimeOut_) { + } else if ((now - hint->second->LastContact_) > SessionTimeOut_) { poco_information( Logger(), fmt::format( From 329d8d4441ebd9ca6c8fd92d8aab3e67255dd413 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Fri, 1 Dec 2023 10:02:41 -0800 Subject: [PATCH 061/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Connection.cpp | 122 +++++++++++++++++++++------------- src/AP_WS_Connection.h | 101 +++++++++++----------------- src/AP_WS_Process_connect.cpp | 2 +- src/AP_WS_Server.cpp | 119 ++++++++++++++++++++++++++++----- src/AP_WS_Server.h | 99 ++++++--------------------- 6 files changed, 235 insertions(+), 210 deletions(-) diff --git a/build b/build index b74e882ae..1758dddcc 100644 --- a/build +++ b/build @@ -1 +1 @@ -31 \ No newline at end of file +32 \ No newline at end of file diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index a788b4910..e83d35dad 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -46,8 +46,6 @@ namespace OpenWifi { std::pair R) : Logger_(L) { - std::lock_guard Guard(ConnectionMutex_); - Reactor_ = R.first; DbSession_ = R.second; State_.sessionId = session_id; @@ -63,7 +61,6 @@ namespace OpenWifi { WS_->setBlocking(false); Registered_ = true; - Valid_ = true; uuid_ = MicroServiceRandom(std::numeric_limits::max()-1); LastContact_ = Utils::Now(); @@ -78,13 +75,51 @@ namespace OpenWifi { *this, &AP_WS_Connection::OnSocketError)); } + AP_WS_Connection::~AP_WS_Connection() { + poco_information(Logger_, fmt::format("DESTRUCTOR({}): Session={} Connection closed.", SerialNumber_, + State_.sessionId)); + EndConnection(); + } + + void AP_WS_Connection::EndConnection() { + if (!Dead_.test_and_set()) { + + if(!SerialNumber_.empty() && State_.LastContact!=0) { + StorageService()->SetDeviceLastRecordedContact(SerialNumber_, State_.LastContact); + } + + if (Registered_) { + Registered_ = false; + Reactor_->removeEventHandler( + *WS_, Poco::NObserver( + *this, &AP_WS_Connection::OnSocketReadable)); + Reactor_->removeEventHandler( + *WS_, Poco::NObserver( + *this, &AP_WS_Connection::OnSocketShutdown)); + Reactor_->removeEventHandler( + *WS_, Poco::NObserver( + *this, &AP_WS_Connection::OnSocketError)); + Registered_=false; + } + WS_->close(); + + if(!SerialNumber_.empty()) { + std::thread Cleanup(DeviceDisconnectionCleanup,SerialNumber_, uuid_); + Cleanup.detach(); + } + + AP_WS_Server()->EndSession(State_.sessionId, SerialNumberInt_); + } + } + bool AP_WS_Connection::ValidatedDevice() { - if (DeviceValidated_) - return true; - if (!Valid_) + if(Dead_.test()) return false; + if (DeviceValidated_) + return true; + try { auto SockImpl = dynamic_cast(WS_->impl()); auto SS = @@ -246,14 +281,7 @@ namespace OpenWifi { } } - AP_WS_Connection::~AP_WS_Connection() { - if(Valid_) { - Valid_ = false; - EndConnection(); - } - } - - void DeviceDisconnectionCleanup(const std::string &SerialNumber, std::uint64_t uuid) { + void AP_WS_Connection::DeviceDisconnectionCleanup(const std::string &SerialNumber, std::uint64_t uuid) { if (KafkaManager()->Enabled()) { NotifyKafkaDisconnect(SerialNumber, uuid); } @@ -263,38 +291,6 @@ namespace OpenWifi { GWWebSocketNotifications::DeviceDisconnected(N); } - void AP_WS_Connection::EndConnection() { - Valid_ = false; - if (!Dead_.test_and_set()) { - - if(!SerialNumber_.empty() && State_.LastContact!=0) { - StorageService()->SetDeviceLastRecordedContact(SerialNumber_, State_.LastContact); - } - - if (Registered_) { - Registered_ = false; - Reactor_->removeEventHandler( - *WS_, Poco::NObserver( - *this, &AP_WS_Connection::OnSocketReadable)); - Reactor_->removeEventHandler( - *WS_, Poco::NObserver( - *this, &AP_WS_Connection::OnSocketShutdown)); - Reactor_->removeEventHandler( - *WS_, Poco::NObserver( - *this, &AP_WS_Connection::OnSocketError)); - Registered_=false; - } - WS_->close(); - - if(!SerialNumber_.empty()) { - std::thread Cleanup(DeviceDisconnectionCleanup,SerialNumber_, uuid_); - Cleanup.detach(); - } - - AP_WS_Server()->EndSession(State_.sessionId, SerialNumberInt_); - } - } - bool AP_WS_Connection::LookForUpgrade(Poco::Data::Session &Session, const uint64_t UUID, uint64_t &UpgradedUUID) { // A UUID of zero means ignore updates for that connection. @@ -665,7 +661,7 @@ namespace OpenWifi { void AP_WS_Connection::OnSocketReadable( [[maybe_unused]] const Poco::AutoPtr &pNf) { - if (!Valid_) + if (Dead_.test()) // we are dead, so we do not process anything. return; std::lock_guard DeviceLock(ConnectionMutex_); @@ -982,4 +978,36 @@ namespace OpenWifi { } } } + + void AP_WS_Connection::SetLastStats(const std::string &LastStats) { + RawLastStats_ = LastStats; + try { + Poco::JSON::Parser P; + auto Stats = P.parse(LastStats).extract(); + hasGPS_ = Stats->isObject("gps"); + auto Unit = Stats->getObject("unit"); + auto Memory = Unit->getObject("memory"); + std::uint64_t TotalMemory = Memory->get("total"); + std::uint64_t FreeMemory = Memory->get("free"); + if (TotalMemory > 0) { + memory_used_ = + (100.0 * ((double)TotalMemory - (double)FreeMemory)) / (double)TotalMemory; + } + if (Unit->isArray("load")) { + Poco::JSON::Array::Ptr Load = Unit->getArray("load"); + if (Load->size() > 1) { + cpu_load_ = Load->get(1); + } + } + if (Unit->isArray("temperature")) { + Poco::JSON::Array::Ptr Temperature = Unit->getArray("temperature"); + if (Temperature->size() > 1) { + temperature_ = Temperature->get(0); + } + } + } catch (const Poco::Exception &E) { + poco_error(Logger_, "Failed to parse last stats: " + E.displayText()); + } + } + } // namespace OpenWifi \ No newline at end of file diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index de3fc9d73..f5d27fd0d 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -37,6 +37,7 @@ namespace OpenWifi { void ProcessIncomingRadiusData(const Poco::JSON::Object::Ptr &Doc); [[nodiscard]] bool Send(const std::string &Payload); + [[nodiscard]] inline bool MustBeSecureRTTY() const { return RTTYMustBeSecure_; } bool SendRadiusAuthenticationData(const unsigned char *buffer, std::size_t size); bool SendRadiusAccountingData(const unsigned char *buffer, std::size_t size); @@ -58,45 +59,34 @@ namespace OpenWifi { bool StopKafkaTelemetry(uint64_t RPCID); inline void GetLastStats(std::string &LastStats) { - std::lock_guard G(ConnectionMutex_); - LastStats = RawLastStats_; + if(!Dead_.test()) { + std::lock_guard G(ConnectionMutex_); + LastStats = RawLastStats_; + } } inline void GetLastHealthCheck(GWObjects::HealthCheck &H) { - std::lock_guard G(ConnectionMutex_); - H = RawLastHealthcheck_; + if(!Dead_.test()) { + std::lock_guard G(ConnectionMutex_); + H = RawLastHealthcheck_; + } } inline void GetState(GWObjects::ConnectionState &State) { - std::lock_guard G(ConnectionMutex_); - State = State_; + if(!Dead_.test()) { + std::lock_guard G(ConnectionMutex_); + State = State_; + } } - inline bool HasGPS() { return hasGPS; } + [[nodiscard]] inline bool HasGPS() const { return hasGPS_; } + [[nodiscard]] bool ValidatedDevice(); inline void GetRestrictions(GWObjects::DeviceRestrictions &R) { std::lock_guard G(ConnectionMutex_); R = Restrictions_; } - void Process_connect(Poco::JSON::Object::Ptr ParamsObj, const std::string &Serial); - void Process_state(Poco::JSON::Object::Ptr ParamsObj); - void Process_healthcheck(Poco::JSON::Object::Ptr ParamsObj); - void Process_log(Poco::JSON::Object::Ptr ParamsObj); - void Process_crashlog(Poco::JSON::Object::Ptr ParamsObj); - void Process_ping(Poco::JSON::Object::Ptr ParamsObj); - void Process_cfgpending(Poco::JSON::Object::Ptr ParamsObj); - void Process_recovery(Poco::JSON::Object::Ptr ParamsObj); - void Process_deviceupdate(Poco::JSON::Object::Ptr ParamsObj, std::string &Serial); - void Process_telemetry(Poco::JSON::Object::Ptr ParamsObj); - void Process_venuebroadcast(Poco::JSON::Object::Ptr ParamsObj); - void Process_event(Poco::JSON::Object::Ptr ParamsObj); - void Process_wifiscan(Poco::JSON::Object::Ptr ParamsObj); - void Process_alarm(Poco::JSON::Object::Ptr ParamsObj); - void Process_rebootLog(Poco::JSON::Object::Ptr ParamsObj); - - bool ValidatedDevice(); - inline bool GetTelemetryParameters(bool &Reporting, uint64_t &Interval, uint64_t &WebSocketTimer, uint64_t &KafkaTimer, uint64_t &WebSocketCount, uint64_t &KafkaCount, @@ -120,8 +110,6 @@ namespace OpenWifi { return Restrictions_; } - inline bool MustBeSecureRTTY() const { return RTTYMustBeSecure_; } - private: std::mutex ConnectionMutex_; std::mutex TelemetryMutex_; @@ -153,51 +141,36 @@ namespace OpenWifi { std::chrono::duration ConnectionCompletionTime_{0.0}; std::atomic_flag Dead_ = false; std::atomic_bool DeviceValidated_ = false; - std::atomic_bool Valid_ = false; OpenWifi::GWObjects::DeviceRestrictions Restrictions_; bool RTTYMustBeSecure_ = false; + bool hasGPS_=false; + std::double_t memory_used_=0.0, cpu_load_ = 0.0, temperature_ = 0.0; + std::uint64_t uuid_=0; + bool Simulated_=false; + std::uint64_t LastContact_=0; static inline std::atomic_uint64_t ConcurrentStartingDevices_ = 0; bool StartTelemetry(uint64_t RPCID, const std::vector &TelemetryTypes); bool StopTelemetry(uint64_t RPCID); void UpdateCounts(); - bool hasGPS=false; - std::double_t memory_used_=0.0, cpu_load_ = 0.0, temperature_ = 0.0; - std::uint64_t uuid_=0; - bool Simulated_=false; - std::uint64_t LastContact_=0; - - inline void SetLastStats(const std::string &LastStats) { - RawLastStats_ = LastStats; - try { - Poco::JSON::Parser P; - auto Stats = P.parse(LastStats).extract(); - hasGPS = Stats->isObject("gps"); - auto Unit = Stats->getObject("unit"); - auto Memory = Unit->getObject("memory"); - std::uint64_t TotalMemory = Memory->get("total"); - std::uint64_t FreeMemory = Memory->get("free"); - if(TotalMemory>0) { - memory_used_ = - (100.0 * ((double)TotalMemory - (double)FreeMemory)) / (double)TotalMemory; - } - if(Unit->isArray("load")) { - Poco::JSON::Array::Ptr Load = Unit->getArray("load"); - if(Load->size()>1) { - cpu_load_ = Load->get(1); - } - } - if(Unit->isArray("temperature")) { - Poco::JSON::Array::Ptr Temperature = Unit->getArray("temperature"); - if(Temperature->size()>1) { - temperature_ = Temperature->get(0); - } - } - } catch (const Poco::Exception &E) { - poco_error(Logger_, "Failed to parse last stats: " + E.displayText()); - } - } + static void DeviceDisconnectionCleanup(const std::string &SerialNumber, std::uint64_t uuid); + void SetLastStats(const std::string &LastStats); + void Process_connect(Poco::JSON::Object::Ptr ParamsObj, const std::string &Serial); + void Process_state(Poco::JSON::Object::Ptr ParamsObj); + void Process_healthcheck(Poco::JSON::Object::Ptr ParamsObj); + void Process_log(Poco::JSON::Object::Ptr ParamsObj); + void Process_crashlog(Poco::JSON::Object::Ptr ParamsObj); + void Process_ping(Poco::JSON::Object::Ptr ParamsObj); + void Process_cfgpending(Poco::JSON::Object::Ptr ParamsObj); + void Process_recovery(Poco::JSON::Object::Ptr ParamsObj); + void Process_deviceupdate(Poco::JSON::Object::Ptr ParamsObj, std::string &Serial); + void Process_telemetry(Poco::JSON::Object::Ptr ParamsObj); + void Process_venuebroadcast(Poco::JSON::Object::Ptr ParamsObj); + void Process_event(Poco::JSON::Object::Ptr ParamsObj); + void Process_wifiscan(Poco::JSON::Object::Ptr ParamsObj); + void Process_alarm(Poco::JSON::Object::Ptr ParamsObj); + void Process_rebootLog(Poco::JSON::Object::Ptr ParamsObj); inline void SetLastHealthCheck(const GWObjects::HealthCheck &H) { RawLastHealthcheck_ = H; diff --git a/src/AP_WS_Process_connect.cpp b/src/AP_WS_Process_connect.cpp index fb5e1d04f..d10f707c1 100644 --- a/src/AP_WS_Process_connect.cpp +++ b/src/AP_WS_Process_connect.cpp @@ -71,7 +71,7 @@ namespace OpenWifi { CommandManager()->ClearQueue(SerialNumberInt_); - AP_WS_Server()->SetSessionDetails(State_.sessionId, SerialNumberInt_); + AP_WS_Server()->StartSession(State_.sessionId, SerialNumberInt_); Config::Capabilities Caps(Capabilities); diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 3bf72e6c7..4b7a9ce4c 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -23,6 +23,39 @@ namespace OpenWifi { + class AP_WS_RequestHandler : public Poco::Net::HTTPRequestHandler { + public: + explicit AP_WS_RequestHandler(Poco::Logger &L, std::uint64_t session_id) : Logger_(L), session_id_(session_id){}; + + void handleRequest(Poco::Net::HTTPServerRequest &request, + Poco::Net::HTTPServerResponse &response) override; + + private: + Poco::Logger &Logger_; + std::uint64_t session_id_; + }; + + class AP_WS_RequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { + public: + inline explicit AP_WS_RequestHandlerFactory(Poco::Logger &L) : Logger_(L) {} + + inline Poco::Net::HTTPRequestHandler * + createRequestHandler(const Poco::Net::HTTPServerRequest &request) override { + if (request.find("Upgrade") != request.end() && + Poco::icompare(request["Upgrade"], "websocket") == 0) { + Utils::SetThreadName("ws:conn-init"); + return new AP_WS_RequestHandler(Logger_, session_id_++); + } else { + return nullptr; + } + } + + private: + Poco::Logger &Logger_; + inline static uint64_t session_id_ = 1; + }; + + void AP_WS_RequestHandler::handleRequest(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response) { try { @@ -156,7 +189,7 @@ namespace OpenWifi { UseDefaultConfig_ = true; } - SimulatorId_ = MicroServiceConfigGetString("simulatorid", ""); + SimulatorId_ = Poco::toLower(MicroServiceConfigGetString("simulatorid", "")); SimulatorEnabled_ = !SimulatorId_.empty(); Utils::SetThreadName(ReactorThread_, "dev:react:head"); @@ -364,7 +397,7 @@ namespace OpenWifi { } - void AP_WS_Server::SetSessionDetails(uint64_t session_id, uint64_t SerialNumber) { + void AP_WS_Server::StartSession(uint64_t session_id, uint64_t SerialNumber) { std::shared_ptr Connection; std::lock_guard SessionLock(SessionMutex_[session_id % 256]); @@ -411,14 +444,18 @@ namespace OpenWifi { GWObjects::DeviceRestrictions &Restrictions) const { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); - auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { + auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); + if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return false; } - Device->second->GetRestrictions(Restrictions); - return Device->second->State_.Connected; + if(!DeviceHint->second->Dead_.test()) { + DeviceHint->second->GetRestrictions(Restrictions); + return DeviceHint->second->State_.Connected; + } + return false; } + bool AP_WS_Server::Connected(uint64_t SerialNumber) const { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); @@ -426,19 +463,26 @@ namespace OpenWifi { if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return false; } - return DeviceHint->second->State_.Connected; + if(!DeviceHint->second->Dead_.test()) { + return DeviceHint->second->State_.Connected; + } + return false; } bool AP_WS_Server::SendFrame(uint64_t SerialNumber, const std::string &Payload) const { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { + auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); + if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { + return false; + } + + if(DeviceHint->second->Dead_.test()) { return false; } try { - return Device->second->Send(Payload); + return DeviceHint->second->Send(Payload); } catch (...) { poco_debug(Logger(), fmt::format(": SendFrame: Could not send data to device '{}'", Utils::IntToSerialNumber(SerialNumber))); @@ -474,16 +518,16 @@ namespace OpenWifi { const std::vector &TelemetryTypes) { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - auto Device = SerialNumbers_[hashIndex].find(SerialNumber); - if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { + auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); + if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return; } - Device->second->SetKafkaTelemetryReporting(RPCID, Interval, Lifetime, TelemetryTypes); + DeviceHint->second->SetKafkaTelemetryReporting(RPCID, Interval, Lifetime, TelemetryTypes); } void AP_WS_Server::StopKafkaTelemetry(uint64_t RPCID, uint64_t SerialNumber) { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); - std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); + std::lock_guard DevicesLock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return; @@ -498,7 +542,7 @@ namespace OpenWifi { uint64_t &TelemetryWebSocketPackets, uint64_t &TelemetryKafkaPackets) { auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); - std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); + std::lock_guard DevicesLock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return; @@ -515,12 +559,16 @@ namespace OpenWifi { auto IntSerialNumber = Utils::SerialNumberToInt(SerialNumber); auto hashIndex = Utils::CalculateMacAddressHash(IntSerialNumber); - std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); + std::lock_guard DevicesLock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(IntSerialNumber); if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return false; } + if(DeviceHint->second->Dead_.test()) { + return false; + } + try { return DeviceHint->second->SendRadiusAccountingData(buffer, size); } catch (...) { @@ -536,12 +584,16 @@ namespace OpenWifi { const unsigned char *buffer, std::size_t size) { auto IntSerialNumber = Utils::SerialNumberToInt(SerialNumber); auto hashIndex = Utils::CalculateMacAddressHash(IntSerialNumber); - std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); + std::lock_guard DevicesLock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(IntSerialNumber); if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return false; } + if(DeviceHint->second->Dead_.test()) { + return false; + } + try { return DeviceHint->second->SendRadiusAuthenticationData(buffer, size); } catch (...) { @@ -557,12 +609,15 @@ namespace OpenWifi { const unsigned char *buffer, std::size_t size) { auto IntSerialNumber = Utils::SerialNumberToInt(SerialNumber); auto hashIndex = Utils::CalculateMacAddressHash(IntSerialNumber); - std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); + std::lock_guard DevicesGuard(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(IntSerialNumber); if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return false; } + if(DeviceHint->second->Dead_.test()) { + return false; + } try { return DeviceHint->second->SendRadiusCoAData(buffer, size); } catch (...) { @@ -573,4 +628,32 @@ namespace OpenWifi { return false; } + bool AP_WS_Server::ExtendedAttributes(const std::string &serialNumber, + bool & hasGPS, + std::uint64_t &Sanity, + std::double_t &MemoryUsed, + std::double_t &Load, + std::double_t &Temperature + ) { + + auto serialNumberInt = Utils::SerialNumberToInt(serialNumber); + auto hashIndex = Utils::CalculateMacAddressHash(serialNumberInt); + std::lock_guard DevicesGuard(SerialNumbersMutex_[hashIndex]); + auto DeviceHint = SerialNumbers_[hashIndex].find(Utils::SerialNumberToInt(serialNumber)); + if(DeviceHint==end(SerialNumbers_[hashIndex])) { + return false; + } + if(DeviceHint->second->Dead_.test()) { + return false; + } + std::lock_guard DeviceGuard(DeviceHint->second->ConnectionMutex_); + hasGPS = DeviceHint->second->hasGPS_; + Sanity = DeviceHint->second->RawLastHealthcheck_.Sanity; + MemoryUsed = DeviceHint->second->memory_used_; + Load = DeviceHint->second->cpu_load_; + Temperature = DeviceHint->second->temperature_; + return true; + } + + } // namespace OpenWifi \ No newline at end of file diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index d1b37beb9..f7ee53e21 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -31,38 +31,6 @@ namespace OpenWifi { - class AP_WS_RequestHandler : public Poco::Net::HTTPRequestHandler { - public: - explicit AP_WS_RequestHandler(Poco::Logger &L, std::uint64_t session_id) : Logger_(L), session_id_(session_id){}; - - void handleRequest(Poco::Net::HTTPServerRequest &request, - Poco::Net::HTTPServerResponse &response) override; - - private: - Poco::Logger &Logger_; - std::uint64_t session_id_; - }; - - class AP_WS_RequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { - public: - inline explicit AP_WS_RequestHandlerFactory(Poco::Logger &L) : Logger_(L) {} - - inline Poco::Net::HTTPRequestHandler * - createRequestHandler(const Poco::Net::HTTPServerRequest &request) override { - if (request.find("Upgrade") != request.end() && - Poco::icompare(request["Upgrade"], "websocket") == 0) { - Utils::SetThreadName("ws:conn-init"); - return new AP_WS_RequestHandler(Logger_, session_id_++); - } else { - return nullptr; - } - } - - private: - Poco::Logger &Logger_; - inline static uint64_t session_id_ = 1; - }; - class AP_WS_Server : public SubSystemServer, public Poco::Runnable { public: static auto instance() { @@ -77,29 +45,24 @@ namespace OpenWifi { const Poco::Crypto::X509Certificate &Certificate); inline bool IsSimSerialNumber(const std::string &SerialNumber) const { - return IsSim(Poco::toLower(SerialNumber)) && - Poco::toLower(SerialNumber) == Poco::toLower(SimulatorId_); + return IsSim(SerialNumber) && + SerialNumber == SimulatorId_; } inline static bool IsSim(const std::string &SerialNumber) { return SerialNumber.substr(0, 6) == "53494d"; } - void run() override; - - inline bool IsSimEnabled() const { return SimulatorEnabled_; } - - inline bool AllowSerialNumberMismatch() const { return AllowSerialNumberMismatch_; } - - inline uint64_t MismatchDepth() const { return MismatchDepth_; } - - inline bool UseProvisioning() const { return LookAtProvisioning_; } - inline bool UseDefaults() const { return UseDefaultConfig_; } - + void run() override; // Garbage collector thread. + [[nodiscard]] inline bool IsSimEnabled() const { return SimulatorEnabled_; } + [[nodiscard]] inline bool AllowSerialNumberMismatch() const { return AllowSerialNumberMismatch_; } + [[nodiscard]] inline uint64_t MismatchDepth() const { return MismatchDepth_; } + [[nodiscard]] inline bool UseProvisioning() const { return LookAtProvisioning_; } + [[nodiscard]] inline bool UseDefaults() const { return UseDefaultConfig_; } + [[nodiscard]] inline bool Running() const { return Running_; } [[nodiscard]] inline std::pair NextReactor() { return Reactor_pool_->NextReactor(); } - [[nodiscard]] inline bool Running() const { return Running_; } inline void AddConnection(uint64_t session_id, std::shared_ptr Connection) { @@ -107,7 +70,7 @@ namespace OpenWifi { Sessions_[session_id % 256][session_id] = std::move(Connection); } - inline bool DeviceRequiresSecureRTTY(uint64_t serialNumber) const { + [[nodiscard]] inline bool DeviceRequiresSecureRTTY(uint64_t serialNumber) const { auto hashIndex = Utils::CalculateMacAddressHash(serialNumber); std::lock_guard G(SerialNumbersMutex_[hashIndex]); @@ -120,7 +83,7 @@ namespace OpenWifi { inline bool GetStatistics(const std::string &SerialNumber, std::string &Statistics) const { return GetStatistics(Utils::SerialNumberToInt(SerialNumber), Statistics); } - bool GetStatistics(uint64_t SerialNumber, std::string &Statistics) const; + [[nodiscard]] bool GetStatistics(uint64_t SerialNumber, std::string &Statistics) const; inline bool GetState(const std::string &SerialNumber, GWObjects::ConnectionState &State) const { @@ -136,13 +99,7 @@ namespace OpenWifi { bool Connected(uint64_t SerialNumber, GWObjects::DeviceRestrictions &Restrictions) const; bool Connected(uint64_t SerialNumber) const; - - inline bool SendFrame(const std::string &SerialNumber, const std::string &Payload) const { - return SendFrame(Utils::SerialNumberToInt(SerialNumber), Payload); - } - bool SendFrame(uint64_t SerialNumber, const std::string &Payload) const; - bool SendRadiusAuthenticationData(const std::string &SerialNumber, const unsigned char *buffer, std::size_t size); bool SendRadiusAccountingData(const std::string &SerialNumber, const unsigned char *buffer, @@ -150,7 +107,7 @@ namespace OpenWifi { bool SendRadiusCoAData(const std::string &SerialNumber, const unsigned char *buffer, std::size_t size); - void SetSessionDetails(uint64_t session_id, uint64_t SerialNumber); + void StartSession(uint64_t session_id, uint64_t SerialNumber); bool EndSession(uint64_t session_id, uint64_t SerialNumber); void SetWebSocketTelemetryReporting(uint64_t RPCID, uint64_t SerialNumber, uint64_t Interval, uint64_t Lifetime, @@ -168,6 +125,10 @@ namespace OpenWifi { uint64_t &TelemetryWebSocketPackets, uint64_t &TelemetryKafkaPackets); + bool GetHealthDevices(std::uint64_t lowLimit, std::uint64_t highLimit, std::vector & SerialNumbers); + bool ExtendedAttributes(const std::string &serialNumber, bool & hasGPS, std::uint64_t &Sanity, + std::double_t &MemoryUsed, std::double_t &Load, std::double_t &Temperature); + inline void AverageDeviceStatistics(uint64_t &Connections, uint64_t &AverageConnectionTime, uint64_t &NumberOfConnectingDevices) const { Connections = NumberOfConnectedDevices_; @@ -175,6 +136,10 @@ namespace OpenWifi { NumberOfConnectingDevices = NumberOfConnectingDevices_; } + inline bool SendFrame(const std::string &SerialNumber, const std::string &Payload) const { + return SendFrame(Utils::SerialNumberToInt(SerialNumber), Payload); + } + inline void AddRX(std::uint64_t bytes) { std::lock_guard G(StatsMutex_); RX_ += bytes; @@ -191,30 +156,6 @@ namespace OpenWifi { RX = RX_; } - bool GetHealthDevices(std::uint64_t lowLimit, std::uint64_t highLimit, std::vector & SerialNumbers); - - inline bool ExtendedAttributes(const std::string &serialNumber, - bool & hasGPS, - std::uint64_t &Sanity, - std::double_t &MemoryUsed, - std::double_t &Load, - std::double_t &Temperature - ) { - - auto serialNumberInt = Utils::SerialNumberToInt(serialNumber); - auto hashIndex = Utils::CalculateMacAddressHash(serialNumberInt); - std::lock_guard G(SerialNumbersMutex_[hashIndex]); - auto session_hint = SerialNumbers_[hashIndex].find(Utils::SerialNumberToInt(serialNumber)); - if(session_hint==end(SerialNumbers_[hashIndex])) { - return false; - } - hasGPS = session_hint->second->hasGPS; - Sanity = session_hint->second->RawLastHealthcheck_.Sanity; - MemoryUsed = session_hint->second->memory_used_; - Load = session_hint->second->cpu_load_; - Temperature = session_hint->second->temperature_; - return true; - } private: mutable std::array SessionMutex_; From f2dec010ee2594d4e6c98a51e7876ce3ae68c71a Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Fri, 1 Dec 2023 10:17:16 -0800 Subject: [PATCH 062/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- CMakeLists.txt | 2 +- build | 2 +- src/AP_WS_Connection.cpp | 14 +++++++------- src/AP_WS_Connection.h | 14 +++++++------- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 70cc7fcf2..64a8c673f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.13) project(owgw VERSION 3.0.0) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) if(UNIX AND APPLE) set(OPENSSL_ROOT_DIR /usr/local/opt/openssl) diff --git a/build b/build index 1758dddcc..dc7b54ad0 100644 --- a/build +++ b/build @@ -1 +1 @@ -32 \ No newline at end of file +33 \ No newline at end of file diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index e83d35dad..3cbc15724 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -581,17 +581,17 @@ namespace OpenWifi { } bool AP_WS_Connection::SetWebSocketTelemetryReporting( - uint64_t RPCID, uint64_t Interval, uint64_t LifeTime, + std::uint64_t RPCID, std::uint64_t Interval, std::uint64_t LifeTime, const std::vector &TelemetryTypes) { std::unique_lock Lock(TelemetryMutex_); TelemetryWebSocketRefCount_++; TelemetryInterval_ = TelemetryInterval_ - ? (Interval < TelemetryInterval_ ? Interval : TelemetryInterval_) + ? (Interval < (std::uint64_t)TelemetryInterval_ ? Interval : (std::uint64_t )TelemetryInterval_) : Interval; auto TelemetryWebSocketTimer = LifeTime + Utils::Now(); - TelemetryWebSocketTimer_ = TelemetryWebSocketTimer > TelemetryWebSocketTimer_ - ? TelemetryWebSocketTimer - : TelemetryWebSocketTimer_; + TelemetryWebSocketTimer_ = TelemetryWebSocketTimer > (std::uint64_t)TelemetryWebSocketTimer_ + ? (std::uint64_t)TelemetryWebSocketTimer + : (std::uint64_t)TelemetryWebSocketTimer_; UpdateCounts(); if (!TelemetryReporting_) { TelemetryReporting_ = true; @@ -607,11 +607,11 @@ namespace OpenWifi { std::unique_lock Lock(TelemetryMutex_); TelemetryKafkaRefCount_++; TelemetryInterval_ = TelemetryInterval_ - ? (Interval < TelemetryInterval_ ? Interval : TelemetryInterval_) + ? (Interval < (std::uint64_t)TelemetryInterval_ ? (std::uint64_t)Interval : (std::uint64_t)TelemetryInterval_) : Interval; auto TelemetryKafkaTimer = LifeTime + Utils::Now(); TelemetryKafkaTimer_ = - TelemetryKafkaTimer > TelemetryKafkaTimer_ ? TelemetryKafkaTimer : TelemetryKafkaTimer_; + TelemetryKafkaTimer > (std::uint64_t)TelemetryKafkaTimer_ ? (std::uint64_t)TelemetryKafkaTimer : (std::uint64_t)TelemetryKafkaTimer_; UpdateCounts(); if (!TelemetryReporting_) { TelemetryReporting_ = true; diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index f5d27fd0d..53a82beb9 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -126,13 +126,13 @@ namespace OpenWifi { uint64_t Errors_ = 0; Poco::Net::IPAddress PeerAddress_; volatile bool TelemetryReporting_ = false; - volatile uint64_t TelemetryWebSocketRefCount_ = 0; - volatile uint64_t TelemetryKafkaRefCount_ = 0; - volatile uint64_t TelemetryWebSocketTimer_ = 0; - volatile uint64_t TelemetryKafkaTimer_ = 0; - volatile uint64_t TelemetryInterval_ = 0; - volatile uint64_t TelemetryWebSocketPackets_ = 0; - volatile uint64_t TelemetryKafkaPackets_ = 0; + std::atomic_uint64_t TelemetryWebSocketRefCount_ = 0; + std::atomic_uint64_t TelemetryKafkaRefCount_ = 0; + std::atomic_uint64_t TelemetryWebSocketTimer_ = 0; + std::atomic_uint64_t TelemetryKafkaTimer_ = 0; + std::atomic_uint64_t TelemetryInterval_ = 0; + std::atomic_uint64_t TelemetryWebSocketPackets_ = 0; + std::atomic_uint64_t TelemetryKafkaPackets_ = 0; GWObjects::ConnectionState State_; Utils::CompressedString RawLastStats_; GWObjects::HealthCheck RawLastHealthcheck_; From b9bd768ca60c2d83b5225e0d823256cf23da623e Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Fri, 1 Dec 2023 11:11:43 -0800 Subject: [PATCH 063/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 7 ++++--- src/AP_WS_Connection.h | 8 ++++---- src/AP_WS_Server.cpp | 14 +++++++------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 3cbc15724..5571e2d54 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -82,7 +82,8 @@ namespace OpenWifi { } void AP_WS_Connection::EndConnection() { - if (!Dead_.test_and_set()) { + bool expectedValue=false; + if (Dead_.compare_exchange_strong(expectedValue,true,std::memory_order_release,std::memory_order_relaxed)) { if(!SerialNumber_.empty() && State_.LastContact!=0) { StorageService()->SetDeviceLastRecordedContact(SerialNumber_, State_.LastContact); @@ -114,7 +115,7 @@ namespace OpenWifi { bool AP_WS_Connection::ValidatedDevice() { - if(Dead_.test()) + if(Dead_) return false; if (DeviceValidated_) @@ -661,7 +662,7 @@ namespace OpenWifi { void AP_WS_Connection::OnSocketReadable( [[maybe_unused]] const Poco::AutoPtr &pNf) { - if (Dead_.test()) // we are dead, so we do not process anything. + if (Dead_) // we are dead, so we do not process anything. return; std::lock_guard DeviceLock(ConnectionMutex_); diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index 53a82beb9..b442ba940 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -59,21 +59,21 @@ namespace OpenWifi { bool StopKafkaTelemetry(uint64_t RPCID); inline void GetLastStats(std::string &LastStats) { - if(!Dead_.test()) { + if(!Dead_) { std::lock_guard G(ConnectionMutex_); LastStats = RawLastStats_; } } inline void GetLastHealthCheck(GWObjects::HealthCheck &H) { - if(!Dead_.test()) { + if(!Dead_) { std::lock_guard G(ConnectionMutex_); H = RawLastHealthcheck_; } } inline void GetState(GWObjects::ConnectionState &State) { - if(!Dead_.test()) { + if(!Dead_) { std::lock_guard G(ConnectionMutex_); State = State_; } @@ -139,7 +139,7 @@ namespace OpenWifi { std::chrono::time_point ConnectionStart_ = std::chrono::high_resolution_clock::now(); std::chrono::duration ConnectionCompletionTime_{0.0}; - std::atomic_flag Dead_ = false; + std::atomic Dead_ = false; std::atomic_bool DeviceValidated_ = false; OpenWifi::GWObjects::DeviceRestrictions Restrictions_; bool RTTYMustBeSecure_ = false; diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 4b7a9ce4c..3fb7819e2 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -448,7 +448,7 @@ namespace OpenWifi { if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return false; } - if(!DeviceHint->second->Dead_.test()) { + if(!DeviceHint->second->Dead_) { DeviceHint->second->GetRestrictions(Restrictions); return DeviceHint->second->State_.Connected; } @@ -463,7 +463,7 @@ namespace OpenWifi { if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { return false; } - if(!DeviceHint->second->Dead_.test()) { + if(!DeviceHint->second->Dead_) { return DeviceHint->second->State_.Connected; } return false; @@ -477,7 +477,7 @@ namespace OpenWifi { return false; } - if(DeviceHint->second->Dead_.test()) { + if(DeviceHint->second->Dead_) { return false; } @@ -565,7 +565,7 @@ namespace OpenWifi { return false; } - if(DeviceHint->second->Dead_.test()) { + if(DeviceHint->second->Dead_) { return false; } @@ -590,7 +590,7 @@ namespace OpenWifi { return false; } - if(DeviceHint->second->Dead_.test()) { + if(DeviceHint->second->Dead_) { return false; } @@ -615,7 +615,7 @@ namespace OpenWifi { return false; } - if(DeviceHint->second->Dead_.test()) { + if(DeviceHint->second->Dead_) { return false; } try { @@ -643,7 +643,7 @@ namespace OpenWifi { if(DeviceHint==end(SerialNumbers_[hashIndex])) { return false; } - if(DeviceHint->second->Dead_.test()) { + if(DeviceHint->second->Dead_) { return false; } std::lock_guard DeviceGuard(DeviceHint->second->ConnectionMutex_); From 567c2d1514332c2700889534bd8d737b711273fa Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Fri, 1 Dec 2023 14:15:46 -0800 Subject: [PATCH 064/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 5571e2d54..c06ee57b0 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -78,6 +78,7 @@ namespace OpenWifi { AP_WS_Connection::~AP_WS_Connection() { poco_information(Logger_, fmt::format("DESTRUCTOR({}): Session={} Connection closed.", SerialNumber_, State_.sessionId)); + std::lock_guard G(ConnectionMutex_); EndConnection(); } From 12e07fa65fc2fecde69f9052cb2de68b60d83b8f Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Fri, 1 Dec 2023 22:27:47 -0800 Subject: [PATCH 065/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- CMakeLists.txt | 2 +- build | 2 +- src/AP_WS_Server.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 64a8c673f..70cc7fcf2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.13) project(owgw VERSION 3.0.0) -set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD 17) if(UNIX AND APPLE) set(OPENSSL_ROOT_DIR /usr/local/opt/openssl) diff --git a/build b/build index dc7b54ad0..dce6588ca 100644 --- a/build +++ b/build @@ -1 +1 @@ -33 \ No newline at end of file +36 \ No newline at end of file diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 3fb7819e2..2e0fa8158 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -52,7 +52,7 @@ namespace OpenWifi { private: Poco::Logger &Logger_; - inline static uint64_t session_id_ = 1; + inline static std::atomic_uint64_t session_id_ = 1; }; From 135b63c021c1da1bdba8530152fcb3bd9cbfa0d4 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Fri, 1 Dec 2023 23:00:09 -0800 Subject: [PATCH 066/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Server.cpp | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/build b/build index dce6588ca..c24b6ae77 100644 --- a/build +++ b/build @@ -1 +1 @@ -36 \ No newline at end of file +38 \ No newline at end of file diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 2e0fa8158..f4494580d 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -403,19 +403,32 @@ namespace OpenWifi { std::lock_guard SessionLock(SessionMutex_[session_id % 256]); auto SessionHint = Sessions_[session_id % 256].find(session_id); if (SessionHint == end(Sessions_[session_id % 256])) { + poco_error(Logger(), fmt::format("StartSession: Could not find session '{}'", session_id)); return; } Connection = SessionHint->second; auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - auto CurrentSerialNumber = SerialNumbers_[hashIndex].find(SerialNumber); - if ((CurrentSerialNumber == SerialNumbers_[hashIndex].end()) || + SerialNumbers_[hashIndex][SerialNumber] = Connection; + Sessions_[session_id % 256].erase(SessionHint); + +/* auto CurrentSerialNumber = SerialNumbers_[hashIndex].find(SerialNumber); + if ((CurrentSerialNumber == SerialNumbers_[hashIndex].end())) { + SerialNumbers_[hashIndex][SerialNumber] = Connection; + Sessions_[session_id % 256].erase(SessionHint); + return; + } + + + + || (CurrentSerialNumber->second != nullptr && CurrentSerialNumber->second->State_.sessionId < session_id)) { SerialNumbers_[hashIndex][SerialNumber] = Connection; Sessions_[session_id % 256].erase(SessionHint); return; } +*/ } bool AP_WS_Server::EndSession(uint64_t session_id, uint64_t SerialNumber) { From 0288d905b7c1f2a9c59db3454533d1a057c4bb78 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Fri, 1 Dec 2023 23:44:15 -0800 Subject: [PATCH 067/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 35 ++++++++++++++++------------------ src/AP_WS_Server.h | 7 +++---- src/framework/MicroService.cpp | 2 +- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index f4494580d..f7ce4bbe1 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -25,10 +25,19 @@ namespace OpenWifi { class AP_WS_RequestHandler : public Poco::Net::HTTPRequestHandler { public: - explicit AP_WS_RequestHandler(Poco::Logger &L, std::uint64_t session_id) : Logger_(L), session_id_(session_id){}; - - void handleRequest(Poco::Net::HTTPServerRequest &request, - Poco::Net::HTTPServerResponse &response) override; + explicit AP_WS_RequestHandler(Poco::Logger &L, std::uint64_t session_id) : Logger_(L), + session_id_(session_id) { + }; + + void handleRequest( Poco::Net::HTTPServerRequest &request, + Poco::Net::HTTPServerResponse &response) override { + try { + AP_WS_Server()->AddConnection(std::make_shared(request, response, session_id_, Logger_, + AP_WS_Server()->NextReactor())); + } catch (...) { + poco_warning(Logger_, "Exception during WS creation"); + } + }; private: Poco::Logger &Logger_; @@ -44,27 +53,15 @@ namespace OpenWifi { if (request.find("Upgrade") != request.end() && Poco::icompare(request["Upgrade"], "websocket") == 0) { Utils::SetThreadName("ws:conn-init"); - return new AP_WS_RequestHandler(Logger_, session_id_++); + session_id_++; + return new AP_WS_RequestHandler(Logger_, session_id_); } else { return nullptr; } } - private: Poco::Logger &Logger_; - inline static std::atomic_uint64_t session_id_ = 1; - }; - - - void AP_WS_RequestHandler::handleRequest(Poco::Net::HTTPServerRequest &request, - Poco::Net::HTTPServerResponse &response) { - try { - AP_WS_Server()->AddConnection( - session_id_, std::make_shared(request, response, session_id_, Logger_, - AP_WS_Server()->NextReactor())); - } catch (...) { - poco_warning(Logger_, "Exception during WS creation"); - } + inline static std::atomic_uint64_t session_id_ = 0; }; bool AP_WS_Server::ValidateCertificate(const std::string &ConnectionId, diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index f7ee53e21..caa84cff6 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -64,10 +64,9 @@ namespace OpenWifi { return Reactor_pool_->NextReactor(); } - inline void AddConnection(uint64_t session_id, - std::shared_ptr Connection) { - std::lock_guard Lock(SessionMutex_[session_id % 256]); - Sessions_[session_id % 256][session_id] = std::move(Connection); + inline void AddConnection(std::shared_ptr Connection) { + std::lock_guard Lock(SessionMutex_[Connection->State_.sessionId % 256]); + Sessions_[Connection->State_.sessionId % 256][Connection->State_.sessionId] = std::move(Connection); } [[nodiscard]] inline bool DeviceRequiresSecureRTTY(uint64_t serialNumber) const { diff --git a/src/framework/MicroService.cpp b/src/framework/MicroService.cpp index 5d517d9ff..1697b1478 100644 --- a/src/framework/MicroService.cpp +++ b/src/framework/MicroService.cpp @@ -136,7 +136,7 @@ namespace OpenWifi { auto i = Services_.begin(); auto now = Utils::Now(); for (; i != Services_.end();) { - if ((now - i->second.LastUpdate) > 60) { + if ((now - i->second.LastUpdate) > 120) { i = Services_.erase(i); } else ++i; From 3f06f00a888af517131e597941bc06993ea3f326 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Fri, 1 Dec 2023 23:51:18 -0800 Subject: [PATCH 068/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Connection.cpp | 13 +++++++++---- src/AP_WS_Connection.h | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/build b/build index c24b6ae77..72f523f36 100644 --- a/build +++ b/build @@ -1 +1 @@ -38 \ No newline at end of file +39 \ No newline at end of file diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index c06ee57b0..3559ac0dc 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -76,13 +76,17 @@ namespace OpenWifi { } AP_WS_Connection::~AP_WS_Connection() { - poco_information(Logger_, fmt::format("DESTRUCTOR({}): Session={} Connection closed.", SerialNumber_, + poco_information(Logger_, fmt::format("DESTRUCTOR({}): 0 - Session={} Connection closed.", SerialNumber_, State_.sessionId)); std::lock_guard G(ConnectionMutex_); - EndConnection(); + poco_information(Logger_, fmt::format("DESTRUCTOR({}): 1 - Session={} Connection closed.", SerialNumber_, + State_.sessionId)); + EndConnection(false); + poco_information(Logger_, fmt::format("DESTRUCTOR({}): 2 - Session={} Connection closed.", SerialNumber_, + State_.sessionId)); } - void AP_WS_Connection::EndConnection() { + void AP_WS_Connection::EndConnection(bool Clean) { bool expectedValue=false; if (Dead_.compare_exchange_strong(expectedValue,true,std::memory_order_release,std::memory_order_relaxed)) { @@ -110,7 +114,8 @@ namespace OpenWifi { Cleanup.detach(); } - AP_WS_Server()->EndSession(State_.sessionId, SerialNumberInt_); + if(Clean) + AP_WS_Server()->EndSession(State_.sessionId, SerialNumberInt_); } } diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index b442ba940..90a8e3acb 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -30,7 +30,7 @@ namespace OpenWifi { Poco::Logger &L, std::pair R); ~AP_WS_Connection(); - void EndConnection(); + void EndConnection(bool Clean = true); void ProcessJSONRPCEvent(Poco::JSON::Object::Ptr &Doc); void ProcessJSONRPCResult(Poco::JSON::Object::Ptr Doc); void ProcessIncomingFrame(); From 246f9c8aadad52143a5a6152602db76ca255ae36 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 08:44:37 -0800 Subject: [PATCH 069/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 207 +++++++++++++++++++-------------------- 1 file changed, 99 insertions(+), 108 deletions(-) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 3559ac0dc..9eb948365 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -141,7 +141,6 @@ namespace OpenWifi { poco_warning(Logger_, fmt::format("TLS-CONNECTION({}): Session={} Connection is " "NOT secure. Device is not allowed.", CId_, State_.sessionId)); - EndConnection(); return false; } @@ -154,7 +153,6 @@ namespace OpenWifi { Logger_, fmt::format("TLS-CONNECTION({}): Session={} No certificates available..", CId_, State_.sessionId)); - EndConnection(); return false; } @@ -165,7 +163,6 @@ namespace OpenWifi { fmt::format("TLS-CONNECTION({}): Session={} Device certificate is not " "valid. Device is not allowed.", CId_, State_.sessionId)); - EndConnection(); return false; } @@ -174,7 +171,6 @@ namespace OpenWifi { poco_trace(Logger_, fmt::format("TLS-CONNECTION({}): Session={} Invalid serial number: CN={}", CId_, State_.sessionId, CN_)); - EndConnection(); return false; } SerialNumber_ = CN_; @@ -189,7 +185,6 @@ namespace OpenWifi { poco_warning(Logger_, fmt::format("TLS-CONNECTION({}): Session={} Sim Device {} is " "not allowed. Disconnecting.", CId_, State_.sessionId, CN_)); - EndConnection(); return false; } @@ -207,7 +202,6 @@ namespace OpenWifi { fmt::format( "TLS-CONNECTION({}): Session={} Device {} is black listed. Disconnecting.", CId_, State_.sessionId, CN_)); - EndConnection(); return false; } @@ -673,33 +667,31 @@ namespace OpenWifi { std::lock_guard DeviceLock(ConnectionMutex_); - if (!AP_WS_Server()->Running()) - return EndConnection(); - LastContact_ = Utils::Now(); - if (!ValidatedDevice()) - return; - - try { - return ProcessIncomingFrame(); - } catch (const Poco::Exception &E) { - Logger_.log(E); - } catch (const std::exception &E) { - std::string W = E.what(); - poco_information( - Logger_, - fmt::format("std::exception caught: {}. Connection terminated with {}", W, CId_)); - } catch (...) { - poco_information(Logger_, - fmt::format("Unknown exception for {}. Connection terminated.", CId_)); + if (AP_WS_Server()->Running() && (DeviceValidated_ || ValidatedDevice())) { + try { + return ProcessIncomingFrame(); + } catch (const Poco::Exception &E) { + Logger_.log(E); + } catch (const std::exception &E) { + std::string W = E.what(); + poco_information( + Logger_, fmt::format("std::exception caught: {}. Connection terminated with {}", + W, CId_)); + } catch (...) { + poco_information( + Logger_, fmt::format("Unknown exception for {}. Connection terminated.", CId_)); + } } - return EndConnection(); + EndConnection(); } void AP_WS_Connection::ProcessIncomingFrame() { Poco::Buffer IncomingFrame(0); + + bool KillConnection=false; try { - int Op, flags; + int Op, flags; auto IncomingSize = WS_->receiveFrame(IncomingFrame, flags); Op = flags & Poco::Net::WebSocket::FRAME_OP_BITMASK; @@ -719,83 +711,82 @@ namespace OpenWifi { State_.LastContact = Utils::Now(); switch (Op) { - case Poco::Net::WebSocket::FRAME_OP_PING: { - poco_trace(Logger_, fmt::format("WS-PING({}): received. PONG sent back.", CId_)); - WS_->sendFrame("", 0, - (int)Poco::Net::WebSocket::FRAME_OP_PONG | - (int)Poco::Net::WebSocket::FRAME_FLAG_FIN); - - if (KafkaManager()->Enabled()) { - Poco::JSON::Object PingObject; - Poco::JSON::Object PingDetails; - PingDetails.set(uCentralProtocol::FIRMWARE, State_.Firmware); - PingDetails.set(uCentralProtocol::SERIALNUMBER, SerialNumber_); - PingDetails.set(uCentralProtocol::COMPATIBLE, Compatible_); - PingDetails.set(uCentralProtocol::CONNECTIONIP, CId_); - PingDetails.set(uCentralProtocol::TIMESTAMP, Utils::Now()); - PingDetails.set(uCentralProtocol::UUID, uuid_); - PingDetails.set("locale", State_.locale); - PingObject.set(uCentralProtocol::PING, PingDetails); - poco_trace(Logger_,fmt::format("Sending PING for {}", SerialNumber_)); - KafkaManager()->PostMessage(KafkaTopics::CONNECTION, SerialNumber_,PingObject); - } - return; - } break; + case Poco::Net::WebSocket::FRAME_OP_PING: { + poco_trace(Logger_, fmt::format("WS-PING({}): received. PONG sent back.", CId_)); + WS_->sendFrame("", 0, + (int)Poco::Net::WebSocket::FRAME_OP_PONG | + (int)Poco::Net::WebSocket::FRAME_FLAG_FIN); + + if (KafkaManager()->Enabled()) { + Poco::JSON::Object PingObject; + Poco::JSON::Object PingDetails; + PingDetails.set(uCentralProtocol::FIRMWARE, State_.Firmware); + PingDetails.set(uCentralProtocol::SERIALNUMBER, SerialNumber_); + PingDetails.set(uCentralProtocol::COMPATIBLE, Compatible_); + PingDetails.set(uCentralProtocol::CONNECTIONIP, CId_); + PingDetails.set(uCentralProtocol::TIMESTAMP, Utils::Now()); + PingDetails.set(uCentralProtocol::UUID, uuid_); + PingDetails.set("locale", State_.locale); + PingObject.set(uCentralProtocol::PING, PingDetails); + poco_trace(Logger_,fmt::format("Sending PING for {}", SerialNumber_)); + KafkaManager()->PostMessage(KafkaTopics::CONNECTION, SerialNumber_,PingObject); + } + } break; - case Poco::Net::WebSocket::FRAME_OP_PONG: { - poco_trace(Logger_, fmt::format("PONG({}): received and ignored.", CId_)); - return; - } break; + case Poco::Net::WebSocket::FRAME_OP_PONG: { + poco_trace(Logger_, fmt::format("PONG({}): received and ignored.", CId_)); + } break; - case Poco::Net::WebSocket::FRAME_OP_TEXT: { - poco_trace(Logger_, - fmt::format("FRAME({}): Frame received (length={}, flags={}). Msg={}", - CId_, IncomingSize, flags, IncomingFrame.begin())); - - Poco::JSON::Parser parser; - auto ParsedMessage = parser.parse(IncomingFrame.begin()); - auto IncomingJSON = ParsedMessage.extract(); - - if (IncomingJSON->has(uCentralProtocol::JSONRPC)) { - if (IncomingJSON->has(uCentralProtocol::METHOD) && - IncomingJSON->has(uCentralProtocol::PARAMS)) { - ProcessJSONRPCEvent(IncomingJSON); - } else if (IncomingJSON->has(uCentralProtocol::RESULT) && - IncomingJSON->has(uCentralProtocol::ID)) { - poco_trace(Logger_, fmt::format("RPC-RESULT({}): payload: {}", CId_, - IncomingFrame.begin())); - ProcessJSONRPCResult(IncomingJSON); + case Poco::Net::WebSocket::FRAME_OP_TEXT: { + poco_trace(Logger_, + fmt::format("FRAME({}): Frame received (length={}, flags={}). Msg={}", + CId_, IncomingSize, flags, IncomingFrame.begin())); + + Poco::JSON::Parser parser; + auto ParsedMessage = parser.parse(IncomingFrame.begin()); + auto IncomingJSON = ParsedMessage.extract(); + + if (IncomingJSON->has(uCentralProtocol::JSONRPC)) { + if (IncomingJSON->has(uCentralProtocol::METHOD) && + IncomingJSON->has(uCentralProtocol::PARAMS)) { + ProcessJSONRPCEvent(IncomingJSON); + } else if (IncomingJSON->has(uCentralProtocol::RESULT) && + IncomingJSON->has(uCentralProtocol::ID)) { + poco_trace(Logger_, fmt::format("RPC-RESULT({}): payload: {}", CId_, + IncomingFrame.begin())); + ProcessJSONRPCResult(IncomingJSON); + } else { + poco_warning( + Logger_, + fmt::format("INVALID-PAYLOAD({}): Payload is not JSON-RPC 2.0: {}", + CId_, IncomingFrame.begin())); + } + } else if (IncomingJSON->has(uCentralProtocol::RADIUS)) { + ProcessIncomingRadiusData(IncomingJSON); } else { + std::ostringstream iS; + IncomingJSON->stringify(iS); + std::cout << iS.str() << std::endl; poco_warning( Logger_, - fmt::format("INVALID-PAYLOAD({}): Payload is not JSON-RPC 2.0: {}", - CId_, IncomingFrame.begin())); + fmt::format("FRAME({}): illegal transaction header, missing 'jsonrpc'", + CId_)); + Errors_++; } - } else if (IncomingJSON->has(uCentralProtocol::RADIUS)) { - ProcessIncomingRadiusData(IncomingJSON); - } else { - std::ostringstream iS; - IncomingJSON->stringify(iS); - std::cout << iS.str() << std::endl; - poco_warning( - Logger_, - fmt::format("FRAME({}): illegal transaction header, missing 'jsonrpc'", - CId_)); - Errors_++; - } - return; - } break; + } break; - case Poco::Net::WebSocket::FRAME_OP_CLOSE: { - poco_information(Logger_, - fmt::format("CLOSE({}): Device is closing its connection.", CId_)); - return EndConnection(); - } break; + case Poco::Net::WebSocket::FRAME_OP_CLOSE: { + poco_information(Logger_, + fmt::format("CLOSE({}): Device is closing its connection.", CId_)); + KillConnection=true; + } break; - default: { - poco_warning(Logger_, fmt::format("UNKNOWN({}): unknown WS Frame operation: {}", - CId_, std::to_string(Op))); - } break; + default: { + poco_warning(Logger_, fmt::format("UNKNOWN({}): unknown WS Frame operation: {}", + CId_, std::to_string(Op))); + Errors_++; + return; + } } } catch (const Poco::Net::ConnectionResetException &E) { poco_warning(Logger_, @@ -803,21 +794,21 @@ namespace OpenWifi { CId_, E.displayText(), IncomingFrame.begin() == nullptr ? "" : IncomingFrame.begin(), State_.sessionId)); - return EndConnection(); + KillConnection=true; } catch (const Poco::JSON::JSONException &E) { poco_warning(Logger_, fmt::format("JSONException({}): Text:{} Payload:{} Session:{}", CId_, E.displayText(), IncomingFrame.begin() == nullptr ? "" : IncomingFrame.begin(), State_.sessionId)); - return EndConnection(); + KillConnection=true; } catch (const Poco::Net::WebSocketException &E) { poco_warning(Logger_, fmt::format("WebSocketException({}): Text:{} Payload:{} Session:{}", CId_, E.displayText(), IncomingFrame.begin() == nullptr ? "" : IncomingFrame.begin(), State_.sessionId)); - return EndConnection(); + KillConnection=true; } catch (const Poco::Net::SSLConnectionUnexpectedlyClosedException &E) { poco_warning( Logger_, @@ -826,54 +817,54 @@ namespace OpenWifi { CId_, E.displayText(), IncomingFrame.begin() == nullptr ? "" : IncomingFrame.begin(), State_.sessionId)); - return EndConnection(); + KillConnection=true; } catch (const Poco::Net::SSLException &E) { poco_warning(Logger_, fmt::format("SSLException({}): Text:{} Payload:{} Session:{}", CId_, E.displayText(), IncomingFrame.begin() == nullptr ? "" : IncomingFrame.begin(), State_.sessionId)); - return EndConnection(); + KillConnection=true; } catch (const Poco::Net::NetException &E) { poco_warning(Logger_, fmt::format("NetException({}): Text:{} Payload:{} Session:{}", CId_, E.displayText(), IncomingFrame.begin() == nullptr ? "" : IncomingFrame.begin(), State_.sessionId)); - return EndConnection(); + KillConnection=true; } catch (const Poco::IOException &E) { poco_warning(Logger_, fmt::format("IOException({}): Text:{} Payload:{} Session:{}", CId_, E.displayText(), IncomingFrame.begin() == nullptr ? "" : IncomingFrame.begin(), State_.sessionId)); - return EndConnection(); + KillConnection=true; } catch (const Poco::Exception &E) { poco_warning(Logger_, fmt::format("Exception({}): Text:{} Payload:{} Session:{}", CId_, E.displayText(), IncomingFrame.begin() == nullptr ? "" : IncomingFrame.begin(), State_.sessionId)); - return EndConnection(); + KillConnection=true; } catch (const std::exception &E) { poco_warning(Logger_, fmt::format("std::exception({}): Text:{} Payload:{} Session:{}", CId_, E.what(), IncomingFrame.begin() == nullptr ? "" : IncomingFrame.begin(), State_.sessionId)); - return EndConnection(); + KillConnection=true; } catch (...) { poco_error(Logger_, fmt::format("UnknownException({}): Device must be disconnected. " "Unknown exception. Session:{}", CId_, State_.sessionId)); - return EndConnection(); + KillConnection=true; } - if (Errors_ < 10) + if (!KillConnection && Errors_ < 10) return; - poco_warning(Logger_, fmt::format("DISCONNECTING({}): Too many errors", CId_)); - return EndConnection(); + poco_warning(Logger_, fmt::format("DISCONNECTING({}): ConnectionException: {} Errors: {}", CId_, KillConnection, Errors_ )); + EndConnection(); } bool AP_WS_Connection::Send(const std::string &Payload) { From fc680a6bc44df69f9d89bd204b7754001d7d4bf8 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 09:37:15 -0800 Subject: [PATCH 070/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 4 ++-- src/AP_WS_Connection.h | 2 +- src/AP_WS_Server.cpp | 23 ++++++++--------------- src/AP_WS_Server.h | 22 +++++++++------------- 4 files changed, 20 insertions(+), 31 deletions(-) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 9eb948365..3be7037cf 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -79,8 +79,8 @@ namespace OpenWifi { poco_information(Logger_, fmt::format("DESTRUCTOR({}): 0 - Session={} Connection closed.", SerialNumber_, State_.sessionId)); std::lock_guard G(ConnectionMutex_); - poco_information(Logger_, fmt::format("DESTRUCTOR({}): 1 - Session={} Connection closed.", SerialNumber_, - State_.sessionId)); +// poco_information(Logger_, fmt::format("DESTRUCTOR({}): 1 - Session={} Connection closed.", SerialNumber_, +// State_.sessionId)); EndConnection(false); poco_information(Logger_, fmt::format("DESTRUCTOR({}): 2 - Session={} Connection closed.", SerialNumber_, State_.sessionId)); diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index 90a8e3acb..4c6d56d31 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -111,7 +111,7 @@ namespace OpenWifi { } private: - std::mutex ConnectionMutex_; + std::recursive_mutex ConnectionMutex_; std::mutex TelemetryMutex_; Poco::Logger &Logger_; Poco::Net::SocketReactor *Reactor_{nullptr}; diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index f7ce4bbe1..35741b163 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -208,14 +208,6 @@ namespace OpenWifi { } Logger().information(fmt::format("Garbage collecting starting run." )); - { - std::lock_guard SessionLock(GarbageMutex_); - if (!GarbageSessions_.empty()) { - Logger().information(fmt::format("Garbage collecting removing {} stale connections.", GarbageSessions_.size())); - GarbageSessions_.clear(); - Logger().information(fmt::format("Garbage collecting removed stale connections.")); - } - } uint64_t total_connected_time = 0, now = Utils::Now(); @@ -397,18 +389,19 @@ namespace OpenWifi { void AP_WS_Server::StartSession(uint64_t session_id, uint64_t SerialNumber) { std::shared_ptr Connection; + auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); std::lock_guard SessionLock(SessionMutex_[session_id % 256]); auto SessionHint = Sessions_[session_id % 256].find(session_id); - if (SessionHint == end(Sessions_[session_id % 256])) { + if (SessionHint != end(Sessions_[session_id % 256])) { + std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); + SerialNumbers_[hashIndex][SerialNumber] = Connection; + Sessions_[session_id % 256].erase(SessionHint); + } else { poco_error(Logger(), fmt::format("StartSession: Could not find session '{}'", session_id)); - return; + std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); + SerialNumbers_[hashIndex][SerialNumber] = Connection; } - Connection = SessionHint->second; - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); - std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - SerialNumbers_[hashIndex][SerialNumber] = Connection; - Sessions_[session_id % 256].erase(SessionHint); /* auto CurrentSerialNumber = SerialNumbers_[hashIndex].find(SerialNumber); if ((CurrentSerialNumber == SerialNumbers_[hashIndex].end())) { diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index caa84cff6..ab44f092a 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -65,8 +65,11 @@ namespace OpenWifi { } inline void AddConnection(std::shared_ptr Connection) { - std::lock_guard Lock(SessionMutex_[Connection->State_.sessionId % 256]); - Sessions_[Connection->State_.sessionId % 256][Connection->State_.sessionId] = std::move(Connection); + std::uint64_t hashIndex = Connection->State_.sessionId % 256; + std::lock_guard Lock(SessionMutex_[hashIndex]); + if(Sessions_[hashIndex].find(Connection->State_.sessionId)==end(Sessions_[hashIndex])) { + Sessions_[hashIndex][Connection->State_.sessionId] = std::move(Connection); + } } [[nodiscard]] inline bool DeviceRequiresSecureRTTY(uint64_t serialNumber) const { @@ -140,17 +143,14 @@ namespace OpenWifi { } inline void AddRX(std::uint64_t bytes) { - std::lock_guard G(StatsMutex_); RX_ += bytes; } inline void AddTX(std::uint64_t bytes) { - std::lock_guard G(StatsMutex_); TX_ += bytes; } inline void GetTotalDataStatistics(std::uint64_t &TX, std::uint64_t &RX) const { - std::lock_guard G(StatsMutex_); TX = TX_; RX = RX_; } @@ -158,17 +158,17 @@ namespace OpenWifi { private: mutable std::array SessionMutex_; - mutable std::mutex StatsMutex_; - mutable std::mutex GarbageMutex_; std::unique_ptr IssuerCert_; std::list> WebServers_; + Poco::ThreadPool DeviceConnectionPool_{"ws:dev-pool", 2, 64}; Poco::Net::SocketReactor Reactor_; Poco::Thread ReactorThread_; std::string SimulatorId_; - Poco::ThreadPool DeviceConnectionPool_{"ws:dev-pool", 2, 64}; bool LookAtProvisioning_ = false; bool UseDefaultConfig_ = true; bool SimulatorEnabled_ = false; + bool AllowSerialNumberMismatch_ = true; + std::unique_ptr Reactor_pool_; std::atomic_bool Running_ = false; std::array>,256> Sessions_; @@ -182,9 +182,7 @@ namespace OpenWifi { std::array SerialNumbers_; mutable std::array SerialNumbersMutex_; - std::atomic_bool AllowSerialNumberMismatch_ = true; - std::atomic_uint64_t MismatchDepth_ = 2; - + std::uint64_t MismatchDepth_ = 2; std::uint64_t NumberOfConnectedDevices_ = 0; std::uint64_t AverageDeviceConnectionTime_ = 0; std::uint64_t NumberOfConnectingDevices_ = 0; @@ -192,8 +190,6 @@ namespace OpenWifi { std::uint64_t LeftOverSessions_ = 0; std::atomic_uint64_t TX_=0,RX_=0; - std::vector> GarbageSessions_; - // std::unique_ptr> GarbageCollectorCallback_; // Poco::Timer Timer_; From 46fb41010897a1cd4098aac0b73843fd6703e99d Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 09:37:53 -0800 Subject: [PATCH 071/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build b/build index 72f523f36..86ee83a4a 100644 --- a/build +++ b/build @@ -1 +1 @@ -39 \ No newline at end of file +40 \ No newline at end of file From f3ccc4964759911e79a4f44ae73762a41bb7ad59 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 09:44:27 -0800 Subject: [PATCH 072/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/framework/MicroService.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/framework/MicroService.cpp b/src/framework/MicroService.cpp index 1697b1478..26fb521a7 100644 --- a/src/framework/MicroService.cpp +++ b/src/framework/MicroService.cpp @@ -137,6 +137,7 @@ namespace OpenWifi { auto now = Utils::Now(); for (; i != Services_.end();) { if ((now - i->second.LastUpdate) > 120) { + poco_warning(logger(), fmt::format("ZombieService: Removing service {}, ", i->second.PublicEndPoint)); i = Services_.erase(i); } else ++i; From 1d22e1153f5ffa5b246084b3797cb79970574e21 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 09:57:13 -0800 Subject: [PATCH 073/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Connection.cpp | 17 ++++++++++------- src/AP_WS_Connection.h | 1 + src/AP_WS_Server.cpp | 8 +++++--- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/build b/build index 86ee83a4a..aaa6442fe 100644 --- a/build +++ b/build @@ -1 +1 @@ -40 \ No newline at end of file +41 \ No newline at end of file diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 3be7037cf..0e2a9249e 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -59,20 +59,23 @@ namespace OpenWifi { WS_->setNoDelay(false); WS_->setKeepAlive(true); WS_->setBlocking(false); + uuid_ = MicroServiceRandom(std::numeric_limits::max()-1); + } + void AP_WS_Connection::Start() { Registered_ = true; - uuid_ = MicroServiceRandom(std::numeric_limits::max()-1); LastContact_ = Utils::Now(); Reactor_->addEventHandler(*WS_, - Poco::NObserver( - *this, &AP_WS_Connection::OnSocketReadable)); + Poco::NObserver( + *this, &AP_WS_Connection::OnSocketReadable)); Reactor_->addEventHandler(*WS_, - Poco::NObserver( - *this, &AP_WS_Connection::OnSocketShutdown)); + Poco::NObserver( + *this, &AP_WS_Connection::OnSocketShutdown)); Reactor_->addEventHandler(*WS_, - Poco::NObserver( - *this, &AP_WS_Connection::OnSocketError)); + Poco::NObserver( + *this, &AP_WS_Connection::OnSocketError)); + } AP_WS_Connection::~AP_WS_Connection() { diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index 4c6d56d31..f4ac0b9fa 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -109,6 +109,7 @@ namespace OpenWifi { std::lock_guard G(ConnectionMutex_); return Restrictions_; } + void Start(); private: std::recursive_mutex ConnectionMutex_; diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 35741b163..355a2638e 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -32,8 +32,10 @@ namespace OpenWifi { void handleRequest( Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response) override { try { - AP_WS_Server()->AddConnection(std::make_shared(request, response, session_id_, Logger_, - AP_WS_Server()->NextReactor())); + auto NewConnection = std::make_shared(request, response, session_id_, Logger_, + AP_WS_Server()->NextReactor()); + AP_WS_Server()->AddConnection(NewConnection); + NewConnection->Start(); } catch (...) { poco_warning(Logger_, "Exception during WS creation"); } @@ -394,7 +396,7 @@ namespace OpenWifi { auto SessionHint = Sessions_[session_id % 256].find(session_id); if (SessionHint != end(Sessions_[session_id % 256])) { std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - SerialNumbers_[hashIndex][SerialNumber] = Connection; + SerialNumbers_[hashIndex][SerialNumber] = SessionHint->second; Sessions_[session_id % 256].erase(SessionHint); } else { poco_error(Logger(), fmt::format("StartSession: Could not find session '{}'", session_id)); From 0cca3caa9b124623d74d8451f892a2635d43f8f8 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 10:40:13 -0800 Subject: [PATCH 074/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Connection.cpp | 6 +++--- src/AP_WS_Server.cpp | 34 +++++++++++++++++----------------- src/AP_WS_Server.h | 30 +++++++++++++++++++++++++++--- src/framework/utils.h | 14 -------------- 5 files changed, 48 insertions(+), 38 deletions(-) diff --git a/build b/build index aaa6442fe..ac4213d6e 100644 --- a/build +++ b/build @@ -1 +1 @@ -41 \ No newline at end of file +43 \ No newline at end of file diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 0e2a9249e..fea4c8636 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -79,13 +79,13 @@ namespace OpenWifi { } AP_WS_Connection::~AP_WS_Connection() { - poco_information(Logger_, fmt::format("DESTRUCTOR({}): 0 - Session={} Connection closed.", SerialNumber_, - State_.sessionId)); +// poco_information(Logger_, fmt::format("DESTRUCTOR({}): 0 - Session={} Connection closed.", SerialNumber_, +// State_.sessionId)); std::lock_guard G(ConnectionMutex_); // poco_information(Logger_, fmt::format("DESTRUCTOR({}): 1 - Session={} Connection closed.", SerialNumber_, // State_.sessionId)); EndConnection(false); - poco_information(Logger_, fmt::format("DESTRUCTOR({}): 2 - Session={} Connection closed.", SerialNumber_, + poco_debug(Logger_, fmt::format("TERMINATION({}): Session={}, Connection removed.", SerialNumber_, State_.sessionId)); } diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 355a2638e..d74ab4d76 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -347,7 +347,7 @@ namespace OpenWifi { bool AP_WS_Server::GetStatistics(uint64_t SerialNumber, std::string &Statistics) const { - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); + auto hashIndex = MACHash::Hash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); if (Device == SerialNumbers_[hashIndex].end() || Device->second == nullptr) { @@ -361,7 +361,7 @@ namespace OpenWifi { bool AP_WS_Server::GetState(uint64_t SerialNumber, GWObjects::ConnectionState &State) const { std::shared_ptr Connection; { - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); + auto hashIndex = MACHash::Hash(SerialNumber); std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); if (DeviceHint == SerialNumbers_[hashIndex].end() || @@ -377,7 +377,7 @@ namespace OpenWifi { bool AP_WS_Server::GetHealthcheck(uint64_t SerialNumber, GWObjects::HealthCheck &CheckData) const { - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); + auto hashIndex = MACHash::Hash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); if (Device == SerialNumbers_[hashIndex].end() || Device->second == nullptr) { @@ -391,7 +391,7 @@ namespace OpenWifi { void AP_WS_Server::StartSession(uint64_t session_id, uint64_t SerialNumber) { std::shared_ptr Connection; - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); + auto hashIndex = MACHash::Hash(SerialNumber); std::lock_guard SessionLock(SessionMutex_[session_id % 256]); auto SessionHint = Sessions_[session_id % 256].find(session_id); if (SessionHint != end(Sessions_[session_id % 256])) { @@ -431,7 +431,7 @@ namespace OpenWifi { } { - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); + auto hashIndex = MACHash::Hash(SerialNumber); std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); if (DeviceHint == SerialNumbers_[hashIndex].end() @@ -447,7 +447,7 @@ namespace OpenWifi { bool AP_WS_Server::Connected(uint64_t SerialNumber, GWObjects::DeviceRestrictions &Restrictions) const { - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); + auto hashIndex = MACHash::Hash(SerialNumber); std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { @@ -462,7 +462,7 @@ namespace OpenWifi { bool AP_WS_Server::Connected(uint64_t SerialNumber) const { - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); + auto hashIndex = MACHash::Hash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { @@ -475,7 +475,7 @@ namespace OpenWifi { } bool AP_WS_Server::SendFrame(uint64_t SerialNumber, const std::string &Payload) const { - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); + auto hashIndex = MACHash::Hash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { @@ -496,7 +496,7 @@ namespace OpenWifi { } void AP_WS_Server::StopWebSocketTelemetry(uint64_t RPCID, uint64_t SerialNumber) { - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); + auto hashIndex = MACHash::Hash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto Device = SerialNumbers_[hashIndex].find(SerialNumber); if (Device == end(SerialNumbers_[hashIndex]) || Device->second == nullptr) { @@ -509,7 +509,7 @@ namespace OpenWifi { AP_WS_Server::SetWebSocketTelemetryReporting(uint64_t RPCID, uint64_t SerialNumber, uint64_t Interval, uint64_t Lifetime, const std::vector &TelemetryTypes) { - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); + auto hashIndex = MACHash::Hash(SerialNumber); std::lock_guard DeviceLock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { @@ -521,7 +521,7 @@ namespace OpenWifi { void AP_WS_Server::SetKafkaTelemetryReporting(uint64_t RPCID, uint64_t SerialNumber, uint64_t Interval, uint64_t Lifetime, const std::vector &TelemetryTypes) { - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); + auto hashIndex = MACHash::Hash(SerialNumber); std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { @@ -531,7 +531,7 @@ namespace OpenWifi { } void AP_WS_Server::StopKafkaTelemetry(uint64_t RPCID, uint64_t SerialNumber) { - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); + auto hashIndex = MACHash::Hash(SerialNumber); std::lock_guard DevicesLock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { @@ -546,7 +546,7 @@ namespace OpenWifi { uint64_t &TelemetryWebSocketCount, uint64_t &TelemetryKafkaCount, uint64_t &TelemetryWebSocketPackets, uint64_t &TelemetryKafkaPackets) { - auto hashIndex = Utils::CalculateMacAddressHash(SerialNumber); + auto hashIndex = MACHash::Hash(SerialNumber); std::lock_guard DevicesLock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(SerialNumber); if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { @@ -563,7 +563,7 @@ namespace OpenWifi { const unsigned char *buffer, std::size_t size) { auto IntSerialNumber = Utils::SerialNumberToInt(SerialNumber); - auto hashIndex = Utils::CalculateMacAddressHash(IntSerialNumber); + auto hashIndex = MACHash::Hash(IntSerialNumber); std::lock_guard DevicesLock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(IntSerialNumber); if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { @@ -588,7 +588,7 @@ namespace OpenWifi { bool AP_WS_Server::SendRadiusAuthenticationData(const std::string &SerialNumber, const unsigned char *buffer, std::size_t size) { auto IntSerialNumber = Utils::SerialNumberToInt(SerialNumber); - auto hashIndex = Utils::CalculateMacAddressHash(IntSerialNumber); + auto hashIndex = MACHash::Hash(IntSerialNumber); std::lock_guard DevicesLock(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(IntSerialNumber); if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { @@ -613,7 +613,7 @@ namespace OpenWifi { bool AP_WS_Server::SendRadiusCoAData(const std::string &SerialNumber, const unsigned char *buffer, std::size_t size) { auto IntSerialNumber = Utils::SerialNumberToInt(SerialNumber); - auto hashIndex = Utils::CalculateMacAddressHash(IntSerialNumber); + auto hashIndex = MACHash::Hash(IntSerialNumber); std::lock_guard DevicesGuard(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(IntSerialNumber); if (DeviceHint == end(SerialNumbers_[hashIndex]) || DeviceHint->second == nullptr) { @@ -642,7 +642,7 @@ namespace OpenWifi { ) { auto serialNumberInt = Utils::SerialNumberToInt(serialNumber); - auto hashIndex = Utils::CalculateMacAddressHash(serialNumberInt); + auto hashIndex = MACHash::Hash(serialNumberInt); std::lock_guard DevicesGuard(SerialNumbersMutex_[hashIndex]); auto DeviceHint = SerialNumbers_[hashIndex].find(Utils::SerialNumberToInt(serialNumber)); if(DeviceHint==end(SerialNumbers_[hashIndex])) { diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index ab44f092a..2cd742915 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -31,6 +31,30 @@ namespace OpenWifi { + constexpr uint MACHashMax = 256; + constexpr uint MACHashMask = MACHashMax-1; + class MACHash { + public: + [[nodiscard]] static inline uint16_t Hash(std::uint64_t value) { + uint8_t hash = 0, i=6; + while(i) { + hash ^= (value & MACHashMask) + 1; + value >>= 8; + --i; + } + return hash; + } + + [[nodiscard]] static inline uint16_t Hash(const std::string & value) { + return Hash(Utils::MACToInt(value)); + } + + [[nodiscard]] static inline uint16_t HashMax() { + return MACHashMax; + } + }; + + class AP_WS_Server : public SubSystemServer, public Poco::Runnable { public: static auto instance() { @@ -73,7 +97,7 @@ namespace OpenWifi { } [[nodiscard]] inline bool DeviceRequiresSecureRTTY(uint64_t serialNumber) const { - auto hashIndex = Utils::CalculateMacAddressHash(serialNumber); + auto hashIndex = MACHash::Hash(serialNumber); std::lock_guard G(SerialNumbersMutex_[hashIndex]); auto Connection = SerialNumbers_[hashIndex].find(serialNumber); @@ -179,8 +203,8 @@ namespace OpenWifi { using SerialNumberMap = std::map>; - std::array SerialNumbers_; - mutable std::array SerialNumbersMutex_; + std::array SerialNumbers_; + mutable std::array SerialNumbersMutex_; std::uint64_t MismatchDepth_ = 2; std::uint64_t NumberOfConnectedDevices_ = 0; diff --git a/src/framework/utils.h b/src/framework/utils.h index cf36bcb49..b20d32af0 100644 --- a/src/framework/utils.h +++ b/src/framework/utils.h @@ -126,20 +126,6 @@ namespace OpenWifi::Utils { [[nodiscard]] std::uint64_t ConvertDate(const std::string &d); - [[nodiscard]] inline uint8_t CalculateMacAddressHash(std::uint64_t value) { - uint8_t hash = 0, i=6; - while(i) { - hash ^= (value & 0xFF) + 1; - value >>= 8; - --i; - } - return hash; - } - - [[nodiscard]] inline uint8_t CalculateMacAddressHash(const std::string & value) { - return CalculateMacAddressHash(MACToInt(value)); - } - template std::string int_to_hex(T i) { std::stringstream stream; stream << std::setfill('0') << std::setw(12) << std::hex << i; From 87d74568f214610e9d611e5fc1d11c9ae55a925f Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 10:45:00 -0800 Subject: [PATCH 075/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index d74ab4d76..9cdacecf1 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -220,7 +220,7 @@ namespace OpenWifi { NumberOfConnectingDevices_ = 0; AverageDeviceConnectionTime_ = 0; last_zombie_run = now; - for(int hashIndex=0;hashIndex<256;hashIndex++) { + for(int hashIndex=0;hashIndex Date: Sat, 2 Dec 2023 12:05:53 -0800 Subject: [PATCH 076/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Server.cpp | 28 +++++++--------------------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/build b/build index ac4213d6e..d2e1cefe8 100644 --- a/build +++ b/build @@ -1 +1 @@ -43 \ No newline at end of file +44 \ No newline at end of file diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 9cdacecf1..10d934090 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -224,17 +224,20 @@ namespace OpenWifi { std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); auto hint = SerialNumbers_[hashIndex].begin(); while (hint != end(SerialNumbers_[hashIndex])) { + if (hint->second == nullptr) { hint = SerialNumbers_[hashIndex].erase(hint); - } else if ((now - hint->second->LastContact_) > + continue; + } + + std::lock_guard DeviceGuard(hint->second->ConnectionMutex_); + if ((now - hint->second->LastContact_) > SessionTimeOut_) { poco_information( Logger(), fmt::format( "{}: Session seems idle. Controller disconnecting device.", hint->second->SerialNumber_)); -// std::lock_guard ConnectionLock(hint->second->ConnectionMutex_); -// hint->second->EndConnection(); hint = SerialNumbers_[hashIndex].erase(hint); } else if (hint->second->State_.Connected) { NumberOfConnectedDevices_++; @@ -244,6 +247,7 @@ namespace OpenWifi { ++NumberOfConnectingDevices_; ++hint; } + } } @@ -403,24 +407,6 @@ namespace OpenWifi { std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); SerialNumbers_[hashIndex][SerialNumber] = Connection; } - - -/* auto CurrentSerialNumber = SerialNumbers_[hashIndex].find(SerialNumber); - if ((CurrentSerialNumber == SerialNumbers_[hashIndex].end())) { - SerialNumbers_[hashIndex][SerialNumber] = Connection; - Sessions_[session_id % 256].erase(SessionHint); - return; - } - - - - || - (CurrentSerialNumber->second != nullptr && CurrentSerialNumber->second->State_.sessionId < session_id)) { - SerialNumbers_[hashIndex][SerialNumber] = Connection; - Sessions_[session_id % 256].erase(SessionHint); - return; - } -*/ } bool AP_WS_Server::EndSession(uint64_t session_id, uint64_t SerialNumber) { From 15805dcaf67ecbce88df6f923fa7e1a4d24130af Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 12:07:52 -0800 Subject: [PATCH 077/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 10d934090..23e0cd697 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -229,19 +229,19 @@ namespace OpenWifi { hint = SerialNumbers_[hashIndex].erase(hint); continue; } - - std::lock_guard DeviceGuard(hint->second->ConnectionMutex_); - if ((now - hint->second->LastContact_) > + auto Device = hint->second; + std::lock_guard DeviceGuard(Device->ConnectionMutex_); + if ((now - Device->LastContact_) > SessionTimeOut_) { poco_information( Logger(), fmt::format( "{}: Session seems idle. Controller disconnecting device.", - hint->second->SerialNumber_)); + Device->SerialNumber_)); hint = SerialNumbers_[hashIndex].erase(hint); - } else if (hint->second->State_.Connected) { + } else if (Device->State_.Connected) { NumberOfConnectedDevices_++; - total_connected_time += (now - hint->second->State_.started); + total_connected_time += (now - Device->State_.started); ++hint; } else { ++NumberOfConnectingDevices_; From efd099b6fad8af0e8daef41463285559924fd9d9 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 12:10:42 -0800 Subject: [PATCH 078/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 23e0cd697..18a92150d 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -239,15 +239,17 @@ namespace OpenWifi { "{}: Session seems idle. Controller disconnecting device.", Device->SerialNumber_)); hint = SerialNumbers_[hashIndex].erase(hint); - } else if (Device->State_.Connected) { + continue; + + if (Device->State_.Connected) { NumberOfConnectedDevices_++; total_connected_time += (now - Device->State_.started); ++hint; - } else { - ++NumberOfConnectingDevices_; - ++hint; + continue; } - + // Device must be in connecting state: established a ws connection but has not sent in a connect message. + ++NumberOfConnectingDevices_; + ++hint; } } From 9c4ecb6165db3d30941008e160073855a3573e87 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 12:12:53 -0800 Subject: [PATCH 079/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Server.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/build b/build index d2e1cefe8..7d3738628 100644 --- a/build +++ b/build @@ -1 +1 @@ -44 \ No newline at end of file +45 \ No newline at end of file diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 18a92150d..509129402 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -240,6 +240,7 @@ namespace OpenWifi { Device->SerialNumber_)); hint = SerialNumbers_[hashIndex].erase(hint); continue; + } if (Device->State_.Connected) { NumberOfConnectedDevices_++; From ef300b0349213c948c7246ad0560c4496df4216e Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 12:53:32 -0800 Subject: [PATCH 080/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Server.cpp | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/build b/build index 7d3738628..abc4eff6a 100644 --- a/build +++ b/build @@ -1 +1 @@ -45 \ No newline at end of file +46 \ No newline at end of file diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 509129402..080264703 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -219,9 +219,10 @@ namespace OpenWifi { NumberOfConnectedDevices_ = 0; NumberOfConnectingDevices_ = 0; AverageDeviceConnectionTime_ = 0; - last_zombie_run = now; for(int hashIndex=0;hashIndexsecond; std::lock_guard DeviceGuard(Device->ConnectionMutex_); - if ((now - Device->LastContact_) > + if (RightNow>Device->LastContact_ && (RightNow - Device->LastContact_) > SessionTimeOut_) { poco_information( Logger(), @@ -244,7 +245,7 @@ namespace OpenWifi { if (Device->State_.Connected) { NumberOfConnectedDevices_++; - total_connected_time += (now - Device->State_.started); + total_connected_time += (RightNow - Device->State_.started); ++hint; continue; } @@ -259,10 +260,11 @@ namespace OpenWifi { for(int i=0;i<256;i++) { std::lock_guard Lock(SessionMutex_[i]); auto hint = Sessions_[i].begin(); + auto RightNow = Utils::Now(); while (hint != end(Sessions_[i])) { if(hint->second == nullptr) { hint = Sessions_[i].erase(hint); - } else if ((now - hint->second->LastContact_) > SessionTimeOut_) { + } else if (RightNow>hint->second->LastContact_ && (RightNow - hint->second->LastContact_) > SessionTimeOut_) { poco_information( Logger(), fmt::format( From 17752fdefed811835d984b809367244f555185ec Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 12:56:53 -0800 Subject: [PATCH 081/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 629f9faab..8b8b74e88 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,8 +17,8 @@ FROM build-base AS poco-build ARG POCO_VERSION -ADD https://api.github.com/repos/AriliaWireless/poco/git/refs/tags/${POCO_VERSION} version.json -RUN git clone https://github.com/AriliaWireless/poco --branch ${POCO_VERSION} /poco +ADD https://api.github.com/repos/TelecomInfraProject/wlan-cloud-lib-poco/git/refs/tags/${POCO_VERSION} version.json +RUN git clone https://github.com/TelecomInfraProject/wlan-cloud-lib-poco --branch ${POCO_VERSION} /poco WORKDIR /poco RUN mkdir cmake-build From 84f42e5a7de48f257559b1d00eab08c4267e1f9d Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 13:04:23 -0800 Subject: [PATCH 082/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8b8b74e88..629f9faab 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,8 +17,8 @@ FROM build-base AS poco-build ARG POCO_VERSION -ADD https://api.github.com/repos/TelecomInfraProject/wlan-cloud-lib-poco/git/refs/tags/${POCO_VERSION} version.json -RUN git clone https://github.com/TelecomInfraProject/wlan-cloud-lib-poco --branch ${POCO_VERSION} /poco +ADD https://api.github.com/repos/AriliaWireless/poco/git/refs/tags/${POCO_VERSION} version.json +RUN git clone https://github.com/AriliaWireless/poco --branch ${POCO_VERSION} /poco WORKDIR /poco RUN mkdir cmake-build From 336c94a25a1cd21f5e2a9f05ae0ea65fd6d959c9 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 13:23:46 -0800 Subject: [PATCH 083/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Server.cpp | 30 ++++++++++++++---------------- src/AP_WS_Server.h | 42 ++++++++++++++++++++++++------------------ 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/build b/build index abc4eff6a..801f18010 100644 --- a/build +++ b/build @@ -1 +1 @@ -46 \ No newline at end of file +47 \ No newline at end of file diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 080264703..ba95a1919 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -253,11 +253,12 @@ namespace OpenWifi { ++NumberOfConnectingDevices_; ++hint; } + } poco_information(Logger(), fmt::format("Garbage collecting zombies... (step 2)")); LeftOverSessions_ = 0; - for(int i=0;i<256;i++) { + for(int i=0;i & SerialNumbers) { SerialNumbers.clear(); - for(int i=0;i<256;i++) { + for(int i=0;iRawLastHealthcheck_.Sanity >= lowLimit && @@ -398,27 +399,24 @@ namespace OpenWifi { } void AP_WS_Server::StartSession(uint64_t session_id, uint64_t SerialNumber) { - std::shared_ptr Connection; - - auto hashIndex = MACHash::Hash(SerialNumber); - std::lock_guard SessionLock(SessionMutex_[session_id % 256]); - auto SessionHint = Sessions_[session_id % 256].find(session_id); - if (SessionHint != end(Sessions_[session_id % 256])) { - std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - SerialNumbers_[hashIndex][SerialNumber] = SessionHint->second; - Sessions_[session_id % 256].erase(SessionHint); + auto deviceHash = MACHash::Hash(SerialNumber); + auto sessionHash = SessionHash::Hash(session_id); + std::lock_guard SessionLock(SessionMutex_[sessionHash]); + auto SessionHint = Sessions_[sessionHash].find(session_id); + if (SessionHint != end(Sessions_[sessionHash])) { + std::lock_guard Lock(SerialNumbersMutex_[deviceHash]); + SerialNumbers_[deviceHash][SerialNumber] = SessionHint->second; + Sessions_[sessionHash].erase(SessionHint); } else { poco_error(Logger(), fmt::format("StartSession: Could not find session '{}'", session_id)); - std::lock_guard Lock(SerialNumbersMutex_[hashIndex]); - SerialNumbers_[hashIndex][SerialNumber] = Connection; } } bool AP_WS_Server::EndSession(uint64_t session_id, uint64_t SerialNumber) { - { - std::lock_guard SessionLock(SessionMutex_[session_id % 256]); - Sessions_[session_id % 256].erase(session_id); + auto sessionHash = SessionHash::Hash(session_id); + std::lock_guard SessionLock(SessionMutex_[sessionHash]); + Sessions_[sessionHash].erase(session_id); } { diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index 2cd742915..87ec80d11 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -54,6 +54,19 @@ namespace OpenWifi { } }; + constexpr uint SessionHashMax = 256; + constexpr uint SessionHashMask = SessionHashMax-1; + class SessionHash { + public: + [[nodiscard]] static inline uint16_t Hash(std::uint64_t value) { + return (value & SessionHashMask); + } + + [[nodiscard]] static inline uint16_t HashMax() { + return SessionHashMax; + } + }; + class AP_WS_Server : public SubSystemServer, public Poco::Runnable { public: @@ -89,10 +102,10 @@ namespace OpenWifi { } inline void AddConnection(std::shared_ptr Connection) { - std::uint64_t hashIndex = Connection->State_.sessionId % 256; - std::lock_guard Lock(SessionMutex_[hashIndex]); - if(Sessions_[hashIndex].find(Connection->State_.sessionId)==end(Sessions_[hashIndex])) { - Sessions_[hashIndex][Connection->State_.sessionId] = std::move(Connection); + std::uint64_t sessionHash = SessionHash::Hash(Connection->State_.sessionId); + std::lock_guard Lock(SessionMutex_[sessionHash]); + if(Sessions_[sessionHash].find(Connection->State_.sessionId)==end(Sessions_[sessionHash])) { + Sessions_[sessionHash][Connection->State_.sessionId] = std::move(Connection); } } @@ -181,7 +194,13 @@ namespace OpenWifi { private: - mutable std::array SessionMutex_; + std::array SessionMutex_; + std::array>,SessionHashMax> Sessions_; + using SerialNumberMap = std::map>; + std::array SerialNumbers_; + mutable std::array SerialNumbersMutex_; + std::unique_ptr IssuerCert_; std::list> WebServers_; Poco::ThreadPool DeviceConnectionPool_{"ws:dev-pool", 2, 64}; @@ -195,16 +214,6 @@ namespace OpenWifi { std::unique_ptr Reactor_pool_; std::atomic_bool Running_ = false; - std::array>,256> Sessions_; - -// using SerialNumberMap = std::map>>; - - using SerialNumberMap = std::map>; - - std::array SerialNumbers_; - mutable std::array SerialNumbersMutex_; std::uint64_t MismatchDepth_ = 2; std::uint64_t NumberOfConnectedDevices_ = 0; @@ -214,9 +223,6 @@ namespace OpenWifi { std::uint64_t LeftOverSessions_ = 0; std::atomic_uint64_t TX_=0,RX_=0; -// std::unique_ptr> GarbageCollectorCallback_; -// Poco::Timer Timer_; - Poco::Thread GarbageCollector_; AP_WS_Server() noexcept From 30bc8e8283ca2fb1b0c59e78c189153bb3d157e4 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 14:07:50 -0800 Subject: [PATCH 084/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Server.cpp | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/build b/build index 801f18010..31ff414b7 100644 --- a/build +++ b/build @@ -1 +1 @@ -47 \ No newline at end of file +48 \ No newline at end of file diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index ba95a1919..94539a5f3 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -290,8 +290,13 @@ namespace OpenWifi { std::lock_guard Lock(SerialNumbersMutex_[i]); NumberOfConnectedDevices_ += SerialNumbers_[i].size(); } - if(last_garbage_run>0) - AverageDeviceConnectionTime_ += (now - last_garbage_run); + if(NumberOfConnectedDevices_) { + if (last_garbage_run > 0) { + AverageDeviceConnectionTime_ += (now - last_garbage_run); + } + } else { + AverageDeviceConnectionTime_ = 0; + } } if ((now - last_log) > 60) { From d3c5b17733d6e85f4110d8cfcd704da4d74eaa42 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 14:53:20 -0800 Subject: [PATCH 085/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Server.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build b/build index 31ff414b7..2e66562e2 100644 --- a/build +++ b/build @@ -1 +1 @@ -48 \ No newline at end of file +49 \ No newline at end of file diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index 87ec80d11..0f0f4cd92 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -203,7 +203,7 @@ namespace OpenWifi { std::unique_ptr IssuerCert_; std::list> WebServers_; - Poco::ThreadPool DeviceConnectionPool_{"ws:dev-pool", 2, 64}; + Poco::ThreadPool DeviceConnectionPool_{"ws:dev-pool", 4, 256}; Poco::Net::SocketReactor Reactor_; Poco::Thread ReactorThread_; std::string SimulatorId_; From c42feca957921264dd9ede085964d63568c19489 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 22:23:31 -0800 Subject: [PATCH 086/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Reactor_Pool.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build b/build index 2e66562e2..c5b431b6c 100644 --- a/build +++ b/build @@ -1 +1 @@ -49 \ No newline at end of file +50 \ No newline at end of file diff --git a/src/AP_WS_Reactor_Pool.h b/src/AP_WS_Reactor_Pool.h index 8a46dff1d..40da430c4 100644 --- a/src/AP_WS_Reactor_Pool.h +++ b/src/AP_WS_Reactor_Pool.h @@ -19,9 +19,9 @@ namespace OpenWifi { class AP_WS_ReactorThreadPool { public: explicit AP_WS_ReactorThreadPool(Poco::Logger &Logger) : Logger_(Logger) { - NumberOfThreads_ = Poco::Environment::processorCount() * 2; + NumberOfThreads_ = Poco::Environment::processorCount() * 4; if (NumberOfThreads_ == 0) - NumberOfThreads_ = 4; + NumberOfThreads_ = 8; } ~AP_WS_ReactorThreadPool() { Stop(); } From 0a5fe39bde43320c034192bb095658d1db866b39 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sat, 2 Dec 2023 23:06:18 -0800 Subject: [PATCH 087/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Reactor_Pool.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/AP_WS_Reactor_Pool.h b/src/AP_WS_Reactor_Pool.h index 40da430c4..adae16d43 100644 --- a/src/AP_WS_Reactor_Pool.h +++ b/src/AP_WS_Reactor_Pool.h @@ -22,6 +22,7 @@ namespace OpenWifi { NumberOfThreads_ = Poco::Environment::processorCount() * 4; if (NumberOfThreads_ == 0) NumberOfThreads_ = 8; + NumberOfThreads_ = std::min(NumberOfThreads_, (std::uint64_t) 128); } ~AP_WS_ReactorThreadPool() { Stop(); } From 0cff2163bb2c8dd3ebab38ed5fc1d21375fe0411 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sun, 3 Dec 2023 10:10:42 -0800 Subject: [PATCH 088/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Connection.cpp | 2 +- src/framework/MicroService.cpp | 68 +++++++++++++++++----------------- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/build b/build index c5b431b6c..7003e7fe1 100644 --- a/build +++ b/build @@ -1 +1 @@ -50 \ No newline at end of file +51 \ No newline at end of file diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index fea4c8636..9fa75c4a9 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -670,7 +670,7 @@ namespace OpenWifi { std::lock_guard DeviceLock(ConnectionMutex_); - LastContact_ = Utils::Now(); + State_.LastContact = LastContact_ = Utils::Now(); if (AP_WS_Server()->Running() && (DeviceValidated_ || ValidatedDevice())) { try { return ProcessIncomingFrame(); diff --git a/src/framework/MicroService.cpp b/src/framework/MicroService.cpp index 26fb521a7..12c471766 100644 --- a/src/framework/MicroService.cpp +++ b/src/framework/MicroService.cpp @@ -55,12 +55,9 @@ namespace OpenWifi { Object->has(KafkaTopics::ServiceEvents::Fields::KEY)) { auto PrivateEndPoint = Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE).toString(); - if (Event == KafkaTopics::ServiceEvents::EVENT_KEEP_ALIVE && - Services_.find(PrivateEndPoint) != Services_.end()) { - Services_[PrivateEndPoint].LastUpdate = Utils::Now(); - } else if (Event == KafkaTopics::ServiceEvents::EVENT_LEAVE) { + if (Event == KafkaTopics::ServiceEvents::EVENT_LEAVE) { Services_.erase(PrivateEndPoint); - poco_debug( + poco_information( logger(), fmt::format( "Service {} ID={} leaving system.", @@ -69,14 +66,7 @@ namespace OpenWifi { ID)); } else if (Event == KafkaTopics::ServiceEvents::EVENT_JOIN || Event == KafkaTopics::ServiceEvents::EVENT_KEEP_ALIVE) { - poco_debug( - logger(), - fmt::format( - "Service {} ID={} joining system.", - Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE) - .toString(), - ID)); - Services_[PrivateEndPoint] = Types::MicroServiceMeta{ + auto ServiceInfo = Types::MicroServiceMeta{ .Id = ID, .Type = Poco::toLower( Object->get(KafkaTopics::ServiceEvents::Fields::TYPE) @@ -94,19 +84,29 @@ namespace OpenWifi { .toString(), .LastUpdate = Utils::Now()}; - std::string SvcList; - for (const auto &Svc : Services_) { - if (SvcList.empty()) - SvcList = Svc.second.Type; - else - SvcList += ", " + Svc.second.Type; + Services_[PrivateEndPoint] = ServiceInfo; + if(Event == KafkaTopics::ServiceEvents::EVENT_JOIN) { + poco_information( + logger(), + fmt::format( + "Service {} ID={} is joining the system.", + Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE) + .toString(), + ID)); + std::string SvcList; + for (const auto &Svc : Services_) { + if (SvcList.empty()) + SvcList = Svc.second.Type; + else + SvcList += ", " + Svc.second.Type; + } + poco_information( + logger(), + fmt::format("Current list of microservices: {}", SvcList)); } - poco_information( - logger(), - fmt::format("Current list of microservices: {}", SvcList)); } } else { - poco_error( + poco_information( logger(), fmt::format("KAFKA-MSG: invalid event '{}', missing a field.", Event)); @@ -118,29 +118,29 @@ namespace OpenWifi { Object->get(KafkaTopics::ServiceEvents::Fields::TOKEN).toString()); #endif } else { - poco_error( + poco_information( logger(), fmt::format("KAFKA-MSG: invalid event '{}', missing token", Event)); } } else { - poco_error(logger(), + poco_information(logger(), fmt::format("Unknown Event: {} Source: {}", Event, ID)); } } } else { - poco_error(logger(), "Bad bus message."); - std::ostringstream os; - Object->stringify(std::cout); + std::ostringstream os; + Object->stringify(std::cout); + poco_error(logger(), fmt::format("Bad bus message: {}", os.str())); } - auto i = Services_.begin(); + auto ServiceHint = Services_.begin(); auto now = Utils::Now(); - for (; i != Services_.end();) { - if ((now - i->second.LastUpdate) > 120) { - poco_warning(logger(), fmt::format("ZombieService: Removing service {}, ", i->second.PublicEndPoint)); - i = Services_.erase(i); + while(ServiceHint!=Services_.end()) { + if ((now - ServiceHint->second.LastUpdate) > 120) { + poco_information(logger(), fmt::format("ZombieService: Removing service {}, ", ServiceHint->second.PublicEndPoint)); + ServiceHint = Services_.erase(ServiceHint); } else - ++i; + ++ServiceHint; } } catch (const Poco::Exception &E) { From af17823df0e126436018b767d82bf1b499d3e791 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sun, 3 Dec 2023 12:03:51 -0800 Subject: [PATCH 089/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/framework/MicroService.cpp | 46 +++++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/build b/build index 7003e7fe1..613955421 100644 --- a/build +++ b/build @@ -1 +1 @@ -51 \ No newline at end of file +52 \ No newline at end of file diff --git a/src/framework/MicroService.cpp b/src/framework/MicroService.cpp index 12c471766..30460fe29 100644 --- a/src/framework/MicroService.cpp +++ b/src/framework/MicroService.cpp @@ -33,6 +33,17 @@ namespace OpenWifi { void MicroService::Exit(int Reason) { std::exit(Reason); } + static std::string MakeServiceListString(const Types::MicroServiceMetaMap &Services) { + std::string SvcList; + for (const auto &Svc : Services) { + if (SvcList.empty()) + SvcList = Svc.second.Type; + else + SvcList += ", " + Svc.second.Type; + } + return SvcList; + } + void MicroService::BusMessageReceived([[maybe_unused]] const std::string &Key, const std::string &Payload) { std::lock_guard G(InfraMutex_); @@ -58,7 +69,7 @@ namespace OpenWifi { if (Event == KafkaTopics::ServiceEvents::EVENT_LEAVE) { Services_.erase(PrivateEndPoint); poco_information( - logger(), + Logger_, fmt::format( "Service {} ID={} leaving system.", Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE) @@ -84,15 +95,16 @@ namespace OpenWifi { .toString(), .LastUpdate = Utils::Now()}; + auto s1 = MakeServiceListString(Services_); Services_[PrivateEndPoint] = ServiceInfo; if(Event == KafkaTopics::ServiceEvents::EVENT_JOIN) { - poco_information( - logger(), + poco_information( + Logger_, fmt::format( - "Service {} ID={} is joining the system.", + "Service {} ID={} is joining the system. old={}", Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE) .toString(), - ID)); + ID, s1)); std::string SvcList; for (const auto &Svc : Services_) { if (SvcList.empty()) @@ -101,13 +113,13 @@ namespace OpenWifi { SvcList += ", " + Svc.second.Type; } poco_information( - logger(), + Logger_, fmt::format("Current list of microservices: {}", SvcList)); } } } else { poco_information( - logger(), + Logger_, fmt::format("KAFKA-MSG: invalid event '{}', missing a field.", Event)); } @@ -119,32 +131,38 @@ namespace OpenWifi { #endif } else { poco_information( - logger(), + Logger_, fmt::format("KAFKA-MSG: invalid event '{}', missing token", Event)); } } else { - poco_information(logger(), + poco_information(Logger_, fmt::format("Unknown Event: {} Source: {}", Event, ID)); } } } else { std::ostringstream os; Object->stringify(std::cout); - poco_error(logger(), fmt::format("Bad bus message: {}", os.str())); + poco_error(Logger_, fmt::format("Bad bus message: {}", os.str())); } auto ServiceHint = Services_.begin(); auto now = Utils::Now(); + auto si1 = Services_.size(); + auto ss1 = MakeServiceListString(Services_); while(ServiceHint!=Services_.end()) { if ((now - ServiceHint->second.LastUpdate) > 120) { - poco_information(logger(), fmt::format("ZombieService: Removing service {}, ", ServiceHint->second.PublicEndPoint)); + poco_information(Logger_, fmt::format("ZombieService: Removing service {}, ", ServiceHint->second.PublicEndPoint)); ServiceHint = Services_.erase(ServiceHint); } else ++ServiceHint; } + if(Services_.size() != si1) { + auto ss2 = MakeServiceListString(Services_); + poco_information(Logger_, fmt::format("Current list of microservices: {} -> {}", ss1, ss2)); + } } catch (const Poco::Exception &E) { - logger().log(E); + Logger_.log(E); } } @@ -413,7 +431,7 @@ namespace OpenWifi { try { DataDir.createDirectory(); } catch (const Poco::Exception &E) { - logger().log(E); + Logger_.log(E); } } WWWAssetsDir_ = ConfigPath("openwifi.restapi.wwwassets", ""); @@ -698,7 +716,7 @@ namespace OpenWifi { auto APIKEY = Request.get("X-API-KEY"); return APIKEY == MyHash_; } catch (const Poco::Exception &E) { - logger().log(E); + Logger_.log(E); } return false; } From 1c6e35fa8b321624f953189423d11723ae6da915 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Sun, 3 Dec 2023 13:16:52 -0800 Subject: [PATCH 090/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/framework/MicroService.cpp | 23 +++++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/build b/build index 613955421..8783e3051 100644 --- a/build +++ b/build @@ -1 +1 @@ -52 \ No newline at end of file +53 \ No newline at end of file diff --git a/src/framework/MicroService.cpp b/src/framework/MicroService.cpp index 30460fe29..6c1aed379 100644 --- a/src/framework/MicroService.cpp +++ b/src/framework/MicroService.cpp @@ -47,6 +47,9 @@ namespace OpenWifi { void MicroService::BusMessageReceived([[maybe_unused]] const std::string &Key, const std::string &Payload) { std::lock_guard G(InfraMutex_); + + Poco::Logger &BusLogger = Poco::Logger::create( + "BusMessageReceived", Poco::Logger::root().getChannel(), Poco::Logger::root().getLevel()); try { Poco::JSON::Parser P; auto Object = P.parse(Payload).extract(); @@ -69,7 +72,7 @@ namespace OpenWifi { if (Event == KafkaTopics::ServiceEvents::EVENT_LEAVE) { Services_.erase(PrivateEndPoint); poco_information( - Logger_, + BusLogger, fmt::format( "Service {} ID={} leaving system.", Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE) @@ -99,7 +102,7 @@ namespace OpenWifi { Services_[PrivateEndPoint] = ServiceInfo; if(Event == KafkaTopics::ServiceEvents::EVENT_JOIN) { poco_information( - Logger_, + BusLogger, fmt::format( "Service {} ID={} is joining the system. old={}", Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE) @@ -113,13 +116,13 @@ namespace OpenWifi { SvcList += ", " + Svc.second.Type; } poco_information( - Logger_, + BusLogger, fmt::format("Current list of microservices: {}", SvcList)); } } } else { poco_information( - Logger_, + BusLogger, fmt::format("KAFKA-MSG: invalid event '{}', missing a field.", Event)); } @@ -131,18 +134,18 @@ namespace OpenWifi { #endif } else { poco_information( - Logger_, + BusLogger, fmt::format("KAFKA-MSG: invalid event '{}', missing token", Event)); } } else { - poco_information(Logger_, + poco_information(BusLogger, fmt::format("Unknown Event: {} Source: {}", Event, ID)); } } } else { std::ostringstream os; Object->stringify(std::cout); - poco_error(Logger_, fmt::format("Bad bus message: {}", os.str())); + poco_error(BusLogger, fmt::format("Bad bus message: {}", os.str())); } auto ServiceHint = Services_.begin(); @@ -151,18 +154,18 @@ namespace OpenWifi { auto ss1 = MakeServiceListString(Services_); while(ServiceHint!=Services_.end()) { if ((now - ServiceHint->second.LastUpdate) > 120) { - poco_information(Logger_, fmt::format("ZombieService: Removing service {}, ", ServiceHint->second.PublicEndPoint)); + poco_information(BusLogger, fmt::format("ZombieService: Removing service {}, ", ServiceHint->second.PublicEndPoint)); ServiceHint = Services_.erase(ServiceHint); } else ++ServiceHint; } if(Services_.size() != si1) { auto ss2 = MakeServiceListString(Services_); - poco_information(Logger_, fmt::format("Current list of microservices: {} -> {}", ss1, ss2)); + poco_information(BusLogger, fmt::format("Current list of microservices: {} -> {}", ss1, ss2)); } } catch (const Poco::Exception &E) { - Logger_.log(E); + BusLogger.log(E); } } From 9f7f4683dff70891c84670b2bf878801dec3b643 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 4 Dec 2023 06:59:39 -0800 Subject: [PATCH 091/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build b/build index 8783e3051..43c451e0c 100644 --- a/build +++ b/build @@ -1 +1 @@ -53 \ No newline at end of file +54 \ No newline at end of file From 8cdd398a6e2b5eadbf6e42f4cd037f6932613d52 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 4 Dec 2023 07:35:34 -0800 Subject: [PATCH 092/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/framework/EventBusManager.cpp | 2 -- src/framework/EventBusManager.h | 12 ++++++++++++ src/framework/MicroService.cpp | 10 ++++------ src/framework/MicroService.h | 1 - 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/build b/build index 43c451e0c..7c6ba0fe1 100644 --- a/build +++ b/build @@ -1 +1 @@ -54 \ No newline at end of file +55 \ No newline at end of file diff --git a/src/framework/EventBusManager.cpp b/src/framework/EventBusManager.cpp index ca28ad940..66774f1eb 100644 --- a/src/framework/EventBusManager.cpp +++ b/src/framework/EventBusManager.cpp @@ -9,8 +9,6 @@ namespace OpenWifi { - EventBusManager::EventBusManager(Poco::Logger &L) : Logger_(L) {} - void EventBusManager::run() { Running_ = true; Utils::SetThreadName("fmwk:EventMgr"); diff --git a/src/framework/EventBusManager.h b/src/framework/EventBusManager.h index 9a7316e96..fe8a82cb6 100644 --- a/src/framework/EventBusManager.h +++ b/src/framework/EventBusManager.h @@ -12,6 +12,16 @@ namespace OpenWifi { class EventBusManager : public Poco::Runnable { public: + EventBusManager() : + Logger_(Poco::Logger::create( + "EventBusManager", Poco::Logger::root().getChannel(), Poco::Logger::root().getLevel())) { + } + + static auto instance() { + static auto instance_ = new EventBusManager; + return instance_; + } + explicit EventBusManager(Poco::Logger &L); void run() final; void Start(); @@ -24,4 +34,6 @@ namespace OpenWifi { Poco::Logger &Logger_; }; + inline auto EventBusManager() { return EventBusManager::instance(); } + } // namespace OpenWifi diff --git a/src/framework/MicroService.cpp b/src/framework/MicroService.cpp index 6c1aed379..fabc65bb6 100644 --- a/src/framework/MicroService.cpp +++ b/src/framework/MicroService.cpp @@ -48,8 +48,8 @@ namespace OpenWifi { const std::string &Payload) { std::lock_guard G(InfraMutex_); - Poco::Logger &BusLogger = Poco::Logger::create( - "BusMessageReceived", Poco::Logger::root().getChannel(), Poco::Logger::root().getLevel()); + Poco::Logger &BusLogger = EventBusManager()->Logger(); + try { Poco::JSON::Parser P; auto Object = P.parse(Payload).extract(); @@ -552,14 +552,12 @@ namespace OpenWifi { for (auto i : SubSystems_) { i->Start(); } - EventBusManager_ = std::make_unique(Poco::Logger::create( - "EventBusManager", Poco::Logger::root().getChannel(), Poco::Logger::root().getLevel())); - EventBusManager_->Start(); + EventBusManager()->Start(); } void MicroService::StopSubSystemServers() { AddActivity("Stopping"); - EventBusManager_->Stop(); + EventBusManager()->Stop(); for (auto i = SubSystems_.rbegin(); i != SubSystems_.rend(); ++i) { (*i)->Stop(); } diff --git a/src/framework/MicroService.h b/src/framework/MicroService.h index 00532b476..7290bbbbe 100644 --- a/src/framework/MicroService.h +++ b/src/framework/MicroService.h @@ -201,7 +201,6 @@ namespace OpenWifi { Poco::JWT::Signer Signer_; Poco::Logger &Logger_; Poco::ThreadPool TimerPool_{"timer:pool", 2, 32}; - std::unique_ptr EventBusManager_; }; inline MicroService *MicroService::instance_ = nullptr; From 644918fa144eecb2e05fa9e1b1a7013276e2edd1 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 4 Dec 2023 08:28:54 -0800 Subject: [PATCH 093/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/framework/MicroService.cpp | 29 ++++++++++++++++++++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/build b/build index 7c6ba0fe1..2ebc6516c 100644 --- a/build +++ b/build @@ -1 +1 @@ -55 \ No newline at end of file +56 \ No newline at end of file diff --git a/src/framework/MicroService.cpp b/src/framework/MicroService.cpp index fabc65bb6..13f89fbf4 100644 --- a/src/framework/MicroService.cpp +++ b/src/framework/MicroService.cpp @@ -99,15 +99,21 @@ namespace OpenWifi { .LastUpdate = Utils::Now()}; auto s1 = MakeServiceListString(Services_); + auto PreviousSize = Services_.size(); Services_[PrivateEndPoint] = ServiceInfo; + auto CurrentSize = Services_.size(); if(Event == KafkaTopics::ServiceEvents::EVENT_JOIN) { - poco_information( - BusLogger, - fmt::format( - "Service {} ID={} is joining the system. old={}", - Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE) - .toString(), - ID, s1)); + if(!s1.empty()) { + poco_information( + BusLogger, + fmt::format( + "Service {} ID={} is joining the system.", + Object + ->get( + KafkaTopics::ServiceEvents::Fields::PRIVATE) + .toString(), + ID)); + } std::string SvcList; for (const auto &Svc : Services_) { if (SvcList.empty()) @@ -118,6 +124,15 @@ namespace OpenWifi { poco_information( BusLogger, fmt::format("Current list of microservices: {}", SvcList)); + } else if(CurrentSize!=PreviousSize) { + poco_information( + BusLogger, + fmt::format( + "Service {} ID={} is being added back in.", + Object + ->get(KafkaTopics::ServiceEvents::Fields::PRIVATE) + .toString(), + ID)); } } } else { From 3b5580a5250d08e487fa8376fbb30eae1ff794f5 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 4 Dec 2023 09:46:20 -0800 Subject: [PATCH 094/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 107 ++++++++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 43 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 94539a5f3..dcf686dfa 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -221,37 +221,47 @@ namespace OpenWifi { AverageDeviceConnectionTime_ = 0; for(int hashIndex=0;hashIndexsecond == nullptr) { - hint = SerialNumbers_[hashIndex].erase(hint); - continue; - } - auto Device = hint->second; - std::lock_guard DeviceGuard(Device->ConnectionMutex_); - if (RightNow>Device->LastContact_ && (RightNow - Device->LastContact_) > - SessionTimeOut_) { - poco_information( - Logger(), - fmt::format( - "{}: Session seems idle. Controller disconnecting device.", - Device->SerialNumber_)); - hint = SerialNumbers_[hashIndex].erase(hint); - continue; - } - - if (Device->State_.Connected) { - NumberOfConnectedDevices_++; - total_connected_time += (RightNow - Device->State_.started); + int waits = 0; + if(SerialNumbersMutex_[hashIndex].try_lock()) { + auto RightNow = Utils::Now(); + auto hint = SerialNumbers_[hashIndex].begin(); + while (hint != end(SerialNumbers_[hashIndex])) { + + if (hint->second == nullptr) { + hint = SerialNumbers_[hashIndex].erase(hint); + continue; + } + auto Device = hint->second; + std::lock_guard DeviceGuard(Device->ConnectionMutex_); + if (RightNow > Device->LastContact_ && + (RightNow - Device->LastContact_) > SessionTimeOut_) { + poco_information( + Logger(), + fmt::format( + "{}: Session seems idle. Controller disconnecting device.", + Device->SerialNumber_)); + hint = SerialNumbers_[hashIndex].erase(hint); + continue; + } + + if (Device->State_.Connected) { + NumberOfConnectedDevices_++; + total_connected_time += (RightNow - Device->State_.started); + ++hint; + continue; + } + // Device must be in connecting state: established a ws connection but has not sent in a connect message. + ++NumberOfConnectingDevices_; ++hint; + } + SerialNumbersMutex_[hashIndex].unlock(); + } else { + if(waits<5) { + waits++; + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } else { continue; } - // Device must be in connecting state: established a ws connection but has not sent in a connect message. - ++NumberOfConnectingDevices_; - ++hint; } } @@ -259,22 +269,33 @@ namespace OpenWifi { poco_information(Logger(), fmt::format("Garbage collecting zombies... (step 2)")); LeftOverSessions_ = 0; for(int i=0;isecond == nullptr) { - hint = Sessions_[i].erase(hint); - } else if (RightNow>hint->second->LastContact_ && (RightNow - hint->second->LastContact_) > SessionTimeOut_) { - poco_information( - Logger(), - fmt::format( - "{}: Session seems idle. Controller disconnecting device.", - hint->second->SerialNumber_)); - hint = Sessions_[i].erase(hint); + int waits = 0 ; + if(SessionMutex_[i].try_lock()) { + auto hint = Sessions_[i].begin(); + auto RightNow = Utils::Now(); + while (hint != end(Sessions_[i])) { + if (hint->second == nullptr) { + hint = Sessions_[i].erase(hint); + } else if (RightNow > hint->second->LastContact_ && + (RightNow - hint->second->LastContact_) > SessionTimeOut_) { + poco_information( + Logger(), + fmt::format( + "{}: Session seems idle. Controller disconnecting device.", + hint->second->SerialNumber_)); + hint = Sessions_[i].erase(hint); + } else { + ++LeftOverSessions_; + ++hint; + } + } + SessionMutex_[i].unlock(); + } else { + if(waits<5) { + waits++; + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } else { - ++LeftOverSessions_; - ++hint; + continue; } } } From 5364adf509037a41906596bca09d1b751f1c8a25 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 4 Dec 2023 11:08:47 -0800 Subject: [PATCH 095/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Server.cpp | 119 ++++++++++++++++++++++--------------------- 2 files changed, 61 insertions(+), 60 deletions(-) diff --git a/build b/build index 2ebc6516c..f0b5c72ca 100644 --- a/build +++ b/build @@ -1 +1 @@ -56 \ No newline at end of file +57 \ No newline at end of file diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index dcf686dfa..4c72ed47d 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -219,48 +219,48 @@ namespace OpenWifi { NumberOfConnectedDevices_ = 0; NumberOfConnectingDevices_ = 0; AverageDeviceConnectionTime_ = 0; + int waits = 0; for(int hashIndex=0;hashIndexsecond == nullptr) { - hint = SerialNumbers_[hashIndex].erase(hint); - continue; - } - auto Device = hint->second; - std::lock_guard DeviceGuard(Device->ConnectionMutex_); - if (RightNow > Device->LastContact_ && - (RightNow - Device->LastContact_) > SessionTimeOut_) { - poco_information( - Logger(), - fmt::format( - "{}: Session seems idle. Controller disconnecting device.", - Device->SerialNumber_)); - hint = SerialNumbers_[hashIndex].erase(hint); - continue; - } - - if (Device->State_.Connected) { - NumberOfConnectedDevices_++; - total_connected_time += (RightNow - Device->State_.started); + while(true) { + if (SerialNumbersMutex_[hashIndex].try_lock()) { + waits = 0; + auto RightNow = Utils::Now(); + auto hint = SerialNumbers_[hashIndex].begin(); + while (hint != end(SerialNumbers_[hashIndex])) { + + if (hint->second == nullptr) { + hint = SerialNumbers_[hashIndex].erase(hint); + continue; + } + auto Device = hint->second; + std::lock_guard DeviceGuard(Device->ConnectionMutex_); + if (RightNow > Device->LastContact_ && + (RightNow - Device->LastContact_) > SessionTimeOut_) { + poco_information(Logger(), + fmt::format("{}: Session seems idle. Controller disconnecting device.", + Device->SerialNumber_)); + hint = SerialNumbers_[hashIndex].erase(hint); + continue; + } + + if (Device->State_.Connected) { + NumberOfConnectedDevices_++; + total_connected_time += (RightNow - Device->State_.started); + ++hint; + continue; + } + // Device must be in connecting state: established a ws connection but has not sent in a connect message. + ++NumberOfConnectingDevices_; ++hint; - continue; } - // Device must be in connecting state: established a ws connection but has not sent in a connect message. - ++NumberOfConnectingDevices_; - ++hint; - } - SerialNumbersMutex_[hashIndex].unlock(); - } else { - if(waits<5) { + SerialNumbersMutex_[hashIndex].unlock(); + break; + } else if (waits < 5) { waits++; std::this_thread::sleep_for(std::chrono::milliseconds(10)); } else { - continue; + break; } } @@ -268,34 +268,35 @@ namespace OpenWifi { poco_information(Logger(), fmt::format("Garbage collecting zombies... (step 2)")); LeftOverSessions_ = 0; + waits = 0 ; for(int i=0;isecond == nullptr) { - hint = Sessions_[i].erase(hint); - } else if (RightNow > hint->second->LastContact_ && - (RightNow - hint->second->LastContact_) > SessionTimeOut_) { - poco_information( - Logger(), - fmt::format( - "{}: Session seems idle. Controller disconnecting device.", - hint->second->SerialNumber_)); - hint = Sessions_[i].erase(hint); - } else { - ++LeftOverSessions_; - ++hint; + while (true) { + if (SessionMutex_[i].try_lock()) { + waits = 0; + auto hint = Sessions_[i].begin(); + auto RightNow = Utils::Now(); + while (hint != end(Sessions_[i])) { + if (hint->second == nullptr) { + hint = Sessions_[i].erase(hint); + } else if (RightNow > hint->second->LastContact_ && + (RightNow - hint->second->LastContact_) > + SessionTimeOut_) { + poco_information(Logger(), + fmt::format("{}: Session seems idle. Controller disconnecting device.", + hint->second->SerialNumber_)); + hint = Sessions_[i].erase(hint); + } else { + ++LeftOverSessions_; + ++hint; + } } - } - SessionMutex_[i].unlock(); - } else { - if(waits<5) { - waits++; + SessionMutex_[i].unlock(); + break; + } else if (waits<5){ std::this_thread::sleep_for(std::chrono::milliseconds(10)); + waits++; } else { - continue; + break; } } } From 4eda1b813f2647f86d532081825bac857de1e459 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 4 Dec 2023 11:10:32 -0800 Subject: [PATCH 096/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Server.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/build b/build index f0b5c72ca..4800c7da6 100644 --- a/build +++ b/build @@ -1 +1 @@ -57 \ No newline at end of file +58 \ No newline at end of file diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 4c72ed47d..2addb8415 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -222,6 +222,7 @@ namespace OpenWifi { int waits = 0; for(int hashIndex=0;hashIndex Date: Mon, 4 Dec 2023 11:37:38 -0800 Subject: [PATCH 097/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/AP_WS_Process_healthcheck.cpp | 3 ++- src/AP_WS_Process_state.cpp | 3 ++- src/AP_WS_Server.cpp | 3 +++ src/AP_WS_Server.h | 5 +++++ 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/build b/build index 4800c7da6..fc9afb48e 100644 --- a/build +++ b/build @@ -1 +1 @@ -58 \ No newline at end of file +59 \ No newline at end of file diff --git a/src/AP_WS_Process_healthcheck.cpp b/src/AP_WS_Process_healthcheck.cpp index a6aab8721..5b0f2bd94 100644 --- a/src/AP_WS_Process_healthcheck.cpp +++ b/src/AP_WS_Process_healthcheck.cpp @@ -3,6 +3,7 @@ // #include "AP_WS_Connection.h" +#include "AP_WS_Server.h" #include "StorageService.h" #include "fmt/format.h" @@ -55,7 +56,7 @@ namespace OpenWifi { } SetLastHealthCheck(Check); - if (KafkaManager()->Enabled()) { + if (KafkaManager()->Enabled() && !AP_WS_Server()->KafkaDisableHealthChecks()) { KafkaManager()->PostMessage(KafkaTopics::HEALTHCHECK, SerialNumber_, *ParamsObj); } } else { diff --git a/src/AP_WS_Process_state.cpp b/src/AP_WS_Process_state.cpp index 567c7cddf..ffacb0ed6 100644 --- a/src/AP_WS_Process_state.cpp +++ b/src/AP_WS_Process_state.cpp @@ -3,6 +3,7 @@ // #include "AP_WS_Connection.h" +#include "AP_WS_Server.h" #include "StateUtils.h" #include "StorageService.h" @@ -59,7 +60,7 @@ namespace OpenWifi { StateUtils::ComputeAssociations(StateObj, State_.Associations_2G, State_.Associations_5G, State_.Associations_6G); - if (KafkaManager()->Enabled()) { + if (KafkaManager()->Enabled() && !AP_WS_Server()->KafkaDisableState()) { KafkaManager()->PostMessage(KafkaTopics::STATE, SerialNumber_, *ParamsObj); } diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 2addb8415..dacb67500 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -167,6 +167,9 @@ namespace OpenWifi { WebServerHttpParams); WebServers_.push_back(std::move(NewWebServer)); } + + KafkaDisableState_ = MicroServiceConfigGetBool("openwifi.kafka.disablestate", false); + KafkaDisableHealthChecks_ = MicroServiceConfigGetBool("openwifi.kafka.disablehealthchecks", false); } for (auto &server : WebServers_) { diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index 0f0f4cd92..9d9cb9ac6 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -192,6 +192,8 @@ namespace OpenWifi { RX = RX_; } + bool KafkaDisableState() const { return KafkaDisableState_; } + bool KafkaDisableHealthChecks() const { return KafkaDisableHealthChecks_; } private: std::array SessionMutex_; @@ -223,6 +225,9 @@ namespace OpenWifi { std::uint64_t LeftOverSessions_ = 0; std::atomic_uint64_t TX_=0,RX_=0; + std::atomic_bool KafkaDisableState_=false, + KafkaDisableHealthChecks_=false; + Poco::Thread GarbageCollector_; AP_WS_Server() noexcept From c27f0390f8d1faa70de736e03446c52fc0f64083 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 4 Dec 2023 11:48:10 -0800 Subject: [PATCH 098/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 174 +++++++++++++++++++++++-------------------- 1 file changed, 93 insertions(+), 81 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index dacb67500..e151be426 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -217,98 +217,110 @@ namespace OpenWifi { uint64_t total_connected_time = 0, now = Utils::Now(); if(now-last_zombie_run > 60) { - poco_information(Logger(), fmt::format("Garbage collecting zombies... (step 1)")); - std::vector SessionsToRemove; - NumberOfConnectedDevices_ = 0; - NumberOfConnectingDevices_ = 0; - AverageDeviceConnectionTime_ = 0; - int waits = 0; - for(int hashIndex=0;hashIndexsecond == nullptr) { - hint = SerialNumbers_[hashIndex].erase(hint); - continue; - } - auto Device = hint->second; - std::lock_guard DeviceGuard(Device->ConnectionMutex_); - if (RightNow > Device->LastContact_ && - (RightNow - Device->LastContact_) > SessionTimeOut_) { - poco_information(Logger(), - fmt::format("{}: Session seems idle. Controller disconnecting device.", - Device->SerialNumber_)); - hint = SerialNumbers_[hashIndex].erase(hint); - continue; - } - - if (Device->State_.Connected) { - NumberOfConnectedDevices_++; - total_connected_time += (RightNow - Device->State_.started); + try { + poco_information(Logger(), + fmt::format("Garbage collecting zombies... (step 1)")); + std::vector SessionsToRemove; + NumberOfConnectedDevices_ = 0; + NumberOfConnectingDevices_ = 0; + AverageDeviceConnectionTime_ = 0; + int waits = 0; + for (int hashIndex = 0; hashIndex < MACHash::HashMax(); hashIndex++) { + last_zombie_run = now; + waits = 0; + while (true) { + if (SerialNumbersMutex_[hashIndex].try_lock()) { + waits = 0; + auto RightNow = Utils::Now(); + auto hint = SerialNumbers_[hashIndex].begin(); + while (hint != end(SerialNumbers_[hashIndex])) { + + if (hint->second == nullptr) { + hint = SerialNumbers_[hashIndex].erase(hint); + continue; + } + auto Device = hint->second; + std::lock_guard DeviceGuard(Device->ConnectionMutex_); + if (RightNow > Device->LastContact_ && + (RightNow - Device->LastContact_) > SessionTimeOut_) { + poco_information( + Logger(), + fmt::format("{}: Session seems idle. Controller disconnecting device.", + Device->SerialNumber_)); + hint = SerialNumbers_[hashIndex].erase(hint); + continue; + } + + if (Device->State_.Connected) { + NumberOfConnectedDevices_++; + total_connected_time += (RightNow - Device->State_.started); + ++hint; + continue; + } + // Device must be in connecting state: established a ws connection but has not sent in a connect message. + ++NumberOfConnectingDevices_; ++hint; - continue; } - // Device must be in connecting state: established a ws connection but has not sent in a connect message. - ++NumberOfConnectingDevices_; - ++hint; + SerialNumbersMutex_[hashIndex].unlock(); + break; + } else if (waits < 5) { + waits++; + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } else { + break; } - SerialNumbersMutex_[hashIndex].unlock(); - break; - } else if (waits < 5) { - waits++; - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } else { - break; } } - } - - poco_information(Logger(), fmt::format("Garbage collecting zombies... (step 2)")); - LeftOverSessions_ = 0; - for(int i=0;isecond == nullptr) { - hint = Sessions_[i].erase(hint); - } else if (RightNow > hint->second->LastContact_ && - (RightNow - hint->second->LastContact_) > - SessionTimeOut_) { - poco_information(Logger(), - fmt::format("{}: Session seems idle. Controller disconnecting device.", - hint->second->SerialNumber_)); - hint = Sessions_[i].erase(hint); - } else { - ++LeftOverSessions_; - ++hint; + poco_information(Logger(), + fmt::format("Garbage collecting zombies... (step 2)")); + LeftOverSessions_ = 0; + for (int i = 0; i < SessionHash::HashMax(); i++) { + waits = 0; + while (true) { + if (SessionMutex_[i].try_lock()) { + waits = 0; + auto hint = Sessions_[i].begin(); + auto RightNow = Utils::Now(); + while (hint != end(Sessions_[i])) { + if (hint->second == nullptr) { + hint = Sessions_[i].erase(hint); + } else if (RightNow > hint->second->LastContact_ && + (RightNow - hint->second->LastContact_) > + SessionTimeOut_) { + poco_information( + Logger(), + fmt::format("{}: Session seems idle. Controller disconnecting device.", + hint->second->SerialNumber_)); + hint = Sessions_[i].erase(hint); + } else { + ++LeftOverSessions_; + ++hint; + } } + SessionMutex_[i].unlock(); + break; + } else if (waits < 5) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + waits++; + } else { + break; } - SessionMutex_[i].unlock(); - break; - } else if (waits<5){ - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - waits++; - } else { - break; } } - } - AverageDeviceConnectionTime_ = - NumberOfConnectedDevices_ > 0 ? total_connected_time / NumberOfConnectedDevices_ - : 0; - poco_information(Logger(), fmt::format("Garbage collecting zombies done...")); + AverageDeviceConnectionTime_ = + NumberOfConnectedDevices_ > 0 + ? total_connected_time / NumberOfConnectedDevices_ + : 0; + poco_information(Logger(), fmt::format("Garbage collecting zombies done...")); + } catch (const Poco::Exception &E) { + poco_error(Logger(), fmt::format("Poco::Exception: Garbage collecting zombies failed: {}", E.displayText())); + } catch (const std::exception &E) { + poco_error(Logger(), fmt::format("std::exception: Garbage collecting zombies failed: {}", E.what())); + } catch (...) { + poco_error(Logger(), fmt::format("exception:Garbage collecting zombies failed: {}", "unknown")); + } } else { NumberOfConnectedDevices_=0; From d1216a8ac433801acec2ef953da4f91df7db320d Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 4 Dec 2023 19:14:34 -0800 Subject: [PATCH 099/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 27 +++++++++++++++------------ src/AP_WS_Server.h | 2 +- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index e151be426..11fd46da9 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -206,19 +206,22 @@ namespace OpenWifi { last_zombie_run = 0, last_garbage_run = 0; + Poco::Logger &LocalLogger = Poco::Logger::create( + "WS-Session-Janitor", Poco::Logger::root().getChannel(), Poco::Logger::root().getLevel()); + while(Running_) { if(!Poco::Thread::trySleep(30000)) { break; } - Logger().information(fmt::format("Garbage collecting starting run." )); + LocalLogger.information(fmt::format("Garbage collecting starting run." )); uint64_t total_connected_time = 0, now = Utils::Now(); if(now-last_zombie_run > 60) { try { - poco_information(Logger(), + poco_information(LocalLogger, fmt::format("Garbage collecting zombies... (step 1)")); std::vector SessionsToRemove; NumberOfConnectedDevices_ = 0; @@ -244,7 +247,7 @@ namespace OpenWifi { if (RightNow > Device->LastContact_ && (RightNow - Device->LastContact_) > SessionTimeOut_) { poco_information( - Logger(), + LocalLogger, fmt::format("{}: Session seems idle. Controller disconnecting device.", Device->SerialNumber_)); hint = SerialNumbers_[hashIndex].erase(hint); @@ -272,7 +275,7 @@ namespace OpenWifi { } } - poco_information(Logger(), + poco_information(LocalLogger, fmt::format("Garbage collecting zombies... (step 2)")); LeftOverSessions_ = 0; for (int i = 0; i < SessionHash::HashMax(); i++) { @@ -289,7 +292,7 @@ namespace OpenWifi { (RightNow - hint->second->LastContact_) > SessionTimeOut_) { poco_information( - Logger(), + LocalLogger, fmt::format("{}: Session seems idle. Controller disconnecting device.", hint->second->SerialNumber_)); hint = Sessions_[i].erase(hint); @@ -313,13 +316,13 @@ namespace OpenWifi { NumberOfConnectedDevices_ > 0 ? total_connected_time / NumberOfConnectedDevices_ : 0; - poco_information(Logger(), fmt::format("Garbage collecting zombies done...")); + poco_information(LocalLogger, fmt::format("Garbage collecting zombies done...")); } catch (const Poco::Exception &E) { - poco_error(Logger(), fmt::format("Poco::Exception: Garbage collecting zombies failed: {}", E.displayText())); + poco_error(LocalLogger, fmt::format("Poco::Exception: Garbage collecting zombies failed: {}", E.displayText())); } catch (const std::exception &E) { - poco_error(Logger(), fmt::format("std::exception: Garbage collecting zombies failed: {}", E.what())); + poco_error(LocalLogger, fmt::format("std::exception: Garbage collecting zombies failed: {}", E.what())); } catch (...) { - poco_error(Logger(), fmt::format("exception:Garbage collecting zombies failed: {}", "unknown")); + poco_error(LocalLogger, fmt::format("exception:Garbage collecting zombies failed: {}", "unknown")); } } else { @@ -339,7 +342,7 @@ namespace OpenWifi { if ((now - last_log) > 60) { last_log = now; - poco_information(Logger(), + poco_information(LocalLogger, fmt::format("Active AP connections: {} Connecting: {} Average connection time: {} seconds. Left Over Sessions: {}", NumberOfConnectedDevices_, NumberOfConnectingDevices_, AverageDeviceConnectionTime_, LeftOverSessions_)); @@ -361,10 +364,10 @@ namespace OpenWifi { FullEvent.set("payload", KafkaNotification); KafkaManager()->PostMessage(KafkaTopics::DEVICE_EVENT_QUEUE, "system", FullEvent); - Logger().information(fmt::format("Garbage collection finished run." )); + LocalLogger.information(fmt::format("Garbage collection finished run." )); last_garbage_run = now; } - Logger().information(fmt::format("Garbage collector done for the day." )); + LocalLogger.information(fmt::format("Garbage collector done for the day." )); } void AP_WS_Server::Stop() { diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index 9d9cb9ac6..2a5f0f3a5 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -201,7 +201,7 @@ namespace OpenWifi { using SerialNumberMap = std::map>; std::array SerialNumbers_; - mutable std::array SerialNumbersMutex_; + mutable std::array SerialNumbersMutex_; std::unique_ptr IssuerCert_; std::list> WebServers_; From c52d3c92f6a6bec700f2ec372f646f0c7273ca11 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 4 Dec 2023 20:08:48 -0800 Subject: [PATCH 100/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 11fd46da9..370e79968 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -239,7 +239,8 @@ namespace OpenWifi { while (hint != end(SerialNumbers_[hashIndex])) { if (hint->second == nullptr) { - hint = SerialNumbers_[hashIndex].erase(hint); + // hint = SerialNumbers_[hashIndex].erase(hint); + hint++; continue; } auto Device = hint->second; From b3ec5b1e817c852c6ed5aea5acd8a7dcb54e88f3 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 4 Dec 2023 20:59:00 -0800 Subject: [PATCH 101/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 370e79968..7f5d3f186 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -223,7 +223,6 @@ namespace OpenWifi { try { poco_information(LocalLogger, fmt::format("Garbage collecting zombies... (step 1)")); - std::vector SessionsToRemove; NumberOfConnectedDevices_ = 0; NumberOfConnectingDevices_ = 0; AverageDeviceConnectionTime_ = 0; @@ -234,7 +233,6 @@ namespace OpenWifi { while (true) { if (SerialNumbersMutex_[hashIndex].try_lock()) { waits = 0; - auto RightNow = Utils::Now(); auto hint = SerialNumbers_[hashIndex].begin(); while (hint != end(SerialNumbers_[hashIndex])) { @@ -244,24 +242,25 @@ namespace OpenWifi { continue; } auto Device = hint->second; - std::lock_guard DeviceGuard(Device->ConnectionMutex_); - if (RightNow > Device->LastContact_ && - (RightNow - Device->LastContact_) > SessionTimeOut_) { - poco_information( - LocalLogger, - fmt::format("{}: Session seems idle. Controller disconnecting device.", - Device->SerialNumber_)); - hint = SerialNumbers_[hashIndex].erase(hint); - continue; - } - - if (Device->State_.Connected) { - NumberOfConnectedDevices_++; - total_connected_time += (RightNow - Device->State_.started); - ++hint; - continue; + if(Device->ConnectionMutex_.try_lock()) { + auto RightNow = Utils::Now(); + if (RightNow > Device->LastContact_ && + (RightNow - Device->LastContact_) > SessionTimeOut_) { + poco_information( + LocalLogger, + fmt::format("{}: Session seems idle. Controller disconnecting device.", + Device->SerialNumber_)); + hint = SerialNumbers_[hashIndex].erase(hint); + } else if (Device->State_.Connected) { + NumberOfConnectedDevices_++; + total_connected_time += + (RightNow - Device->State_.started); + ++hint; + } else { + ++hint; + } + Device->ConnectionMutex_.unlock(); } - // Device must be in connecting state: established a ws connection but has not sent in a connect message. ++NumberOfConnectingDevices_; ++hint; } From 607507ce9a8c0ef4035185c8ff53d7ef9685cae3 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 4 Dec 2023 21:00:32 -0800 Subject: [PATCH 102/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 7f5d3f186..adb3e9220 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -260,6 +260,9 @@ namespace OpenWifi { ++hint; } Device->ConnectionMutex_.unlock(); + } else { + poco_warning(LocalLogger, fmt::format("Could not lock device mutex for {}", + Device->SerialNumber_)); } ++NumberOfConnectingDevices_; ++hint; From e40d53f3e53b0de67af32c972f5ef0d325476df0 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 4 Dec 2023 21:08:20 -0800 Subject: [PATCH 103/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index adb3e9220..771543ec4 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -260,6 +260,7 @@ namespace OpenWifi { ++hint; } Device->ConnectionMutex_.unlock(); + continue; } else { poco_warning(LocalLogger, fmt::format("Could not lock device mutex for {}", Device->SerialNumber_)); From 40e814cab8d9ce7d219562a0098f6c752af1324e Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 4 Dec 2023 21:15:27 -0800 Subject: [PATCH 104/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Server.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/AP_WS_Server.cpp b/src/AP_WS_Server.cpp index 771543ec4..87fc8949e 100644 --- a/src/AP_WS_Server.cpp +++ b/src/AP_WS_Server.cpp @@ -237,6 +237,9 @@ namespace OpenWifi { while (hint != end(SerialNumbers_[hashIndex])) { if (hint->second == nullptr) { + poco_information( + LocalLogger, + fmt::format("Dead device found in hash index {}", hashIndex)); // hint = SerialNumbers_[hashIndex].erase(hint); hint++; continue; From 0d549137de1f3b2952366650315b9db65333e269 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 4 Dec 2023 21:27:54 -0800 Subject: [PATCH 105/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 9fa75c4a9..239fc4659 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -89,6 +89,19 @@ namespace OpenWifi { State_.sessionId)); } + static void NotifyKafkaDisconnect(const std::string &SerialNumber, std::uint64_t uuid) { + try { + Poco::JSON::Object Disconnect; + Poco::JSON::Object Details; + Details.set(uCentralProtocol::SERIALNUMBER, SerialNumber); + Details.set(uCentralProtocol::TIMESTAMP, Utils::Now()); + Details.set(uCentralProtocol::UUID,uuid); + Disconnect.set(uCentralProtocol::DISCONNECTION, Details); + KafkaManager()->PostMessage(KafkaTopics::CONNECTION, SerialNumber, Disconnect); + } catch (...) { + } + } + void AP_WS_Connection::EndConnection(bool Clean) { bool expectedValue=false; if (Dead_.compare_exchange_strong(expectedValue,true,std::memory_order_release,std::memory_order_relaxed)) { @@ -113,8 +126,15 @@ namespace OpenWifi { WS_->close(); if(!SerialNumber_.empty()) { - std::thread Cleanup(DeviceDisconnectionCleanup,SerialNumber_, uuid_); - Cleanup.detach(); +// std::thread Cleanup(DeviceDisconnectionCleanup,SerialNumber_, uuid_); +// Cleanup.detach(); + if (KafkaManager()->Enabled()) { + NotifyKafkaDisconnect(SerialNumber_, uuid_); + } + RADIUSSessionTracker()->DeviceDisconnect(SerialNumber_); + GWWebSocketNotifications::SingleDevice_t N; + N.content.serialNumber = SerialNumber_; + GWWebSocketNotifications::DeviceDisconnected(N); } if(Clean) @@ -272,19 +292,6 @@ namespace OpenWifi { return false; } - static void NotifyKafkaDisconnect(const std::string &SerialNumber, std::uint64_t uuid) { - try { - Poco::JSON::Object Disconnect; - Poco::JSON::Object Details; - Details.set(uCentralProtocol::SERIALNUMBER, SerialNumber); - Details.set(uCentralProtocol::TIMESTAMP, Utils::Now()); - Details.set(uCentralProtocol::UUID,uuid); - Disconnect.set(uCentralProtocol::DISCONNECTION, Details); - KafkaManager()->PostMessage(KafkaTopics::CONNECTION, SerialNumber, Disconnect); - } catch (...) { - } - } - void AP_WS_Connection::DeviceDisconnectionCleanup(const std::string &SerialNumber, std::uint64_t uuid) { if (KafkaManager()->Enabled()) { NotifyKafkaDisconnect(SerialNumber, uuid); From 25a03ca801f460db5d8b1a3f14cc20a3dbc4a5d5 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 4 Dec 2023 21:44:27 -0800 Subject: [PATCH 106/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 239fc4659..6c8bde4c2 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -126,15 +126,7 @@ namespace OpenWifi { WS_->close(); if(!SerialNumber_.empty()) { -// std::thread Cleanup(DeviceDisconnectionCleanup,SerialNumber_, uuid_); -// Cleanup.detach(); - if (KafkaManager()->Enabled()) { - NotifyKafkaDisconnect(SerialNumber_, uuid_); - } - RADIUSSessionTracker()->DeviceDisconnect(SerialNumber_); - GWWebSocketNotifications::SingleDevice_t N; - N.content.serialNumber = SerialNumber_; - GWWebSocketNotifications::DeviceDisconnected(N); + DeviceDisconnectionCleanup(SerialNumber_, uuid_); } if(Clean) From 082153a229ec450afbdc68a882b34dbadae5b7ca Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 11 Dec 2023 09:47:13 -0800 Subject: [PATCH 107/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/framework/orm.h | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/build b/build index fc9afb48e..2b82dfea3 100644 --- a/build +++ b/build @@ -1 +1 @@ -59 \ No newline at end of file +60 \ No newline at end of file diff --git a/src/framework/orm.h b/src/framework/orm.h index 9e83540ff..c0be13736 100644 --- a/src/framework/orm.h +++ b/src/framework/orm.h @@ -576,8 +576,8 @@ namespace ORM { bool UpdateRecord(field_name_t FieldName, const T &Value, const RecordType &R) { try { assert(ValidFieldName(FieldName)); - Poco::Data::Session Session = Pool_.get(); + Session.begin(); Poco::Data::Statement Update(Session); RecordTuple RT; @@ -593,6 +593,7 @@ namespace ORM { Update.execute(); if (Cache_) Cache_->UpdateCache(R); + Session.commit(); return true; } catch (const Poco::Exception &E) { Logger_.log(E); @@ -662,6 +663,7 @@ namespace ORM { assert(ValidFieldName(FieldName)); Poco::Data::Session Session = Pool_.get(); + Session.begin(); Poco::Data::Statement Delete(Session); std::string St = "delete from " + TableName_ + " where " + FieldName + "=?"; @@ -671,6 +673,7 @@ namespace ORM { Delete.execute(); if (Cache_) Cache_->Delete(FieldName, Value); + Session.commit(); return true; } catch (const Poco::Exception &E) { Logger_.log(E); @@ -682,11 +685,13 @@ namespace ORM { try { assert(!WhereClause.empty()); Poco::Data::Session Session = Pool_.get(); + Session.begin(); Poco::Data::Statement Delete(Session); std::string St = "delete from " + TableName_ + " where " + WhereClause; Delete << St; Delete.execute(); + Session.commit(); return true; } catch (const Poco::Exception &E) { Logger_.log(E); From eb441d71aaa49875786c1a46ff55fea3a2007faf Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 12 Dec 2023 21:39:14 -0800 Subject: [PATCH 108/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- owgw.properties.tmpl | 2 +- src/AP_WS_Process_connect.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/owgw.properties.tmpl b/owgw.properties.tmpl index fd9353c20..15d733b88 100644 --- a/owgw.properties.tmpl +++ b/owgw.properties.tmpl @@ -145,7 +145,7 @@ storage.type.sqlite.db = devices.db storage.type.sqlite.idletime = 120 storage.type.sqlite.maxsessions = 128 -storage.type.postgresql.maxsessions = 64 +storage.type.postgresql.maxsessions = 250 storage.type.postgresql.idletime = 60 storage.type.postgresql.host = ${STORAGE_TYPE_POSTGRESQL_HOST} storage.type.postgresql.username = ${STORAGE_TYPE_POSTGRESQL_USERNAME} diff --git a/src/AP_WS_Process_connect.cpp b/src/AP_WS_Process_connect.cpp index d10f707c1..002d1161d 100644 --- a/src/AP_WS_Process_connect.cpp +++ b/src/AP_WS_Process_connect.cpp @@ -99,7 +99,7 @@ namespace OpenWifi { Restrictions_.from_json(RestrictionObject); } - if (Capabilities->has("developer")) { + if (Capabilities->has("developer") && !Capabilities->isNull("developer")) { Restrictions_.developer = Capabilities->getValue("developer"); } From 0e763cf03449a682dae58f89ba3cb72e105b18c5 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 13 Dec 2023 07:12:58 -0800 Subject: [PATCH 109/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Process_state.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/AP_WS_Process_state.cpp b/src/AP_WS_Process_state.cpp index ffacb0ed6..ab26c3e79 100644 --- a/src/AP_WS_Process_state.cpp +++ b/src/AP_WS_Process_state.cpp @@ -40,10 +40,13 @@ namespace OpenWifi { UUID, request_uuid)); } + std::cout << SerialNumber_ << __LINE__ << std::endl; std::lock_guard Guard(*DbSession_->Mutex); if(!Simulated_) { + std::cout << SerialNumber_ << __LINE__ << std::endl; uint64_t UpgradedUUID; LookForUpgrade(*DbSession_->Session, UUID, UpgradedUUID); + std::cout << SerialNumber_ << __LINE__ << std::endl; State_.UUID = UpgradedUUID; } @@ -52,7 +55,9 @@ namespace OpenWifi { GWObjects::Statistics Stats{ .SerialNumber = SerialNumber_, .UUID = UUID, .Data = StateStr}; Stats.Recorded = Utils::Now(); + std::cout << SerialNumber_ << __LINE__ << std::endl; StorageService()->AddStatisticsData(*DbSession_->Session,Stats); + std::cout << SerialNumber_ << __LINE__ << std::endl; if (!request_uuid.empty()) { StorageService()->SetCommandResult(request_uuid, StateStr); } @@ -61,7 +66,9 @@ namespace OpenWifi { State_.Associations_5G, State_.Associations_6G); if (KafkaManager()->Enabled() && !AP_WS_Server()->KafkaDisableState()) { + std::cout << SerialNumber_ << __LINE__ << std::endl; KafkaManager()->PostMessage(KafkaTopics::STATE, SerialNumber_, *ParamsObj); + std::cout << SerialNumber_ << __LINE__ << std::endl; } GWWebSocketNotifications::SingleDevice_t N; From c66b936c1e6629e2abe7aae93419010591d4a8cc Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 13 Dec 2023 07:16:09 -0800 Subject: [PATCH 110/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Process_state.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/AP_WS_Process_state.cpp b/src/AP_WS_Process_state.cpp index ab26c3e79..6ac45b3b2 100644 --- a/src/AP_WS_Process_state.cpp +++ b/src/AP_WS_Process_state.cpp @@ -40,13 +40,13 @@ namespace OpenWifi { UUID, request_uuid)); } - std::cout << SerialNumber_ << __LINE__ << std::endl; + std::cout << SerialNumber_ << ": " << __LINE__ << std::endl; std::lock_guard Guard(*DbSession_->Mutex); if(!Simulated_) { - std::cout << SerialNumber_ << __LINE__ << std::endl; + std::cout << SerialNumber_ << ": " << __LINE__ << std::endl; uint64_t UpgradedUUID; LookForUpgrade(*DbSession_->Session, UUID, UpgradedUUID); - std::cout << SerialNumber_ << __LINE__ << std::endl; + std::cout << SerialNumber_ << ": " << __LINE__ << std::endl; State_.UUID = UpgradedUUID; } @@ -55,9 +55,9 @@ namespace OpenWifi { GWObjects::Statistics Stats{ .SerialNumber = SerialNumber_, .UUID = UUID, .Data = StateStr}; Stats.Recorded = Utils::Now(); - std::cout << SerialNumber_ << __LINE__ << std::endl; + std::cout << SerialNumber_ << ": " << __LINE__ << std::endl; StorageService()->AddStatisticsData(*DbSession_->Session,Stats); - std::cout << SerialNumber_ << __LINE__ << std::endl; + std::cout << SerialNumber_ << ": " << __LINE__ << std::endl; if (!request_uuid.empty()) { StorageService()->SetCommandResult(request_uuid, StateStr); } @@ -66,9 +66,9 @@ namespace OpenWifi { State_.Associations_5G, State_.Associations_6G); if (KafkaManager()->Enabled() && !AP_WS_Server()->KafkaDisableState()) { - std::cout << SerialNumber_ << __LINE__ << std::endl; + std::cout << SerialNumber_ << ": " << __LINE__ << std::endl; KafkaManager()->PostMessage(KafkaTopics::STATE, SerialNumber_, *ParamsObj); - std::cout << SerialNumber_ << __LINE__ << std::endl; + std::cout << SerialNumber_ << ": " << __LINE__ << std::endl; } GWWebSocketNotifications::SingleDevice_t N; From a5fe59086ab828b20be914f7f1a7c5ce54256178 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 13 Dec 2023 07:17:22 -0800 Subject: [PATCH 111/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index 6c8bde4c2..cb8a09c39 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -297,6 +297,7 @@ namespace OpenWifi { bool AP_WS_Connection::LookForUpgrade(Poco::Data::Session &Session, const uint64_t UUID, uint64_t &UpgradedUUID) { // A UUID of zero means ignore updates for that connection. + std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; if (UUID == 0) return false; From 3dda87b41faa1e3b16541f7520929125879c98bb Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 13 Dec 2023 07:19:25 -0800 Subject: [PATCH 112/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index cb8a09c39..c44d59e65 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -304,6 +304,7 @@ namespace OpenWifi { uint64_t GoodConfig = ConfigurationCache().CurrentConfig(SerialNumberInt_); if (GoodConfig && (GoodConfig == UUID || GoodConfig == State_.PendingUUID)) { UpgradedUUID = UUID; + std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; return false; } @@ -314,6 +315,7 @@ namespace OpenWifi { // so we sent an upgrade to a device, and now it is completing now... UpgradedUUID = D.pendingUUID; StorageService()->CompleteDeviceConfigurationChange(Session, SerialNumber_); + std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; return true; } @@ -322,6 +324,7 @@ namespace OpenWifi { if (D.UUID == UUID) { UpgradedUUID = UUID; ConfigurationCache().Add(SerialNumberInt_, UUID); + std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; return false; } @@ -358,12 +361,14 @@ namespace OpenWifi { fmt::format("CFG-UPGRADE({}): Current ID: {}, newer configuration {}.", CId_, UUID, D.UUID)); bool Sent; + std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; StorageService()->AddCommand(SerialNumber_, Cmd, Storage::CommandExecutionType::COMMAND_EXECUTED); CommandManager()->PostCommand( CommandManager()->Next_RPC_ID(), APCommands::to_apcommand(Cmd.Command.c_str()), SerialNumber_, Cmd.Command, Params, Cmd.UUID, Sent, false, false); + std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; GWWebSocketNotifications::SingleDeviceConfigurationChange_t Notification; Notification.content.serialNumber = D.SerialNumber; From 58fc925a2f32159fb57fbce4bcd5fca6e4d1ab3c Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 13 Dec 2023 21:21:20 -0800 Subject: [PATCH 113/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- CMakeLists.txt | 2 +- build | 2 +- src/AP_WS_Connection.cpp | 89 +--------------------------- src/AP_WS_Connection.h | 6 +- src/AP_WS_Locked_Session.h | 19 ------ src/AP_WS_LookForUpgrade.cpp | 90 +++++++++++++++++++++++++++++ src/AP_WS_Process_connect.cpp | 12 ++-- src/AP_WS_Process_state.cpp | 13 +---- src/AP_WS_Reactor_Pool.h | 15 ++--- src/AP_WS_Server.h | 2 +- src/StorageService.h | 26 ++++++++- src/storage/storage_device.cpp | 24 ++++---- src/storage/storage_healthcheck.cpp | 8 +-- src/storage/storage_logs.cpp | 8 +-- 14 files changed, 157 insertions(+), 159 deletions(-) delete mode 100644 src/AP_WS_Locked_Session.h create mode 100644 src/AP_WS_LookForUpgrade.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 70cc7fcf2..ebfd1ad61 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -212,7 +212,7 @@ add_executable( owgw src/RADIUSSessionTracker.cpp src/RADIUSSessionTracker.h src/libs/Scheduler.h src/libs/InterruptableSleep.h src/libs/ctpl_stl.h src/libs/Cron.h src/GenericScheduler.cpp src/GenericScheduler.h src/framework/default_device_types.h src/AP_WS_Process_rebootLog.cpp src/AP_WS_ConfigAutoUpgrader.cpp src/AP_WS_ConfigAutoUpgrader.h src/RESTAPI/RESTAPI_default_firmwares.cpp src/RESTAPI/RESTAPI_default_firmwares.h src/RESTAPI/RESTAPI_default_firmware.cpp src/RESTAPI/RESTAPI_default_firmware.h src/storage/storage_def_firmware.cpp src/firmware_revision_cache.h src/sdks/sdk_fms.h - src/AP_WS_Locked_Session.h) + src/AP_WS_LookForUpgrade.cpp) if(NOT SMALL_BUILD) diff --git a/build b/build index 2b82dfea3..b2412e34d 100644 --- a/build +++ b/build @@ -1 +1 @@ -60 \ No newline at end of file +62 \ No newline at end of file diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index c44d59e65..ba2a4be91 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -43,7 +43,7 @@ namespace OpenWifi { AP_WS_Connection::AP_WS_Connection(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response, uint64_t session_id, Poco::Logger &L, - std::pair R) + std::pair, std::shared_ptr> R) : Logger_(L) { Reactor_ = R.first; @@ -294,93 +294,6 @@ namespace OpenWifi { GWWebSocketNotifications::DeviceDisconnected(N); } - bool AP_WS_Connection::LookForUpgrade(Poco::Data::Session &Session, const uint64_t UUID, uint64_t &UpgradedUUID) { - - // A UUID of zero means ignore updates for that connection. - std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; - if (UUID == 0) - return false; - - uint64_t GoodConfig = ConfigurationCache().CurrentConfig(SerialNumberInt_); - if (GoodConfig && (GoodConfig == UUID || GoodConfig == State_.PendingUUID)) { - UpgradedUUID = UUID; - std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; - return false; - } - - GWObjects::Device D; - if (StorageService()->GetDevice(Session,SerialNumber_, D)) { - - if(D.pendingUUID!=0 && UUID==D.pendingUUID) { - // so we sent an upgrade to a device, and now it is completing now... - UpgradedUUID = D.pendingUUID; - StorageService()->CompleteDeviceConfigurationChange(Session, SerialNumber_); - std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; - return true; - } - - // This is the case where the cache is empty after a restart. So GoodConfig will 0. If - // the device already has the right UUID, we just return. - if (D.UUID == UUID) { - UpgradedUUID = UUID; - ConfigurationCache().Add(SerialNumberInt_, UUID); - std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; - return false; - } - - Config::Config Cfg(D.Configuration); - if (UUID > D.UUID) { - // so we have a problem, the device has a newer config than we have. So we need to - // make sure our config is newer. - D.UUID = UUID + 2; - UpgradedUUID = D.UUID; - } - - Cfg.SetUUID(D.UUID); - D.Configuration = Cfg.get(); - State_.PendingUUID = UpgradedUUID = D.UUID; - - GWObjects::CommandDetails Cmd; - Cmd.SerialNumber = SerialNumber_; - Cmd.UUID = MicroServiceCreateUUID(); - Cmd.SubmittedBy = uCentralProtocol::SUBMITTED_BY_SYSTEM; - Cmd.Status = uCentralProtocol::PENDING; - Cmd.Command = uCentralProtocol::CONFIGURE; - Poco::JSON::Parser P; - auto ParsedConfig = P.parse(D.Configuration).extract(); - Poco::JSON::Object Params; - Params.set(uCentralProtocol::SERIAL, SerialNumber_); - Params.set(uCentralProtocol::UUID, D.UUID); - Params.set(uCentralProtocol::WHEN, 0); - Params.set(uCentralProtocol::CONFIG, ParsedConfig); - - std::ostringstream O; - Poco::JSON::Stringifier::stringify(Params, O); - Cmd.Details = O.str(); - poco_information(Logger_, - fmt::format("CFG-UPGRADE({}): Current ID: {}, newer configuration {}.", - CId_, UUID, D.UUID)); - bool Sent; - std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; - - StorageService()->AddCommand(SerialNumber_, Cmd, - Storage::CommandExecutionType::COMMAND_EXECUTED); - CommandManager()->PostCommand( - CommandManager()->Next_RPC_ID(), APCommands::to_apcommand(Cmd.Command.c_str()), - SerialNumber_, Cmd.Command, Params, Cmd.UUID, Sent, false, false); - std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; - - GWWebSocketNotifications::SingleDeviceConfigurationChange_t Notification; - Notification.content.serialNumber = D.SerialNumber; - Notification.content.oldUUID = UUID; - Notification.content.newUUID = UpgradedUUID; - GWWebSocketNotifications::DeviceConfigurationChange(Notification); - - return true; - } - return false; - } - void AP_WS_Connection::ProcessJSONRPCResult(Poco::JSON::Object::Ptr Doc) { poco_trace(Logger_, fmt::format("RECEIVED-RPC({}): {}.", CId_, Doc->get(uCentralProtocol::ID).toString())); diff --git a/src/AP_WS_Connection.h b/src/AP_WS_Connection.h index f4ac0b9fa..be6d37eec 100644 --- a/src/AP_WS_Connection.h +++ b/src/AP_WS_Connection.h @@ -27,7 +27,7 @@ namespace OpenWifi { public: explicit AP_WS_Connection(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response, uint64_t connection_id, - Poco::Logger &L, std::pair R); + Poco::Logger &L, std::pair, std::shared_ptr> R); ~AP_WS_Connection(); void EndConnection(bool Clean = true); @@ -115,8 +115,8 @@ namespace OpenWifi { std::recursive_mutex ConnectionMutex_; std::mutex TelemetryMutex_; Poco::Logger &Logger_; - Poco::Net::SocketReactor *Reactor_{nullptr}; - LockedDbSession *DbSession_{nullptr}; + std::shared_ptr Reactor_; + std::shared_ptr DbSession_; std::unique_ptr WS_; std::string SerialNumber_; uint64_t SerialNumberInt_ = 0; diff --git a/src/AP_WS_Locked_Session.h b/src/AP_WS_Locked_Session.h deleted file mode 100644 index 245f30a2c..000000000 --- a/src/AP_WS_Locked_Session.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by stephane bourque on 2023-11-28. -// - -#pragma once - -#include -#include - -namespace OpenWifi { - - struct LockedDbSession { - std::unique_ptr Session; - std::unique_ptr Mutex; - - inline Poco::Data::Session & operator *() { return *Session; } - }; - -} \ No newline at end of file diff --git a/src/AP_WS_LookForUpgrade.cpp b/src/AP_WS_LookForUpgrade.cpp new file mode 100644 index 000000000..2e41e93c7 --- /dev/null +++ b/src/AP_WS_LookForUpgrade.cpp @@ -0,0 +1,90 @@ + +#include +#include "ConfigurationCache.h" +#include "UI_GW_WebSocketNotifications.h" +#include "CommandManager.h" + +namespace OpenWifi { + bool AP_WS_Connection::LookForUpgrade(Poco::Data::Session &Session, const uint64_t UUID, uint64_t &UpgradedUUID) { + + // A UUID of zero means ignore updates for that connection. + if (UUID == 0) + return false; + + uint64_t GoodConfig = ConfigurationCache().CurrentConfig(SerialNumberInt_); + if (GoodConfig && (GoodConfig == UUID || GoodConfig == State_.PendingUUID)) { + UpgradedUUID = UUID; + return false; + } + + GWObjects::Device D; + if (StorageService()->GetDevice(Session,SerialNumber_, D)) { + if(D.pendingUUID!=0 && UUID==D.pendingUUID) { + // so we sent an upgrade to a device, and now it is completing now... + UpgradedUUID = D.pendingUUID; + StorageService()->CompleteDeviceConfigurationChange(Session, SerialNumber_); + return true; + } + + // This is the case where the cache is empty after a restart. So GoodConfig will 0. If + // the device already has the right UUID, we just return. + if (D.UUID == UUID) { + UpgradedUUID = UUID; + ConfigurationCache().Add(SerialNumberInt_, UUID); + return false; + } + + Config::Config Cfg(D.Configuration); + if (UUID > D.UUID) { + // so we have a problem, the device has a newer config than we have. So we need to + // make sure our config is newer. + D.UUID = UUID + 2; + UpgradedUUID = D.UUID; + } + + Cfg.SetUUID(D.UUID); + D.Configuration = Cfg.get(); + State_.PendingUUID = UpgradedUUID = D.UUID; + + GWObjects::CommandDetails Cmd; + Cmd.SerialNumber = SerialNumber_; + Cmd.UUID = MicroServiceCreateUUID(); + Cmd.SubmittedBy = uCentralProtocol::SUBMITTED_BY_SYSTEM; + Cmd.Status = uCentralProtocol::PENDING; + Cmd.Command = uCentralProtocol::CONFIGURE; + Poco::JSON::Parser P; + auto ParsedConfig = P.parse(D.Configuration).extract(); + Poco::JSON::Object Params; + Params.set(uCentralProtocol::SERIAL, SerialNumber_); + Params.set(uCentralProtocol::UUID, D.UUID); + Params.set(uCentralProtocol::WHEN, 0); + Params.set(uCentralProtocol::CONFIG, ParsedConfig); + + std::ostringstream O; + Poco::JSON::Stringifier::stringify(Params, O); + Cmd.Details = O.str(); + poco_information(Logger_, + fmt::format("CFG-UPGRADE({}): Current ID: {}, newer configuration {}.", + CId_, UUID, D.UUID)); + bool Sent; + std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; + + StorageService()->AddCommand(SerialNumber_, Cmd, + Storage::CommandExecutionType::COMMAND_EXECUTED); + CommandManager()->PostCommand( + CommandManager()->Next_RPC_ID(), APCommands::to_apcommand(Cmd.Command.c_str()), + SerialNumber_, Cmd.Command, Params, Cmd.UUID, Sent, false, false); + std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; + + GWWebSocketNotifications::SingleDeviceConfigurationChange_t Notification; + Notification.content.serialNumber = D.SerialNumber; + Notification.content.oldUUID = UUID; + Notification.content.newUUID = UpgradedUUID; + GWWebSocketNotifications::DeviceConfigurationChange(Notification); + + return true; + } + return false; + } + +} \ No newline at end of file diff --git a/src/AP_WS_Process_connect.cpp b/src/AP_WS_Process_connect.cpp index 002d1161d..7e7a61bde 100644 --- a/src/AP_WS_Process_connect.cpp +++ b/src/AP_WS_Process_connect.cpp @@ -109,9 +109,9 @@ namespace OpenWifi { State_.locale = FindCountryFromIP()->Get(IP); GWObjects::Device DeviceInfo; - std::lock_guard DbSessionLock(*DbSession_->Mutex); + std::lock_guard DbSessionLock(DbSession_->Mutex()); - auto DeviceExists = StorageService()->GetDevice(*DbSession_->Session, SerialNumber_, DeviceInfo); + auto DeviceExists = StorageService()->GetDevice(DbSession_->Session(), SerialNumber_, DeviceInfo); if (Daemon()->AutoProvisioning() && !DeviceExists) { // check the firmware version. if this is too old, we cannot let that device connect yet, we must // force a firmware upgrade @@ -144,7 +144,7 @@ namespace OpenWifi { } return; } else { - StorageService()->CreateDefaultDevice( *DbSession_->Session, + StorageService()->CreateDefaultDevice( DbSession_->Session(), SerialNumber_, Caps, Firmware, PeerAddress_, State_.VerifiedCertificate == GWObjects::SIMULATED); } @@ -153,7 +153,7 @@ namespace OpenWifi { poco_warning(Logger(),fmt::format("Device {} is a {} from {} and cannot be provisioned.",SerialNumber_,Compatible_, CId_)); return EndConnection(); } else if (DeviceExists) { - StorageService()->UpdateDeviceCapabilities(*DbSession_->Session, SerialNumber_, Caps); + StorageService()->UpdateDeviceCapabilities(DbSession_->Session(), SerialNumber_, Caps); int Updated{0}; if (!Firmware.empty()) { if (Firmware != DeviceInfo.Firmware) { @@ -225,12 +225,12 @@ namespace OpenWifi { } if (Updated) { - StorageService()->UpdateDevice(*DbSession_->Session, DeviceInfo); + StorageService()->UpdateDevice(DbSession_->Session(), DeviceInfo); } if(!Simulated_) { uint64_t UpgradedUUID = 0; - LookForUpgrade(*DbSession_->Session, UUID, UpgradedUUID); + LookForUpgrade(DbSession_->Session(), UUID, UpgradedUUID); State_.UUID = UpgradedUUID; } } diff --git a/src/AP_WS_Process_state.cpp b/src/AP_WS_Process_state.cpp index 6ac45b3b2..8abb1e2b1 100644 --- a/src/AP_WS_Process_state.cpp +++ b/src/AP_WS_Process_state.cpp @@ -40,13 +40,10 @@ namespace OpenWifi { UUID, request_uuid)); } - std::cout << SerialNumber_ << ": " << __LINE__ << std::endl; - std::lock_guard Guard(*DbSession_->Mutex); + std::lock_guard Guard(DbSession_->Mutex()); if(!Simulated_) { - std::cout << SerialNumber_ << ": " << __LINE__ << std::endl; uint64_t UpgradedUUID; - LookForUpgrade(*DbSession_->Session, UUID, UpgradedUUID); - std::cout << SerialNumber_ << ": " << __LINE__ << std::endl; + LookForUpgrade(DbSession_->Session(), UUID, UpgradedUUID); State_.UUID = UpgradedUUID; } @@ -55,9 +52,7 @@ namespace OpenWifi { GWObjects::Statistics Stats{ .SerialNumber = SerialNumber_, .UUID = UUID, .Data = StateStr}; Stats.Recorded = Utils::Now(); - std::cout << SerialNumber_ << ": " << __LINE__ << std::endl; - StorageService()->AddStatisticsData(*DbSession_->Session,Stats); - std::cout << SerialNumber_ << ": " << __LINE__ << std::endl; + StorageService()->AddStatisticsData(DbSession_->Session(),Stats); if (!request_uuid.empty()) { StorageService()->SetCommandResult(request_uuid, StateStr); } @@ -66,9 +61,7 @@ namespace OpenWifi { State_.Associations_5G, State_.Associations_6G); if (KafkaManager()->Enabled() && !AP_WS_Server()->KafkaDisableState()) { - std::cout << SerialNumber_ << ": " << __LINE__ << std::endl; KafkaManager()->PostMessage(KafkaTopics::STATE, SerialNumber_, *ParamsObj); - std::cout << SerialNumber_ << ": " << __LINE__ << std::endl; } GWWebSocketNotifications::SingleDevice_t N; diff --git a/src/AP_WS_Reactor_Pool.h b/src/AP_WS_Reactor_Pool.h index adae16d43..654810c4f 100644 --- a/src/AP_WS_Reactor_Pool.h +++ b/src/AP_WS_Reactor_Pool.h @@ -12,7 +12,6 @@ #include #include "framework/utils.h" #include -#include namespace OpenWifi { @@ -33,16 +32,14 @@ namespace OpenWifi { Threads_.reserve(NumberOfThreads_); Logger_.information(fmt::format("WebSocket Processor: starting {} threads.", NumberOfThreads_)); for (uint64_t i = 0; i < NumberOfThreads_; ++i) { - auto NewReactor = std::make_unique(); + auto NewReactor = std::make_shared(); auto NewThread = std::make_unique(); NewThread->start(*NewReactor); std::string ThreadName{"ap:react:" + std::to_string(i)}; Utils::SetThreadName(*NewThread, ThreadName.c_str()); Reactors_.emplace_back(std::move(NewReactor)); Threads_.emplace_back(std::move(NewThread)); - LockedDbSession S{ .Session = std::make_unique(Poco::Data::Session(StorageService()->Pool().get())), - .Mutex = std::make_unique() }; - DbSessions_.emplace_back(std::move(S)); + DbSessions_.emplace_back(std::make_shared()); } Logger_.information(fmt::format("WebSocket Processor: {} threads started.", NumberOfThreads_)); } @@ -58,20 +55,20 @@ namespace OpenWifi { DbSessions_.clear(); } - std::pair NextReactor() { + std::pair, std::shared_ptr > NextReactor() { std::lock_guard Lock(Mutex_); NextReactor_++; NextReactor_ %= NumberOfThreads_; - return std::make_pair(Reactors_[NextReactor_].get(), &DbSessions_[NextReactor_]); + return std::make_pair(Reactors_[NextReactor_], DbSessions_[NextReactor_]); } private: std::mutex Mutex_; uint64_t NumberOfThreads_; uint64_t NextReactor_ = 0; - std::vector> Reactors_; + std::vector> Reactors_; std::vector> Threads_; - std::vector DbSessions_; + std::vector> DbSessions_; Poco::Logger &Logger_; }; diff --git a/src/AP_WS_Server.h b/src/AP_WS_Server.h index 2a5f0f3a5..4391451b3 100644 --- a/src/AP_WS_Server.h +++ b/src/AP_WS_Server.h @@ -97,7 +97,7 @@ namespace OpenWifi { [[nodiscard]] inline bool UseProvisioning() const { return LookAtProvisioning_; } [[nodiscard]] inline bool UseDefaults() const { return UseDefaultConfig_; } [[nodiscard]] inline bool Running() const { return Running_; } - [[nodiscard]] inline std::pair NextReactor() { + [[nodiscard]] inline std::pair, std::shared_ptr> NextReactor() { return Reactor_pool_->NextReactor(); } diff --git a/src/StorageService.h b/src/StorageService.h index 3136616a8..cd03df24d 100644 --- a/src/StorageService.h +++ b/src/StorageService.h @@ -13,10 +13,25 @@ #include "RESTObjects//RESTAPI_GWobjects.h" #include "framework/StorageClass.h" #include "storage/storage_scripts.h" -#include namespace OpenWifi { + class LockedDbSession { + public: + explicit LockedDbSession(); + ~LockedDbSession() = default; + inline std::mutex &Mutex() { return *Mutex_; }; + inline Poco::Data::Session &Session() { + if(!Session_->isConnected()) { + Session_->reconnect(); + } + return *Session_; + }; + private: + std::shared_ptr Session_; + std::shared_ptr Mutex_; + }; + class Storage : public StorageClass { public: @@ -279,10 +294,19 @@ namespace OpenWifi { int Start() override; void Stop() override; + inline Poco::Data::Session StartSession() { + return Pool_->get(); + } + private: std::unique_ptr ScriptDB_; }; inline auto StorageService() { return Storage::instance(); } + inline LockedDbSession::LockedDbSession() { + Session_ = std::make_shared(Poco::Data::Session(StorageService()->StartSession())); + Mutex_ = std::make_shared(); + } + } // namespace OpenWifi diff --git a/src/storage/storage_device.cpp b/src/storage/storage_device.cpp index f0a7aa9c7..97882cca0 100644 --- a/src/storage/storage_device.cpp +++ b/src/storage/storage_device.cpp @@ -383,8 +383,8 @@ namespace OpenWifi { bool Storage::SetDeviceLastRecordedContact(LockedDbSession &Session, std::string &SerialNumber, std::uint64_t lastRecordedContact) { try { - std::lock_guard Lock(*Session.Mutex); - return SetDeviceLastRecordedContact(*Session.Session, SerialNumber, lastRecordedContact); + std::lock_guard Lock(Session.Mutex()); + return SetDeviceLastRecordedContact(Session.Session(), SerialNumber, lastRecordedContact); } catch (const Poco::Exception &E) { Logger().log(E); } @@ -458,8 +458,8 @@ namespace OpenWifi { bool Storage::CreateDevice(LockedDbSession &Session, GWObjects::Device &DeviceDetails) { try { - std::lock_guard Lock(*Session.Mutex); - return CreateDevice(*Session.Session, DeviceDetails); + std::lock_guard Lock(Session.Mutex()); + return CreateDevice(Session.Session(), DeviceDetails); } catch (const Poco::Exception &E) { Logger().log(E); } @@ -603,16 +603,16 @@ namespace OpenWifi { */ bool Storage::SetDevicePassword(LockedDbSession &Sess, std::string &SerialNumber, std::string &Password) { try { - std::lock_guard Lock(*Sess.Mutex); - Sess.Session->begin(); + std::lock_guard Lock(Sess.Mutex()); + Sess.Session().begin(); - Poco::Data::Statement Update(*Sess.Session); + Poco::Data::Statement Update(Sess.Session()); std::string St{"UPDATE Devices SET DevicePassword=? WHERE SerialNumber=?"}; Update << ConvertParams(St), Poco::Data::Keywords::use(Password), Poco::Data::Keywords::use(SerialNumber); Update.execute(); - Sess.Session->commit(); + Sess.Session().commit(); return true; } catch (const Poco::Exception &E) { Logger().log(E); @@ -741,8 +741,8 @@ namespace OpenWifi { bool Storage::GetDevice(LockedDbSession &Session, std::string &SerialNumber, GWObjects::Device &DeviceDetails) { try { - std::lock_guard Lock(*Session.Mutex); - return GetDevice(*Session.Session, SerialNumber, DeviceDetails); + std::lock_guard Lock(Session.Mutex()); + return GetDevice(Session.Session(), SerialNumber, DeviceDetails); } catch (const Poco::Exception &E) { Logger().log(E); } @@ -784,8 +784,8 @@ namespace OpenWifi { bool Storage::UpdateDevice(LockedDbSession &Session, GWObjects::Device &NewDeviceDetails) { try { - std::lock_guard Lock(*Session.Mutex); - return UpdateDevice(*Session.Session, NewDeviceDetails); + std::lock_guard Lock(Session.Mutex()); + return UpdateDevice(Session.Session(), NewDeviceDetails); } catch (const Poco::Exception &E) { Logger().log(E); } diff --git a/src/storage/storage_healthcheck.cpp b/src/storage/storage_healthcheck.cpp index ca487d5a7..2d77f844b 100644 --- a/src/storage/storage_healthcheck.cpp +++ b/src/storage/storage_healthcheck.cpp @@ -37,9 +37,9 @@ namespace OpenWifi { bool Storage::AddHealthCheckData(LockedDbSession &Session, const GWObjects::HealthCheck &Check) { try { - std::lock_guard Guard(*Session.Mutex); - Session.Session->begin(); - Poco::Data::Statement Insert(*Session.Session); + std::lock_guard Guard(Session.Mutex()); + Session.Session().begin(); + Poco::Data::Statement Insert(Session.Session()); std::string St{"INSERT INTO HealthChecks ( " + DB_HealthCheckSelectFields + " ) VALUES( " + DB_HealthCheckInsertValues + " )"}; @@ -48,7 +48,7 @@ namespace OpenWifi { ConvertHealthCheckRecord(Check, R); Insert << ConvertParams(St), Poco::Data::Keywords::use(R); Insert.execute(); - Session.Session->commit(); + Session.Session().commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), diff --git a/src/storage/storage_logs.cpp b/src/storage/storage_logs.cpp index 7046c576c..ae998b2a0 100644 --- a/src/storage/storage_logs.cpp +++ b/src/storage/storage_logs.cpp @@ -41,9 +41,9 @@ namespace OpenWifi { bool Storage::AddLog(LockedDbSession &Session, const GWObjects::DeviceLog &Log) { try { - std::lock_guard Guard(*Session.Mutex); - Session.Session->begin(); - Poco::Data::Statement Insert(*Session.Session); + std::lock_guard Guard(Session.Mutex()); + Session.Session().begin(); + Poco::Data::Statement Insert(Session.Session()); std::string St{"INSERT INTO DeviceLogs (" + DB_LogsSelectFields + ") values( " + DB_LogsInsertValues + " )"}; @@ -53,7 +53,7 @@ namespace OpenWifi { Insert << ConvertParams(St), Poco::Data::Keywords::use(R); Insert.execute(); - Session.Session->commit(); + Session.Session().commit(); return true; } catch (const Poco::Exception &E) { poco_warning(Logger(), fmt::format("{}: Failed with: {}", std::string(__func__), From e1bf6a9ab7947153bc473f055a7f661950f7c484 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 14 Dec 2023 07:23:48 -0800 Subject: [PATCH 114/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- src/AP_WS_Connection.cpp | 5 ++--- src/AP_WS_LookForUpgrade.cpp | 2 -- src/AP_WS_Process_telemetry.cpp | 4 +--- src/CommandManager.cpp | 15 --------------- src/Dashboard.cpp | 2 -- src/ParseWifiScan.h | 12 ------------ src/RADIUSSessionTracker.cpp | 9 --------- 7 files changed, 3 insertions(+), 46 deletions(-) diff --git a/src/AP_WS_Connection.cpp b/src/AP_WS_Connection.cpp index ba2a4be91..3c33094ec 100644 --- a/src/AP_WS_Connection.cpp +++ b/src/AP_WS_Connection.cpp @@ -687,11 +687,10 @@ namespace OpenWifi { } else { std::ostringstream iS; IncomingJSON->stringify(iS); - std::cout << iS.str() << std::endl; poco_warning( Logger_, - fmt::format("FRAME({}): illegal transaction header, missing 'jsonrpc'", - CId_)); + fmt::format("FRAME({}): illegal transaction header, missing 'jsonrpc': {}", + CId_, iS.str())); Errors_++; } } break; diff --git a/src/AP_WS_LookForUpgrade.cpp b/src/AP_WS_LookForUpgrade.cpp index 2e41e93c7..bb5811096 100644 --- a/src/AP_WS_LookForUpgrade.cpp +++ b/src/AP_WS_LookForUpgrade.cpp @@ -67,14 +67,12 @@ namespace OpenWifi { fmt::format("CFG-UPGRADE({}): Current ID: {}, newer configuration {}.", CId_, UUID, D.UUID)); bool Sent; - std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; StorageService()->AddCommand(SerialNumber_, Cmd, Storage::CommandExecutionType::COMMAND_EXECUTED); CommandManager()->PostCommand( CommandManager()->Next_RPC_ID(), APCommands::to_apcommand(Cmd.Command.c_str()), SerialNumber_, Cmd.Command, Params, Cmd.UUID, Sent, false, false); - std::cout << "CFG: " << SerialNumber_ << ": " << __LINE__ << std::endl; GWWebSocketNotifications::SingleDeviceConfigurationChange_t Notification; Notification.content.serialNumber = D.SerialNumber; diff --git a/src/AP_WS_Process_telemetry.cpp b/src/AP_WS_Process_telemetry.cpp index d5827237a..9ea35cb7a 100644 --- a/src/AP_WS_Process_telemetry.cpp +++ b/src/AP_WS_Process_telemetry.cpp @@ -35,8 +35,7 @@ namespace OpenWifi { } if (TelemetryWebSocketRefCount_) { if (now < TelemetryWebSocketTimer_) { - // std::cout << SerialNumber_ << ": Updating WebSocket telemetry" << - // std::endl; + TelemetryWebSocketPackets_++; State_.websocketPackets = TelemetryWebSocketPackets_; TelemetryStream()->NotifyEndPoint(SerialNumberInt_, KafkaPayload); @@ -46,7 +45,6 @@ namespace OpenWifi { } if (TelemetryKafkaRefCount_) { if (KafkaManager()->Enabled() && now < TelemetryKafkaTimer_) { - // std::cout << SerialNumber_ << ": Updating Kafka telemetry" << std::endl; TelemetryKafkaPackets_++; State_.kafkaPackets = TelemetryKafkaPackets_; KafkaManager()->PostMessage(KafkaTopics::DEVICE_TELEMETRY, SerialNumber_, diff --git a/src/CommandManager.cpp b/src/CommandManager.cpp index c9ee370a8..82ccd3329 100644 --- a/src/CommandManager.cpp +++ b/src/CommandManager.cpp @@ -45,11 +45,9 @@ namespace OpenWifi { std::lock_guard Lock(LocalMutex_); auto RPC = OutStandingRequests_.find(ID); if (RPC == OutStandingRequests_.end()) { - // std::cout << __LINE__ << std::endl; poco_debug(Logger(), fmt::format("({}): RPC {} cannot be found.", SerialNumberStr, ID)); } else if (RPC->second.SerialNumber != Resp->SerialNumber_) { - // std::cout << __LINE__ << std::endl; poco_debug( Logger(), fmt::format("({}): RPC {} serial number mismatch {}!={}.", @@ -60,7 +58,6 @@ namespace OpenWifi { std::chrono::duration rpc_execution_time = std::chrono::high_resolution_clock::now() - RPC->second.submitted; - // std::cout << __LINE__ << std::endl; poco_debug(Logger(), fmt::format("({}): Received RPC answer {}. Command={}", SerialNumberStr, ID, @@ -140,7 +137,6 @@ namespace OpenWifi { } } } else { - // std::cout << __LINE__ << std::endl; } Command.State = 0; @@ -163,7 +159,6 @@ namespace OpenWifi { if (Command.rpc_entry) { TmpRpcEntry = Command.rpc_entry; } - // std::cout << __LINE__ << " State=" << Command.State << std::endl; if (Command.State == 2) { // look at the payload to see if we should continue or not... if (Payload->has("result")) { @@ -173,12 +168,10 @@ namespace OpenWifi { std::uint64_t Error = Status->get("error"); if (Error == 0) { - // std::cout << __LINE__ << std::endl; StorageService()->CommandCompleted(Command.UUID, Payload, rpc_execution_time, true); Command.State = 1; } else { - // std::cout << __LINE__ << std::endl; StorageService()->CommandCompleted(Command.UUID, Payload, rpc_execution_time, true); std::string ErrorTxt = Status->get("result"); @@ -186,14 +179,11 @@ namespace OpenWifi { Command.State = 0; } } else { - // std::cout << __LINE__ << std::endl; } } else { - // std::cout << __LINE__ << std::endl; Command.State = 0; } } else if (Command.State == 1) { - // std::cout << "Completing script 2 phase commit." << std::endl; StorageService()->CommandCompleted(Command.UUID, Payload, rpc_execution_time, true); if (Command.Deferred) { Reply = false; @@ -202,7 +192,6 @@ namespace OpenWifi { } if (Command.State == 0) { - // std::cout << __LINE__ << " State=" << Command.State << std::endl; OutStandingRequests_.erase(Command.Id); } if (Reply && TmpRpcEntry != nullptr) @@ -262,8 +251,6 @@ namespace OpenWifi { for (auto request = OutStandingRequests_.begin(); request != OutStandingRequests_.end();) { std::chrono::duration delta = now - request->second.submitted; if (delta > 10min) { - // std::cout << __LINE__ << " -->> " << request->second.Id << - // std::endl; MyLogger.debug(fmt::format("{}: Command={} for {} Timed out.", request->second.UUID, APCommands::to_string(request->second.Command), Utils::IntToSerialNumber(request->second.SerialNumber))); @@ -275,8 +262,6 @@ namespace OpenWifi { StorageService()->SetCommandTimedOut(request->second.UUID); request = OutStandingRequests_.erase(request); } else { - // std::cout << __LINE__ << " -->> " << request->second.Id << - // std::endl; ++request; } } diff --git a/src/Dashboard.cpp b/src/Dashboard.cpp index 43cff93e5..ff76b41fd 100644 --- a/src/Dashboard.cpp +++ b/src/Dashboard.cpp @@ -21,7 +21,6 @@ namespace OpenWifi { void DeviceDashboard::Generate(GWObjects::Dashboard &D, Poco::Logger &Logger) { if (GeneratingDashboard_.load()) { - // std::cout << "Trying to generate dashboard but already being generated" << std::endl; while (GeneratingDashboard_.load()) { Poco::Thread::trySleep(100); } @@ -31,7 +30,6 @@ namespace OpenWifi { GeneratingDashboard_ = true; ValidDashboard_ = false; try { - // std::cout << "Generating dashboard." << std::endl; poco_information(Logger, "DASHBOARD: Generating a new dashboard."); GWObjects::Dashboard NewData; StorageService()->AnalyzeCommands(NewData.commands); diff --git a/src/ParseWifiScan.h b/src/ParseWifiScan.h index 67df6e780..f27a009e9 100644 --- a/src/ParseWifiScan.h +++ b/src/ParseWifiScan.h @@ -1753,7 +1753,6 @@ namespace OpenWifi { nlohmann::json new_ie; nlohmann::json content; - // std::cout << BufferToHex(&data[0],data.size()) << std::endl; uint offset = 0; auto sub_ie = data[offset++]; switch (sub_ie) { @@ -1788,7 +1787,6 @@ namespace OpenWifi { try { nlohmann::json D = nlohmann::json::parse(ofs.str()); - // std::cout << "Start of parsing wifi" << std::endl; if (D.contains("status")) { auto Status = D["status"]; if (Status.contains("scan") && Status["scan"].is_array()) { @@ -1803,8 +1801,6 @@ namespace OpenWifi { if (ie.contains("type") && ie.contains("data")) { uint64_t ie_type = ie["type"]; std::string ie_data = ie["data"]; - // std::cout << "TYPE:" << ie_type << " DATA:" << ie_data - // << std::endl; auto data = Base64Decode2Vec(ie_data); if (ie_type == ieee80211_eid::WLAN_EID_COUNTRY) { new_ies.push_back(WFS_WLAN_EID_COUNTRY(data)); @@ -1858,18 +1854,12 @@ namespace OpenWifi { } else if (ie_type == ieee80211_eid::WLAN_EID_EXTENSION) { new_ies.push_back(WFS_WLAN_EID_EXTENSION(data)); } else { - // std::cout - // << "Skipping IE: no parsing available: " << ie_type - // << std::endl; new_ies.push_back(ie); } } else { - // std::cout << "Skipping IE: no data and type" << - // std::endl; new_ies.push_back(ie); } } catch (...) { - // std::cout << "Skipping IE: exception" << std::endl; Logger.information(fmt::format("Error parsing IEs")); new_ies.push_back(ie); } @@ -1877,7 +1867,6 @@ namespace OpenWifi { scan_entry["ies"] = new_ies; ParsedScan.push_back(scan_entry); } else { - // std::cout << "Skipping scan" << std::endl; ParsedScan.push_back(scan_entry); } } @@ -1886,7 +1875,6 @@ namespace OpenWifi { } } Result << to_string(D); - // std::cout << "End of parsing wifi" << std::endl; return true; } catch (const Poco::Exception &E) { Logger.log(E); diff --git a/src/RADIUSSessionTracker.cpp b/src/RADIUSSessionTracker.cpp index 2c4fe52f6..7442126b4 100644 --- a/src/RADIUSSessionTracker.cpp +++ b/src/RADIUSSessionTracker.cpp @@ -177,15 +177,6 @@ namespace OpenWifi { } else { session_hint->second->lastTransaction = Utils::Now(); } - -/* - if(ap_hint!=AccountingSessions_.end()) { - std::cout << "Auth table:" << std::endl; - for(const auto &session:ap_hint->second) { - std::cout << Notification.SerialNumber_ << ": Index: " << session.first << ": ID: " << session.second->accountingSessionId << " MID:" << session.second->accountingMultiSessionId << std::endl; - } - } -*/ } std::uint32_t GetUiInt32(const std::uint8_t *buf) { From 915a2c936d8436328fb62f043096fd39e0a94b6b Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Fri, 15 Dec 2023 10:48:13 -0800 Subject: [PATCH 115/115] https://telecominfraproject.atlassian.net/browse/WIFI-13147 Signed-off-by: stephb9959 --- build | 2 +- src/rttys/RTTYS_server.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build b/build index b2412e34d..4e9e28848 100644 --- a/build +++ b/build @@ -1 +1 @@ -62 \ No newline at end of file +63 \ No newline at end of file diff --git a/src/rttys/RTTYS_server.cpp b/src/rttys/RTTYS_server.cpp index a84d05a64..e71d34b0e 100644 --- a/src/rttys/RTTYS_server.cpp +++ b/src/rttys/RTTYS_server.cpp @@ -146,7 +146,7 @@ namespace OpenWifi { auto WebClientSecureContext = new Poco::Net::Context(Poco::Net::Context::SERVER_USE, KeyFileName, - CertFileName, "", Poco::Net::Context::VERIFY_RELAXED); + CertFileName, "", Poco::Net::Context::VERIFY_NONE); Poco::Crypto::X509Certificate WebRoot(RootCaFileName); WebClientSecureContext->addCertificateAuthority(WebRoot); WebClientSecureContext->disableStatelessSessionResumption();