This commit is contained in:
stephb9959
2023-04-16 09:42:24 -07:00
8 changed files with 83 additions and 9 deletions

View File

@@ -26,6 +26,12 @@ namespace OpenWifi::OWLSclientEvents {
Runner->Reactor().removeEventHandler(
*Client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ReadableNotification>(
*Client->Runner_, &SimulationRunner::OnSocketReadable));
Runner->Reactor().removeEventHandler(
*Client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ErrorNotification>(
*Client->Runner_, &SimulationRunner::OnSocketError));
Runner->Reactor().removeEventHandler(
*Client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ShutdownNotification>(
*Client->Runner_, &SimulationRunner::OnSocketShutdown));
(*Client->WS_).close();
}
Client->Connected_ = false;

View File

@@ -73,6 +73,12 @@ namespace OpenWifi::OWLSclientEvents {
Runner->Reactor().addEventHandler(
*Client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ReadableNotification>(
*Runner, &SimulationRunner::OnSocketReadable));
Runner->Reactor().addEventHandler(
*Client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ErrorNotification>(
*Runner, &SimulationRunner::OnSocketError));
Runner->Reactor().addEventHandler(
*Client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ShutdownNotification>(
*Runner, &SimulationRunner::OnSocketShutdown));
Client->Connected_ = true;
Runner->AddClientFd(Client->WS_->impl()->sockfd(), Client);
Runner->Scheduler().in(std::chrono::seconds(1), Connect, Client, Runner);

View File

@@ -28,7 +28,7 @@ namespace OpenWifi::OWLSclientEvents {
M["params"]["sanity"] = 100;
M["params"]["data"] = P;
if (Client->Send(to_string(M))) {
if (Client->SendObject(M)) {
DEBUG_LINE("sent");
Runner->Scheduler().in(std::chrono::seconds(Client->HealthInterval_),
OWLSclientEvents::HealthCheck, Client, Runner);

View File

@@ -19,11 +19,11 @@ namespace OpenWifi::OWLSclientEvents {
if(Client->Valid_ && Client->Connected_) {
Runner->Report().ev_wsping++;
try {
if (Client->SendWSPing()) {
Runner->Scheduler().in(std::chrono::seconds(60 * 4),
OWLSclientEvents::WSPing, Client, Runner);
return;
}
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);
return;
} catch (const Poco::Exception &E) {
DEBUG_LINE("exception1");
Client->Logger().log(E);

View File

@@ -652,9 +652,9 @@ namespace OpenWifi {
return false;
}
bool OWLSclient::SendObject(nlohmann::json &O) {
bool OWLSclient::SendObject(const nlohmann::json &O) {
try {
auto M = to_string(O);
std::string M = to_string(O);
uint32_t BytesSent = WS_->sendFrame(M.c_str(), M.size());
if (BytesSent == M.size()) {
DEBUG_LINE("sent");

View File

@@ -33,10 +33,13 @@ namespace OpenWifi {
public:
OWLSclient(std::string SerialNumber,
Poco::Logger &Logger, SimulationRunner *runner);
~OWLSclient() {
std::cout << SerialNumber_ << ": Deleting client" << std::endl;
}
bool Send(const std::string &Cmd);
bool SendWSPing();
bool SendObject(nlohmann::json &O);
bool SendObject(const nlohmann::json &O);
void SetFirmware(const std::string &S = "sim-firmware-1") { Firmware_ = S; }

View File

@@ -17,6 +17,7 @@
#include <Poco/Net/NetException.h>
#include <Poco/Net/SSLException.h>
#include <Poco/NObserver.h>
namespace OpenWifi {
void SimulationRunner::Start() {
@@ -62,6 +63,62 @@ namespace OpenWifi {
}
}
void SimulationRunner::OnSocketError(const Poco::AutoPtr<Poco::Net::ErrorNotification> &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;
}
client = client_hint->second;
Clients_fd_.erase(socket);
Reactor_.removeEventHandler(
*client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ReadableNotification>(
*this, &SimulationRunner::OnSocketReadable));
Reactor_.removeEventHandler(
*client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ErrorNotification>(
*this, &SimulationRunner::OnSocketError));
Reactor_.removeEventHandler(
*client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ShutdownNotification>(
*this, &SimulationRunner::OnSocketShutdown));
client->fd_ = -1;
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;
}
client = client_hint->second;
Clients_fd_.erase(socket);
Reactor_.removeEventHandler(
*client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ReadableNotification>(
*this, &SimulationRunner::OnSocketReadable));
Reactor_.removeEventHandler(
*client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ErrorNotification>(
*this, &SimulationRunner::OnSocketError));
Reactor_.removeEventHandler(
*client->WS_, Poco::NObserver<SimulationRunner, Poco::Net::ShutdownNotification>(
*this, &SimulationRunner::OnSocketShutdown));
client->fd_ = -1;
if(Running_)
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;

View File

@@ -31,6 +31,8 @@ namespace OpenWifi {
CensusReport & Report() { return CensusReport_; }
void OnSocketReadable(const Poco::AutoPtr<Poco::Net::ReadableNotification> &pNf);
void OnSocketError(const Poco::AutoPtr<Poco::Net::ErrorNotification> &pNf);
void OnSocketShutdown(const Poco::AutoPtr<Poco::Net::ShutdownNotification> &pNf);
const std::string & Id() const { return Id_; }