stephb9959
2023-05-25 22:47:42 -07:00
parent 93aa2e300f
commit adfbd2d52b
19 changed files with 163 additions and 495 deletions

View File

@@ -10,11 +10,10 @@
#include <Poco/NObserver.h>
#include "OWLSclientEvents.h"
namespace OpenWifi::OWLSclientEvents {
void Connect(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner) {
std::lock_guard G(Client->Mutex_);
namespace OpenWifi::OWLSClientEvents {
void Connect(const std::shared_ptr<OWLSclient> &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);
}
}

View File

@@ -9,13 +9,11 @@
#include <Poco/NObserver.h>
#include "OWLSclientEvents.h"
#include "OWLSevent.h"
namespace OpenWifi::OWLSclientEvents {
void CrashLog(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner) {
std::lock_guard G(Client->Mutex_);
namespace OpenWifi::OWLSClientEvents {
void CrashLog([[
maybe_unused]] std::lock_guard<std::mutex> &ClientGuard, const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner) {
if(Client->Valid_) {
Runner->Report().ev_crashlog++;
}

View File

@@ -9,20 +9,18 @@
#include <Poco/NObserver.h>
#include "OWLSclientEvents.h"
#include "OWLSevent.h"
#include "OWLS_utils.h"
namespace OpenWifi::OWLSclientEvents {
namespace OpenWifi::OWLSClientEvents {
void Disconnect(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner, const std::string &Reason, bool Reconnect) {
std::lock_guard G(Client->Mutex_);
void Disconnect(std::lock_guard<std::mutex> &ClientGuard, const std::shared_ptr<OWLSclient> &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");
}

View File

@@ -12,9 +12,9 @@
#include "OWLSclientEvents.h"
namespace OpenWifi::OWLSclientEvents {
namespace OpenWifi::OWLSClientEvents {
void EstablishConnection(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner) {
void EstablishConnection( const std::shared_ptr<OWLSclient> &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()));

View File

@@ -10,10 +10,10 @@
#include "OWLSclientEvents.h"
namespace OpenWifi::OWLSclientEvents {
namespace OpenWifi::OWLSClientEvents {
void HealthCheck(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner) {
std::lock_guard G(Client->Mutex_);
void HealthCheck(const std::shared_ptr<OWLSclient> &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);
}
}

View File

@@ -10,11 +10,10 @@
#include "OWLSclientEvents.h"
namespace OpenWifi::OWLSclientEvents {
void KeepAlive(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner) {
std::lock_guard G(Client->Mutex_);
namespace OpenWifi::OWLSClientEvents {
void KeepAlive(const std::shared_ptr<OWLSclient> &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);
}
}

View File

@@ -10,11 +10,11 @@
#include "OWLSclientEvents.h"
namespace OpenWifi::OWLSclientEvents {
namespace OpenWifi::OWLSClientEvents {
void Log(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner, std::uint64_t Severity, const std::string & LogLine) {
std::lock_guard G(Client->Mutex_);
void Log(const std::shared_ptr<OWLSclient> &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);
}
}

View File

@@ -9,12 +9,10 @@
#include <Poco/NObserver.h>
#include "OWLSclientEvents.h"
#include "OWLSevent.h"
namespace OpenWifi::OWLSclientEvents {
namespace OpenWifi::OWLSClientEvents {
void PendingConfig(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner) {
std::lock_guard G(Client->Mutex_);
void PendingConfig(std::lock_guard<std::mutex> &ClientGuard, const std::shared_ptr<OWLSclient> &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);
}
}

View File

@@ -12,16 +12,16 @@
#include "OWLSclientEvents.h"
#include "OWLS_utils.h"
namespace OpenWifi::OWLSclientEvents {
namespace OpenWifi::OWLSClientEvents {
void Reconnect(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner) {
std::lock_guard G(Client->Mutex_);
void Reconnect(const std::shared_ptr<OWLSclient> &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");

View File

@@ -11,11 +11,11 @@
#include "OWLSdefinitions.h"
#include "OWLSclientEvents.h"
namespace OpenWifi::OWLSclientEvents {
namespace OpenWifi::OWLSClientEvents {
void State(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner) {
void State(const std::shared_ptr<OWLSclient> &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);
}
}

View File

@@ -7,17 +7,17 @@
#include <Poco/NObserver.h>
#include "OWLSclientEvents.h"
namespace OpenWifi::OWLSclientEvents {
namespace OpenWifi::OWLSClientEvents {
void Update(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner) {
std::lock_guard G(Client->Mutex_);
void Update(const std::shared_ptr<OWLSclient> &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");

View File

@@ -10,18 +10,17 @@
#include "OWLSclientEvents.h"
namespace OpenWifi::OWLSclientEvents {
void WSPing(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner) {
std::lock_guard G(Client->Mutex_);
namespace OpenWifi::OWLSClientEvents {
void WSPing(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner) {
std::lock_guard<std::mutex> 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);
}
}

View File

@@ -365,9 +365,9 @@ namespace OpenWifi {
return State;
}
void OWLSclient::DoConfigure([[maybe_unused]] std::shared_ptr<OWLSclient> Client, uint64_t Id, const Poco::JSON::Object::Ptr Params) {
void OWLSclient::DoConfigure(const std::shared_ptr<OWLSclient> &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<OWLSclient> Client, uint64_t Id, const Poco::JSON::Object::Ptr Params) {
void OWLSclient::DoReboot(const std::shared_ptr<OWLSclient> &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<OWLSclient> Client, uint64_t Id, const Poco::JSON::Object::Ptr Params) {
void OWLSclient::DoUpgrade(const std::shared_ptr<OWLSclient> &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<OWLSclient> Client, uint64_t Id, const Poco::JSON::Object::Ptr Params) {
void OWLSclient::DoFactory(const std::shared_ptr<OWLSclient> &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<OWLSclient> Client, uint64_t Id, const Poco::JSON::Object::Ptr Params) {
void OWLSclient::DoLEDs(const std::shared_ptr<OWLSclient> &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<OWLSclient> Client, uint64_t Id,
void OWLSclient::UnSupportedCommand([[
maybe_unused]] std::lock_guard<std::mutex> &ClientGuard,const std::shared_ptr<OWLSclient> &Client, uint64_t Id,
const std::string & Method) {
try {
Poco::JSON::Object Answer, Result, Status;

View File

@@ -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<OWLSclient> Client, uint64_t Id, const Poco::JSON::Object::Ptr Params);
static void DoReboot(std::shared_ptr<OWLSclient> Client, uint64_t Id, const Poco::JSON::Object::Ptr Params);
static void DoUpgrade(std::shared_ptr<OWLSclient> Client, uint64_t Id, const Poco::JSON::Object::Ptr Params);
static void DoFactory(std::shared_ptr<OWLSclient> Client, uint64_t Id, const Poco::JSON::Object::Ptr Params);
static void DoLEDs(std::shared_ptr<OWLSclient> Client, uint64_t Id, const Poco::JSON::Object::Ptr Params);
void UNsupportedCommand(std::shared_ptr<OWLSclient> Client, uint64_t Id, const std::string &Method);
static void DoConfigure(const std::shared_ptr<OWLSclient> &Client, uint64_t Id, Poco::JSON::Object::Ptr Params);
static void DoReboot(const std::shared_ptr<OWLSclient> &Client, uint64_t Id, Poco::JSON::Object::Ptr Params);
static void DoUpgrade(const std::shared_ptr<OWLSclient> &Client, uint64_t Id, Poco::JSON::Object::Ptr Params);
static void DoFactory(const std::shared_ptr<OWLSclient> &Client, uint64_t Id, Poco::JSON::Object::Ptr Params);
static void DoLEDs(const std::shared_ptr<OWLSclient> &Client, uint64_t Id, Poco::JSON::Object::Ptr Params);
void UnSupportedCommand(std::lock_guard<std::mutex> &G,const std::shared_ptr<OWLSclient> &Client, uint64_t Id, const std::string &Method);
using interface_location_t = std::tuple<ap_interface_types, std::string, radio_bands>;
using associations_map_t = std::map<interface_location_t, MockAssociations>;
@@ -93,18 +94,18 @@ namespace OpenWifi {
friend class SimulationRunner;
friend void OWLSclientEvents::EstablishConnection(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
friend void OWLSclientEvents::Reconnect(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
friend void OWLSclientEvents::Connect(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
friend void OWLSclientEvents::Log(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner, std::uint64_t Severity, const std::string & LogLine);
friend void OWLSclientEvents::State(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
friend void OWLSclientEvents::HealthCheck(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
friend void OWLSclientEvents::Update(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
friend void OWLSclientEvents::WSPing(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
friend void OWLSclientEvents::KeepAlive(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
friend void OWLSclientEvents::Disconnect(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner, const std::string &Reason, bool Reconnect);
friend void OWLSclientEvents::CrashLog(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
friend void OWLSclientEvents::PendingConfig(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
friend void OWLSClientEvents::EstablishConnection(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
friend void OWLSClientEvents::Reconnect(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
friend void OWLSClientEvents::Connect(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
friend void OWLSClientEvents::Log(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner, std::uint64_t Severity, const std::string & LogLine);
friend void OWLSClientEvents::State(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
friend void OWLSClientEvents::HealthCheck(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
friend void OWLSClientEvents::Update(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
friend void OWLSClientEvents::WSPing(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
friend void OWLSClientEvents::KeepAlive(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
friend void OWLSClientEvents::Disconnect(std::lock_guard<std::mutex> &g, const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner, const std::string &Reason, bool Reconnect);
friend void OWLSClientEvents::CrashLog(std::lock_guard<std::mutex> &g, const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
friend void OWLSClientEvents::PendingConfig(std::lock_guard<std::mutex> &g, const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
private:
// std::recursive_mutex Mutex_;

View File

@@ -11,17 +11,17 @@ namespace OpenWifi {
class SimulationRunner;
}
namespace OpenWifi::OWLSclientEvents {
void EstablishConnection(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
void Reconnect(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
void Connect(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
void State(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
void HealthCheck(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
void Log(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner, std::uint64_t Severity, const std::string & LogLine);
void WSPing(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
void Update(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
void KeepAlive(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
void Disconnect(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner, const std::string &Reason, bool Reconnect);
void CrashLog(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
void PendingConfig(std::shared_ptr<OWLSclient> Client, SimulationRunner *Runner);
namespace OpenWifi::OWLSClientEvents {
void EstablishConnection(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
void Reconnect(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
void Connect(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
void State(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
void HealthCheck(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
void Log(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner, std::uint64_t Severity, const std::string & LogLine);
void WSPing(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
void Update(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
void KeepAlive(const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
void Disconnect(std::lock_guard<std::mutex> &g, const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner, const std::string &Reason, bool Reconnect);
void CrashLog(std::lock_guard<std::mutex> &g, const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
void PendingConfig(std::lock_guard<std::mutex> &g, const std::shared_ptr<OWLSclient> &Client, SimulationRunner *Runner);
};

View File

@@ -1,202 +0,0 @@
//
// Created by stephane bourque on 2021-04-03.
//
#include <cstdlib>
#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<Bytef> 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

View File

@@ -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<OWLSclient> Client)
: Client_(std::move(Client)) {}
virtual bool Send() = 0;
protected:
bool SendObject(Poco::JSON::Object &Obj);
std::shared_ptr<OWLSclient> Client_;
};
class OWLSConnectEvent : public OWLSevent {
public:
explicit OWLSConnectEvent(std::shared_ptr<OWLSclient> Client)
: OWLSevent(std::move(Client)){};
bool Send() override;
private:
};
class OWLSStateEvent : public OWLSevent {
public:
explicit OWLSStateEvent(std::shared_ptr<OWLSclient> Client)
: OWLSevent(std::move(Client)){};
bool Send() override;
private:
};
class OWLSHealthCheckEvent : public OWLSevent {
public:
explicit OWLSHealthCheckEvent(std::shared_ptr<OWLSclient> Client)
: OWLSevent(std::move(Client)){};
bool Send() override;
private:
};
class OWLSLogEvent : public OWLSevent {
public:
explicit OWLSLogEvent(std::shared_ptr<OWLSclient> 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<OWLSclient> Client)
: OWLSevent(std::move(Client)){};
bool Send() override;
private:
};
class OWLSConfigChangePendingEvent : public OWLSevent {
public:
explicit OWLSConfigChangePendingEvent(std::shared_ptr<OWLSclient> Client)
: OWLSevent(std::move(Client)){};
bool Send() override;
private:
};
class OWLSKeepAliveEvent : public OWLSevent {
public:
explicit OWLSKeepAliveEvent(std::shared_ptr<OWLSclient> Client)
: OWLSevent(std::move(Client)){};
bool Send() override;
private:
};
class OWLSRebootEvent : public OWLSevent {
public:
explicit OWLSRebootEvent(std::shared_ptr<OWLSclient> Client)
: OWLSevent(std::move(Client)){};
bool Send() override;
private:
};
class OWLSDisconnectEvent : public OWLSevent {
public:
explicit OWLSDisconnectEvent(std::shared_ptr<OWLSclient> Client)
: OWLSevent(std::move(Client)){};
bool Send() override;
private:
};
class OWLSWSPingEvent : public OWLSevent {
public:
explicit OWLSWSPingEvent(std::shared_ptr<OWLSclient> Client)
: OWLSevent(std::move(Client)){};
bool Send() override;
private:
};
class OWLSUpdate : public OWLSevent {
public:
explicit OWLSUpdate(std::shared_ptr<OWLSclient> Client)
: OWLSevent(std::move(Client)){};
bool Send() override;
private:
};
} // namespace OpenWifi
#endif // UCENTRAL_CLNT_OWLSEVENT_H

View File

@@ -5,16 +5,13 @@
#include <thread>
#include <chrono>
#include "Poco/Logger.h"
#include "SimStats.h"
#include "SimulationRunner.h"
#include "OWLSevent.h"
#include "fmt/format.h"
#include "UI_Owls_WebSocketNotifications.h"
#include <Poco/Logger.h>
#include <Poco/Net/NetException.h>
#include <Poco/Net/SSLException.h>
#include <Poco/NObserver.h>
@@ -44,7 +41,7 @@ namespace OpenWifi {
auto Client = std::make_shared<OWLSclient>(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<std::mutex> 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<std::int64_t, std::shared_ptr<OWLSclient>>::iterator client_hint;
std::shared_ptr<OWLSclient> 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<SimulationRunner, Poco::Net::ReadableNotification>(
*this, &SimulationRunner::OnSocketReadable));
client->Reactor_.removeEventHandler(
*client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ErrorNotification>(
*this, &SimulationRunner::OnSocketError));
client->Reactor_.removeEventHandler(
*client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ShutdownNotification>(
*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<SimulationRunner, Poco::Net::ReadableNotification>(
*this, &SimulationRunner::OnSocketReadable));
client->Reactor_.removeEventHandler(
*client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ErrorNotification>(
*this, &SimulationRunner::OnSocketError));
client->Reactor_.removeEventHandler(
*client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ShutdownNotification>(
*this, &SimulationRunner::OnSocketShutdown));
client->fd_ = -1;
Clients_fd_.erase(socket);
if(Running_)
OWLSclientEvents::Reconnect(client,this);
}
void SimulationRunner::OnSocketShutdown(const Poco::AutoPtr<Poco::Net::ShutdownNotification> &pNf) {
std::lock_guard G(Mutex_);
auto socket = pNf->socket().impl()->sockfd();
std::map<std::int64_t, std::shared_ptr<OWLSclient>>::iterator client_hint;
std::shared_ptr<OWLSclient> 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<Poco::Net::ReadableNotification> &pNf) {
std::map<std::int64_t, std::shared_ptr<OWLSclient>>::iterator client_hint;
std::shared_ptr<OWLSclient> 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<OWLSclient> Client, Poco::JSON::Object::Ptr Frame) {
void SimulationRunner::ProcessCommand(std::lock_guard<std::mutex> &ClientGuard, const std::shared_ptr<OWLSclient> & 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);
}
}

View File

@@ -38,22 +38,23 @@ namespace OpenWifi {
const std::string & Id() const { return Id_; }
inline void AddClientFd(std::int64_t fd, std::shared_ptr<OWLSclient> c) {
std::lock_guard G(Mutex_);
inline void AddClientFd(std::int64_t fd, const std::shared_ptr<OWLSclient> &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<OWLSclient> Client, Poco::JSON::Object::Ptr Vars);
void ProcessCommand(std::lock_guard<std::mutex> &G, const std::shared_ptr<OWLSclient> &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_;