From adfbd2d52b45fe8355427dd64b6b86df83ffd732 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 25 May 2023 22:47:42 -0700 Subject: [PATCH] https://telecominfraproject.atlassian.net/browse/WIFI-12525 Signed-off-by: stephb9959 --- src/OWLS_Connect.cpp | 19 ++- src/OWLS_CrashLog.cpp | 8 +- src/OWLS_Disconnect.cpp | 10 +- src/OWLS_EstablishConnection.cpp | 6 +- src/OWLS_HealthCheck.cpp | 10 +- src/OWLS_KeepAlive.cpp | 11 +- src/OWLS_Log.cpp | 8 +- src/OWLS_PendingConfig.cpp | 8 +- src/OWLS_Reconnect.cpp | 8 +- src/OWLS_State.cpp | 10 +- src/OWLS_Update.cpp | 8 +- src/OWLS_WSPing.cpp | 11 +- src/OWLSclient.cpp | 35 +++--- src/OWLSclient.h | 37 +++--- src/OWLSclientEvents.h | 26 ++-- src/OWLSevent.cpp | 202 ------------------------------- src/OWLSevent.h | 130 -------------------- src/SimulationRunner.cpp | 102 ++++++++-------- src/SimulationRunner.h | 9 +- 19 files changed, 163 insertions(+), 495 deletions(-) delete mode 100644 src/OWLSevent.cpp delete mode 100644 src/OWLSevent.h diff --git a/src/OWLS_Connect.cpp b/src/OWLS_Connect.cpp index 6f289b0..40ef0be 100644 --- a/src/OWLS_Connect.cpp +++ b/src/OWLS_Connect.cpp @@ -10,11 +10,10 @@ #include #include "OWLSclientEvents.h" -namespace OpenWifi::OWLSclientEvents { - - void Connect(std::shared_ptr Client, SimulationRunner *Runner) { - std::lock_guard G(Client->Mutex_); +namespace OpenWifi::OWLSClientEvents { + void Connect(const std::shared_ptr &Client, SimulationRunner *Runner) { + std::lock_guard ClientGuard(Client->Mutex_); if(Client->Valid_) { try { Runner->Report().ev_connect++; @@ -36,22 +35,22 @@ namespace OpenWifi::OWLSclientEvents { if (Client->SendObject(ConnectMessage)) { Client->Reset(); Runner->Scheduler().in(std::chrono::seconds(Client->StatisticsInterval_), - OWLSclientEvents::State, Client, Runner); + OWLSClientEvents::State, Client, Runner); Runner->Scheduler().in(std::chrono::seconds(Client->HealthInterval_), - OWLSclientEvents::HealthCheck, Client, Runner); + OWLSClientEvents::HealthCheck, Client, Runner); Runner->Scheduler().in(std::chrono::seconds(MicroServiceRandom(120, 200)), - OWLSclientEvents::Log, Client, Runner, 1, "Device started"); + OWLSClientEvents::Log, Client, Runner, 1, "Device started"); Runner->Scheduler().in(std::chrono::seconds(60 * 4), - OWLSclientEvents::WSPing, Client, Runner); + OWLSClientEvents::WSPing, Client, Runner); Runner->Scheduler().in(std::chrono::seconds(30), - OWLSclientEvents::Update, Client, Runner); + OWLSClientEvents::Update, Client, Runner); Client->Logger_.information(fmt::format("connect({}): completed.", Client->SerialNumber_)); return; } } catch (const Poco::Exception &E) { Client->Logger().log(E); } - OWLSclientEvents::Disconnect(Client, Runner, "Error occurred during connection", true); + OWLSClientEvents::Disconnect(ClientGuard,Client, Runner, "Error occurred during connection", true); } } diff --git a/src/OWLS_CrashLog.cpp b/src/OWLS_CrashLog.cpp index b39dddd..66b58a4 100644 --- a/src/OWLS_CrashLog.cpp +++ b/src/OWLS_CrashLog.cpp @@ -9,13 +9,11 @@ #include #include "OWLSclientEvents.h" -#include "OWLSevent.h" -namespace OpenWifi::OWLSclientEvents { - - void CrashLog(std::shared_ptr Client, SimulationRunner *Runner) { - std::lock_guard G(Client->Mutex_); +namespace OpenWifi::OWLSClientEvents { + void CrashLog([[ + maybe_unused]] std::lock_guard &ClientGuard, const std::shared_ptr &Client, SimulationRunner *Runner) { if(Client->Valid_) { Runner->Report().ev_crashlog++; } diff --git a/src/OWLS_Disconnect.cpp b/src/OWLS_Disconnect.cpp index 2651436..725e6b1 100644 --- a/src/OWLS_Disconnect.cpp +++ b/src/OWLS_Disconnect.cpp @@ -9,20 +9,18 @@ #include #include "OWLSclientEvents.h" -#include "OWLSevent.h" #include "OWLS_utils.h" -namespace OpenWifi::OWLSclientEvents { +namespace OpenWifi::OWLSClientEvents { - void Disconnect(std::shared_ptr Client, SimulationRunner *Runner, const std::string &Reason, bool Reconnect) { - std::lock_guard G(Client->Mutex_); + void Disconnect(std::lock_guard &ClientGuard, const std::shared_ptr &Client, SimulationRunner *Runner, const std::string &Reason, bool Reconnect) { if(Client->Valid_) { - Client->Disconnect(G); + Client->Disconnect(ClientGuard); poco_debug(Client->Logger(),fmt::format("{}: disconnecting. Reason: {}", Client->SerialNumber_, Reason)); if(Reconnect) { Runner->Scheduler().in(std::chrono::seconds(OWLSutils::local_random(3, 15)), - OWLSclientEvents::EstablishConnection, Client, Runner); + OWLSClientEvents::EstablishConnection, Client, Runner); } else { // DEBUG_LINE("not reconnecting"); } diff --git a/src/OWLS_EstablishConnection.cpp b/src/OWLS_EstablishConnection.cpp index 329a476..cf8b46d 100644 --- a/src/OWLS_EstablishConnection.cpp +++ b/src/OWLS_EstablishConnection.cpp @@ -12,9 +12,9 @@ #include "OWLSclientEvents.h" -namespace OpenWifi::OWLSclientEvents { +namespace OpenWifi::OWLSClientEvents { - void EstablishConnection(std::shared_ptr Client, SimulationRunner *Runner) { + void EstablishConnection( const std::shared_ptr &Client, SimulationRunner *Runner) { Poco::URI uri(Runner->Details().gateway); Poco::Net::Context::Params P; @@ -60,6 +60,8 @@ namespace OpenWifi::OWLSclientEvents { Request.set("origin", "http://www.websocket.org"); Poco::Net::HTTPResponse Response; + std::lock_guard ClientGuard(Client->Mutex_); + Client->Logger_.information(fmt::format("connecting({}): host={} port={}", Client->SerialNumber_, uri.getHost(), uri.getPort())); diff --git a/src/OWLS_HealthCheck.cpp b/src/OWLS_HealthCheck.cpp index 8ff6fb4..e6cef7a 100644 --- a/src/OWLS_HealthCheck.cpp +++ b/src/OWLS_HealthCheck.cpp @@ -10,10 +10,10 @@ #include "OWLSclientEvents.h" -namespace OpenWifi::OWLSclientEvents { +namespace OpenWifi::OWLSClientEvents { - void HealthCheck(std::shared_ptr Client, SimulationRunner *Runner) { - std::lock_guard G(Client->Mutex_); + void HealthCheck(const std::shared_ptr &Client, SimulationRunner *Runner) { + std::lock_guard ClientGuard(Client->Mutex_); // DEBUG_LINE("start"); if(Client->Valid_ && Client->Connected_) { @@ -33,7 +33,7 @@ namespace OpenWifi::OWLSclientEvents { if (Client->SendObject(Message)) { Runner->Scheduler().in(std::chrono::seconds(Client->HealthInterval_), - OWLSclientEvents::HealthCheck, Client, Runner); + OWLSClientEvents::HealthCheck, Client, Runner); return; } } catch (const Poco::Exception &E) { @@ -42,7 +42,7 @@ namespace OpenWifi::OWLSclientEvents { } catch (const std::exception &E) { DEBUG_LINE("exception2"); } - OWLSclientEvents::Disconnect(Client, Runner, "Error while sending HealthCheck", true); + OWLSClientEvents::Disconnect(ClientGuard, Client, Runner, "Error while sending HealthCheck", true); } } diff --git a/src/OWLS_KeepAlive.cpp b/src/OWLS_KeepAlive.cpp index 9df3001..50e8d69 100644 --- a/src/OWLS_KeepAlive.cpp +++ b/src/OWLS_KeepAlive.cpp @@ -10,11 +10,10 @@ #include "OWLSclientEvents.h" -namespace OpenWifi::OWLSclientEvents { - - void KeepAlive(std::shared_ptr Client, SimulationRunner *Runner) { - std::lock_guard G(Client->Mutex_); +namespace OpenWifi::OWLSClientEvents { + void KeepAlive(const std::shared_ptr &Client, SimulationRunner *Runner) { + std::lock_guard ClientGuard(Client->Mutex_); if(Client->Valid_ && Client->Connected_) { Runner->Report().ev_keepalive++; try { @@ -26,7 +25,7 @@ namespace OpenWifi::OWLSclientEvents { if (Client->SendObject(Message)) { Runner->Scheduler().in(std::chrono::seconds(Runner->Details().keepAlive), - OWLSclientEvents::KeepAlive, Client, Runner); + OWLSClientEvents::KeepAlive, Client, Runner); return; } } catch (const Poco::Exception &E) { @@ -35,7 +34,7 @@ namespace OpenWifi::OWLSclientEvents { } catch (const std::exception &E) { DEBUG_LINE("exception2"); } - OWLSclientEvents::Disconnect(Client, Runner, "Error while sending keepalive", true); + OWLSClientEvents::Disconnect(ClientGuard,Client, Runner, "Error while sending keepalive", true); } } diff --git a/src/OWLS_Log.cpp b/src/OWLS_Log.cpp index a7c5170..11ba8d5 100644 --- a/src/OWLS_Log.cpp +++ b/src/OWLS_Log.cpp @@ -10,11 +10,11 @@ #include "OWLSclientEvents.h" -namespace OpenWifi::OWLSclientEvents { +namespace OpenWifi::OWLSClientEvents { - void Log(std::shared_ptr Client, SimulationRunner *Runner, std::uint64_t Severity, const std::string & LogLine) { - std::lock_guard G(Client->Mutex_); + void Log(const std::shared_ptr &Client, SimulationRunner *Runner, std::uint64_t Severity, const std::string & LogLine) { + std::lock_guard ClientGuard(Client->Mutex_); if(Client->Valid_ && Client->Connected_ ) { Runner->Report().ev_log++; try { @@ -34,7 +34,7 @@ namespace OpenWifi::OWLSclientEvents { } catch (const std::exception &E) { DEBUG_LINE("exception2"); } - OWLSclientEvents::Disconnect(Client, Runner, "Error while sending a Log event", true); + OWLSClientEvents::Disconnect(ClientGuard,Client, Runner, "Error while sending a Log event", true); } } diff --git a/src/OWLS_PendingConfig.cpp b/src/OWLS_PendingConfig.cpp index d020352..1496a3c 100644 --- a/src/OWLS_PendingConfig.cpp +++ b/src/OWLS_PendingConfig.cpp @@ -9,12 +9,10 @@ #include #include "OWLSclientEvents.h" -#include "OWLSevent.h" -namespace OpenWifi::OWLSclientEvents { +namespace OpenWifi::OWLSClientEvents { - void PendingConfig(std::shared_ptr Client, SimulationRunner *Runner) { - std::lock_guard G(Client->Mutex_); + void PendingConfig(std::lock_guard &ClientGuard, const std::shared_ptr &Client, SimulationRunner *Runner) { if(Client->Valid_ && Client->Connected_) { Runner->Report().ev_configpendingchange++; @@ -34,7 +32,7 @@ namespace OpenWifi::OWLSclientEvents { } catch (const std::exception &E) { DEBUG_LINE("exception2"); } - OWLSclientEvents::Disconnect(Client, Runner, "Error while sending ConfigPendingEvent", true); + OWLSClientEvents::Disconnect(ClientGuard, Client, Runner, "Error while sending ConfigPendingEvent", true); } } diff --git a/src/OWLS_Reconnect.cpp b/src/OWLS_Reconnect.cpp index c35d255..4e0488c 100644 --- a/src/OWLS_Reconnect.cpp +++ b/src/OWLS_Reconnect.cpp @@ -12,16 +12,16 @@ #include "OWLSclientEvents.h" #include "OWLS_utils.h" -namespace OpenWifi::OWLSclientEvents { +namespace OpenWifi::OWLSClientEvents { - void Reconnect(std::shared_ptr Client, SimulationRunner *Runner) { - std::lock_guard G(Client->Mutex_); + void Reconnect(const std::shared_ptr &Client, SimulationRunner *Runner) { + std::lock_guard ClientGuard(Client->Mutex_); try { if(Client->Valid_) { Runner->Report().ev_reconnect++; Client->Connected_ = false; - Runner->Scheduler().in(std::chrono::seconds(OWLSutils::local_random(3,15)), OWLSclientEvents::EstablishConnection, Client, Runner); + Runner->Scheduler().in(std::chrono::seconds(OWLSutils::local_random(3,15)), OWLSClientEvents::EstablishConnection, Client, Runner); } } catch (const Poco::Exception &E) { DEBUG_LINE("exception1"); diff --git a/src/OWLS_State.cpp b/src/OWLS_State.cpp index 76002cb..9389eb6 100644 --- a/src/OWLS_State.cpp +++ b/src/OWLS_State.cpp @@ -11,11 +11,11 @@ #include "OWLSdefinitions.h" #include "OWLSclientEvents.h" -namespace OpenWifi::OWLSclientEvents { +namespace OpenWifi::OWLSClientEvents { - void State(std::shared_ptr Client, SimulationRunner *Runner) { + void State(const std::shared_ptr &Client, SimulationRunner *Runner) { - std::lock_guard G(Client->Mutex_); + std::lock_guard ClientGuard(Client->Mutex_); if(Client->Valid_ && Client->Connected_) { @@ -44,7 +44,7 @@ namespace OpenWifi::OWLSclientEvents { if (Client->SendObject(Message)) { Runner->Scheduler().in(std::chrono::seconds(Client->StatisticsInterval_), - OWLSclientEvents::State, Client, Runner); + OWLSClientEvents::State, Client, Runner); return; } } catch (const Poco::Exception &E) { @@ -54,7 +54,7 @@ namespace OpenWifi::OWLSclientEvents { DEBUG_LINE("exception2"); } DEBUG_LINE("failed"); - OWLSclientEvents::Disconnect(Client, Runner, "Error sending stats event", true); + OWLSClientEvents::Disconnect(ClientGuard,Client, Runner, "Error sending stats event", true); } } diff --git a/src/OWLS_Update.cpp b/src/OWLS_Update.cpp index d8a063e..593f747 100644 --- a/src/OWLS_Update.cpp +++ b/src/OWLS_Update.cpp @@ -7,17 +7,17 @@ #include #include "OWLSclientEvents.h" -namespace OpenWifi::OWLSclientEvents { +namespace OpenWifi::OWLSClientEvents { - void Update(std::shared_ptr Client, SimulationRunner *Runner) { - std::lock_guard G(Client->Mutex_); + void Update(const std::shared_ptr &Client, SimulationRunner *Runner) { + std::lock_guard ClientGuard(Client->Mutex_); try { if(Client->Valid_ && Client->Connected_) { Runner->Report().ev_update++; Client->Update(); Runner->Scheduler().in(std::chrono::seconds(30), - OWLSclientEvents::Update, Client, Runner); + OWLSClientEvents::Update, Client, Runner); } } catch (const Poco::Exception &E) { DEBUG_LINE("exception1"); diff --git a/src/OWLS_WSPing.cpp b/src/OWLS_WSPing.cpp index efb7faf..ab24ee0 100644 --- a/src/OWLS_WSPing.cpp +++ b/src/OWLS_WSPing.cpp @@ -10,18 +10,17 @@ #include "OWLSclientEvents.h" -namespace OpenWifi::OWLSclientEvents { - - void WSPing(std::shared_ptr Client, SimulationRunner *Runner) { - std::lock_guard G(Client->Mutex_); +namespace OpenWifi::OWLSClientEvents { + void WSPing(const std::shared_ptr &Client, SimulationRunner *Runner) { + std::lock_guard ClientGuard(Client->Mutex_); if(Client->Valid_ && Client->Connected_) { Runner->Report().ev_wsping++; try { Client->WS_->sendFrame( "", 0, Poco::Net::WebSocket::FRAME_OP_PING | Poco::Net::WebSocket::FRAME_FLAG_FIN); Runner->Scheduler().in(std::chrono::seconds(60 * 4), - OWLSclientEvents::WSPing, Client, Runner); + OWLSClientEvents::WSPing, Client, Runner); return; } catch (const Poco::Exception &E) { DEBUG_LINE("exception1"); @@ -29,7 +28,7 @@ namespace OpenWifi::OWLSclientEvents { } catch (const std::exception &E) { DEBUG_LINE("exception2"); } - OWLSclientEvents::Disconnect(Client, Runner, "Error in WSPing", true); + OWLSClientEvents::Disconnect(ClientGuard, Client, Runner, "Error in WSPing", true); } } diff --git a/src/OWLSclient.cpp b/src/OWLSclient.cpp index 62bc434..500f8cb 100644 --- a/src/OWLSclient.cpp +++ b/src/OWLSclient.cpp @@ -365,9 +365,9 @@ namespace OpenWifi { return State; } - void OWLSclient::DoConfigure([[maybe_unused]] std::shared_ptr Client, uint64_t Id, const Poco::JSON::Object::Ptr Params) { + void OWLSclient::DoConfigure(const std::shared_ptr &Client, uint64_t Id, Poco::JSON::Object::Ptr Params) { + std::lock_guard ClientGuard(Client->Mutex_); try { - std::lock_guard G(Client->Mutex_); if (Params->has(uCentralProtocol::SERIAL) && Params->has(uCentralProtocol::CONFIG) && Params->has(uCentralProtocol::UUID)) { @@ -440,9 +440,9 @@ namespace OpenWifi { } } - void OWLSclient::DoReboot(std::shared_ptr Client, uint64_t Id, const Poco::JSON::Object::Ptr Params) { + void OWLSclient::DoReboot(const std::shared_ptr &Client, uint64_t Id, Poco::JSON::Object::Ptr Params) { + std::lock_guard ClientGuard(Client->Mutex_); try { - std::lock_guard G(Client->Mutex_); if (Params->has("serial") && Params->has("when")) { std::string Serial = Params->get("serial"); @@ -456,10 +456,10 @@ namespace OpenWifi { OWLSutils::MakeRPCHeader(Answer,Id,Result); poco_information(Client->Logger_,fmt::format("reboot({}): done.", Client->SerialNumber_)); Client->SendObject(Answer); - Client->Disconnect(G); + Client->Disconnect(ClientGuard); Client->Reset(); std::this_thread::sleep_for(std::chrono::seconds(20)); - OWLSclientEvents::Disconnect(Client, Client->Runner_, "Command: reboot", true); + OWLSClientEvents::Disconnect(ClientGuard,Client, Client->Runner_, "Command: reboot", true); } else { Client->Logger_.warning(fmt::format("reboot({}): Illegal command.", Client->SerialNumber_)); } @@ -486,9 +486,9 @@ namespace OpenWifi { return p; } - void OWLSclient::DoUpgrade(std::shared_ptr Client, uint64_t Id, const Poco::JSON::Object::Ptr Params) { + void OWLSclient::DoUpgrade(const std::shared_ptr &Client, uint64_t Id, Poco::JSON::Object::Ptr Params) { + std::lock_guard ClientGuard(Client->Mutex_); try { - std::lock_guard G(Client->Mutex_); if (Params->has("serial") && Params->has("uri")) { std::string Serial = Params->get("serial"); std::string URI = Params->get("uri"); @@ -502,12 +502,12 @@ namespace OpenWifi { OWLSutils::MakeRPCHeader(Answer, Id, Result); poco_information(Client->Logger_,fmt::format("upgrade({}): from URI={}.", Client->SerialNumber_, URI)); Client->SendObject(Answer); - Client->Disconnect(G); + Client->Disconnect(ClientGuard); Client->Version_++; Client->SetFirmware(GetFirmware(URI)); std::this_thread::sleep_for(std::chrono::seconds(30)); Client->Reset(); - OWLSclientEvents::Disconnect(Client, Client->Runner_, "Command: upgrade", true); + OWLSClientEvents::Disconnect(ClientGuard,Client, Client->Runner_, "Command: upgrade", true); } else { Client->Logger_.warning(fmt::format("upgrade({}): Illegal command.", Client->SerialNumber_)); } @@ -517,9 +517,9 @@ namespace OpenWifi { } } - void OWLSclient::DoFactory(std::shared_ptr Client, uint64_t Id, const Poco::JSON::Object::Ptr Params) { + void OWLSclient::DoFactory(const std::shared_ptr &Client, uint64_t Id, Poco::JSON::Object::Ptr Params) { + std::lock_guard ClientGuard(Client->Mutex_); try { - std::lock_guard G(Client->Mutex_); if (Params->has("serial") && Params->has("when")) { std::string Serial = Params->get("serial"); @@ -535,12 +535,12 @@ namespace OpenWifi { OWLSutils::MakeRPCHeader(Answer, Id, Result); poco_information(Client->Logger_, fmt::format("factory({}): done.", Client->SerialNumber_)); Client->SendObject(Answer); - Client->Disconnect(G); + Client->Disconnect(ClientGuard); Client->CurrentConfig_ = SimulationCoordinator()->GetSimConfigurationPtr(Utils::Now()); Client->UpdateConfiguration(); std::this_thread::sleep_for(std::chrono::seconds(5)); Client->Reset(); - OWLSclientEvents::Disconnect(Client, Client->Runner_, "Command: upgrade", true); + OWLSClientEvents::Disconnect(ClientGuard, Client, Client->Runner_, "Command: upgrade", true); } else { Client->Logger_.warning(fmt::format("factory({}): Illegal command.", Client->SerialNumber_)); } @@ -550,9 +550,9 @@ namespace OpenWifi { } } - void OWLSclient::DoLEDs([[maybe_unused]] std::shared_ptr Client, uint64_t Id, const Poco::JSON::Object::Ptr Params) { + void OWLSclient::DoLEDs(const std::shared_ptr &Client, uint64_t Id, Poco::JSON::Object::Ptr Params) { + std::lock_guard ClientGuard(Client->Mutex_); try { - std::lock_guard G(Client->Mutex_); if (Params->has("serial") && Params->has("pattern")) { std::string Serial = Params->get("serial"); auto Pattern = Params->get("pattern").toString(); @@ -576,7 +576,8 @@ namespace OpenWifi { } } - void OWLSclient::UNsupportedCommand(std::shared_ptr Client, uint64_t Id, + void OWLSclient::UnSupportedCommand([[ + maybe_unused]] std::lock_guard &ClientGuard,const std::shared_ptr &Client, uint64_t Id, const std::string & Method) { try { Poco::JSON::Object Answer, Result, Status; diff --git a/src/OWLSclient.h b/src/OWLSclient.h index 08039a2..2228273 100644 --- a/src/OWLSclient.h +++ b/src/OWLSclient.h @@ -48,12 +48,13 @@ namespace OpenWifi { [[nodiscard]] bool Connected() const { return Connected_; } [[nodiscard]] inline uint64_t GetStartTime() const { return StartTime_; } - static void DoConfigure(std::shared_ptr Client, uint64_t Id, const Poco::JSON::Object::Ptr Params); - static void DoReboot(std::shared_ptr Client, uint64_t Id, const Poco::JSON::Object::Ptr Params); - static void DoUpgrade(std::shared_ptr Client, uint64_t Id, const Poco::JSON::Object::Ptr Params); - static void DoFactory(std::shared_ptr Client, uint64_t Id, const Poco::JSON::Object::Ptr Params); - static void DoLEDs(std::shared_ptr Client, uint64_t Id, const Poco::JSON::Object::Ptr Params); - void UNsupportedCommand(std::shared_ptr Client, uint64_t Id, const std::string &Method); + static void DoConfigure(const std::shared_ptr &Client, uint64_t Id, Poco::JSON::Object::Ptr Params); + static void DoReboot(const std::shared_ptr &Client, uint64_t Id, Poco::JSON::Object::Ptr Params); + static void DoUpgrade(const std::shared_ptr &Client, uint64_t Id, Poco::JSON::Object::Ptr Params); + static void DoFactory(const std::shared_ptr &Client, uint64_t Id, Poco::JSON::Object::Ptr Params); + static void DoLEDs(const std::shared_ptr &Client, uint64_t Id, Poco::JSON::Object::Ptr Params); + + void UnSupportedCommand(std::lock_guard &G,const std::shared_ptr &Client, uint64_t Id, const std::string &Method); using interface_location_t = std::tuple; using associations_map_t = std::map; @@ -93,18 +94,18 @@ namespace OpenWifi { friend class SimulationRunner; - friend void OWLSclientEvents::EstablishConnection(std::shared_ptr Client, SimulationRunner *Runner); - friend void OWLSclientEvents::Reconnect(std::shared_ptr Client, SimulationRunner *Runner); - friend void OWLSclientEvents::Connect(std::shared_ptr Client, SimulationRunner *Runner); - friend void OWLSclientEvents::Log(std::shared_ptr Client, SimulationRunner *Runner, std::uint64_t Severity, const std::string & LogLine); - friend void OWLSclientEvents::State(std::shared_ptr Client, SimulationRunner *Runner); - friend void OWLSclientEvents::HealthCheck(std::shared_ptr Client, SimulationRunner *Runner); - friend void OWLSclientEvents::Update(std::shared_ptr Client, SimulationRunner *Runner); - friend void OWLSclientEvents::WSPing(std::shared_ptr Client, SimulationRunner *Runner); - friend void OWLSclientEvents::KeepAlive(std::shared_ptr Client, SimulationRunner *Runner); - friend void OWLSclientEvents::Disconnect(std::shared_ptr Client, SimulationRunner *Runner, const std::string &Reason, bool Reconnect); - friend void OWLSclientEvents::CrashLog(std::shared_ptr Client, SimulationRunner *Runner); - friend void OWLSclientEvents::PendingConfig(std::shared_ptr Client, SimulationRunner *Runner); + friend void OWLSClientEvents::EstablishConnection(const std::shared_ptr &Client, SimulationRunner *Runner); + friend void OWLSClientEvents::Reconnect(const std::shared_ptr &Client, SimulationRunner *Runner); + friend void OWLSClientEvents::Connect(const std::shared_ptr &Client, SimulationRunner *Runner); + friend void OWLSClientEvents::Log(const std::shared_ptr &Client, SimulationRunner *Runner, std::uint64_t Severity, const std::string & LogLine); + friend void OWLSClientEvents::State(const std::shared_ptr &Client, SimulationRunner *Runner); + friend void OWLSClientEvents::HealthCheck(const std::shared_ptr &Client, SimulationRunner *Runner); + friend void OWLSClientEvents::Update(const std::shared_ptr &Client, SimulationRunner *Runner); + friend void OWLSClientEvents::WSPing(const std::shared_ptr &Client, SimulationRunner *Runner); + friend void OWLSClientEvents::KeepAlive(const std::shared_ptr &Client, SimulationRunner *Runner); + friend void OWLSClientEvents::Disconnect(std::lock_guard &g, const std::shared_ptr &Client, SimulationRunner *Runner, const std::string &Reason, bool Reconnect); + friend void OWLSClientEvents::CrashLog(std::lock_guard &g, const std::shared_ptr &Client, SimulationRunner *Runner); + friend void OWLSClientEvents::PendingConfig(std::lock_guard &g, const std::shared_ptr &Client, SimulationRunner *Runner); private: // std::recursive_mutex Mutex_; diff --git a/src/OWLSclientEvents.h b/src/OWLSclientEvents.h index b8d676f..62ec530 100644 --- a/src/OWLSclientEvents.h +++ b/src/OWLSclientEvents.h @@ -11,17 +11,17 @@ namespace OpenWifi { class SimulationRunner; } -namespace OpenWifi::OWLSclientEvents { - void EstablishConnection(std::shared_ptr Client, SimulationRunner *Runner); - void Reconnect(std::shared_ptr Client, SimulationRunner *Runner); - void Connect(std::shared_ptr Client, SimulationRunner *Runner); - void State(std::shared_ptr Client, SimulationRunner *Runner); - void HealthCheck(std::shared_ptr Client, SimulationRunner *Runner); - void Log(std::shared_ptr Client, SimulationRunner *Runner, std::uint64_t Severity, const std::string & LogLine); - void WSPing(std::shared_ptr Client, SimulationRunner *Runner); - void Update(std::shared_ptr Client, SimulationRunner *Runner); - void KeepAlive(std::shared_ptr Client, SimulationRunner *Runner); - void Disconnect(std::shared_ptr Client, SimulationRunner *Runner, const std::string &Reason, bool Reconnect); - void CrashLog(std::shared_ptr Client, SimulationRunner *Runner); - void PendingConfig(std::shared_ptr Client, SimulationRunner *Runner); +namespace OpenWifi::OWLSClientEvents { + void EstablishConnection(const std::shared_ptr &Client, SimulationRunner *Runner); + void Reconnect(const std::shared_ptr &Client, SimulationRunner *Runner); + void Connect(const std::shared_ptr &Client, SimulationRunner *Runner); + void State(const std::shared_ptr &Client, SimulationRunner *Runner); + void HealthCheck(const std::shared_ptr &Client, SimulationRunner *Runner); + void Log(const std::shared_ptr &Client, SimulationRunner *Runner, std::uint64_t Severity, const std::string & LogLine); + void WSPing(const std::shared_ptr &Client, SimulationRunner *Runner); + void Update(const std::shared_ptr &Client, SimulationRunner *Runner); + void KeepAlive(const std::shared_ptr &Client, SimulationRunner *Runner); + void Disconnect(std::lock_guard &g, const std::shared_ptr &Client, SimulationRunner *Runner, const std::string &Reason, bool Reconnect); + void CrashLog(std::lock_guard &g, const std::shared_ptr &Client, SimulationRunner *Runner); + void PendingConfig(std::lock_guard &g, const std::shared_ptr &Client, SimulationRunner *Runner); }; diff --git a/src/OWLSevent.cpp b/src/OWLSevent.cpp deleted file mode 100644 index efa6b38..0000000 --- a/src/OWLSevent.cpp +++ /dev/null @@ -1,202 +0,0 @@ -// -// Created by stephane bourque on 2021-04-03. -// -#include - -#include "Poco/zlib.h" -#include "nlohmann/json.hpp" - -#include "SimulationCoordinator.h" -#include "OWLSevent.h" - -#include "framework/MicroServiceFuncs.h" - -namespace OpenWifi { - - bool OWLSConnectEvent::Send() { - try { - nlohmann::json M; - M["jsonrpc"] = "2.0"; - M["method"] = "connect"; - M["params"]["serial"] = Client_->Serial(); - M["params"]["uuid"] = Client_->UUID(); - M["params"]["firmware"] = Client_->Firmware(); - auto TmpCapabilities = SimulationCoordinator()->GetSimCapabilities(); - auto LabelMac = Utils::SerialNumberToInt(Client_->Serial()); - auto LabelMacFormatted = Utils::SerialToMAC(Utils::IntToSerialNumber(LabelMac)); - auto LabelLanMacFormatted = Utils::SerialToMAC(Utils::IntToSerialNumber(LabelMac + 1)); - TmpCapabilities["label_macaddr"] = LabelMac; - TmpCapabilities["macaddr"]["wan"] = LabelMac; - TmpCapabilities["macaddr"]["lan"] = LabelLanMacFormatted; - M["params"]["capabilities"] = TmpCapabilities; - if (Client_->Send(to_string(M))) { - Client_->Reset(); - Client_->AddEvent(ev_state, - SimulationCoordinator()->GetSimulationInfo().stateInterval); - Client_->AddEvent(ev_healthcheck, - SimulationCoordinator()->GetSimulationInfo().healthCheckInterval); - Client_->AddEvent(ev_log, MicroServiceRandom(120, 200)); - Client_->AddEvent(ev_wsping, 60 * 5); - return true; - } - } catch (const Poco::Exception &E) { - Client_->Logger().log(E); - } - Client_->Disconnect("Error occurred during connection", true); - return false; - } - - bool OWLSStateEvent::Send() { - try { - nlohmann::json M; - - M["jsonrpc"] = "2.0"; - M["method"] = "state"; - - nlohmann::json ParamsObj; - ParamsObj["serial"] = Client_->Serial(); - ParamsObj["uuid"] = Client_->UUID(); - ParamsObj["state"] = Client_->CreateState(); - - auto ParamsStr = to_string(ParamsObj); - - unsigned long BufSize = ParamsStr.size() + 4000; - std::vector Buffer(BufSize); - compress(&Buffer[0], &BufSize, (Bytef *)ParamsStr.c_str(), ParamsStr.size()); - - auto CompressedBase64Encoded = OpenWifi::Utils::base64encode(&Buffer[0], BufSize); - - M["params"]["compress_64"] = CompressedBase64Encoded; - M["params"]["compress_sz"] = ParamsStr.size(); - - if (Client_->Send(to_string(M))) { - Client_->AddEvent(ev_state, Client_->GetStateInterval()); - return true; - } - } catch (const Poco::Exception &E) { - Client_->Logger().log(E); - } - Client_->Disconnect("Error sending stats event", true); - return false; - } - - bool OWLSHealthCheckEvent::Send() { - try { - nlohmann::json M, P; - - - P["memory"] = 23; - - M["jsonrpc"] = "2.0"; - M["method"] = "healthcheck"; - M["params"]["serial"] = Client_->Serial(); - M["params"]["uuid"] = Client_->UUID(); - M["params"]["sanity"] = 100; - M["params"]["data"] = P; - - if (Client_->Send(to_string(M))) { - Client_->AddEvent(ev_healthcheck, Client_->GetHealthInterval()); - return true; - } - } catch (const Poco::Exception &E) { - Client_->Logger().log(E); - } - Client_->Disconnect("Error while sending HealthCheck", true); - return false; - } - - bool OWLSLogEvent::Send() { - try { - nlohmann::json M; - - M["jsonrpc"] = "2.0"; - M["method"] = "log"; - M["params"]["serial"] = Client_->Serial(); - M["params"]["uuid"] = Client_->UUID(); - M["params"]["severity"] = Severity_; - M["params"]["log"] = LogLine_; - - if (Client_->Send(to_string(M))) { - Client_->AddEvent(ev_log, MicroServiceRandom(300, 600)); - return true; - } - } catch (const Poco::Exception &E) { - Client_->Logger().log(E); - } - Client_->Disconnect("Error while sending a Log event", true); - return false; - }; - - bool OWLSCrashLogEvent::Send() { return false; }; - - bool OWLSConfigChangePendingEvent::Send() { - try { - nlohmann::json M; - - M["jsonrpc"] = "2.0"; - M["method"] = "cfgpending"; - M["params"]["serial"] = Client_->Serial(); - M["params"]["uuid"] = Client_->UUID(); - M["params"]["active"] = Client_->Active(); - - if (Client_->Send(to_string(M))) { - Client_->AddEvent(ev_configpendingchange, - SimulationCoordinator()->GetSimulationInfo().clientInterval); - return true; - } - } catch (const Poco::Exception &E) { - Client_->Logger().log(E); - } - Client_->Disconnect("Error while sending ConfigPendingEvent", true); - return false; - } - - bool OWLSKeepAliveEvent::Send() { - try { - nlohmann::json M; - - M["jsonrpc"] = "2.0"; - M["method"] = "ping"; - M["params"]["serial"] = Client_->Serial(); - M["params"]["uuid"] = Client_->UUID(); - - if (Client_->Send(to_string(M))) { - Client_->AddEvent(ev_keepalive, - SimulationCoordinator()->GetSimulationInfo().keepAlive); - return true; - } - } catch (const Poco::Exception &E) { - Client_->Logger().log(E); - } - Client_->Disconnect("Error while sending keepalive", true); - return false; - }; - - // This is just a fake event, reboot is handled somewhere else. - bool OWLSRebootEvent::Send() { return true; } - - // This is just a fake event, disconnect is handled somewhere else. - bool OWLSDisconnectEvent::Send() { return true; } - - bool OWLSWSPingEvent::Send() { - try { - if (Client_->SendWSPing()) { - Client_->AddEvent(ev_wsping, 60 * 5); - return true; - } - } catch (const Poco::Exception &E) { - Client_->Logger().log(E); - } - Client_->Disconnect("Error in WSPing", true); - return false; - } - - bool OWLSUpdate::Send() { - try { - Client_->Update(); - } catch (const Poco::Exception &E) { - Client_->Logger().log(E); - } - return false; - } -} // namespace OpenWifi \ No newline at end of file diff --git a/src/OWLSevent.h b/src/OWLSevent.h deleted file mode 100644 index a982d68..0000000 --- a/src/OWLSevent.h +++ /dev/null @@ -1,130 +0,0 @@ -// -// Created by stephane bourque on 2021-04-03. -// - -#ifndef UCENTRAL_CLNT_OWLSEVENT_H -#define UCENTRAL_CLNT_OWLSEVENT_H - -#include "Poco/JSON/Object.h" -#include "OWLSclient.h" - -#include "OWLSclient.h" -#include "OWLSdefinitions.h" - -namespace OpenWifi { - class OWLSevent { - public: - explicit OWLSevent(std::shared_ptr Client) - : Client_(std::move(Client)) {} - virtual bool Send() = 0; - - protected: - bool SendObject(Poco::JSON::Object &Obj); - std::shared_ptr Client_; - }; - - class OWLSConnectEvent : public OWLSevent { - public: - explicit OWLSConnectEvent(std::shared_ptr Client) - : OWLSevent(std::move(Client)){}; - bool Send() override; - - private: - }; - - class OWLSStateEvent : public OWLSevent { - public: - explicit OWLSStateEvent(std::shared_ptr Client) - : OWLSevent(std::move(Client)){}; - bool Send() override; - - private: - }; - - class OWLSHealthCheckEvent : public OWLSevent { - public: - explicit OWLSHealthCheckEvent(std::shared_ptr Client) - : OWLSevent(std::move(Client)){}; - bool Send() override; - - private: - }; - - class OWLSLogEvent : public OWLSevent { - public: - explicit OWLSLogEvent(std::shared_ptr Client, std::string LogLine, - uint64_t Severity) - : OWLSevent(std::move(Client)), LogLine_(std::move(LogLine)), Severity_(Severity){}; - bool Send() override; - - private: - std::string LogLine_; - uint64_t Severity_; - }; - - class OWLSCrashLogEvent : public OWLSevent { - public: - explicit OWLSCrashLogEvent(std::shared_ptr Client) - : OWLSevent(std::move(Client)){}; - bool Send() override; - - private: - }; - - class OWLSConfigChangePendingEvent : public OWLSevent { - public: - explicit OWLSConfigChangePendingEvent(std::shared_ptr Client) - : OWLSevent(std::move(Client)){}; - bool Send() override; - - private: - }; - - class OWLSKeepAliveEvent : public OWLSevent { - public: - explicit OWLSKeepAliveEvent(std::shared_ptr Client) - : OWLSevent(std::move(Client)){}; - bool Send() override; - - private: - }; - - class OWLSRebootEvent : public OWLSevent { - public: - explicit OWLSRebootEvent(std::shared_ptr Client) - : OWLSevent(std::move(Client)){}; - bool Send() override; - - private: - }; - - class OWLSDisconnectEvent : public OWLSevent { - public: - explicit OWLSDisconnectEvent(std::shared_ptr Client) - : OWLSevent(std::move(Client)){}; - bool Send() override; - - private: - }; - - class OWLSWSPingEvent : public OWLSevent { - public: - explicit OWLSWSPingEvent(std::shared_ptr Client) - : OWLSevent(std::move(Client)){}; - bool Send() override; - - private: - }; - - class OWLSUpdate : public OWLSevent { - public: - explicit OWLSUpdate(std::shared_ptr Client) - : OWLSevent(std::move(Client)){}; - bool Send() override; - - private: - }; - -} // namespace OpenWifi - -#endif // UCENTRAL_CLNT_OWLSEVENT_H diff --git a/src/SimulationRunner.cpp b/src/SimulationRunner.cpp index 16b5547..70275df 100644 --- a/src/SimulationRunner.cpp +++ b/src/SimulationRunner.cpp @@ -5,16 +5,13 @@ #include #include -#include "Poco/Logger.h" #include "SimStats.h" #include "SimulationRunner.h" -#include "OWLSevent.h" - #include "fmt/format.h" - #include "UI_Owls_WebSocketNotifications.h" +#include #include #include #include @@ -44,7 +41,7 @@ namespace OpenWifi { auto Client = std::make_shared(Buffer, Logger_, this, *SocketReactorPool_[ReactorIndex++ % NumberOfReactors_]); Client->SerialNumber_ = Buffer; Client->Valid_ = true; - Scheduler_.in(std::chrono::seconds(distrib(gen)), OWLSclientEvents::EstablishConnection, Client, this); + Scheduler_.in(std::chrono::seconds(distrib(gen)), OWLSClientEvents::EstablishConnection, Client, this); Clients_[Buffer] = Client; } Scheduler_.in(std::chrono::seconds(10), ProgressUpdate, this); @@ -67,7 +64,8 @@ namespace OpenWifi { int valids=0,invalids=0; for(auto &client:Clients_) { if(client.second->Valid_) { - OWLSclientEvents::Disconnect(client.second, this, "Simulation shutting down", false); + std::lock_guard Guard(client.second->Mutex_); + OWLSClientEvents::Disconnect(Guard, client.second, this, "Simulation shutting down", false); client.second->Valid_ = false; valids++; } else { @@ -92,58 +90,64 @@ namespace OpenWifi { std::map>::iterator client_hint; std::shared_ptr client; - client_hint = Clients_fd_.find(socket); - if (client_hint == end(Clients_fd_)) { - poco_warning(Logger_, fmt::format("{}: Invalid socket", socket)); - return; + { + std::lock_guard GG(SocketFdMutex_); + client_hint = Clients_fd_.find(socket); + if (client_hint == end(Clients_fd_)) { + poco_warning(Logger_, fmt::format("{}: Invalid socket", socket)); + return; + } + client = client_hint->second; + } + + { + std::lock_guard Guard(client->Mutex_); + client->Disconnect(Guard); + client->Reactor_.removeEventHandler( + *client->WS_, Poco::NObserver( + *this, &SimulationRunner::OnSocketReadable)); + client->Reactor_.removeEventHandler( + *client->WS_, Poco::NObserver( + *this, &SimulationRunner::OnSocketError)); + client->Reactor_.removeEventHandler( + *client->WS_, Poco::NObserver( + *this, &SimulationRunner::OnSocketShutdown)); + client->fd_ = -1; + RemoveClientFd(socket); + } + if (Running_) { + OWLSClientEvents::Reconnect(client, this); } - client = client_hint->second; - std::lock_guard Guard(client->Mutex_); - client->Disconnect(Guard); - client->Reactor_.removeEventHandler( - *client->WS_, Poco::NObserver( - *this, &SimulationRunner::OnSocketReadable)); - client->Reactor_.removeEventHandler( - *client->WS_, Poco::NObserver( - *this, &SimulationRunner::OnSocketError)); - client->Reactor_.removeEventHandler( - *client->WS_, Poco::NObserver( - *this, &SimulationRunner::OnSocketShutdown)); - client->fd_ = -1; - Clients_fd_.erase(socket); - if(Running_) - OWLSclientEvents::Reconnect(client,this); } void SimulationRunner::OnSocketShutdown(const Poco::AutoPtr &pNf) { - std::lock_guard G(Mutex_); - auto socket = pNf->socket().impl()->sockfd(); - std::map>::iterator client_hint; std::shared_ptr client; - - client_hint = Clients_fd_.find(socket); - if (client_hint == end(Clients_fd_)) { - poco_warning(Logger_, fmt::format("{}: Invalid socket", socket)); - return; + { + std::lock_guard G(SocketFdMutex_); + auto client_hint = Clients_fd_.find(socket); + if (client_hint == end(Clients_fd_)) { + poco_warning(Logger_, fmt::format("{}: Invalid socket", socket)); + return; + } + client = client_hint->second; + } + { + std::lock_guard Guard(client->Mutex_); + client->Disconnect(Guard); } - client = client_hint->second; - std::lock_guard Guard(client->Mutex_); - client->Disconnect(Guard); if(Running_) - OWLSclientEvents::Reconnect(client,this); + OWLSClientEvents::Reconnect(client,this); } void SimulationRunner::OnSocketReadable(const Poco::AutoPtr &pNf) { - std::map>::iterator client_hint; std::shared_ptr client; int socket; { - std::lock_guard G(Mutex_); - + std::lock_guard G(SocketFdMutex_); socket = pNf->socket().impl()->sockfd(); - client_hint = Clients_fd_.find(socket); + auto client_hint = Clients_fd_.find(socket); if (client_hint == end(Clients_fd_)) { poco_warning(Logger_, fmt::format("{}: Invalid socket", socket)); return; @@ -161,8 +165,8 @@ namespace OpenWifi { auto Op = Flags & Poco::Net::WebSocket::FRAME_OP_BITMASK; if (MessageSize == 0 && Flags == 0 && Op == 0) { - Clients_fd_.erase(socket); - OWLSclientEvents::Disconnect(client, this, "Error while waiting for data in WebSocket", true); + RemoveClientFd(socket); + OWLSClientEvents::Disconnect(Guard, client, this, "Error while waiting for data in WebSocket", true); return; } IncomingFrame.append(0); @@ -185,7 +189,7 @@ namespace OpenWifi { if (Frame->has("jsonrpc") && Frame->has("id") && Frame->has("method") && Frame->has("params")) { - ProcessCommand(client, Frame); + ProcessCommand(Guard,client, Frame); } else { Logger_.warning( fmt::format("MESSAGE({}): invalid incoming message.", client->SerialNumber_)); @@ -204,11 +208,11 @@ namespace OpenWifi { E.displayText())); } - Clients_fd_.erase(socket); - OWLSclientEvents::Disconnect(client, this, "Error while waiting for data in WebSocket", true); + RemoveClientFd(socket); + OWLSClientEvents::Disconnect(Guard,client, this, "Error while waiting for data in WebSocket", true); } - void SimulationRunner::ProcessCommand(std::shared_ptr Client, Poco::JSON::Object::Ptr Frame) { + void SimulationRunner::ProcessCommand(std::lock_guard &ClientGuard, const std::shared_ptr & Client, Poco::JSON::Object::Ptr Frame) { std::string Method = Frame->get("method"); std::uint64_t Id = Frame->get("id"); @@ -236,7 +240,7 @@ namespace OpenWifi { t.detach(); } else { Logger_.warning(fmt::format("COMMAND({}): unknown method '{}'", Client->SerialNumber_, Method)); - Client->UNsupportedCommand(Client, Id, Method); + Client->UnSupportedCommand(ClientGuard,Client, Id, Method); } } diff --git a/src/SimulationRunner.h b/src/SimulationRunner.h index 8307ecb..efbea14 100644 --- a/src/SimulationRunner.h +++ b/src/SimulationRunner.h @@ -38,22 +38,23 @@ namespace OpenWifi { const std::string & Id() const { return Id_; } - inline void AddClientFd(std::int64_t fd, std::shared_ptr c) { - std::lock_guard G(Mutex_); + inline void AddClientFd(std::int64_t fd, const std::shared_ptr &c) { + std::lock_guard G(SocketFdMutex_); Clients_fd_[fd] = c; } inline void RemoveClientFd(std::int64_t fd) { - std::lock_guard G(Mutex_); + std::lock_guard G(SocketFdMutex_); Clients_fd_.erase(fd); } - void ProcessCommand(std::shared_ptr Client, Poco::JSON::Object::Ptr Vars); + void ProcessCommand(std::lock_guard &G, const std::shared_ptr &Client, Poco::JSON::Object::Ptr Vars); // Poco::Net::SocketReactor & Reactor() { return Reactor_; } inline auto & Scheduler() { return Scheduler_; } private: + std::mutex SocketFdMutex_; my_mutex Mutex_; OWLSObjects::SimulationDetails Details_; Poco::Logger &Logger_;