Skip to content

Commit 75ebc07

Browse files
committed
https://telecominfraproject.atlassian.net/browse/WIFI-13172
Signed-off-by: stephb9959 <stephane.bourque@gmail.com>
1 parent d050635 commit 75ebc07

6 files changed

Lines changed: 104 additions & 76 deletions

File tree

src/framework/EventBusManager.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ namespace OpenWifi {
1616
KafkaManager()->PostMessage(KafkaTopics::SERVICE_EVENTS, MicroServicePrivateEndPoint(), Msg,
1717
false);
1818
while (Running_) {
19-
Poco::Thread::trySleep((unsigned long)MicroServiceDaemonBusTimer());
20-
if (!Running_)
21-
break;
19+
if(!Poco::Thread::trySleep((unsigned long)MicroServiceDaemonBusTimer())) {
20+
break;
21+
}
2222
Msg = (MicroServiceMakeSystemEventMessage(KafkaTopics::ServiceEvents::EVENT_KEEP_ALIVE));
2323
KafkaManager()->PostMessage(KafkaTopics::SERVICE_EVENTS, MicroServicePrivateEndPoint(),
2424
Msg, false);
@@ -29,19 +29,19 @@ namespace OpenWifi {
2929
};
3030

3131
void EventBusManager::Start() {
32-
poco_information(Logger(), "Starting...");
32+
poco_information(Logger_, "Starting...");
3333
if (KafkaManager()->Enabled()) {
3434
Thread_.start(*this);
3535
}
3636
}
3737

3838
void EventBusManager::Stop() {
3939
if (KafkaManager()->Enabled()) {
40-
poco_information(Logger(), "Stopping...");
40+
poco_information(Logger_, "Stopping...");
4141
Running_ = false;
4242
Thread_.wakeUp();
4343
Thread_.join();
44-
poco_information(Logger(), "Stopped...");
44+
poco_information(Logger_, "Stopped...");
4545
}
4646
}
4747

src/framework/EventBusManager.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ namespace OpenWifi {
2222
return instance_;
2323
}
2424

25-
explicit EventBusManager(Poco::Logger &L);
2625
void run() final;
2726
void Start();
2827
void Stop();

src/framework/MicroService.cpp

Lines changed: 81 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
#include "framework/WebSocketLogger.h"
3030
#include "framework/utils.h"
3131

32-
namespace OpenWifi {
32+
#ifdef USE_MEDUSA_CLIENT
33+
#include <medusa/MedusaClient.h>
34+
#endif
3335

34-
void MicroService::Exit(int Reason) { std::exit(Reason); }
36+
namespace OpenWifi {
3537

36-
static std::string MakeServiceListString(const Types::MicroServiceMetaMap &Services) {
38+
static std::string MakeServiceListString(const Types::MicroServiceMetaMap &Services) {
3739
std::string SvcList;
3840
for (const auto &Svc : Services) {
3941
if (SvcList.empty())
@@ -204,25 +206,29 @@ namespace OpenWifi {
204206
Res.push_back(ServiceRec);
205207
}
206208
return Res;
209+
207210
}
208211

209212
void MicroService::LoadConfigurationFile() {
210-
std::string Location = Poco::Environment::get(DAEMON_CONFIG_ENV_VAR, ".");
211-
ConfigFileName_ =
212-
ConfigFileName_.empty() ? Location + "/" + DAEMON_PROPERTIES_FILENAME : ConfigFileName_;
213-
Poco::Path ConfigFile(ConfigFileName_);
214-
215-
if (!ConfigFile.isFile()) {
216-
std::cerr << DAEMON_APP_NAME << ": Configuration " << ConfigFile.toString()
217-
<< " does not seem to exist. Please set " + DAEMON_CONFIG_ENV_VAR +
218-
" env variable the path of the " + DAEMON_PROPERTIES_FILENAME +
219-
" file."
220-
<< std::endl;
221-
std::exit(Poco::Util::Application::EXIT_CONFIG);
222-
}
223-
224-
// loadConfiguration(ConfigFile.toString());
225-
PropConfigurationFile_ = new Poco::Util::PropertyFileConfiguration(ConfigFile.toString());
213+
if(ConfigContent_.empty()) {
214+
std::string Location = Poco::Environment::get(DAEMON_CONFIG_ENV_VAR, ".");
215+
ConfigFileName_ =
216+
ConfigFileName_.empty() ? Location + "/" + DAEMON_PROPERTIES_FILENAME : ConfigFileName_;
217+
Poco::Path ConfigFile(ConfigFileName_);
218+
219+
if (!ConfigFile.isFile()) {
220+
std::cerr << DAEMON_APP_NAME << ": Configuration " << ConfigFile.toString()
221+
<< " does not seem to exist. Please set " + DAEMON_CONFIG_ENV_VAR +
222+
" env variable the path of the " + DAEMON_PROPERTIES_FILENAME +
223+
" file."
224+
<< std::endl;
225+
std::exit(Poco::Util::Application::EXIT_CONFIG);
226+
}
227+
PropConfigurationFile_ = new Poco::Util::PropertyFileConfiguration(ConfigFile.toString());
228+
} else {
229+
std::istringstream is(ConfigContent_);
230+
PropConfigurationFile_ = new Poco::Util::PropertyFileConfiguration(is);
231+
}
226232
configPtr()->addWriteable(PropConfigurationFile_, PRIO_DEFAULT);
227233
}
228234

@@ -425,49 +431,59 @@ namespace OpenWifi {
425431

426432
void DaemonPostInitialization(Poco::Util::Application &self);
427433

428-
void MicroService::initialize(Poco::Util::Application &self) {
429-
// add the default services
430-
LoadConfigurationFile();
431-
InitializeLoggingSystem();
432-
433-
SubSystems_.push_back(KafkaManager());
434-
SubSystems_.push_back(ALBHealthCheckServer());
435-
SubSystems_.push_back(RESTAPI_ExtServer());
436-
SubSystems_.push_back(RESTAPI_IntServer());
434+
void MicroService::StartEverything(Poco::Util::Application &self) {
435+
LoadConfigurationFile();
436+
InitializeLoggingSystem();
437+
438+
static bool InitializedBaseService=false;
439+
if(!InitializedBaseService) {
440+
InitializedBaseService = true;
441+
SubSystems_.push_back(KafkaManager());
442+
SubSystems_.push_back(ALBHealthCheckServer());
443+
SubSystems_.push_back(RESTAPI_ExtServer());
444+
SubSystems_.push_back(RESTAPI_IntServer());
437445
#ifndef TIP_SECURITY_SERVICE
438-
SubSystems_.push_back(AuthClient());
446+
SubSystems_.push_back(AuthClient());
439447
#endif
440-
Poco::Net::initializeSSL();
441-
Poco::Net::HTTPStreamFactory::registerFactory();
442-
Poco::Net::HTTPSStreamFactory::registerFactory();
443-
Poco::Net::FTPStreamFactory::registerFactory();
444-
Poco::Net::FTPSStreamFactory::registerFactory();
445-
446-
Poco::File DataDir(ConfigPath("openwifi.system.data"));
447-
DataDir_ = DataDir.path();
448-
if (!DataDir.exists()) {
449-
try {
450-
DataDir.createDirectory();
451-
} catch (const Poco::Exception &E) {
452-
Logger_.log(E);
453-
}
454-
}
455-
WWWAssetsDir_ = ConfigPath("openwifi.restapi.wwwassets", "");
456-
if (WWWAssetsDir_.empty())
457-
WWWAssetsDir_ = DataDir_;
458448

459-
LoadMyConfig();
449+
Poco::Net::initializeSSL();
450+
Poco::Net::HTTPStreamFactory::registerFactory();
451+
Poco::Net::HTTPSStreamFactory::registerFactory();
452+
Poco::Net::FTPStreamFactory::registerFactory();
453+
Poco::Net::FTPSStreamFactory::registerFactory();
454+
}
460455

461-
AllowExternalMicroServices_ = ConfigGetBool("allowexternalmicroservices", true);
456+
Poco::File DataDir(ConfigPath("openwifi.system.data"));
457+
DataDir_ = DataDir.path();
458+
if (!DataDir.exists()) {
459+
try {
460+
DataDir.createDirectory();
461+
} catch (const Poco::Exception &E) {
462+
Logger_.log(E);
463+
}
464+
}
465+
WWWAssetsDir_ = ConfigPath("openwifi.restapi.wwwassets", "");
466+
if (WWWAssetsDir_.empty())
467+
WWWAssetsDir_ = DataDir_;
462468

463-
InitializeSubSystemServers();
464-
ServerApplication::initialize(self);
465-
DaemonPostInitialization(self);
469+
LoadMyConfig();
466470

467-
Types::TopicNotifyFunction F = [this](const std::string &Key, const std::string &Payload) {
468-
this->BusMessageReceived(Key, Payload);
469-
};
470-
KafkaManager()->RegisterTopicWatcher(KafkaTopics::SERVICE_EVENTS, F);
471+
AllowExternalMicroServices_ = ConfigGetBool("allowexternalmicroservices", true);
472+
473+
InitializeSubSystemServers();
474+
ServerApplication::initialize(self);
475+
DaemonPostInitialization(self);
476+
477+
Types::TopicNotifyFunction F = [this](const std::string &Key, const std::string &Payload) {
478+
this->BusMessageReceived(Key, Payload);
479+
};
480+
KafkaManager()->RegisterTopicWatcher(KafkaTopics::SERVICE_EVENTS, F);
481+
}
482+
483+
void MicroService::initialize([[maybe_unused]] Poco::Util::Application &self) {
484+
#ifndef USE_MEDUSA_CLIENT
485+
StartEverything(self);
486+
#endif
471487
}
472488

473489
void MicroService::uninitialize() {
@@ -753,6 +769,8 @@ namespace OpenWifi {
753769
MicroServiceErrorHandler ErrorHandler(*this);
754770
Poco::ErrorHandler::set(&ErrorHandler);
755771

772+
Args_ = args;
773+
756774
if (!HelpRequested_) {
757775
SavePID();
758776

@@ -768,11 +786,18 @@ namespace OpenWifi {
768786
poco_information(logger, "Starting as a daemon.");
769787
}
770788

789+
#ifdef USE_MEDUSA_CLIENT
790+
MedusaClient::instance()->SetSubSystems(SubSystems_);
791+
MedusaClient::instance()->Start();
792+
waitForTerminationRequest();
793+
MedusaClient::instance()->Stop();
794+
#else
771795
poco_information(logger, fmt::format("System ID set to {}", ID_));
772796
StartSubSystemServers();
773797
waitForTerminationRequest();
774798
StopSubSystemServers();
775799
logger.notice(fmt::format("Stopped {}...", DAEMON_APP_NAME));
800+
#endif
776801
}
777802

778803
return Application::EXIT_OK;

src/framework/MicroService.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ namespace OpenWifi {
5555
#include "nlohmann/json.hpp"
5656
#include "ow_version.h"
5757

58-
#define _OWDEBUG_ std::cout << __FILE__ << ":" << __LINE__ << std::endl;
59-
// #define _OWDEBUG_ Logger().debug(Poco::format("%s: %lu",__FILE__,__LINE__));
60-
6158
namespace OpenWifi {
6259

6360
class MicroService : public Poco::Util::ServerApplication {
@@ -70,7 +67,6 @@ namespace OpenWifi {
7067
SubSystems_(std::move(Subsystems)), Logger_(Poco::Logger::get("FRAMEWORK")) {
7168
instance_ = this;
7269
RandomEngine_.seed(std::chrono::steady_clock::now().time_since_epoch().count());
73-
// Logger_ = Poco::Logger::root().get("BASE-SVC");
7470
}
7571

7672
inline static const char *ExtraConfigurationFilename = "/configuration_override.json";
@@ -92,7 +88,7 @@ namespace OpenWifi {
9288
inline uint64_t DaemonBusTimer() const { return DAEMON_BUS_TIMER; };
9389
[[nodiscard]] const std::string &AppName() { return DAEMON_APP_NAME; }
9490
static inline uint64_t GetPID() { return Poco::Process::id(); };
95-
[[nodiscard]] inline const std::string GetPublicAPIEndPoint() {
91+
[[nodiscard]] inline std::string GetPublicAPIEndPoint() const {
9692
return MyPublicEndPoint_ + "/api/v1";
9793
};
9894
[[nodiscard]] inline const std::string &GetUIURI() const { return UIURI_; };
@@ -107,14 +103,16 @@ namespace OpenWifi {
107103
}
108104
static MicroService &instance() { return *instance_; }
109105

110-
inline void Exit(int Reason);
106+
inline void Exit(int Reason) { std::exit(Reason); }
107+
111108
void BusMessageReceived(const std::string &Key, const std::string &Payload);
112109
Types::MicroServiceMetaVec GetServices(const std::string &Type);
113110
Types::MicroServiceMetaVec GetServices();
114111
void LoadConfigurationFile();
115112
void Reload();
116113
void LoadMyConfig();
117114
void initialize(Poco::Util::Application &self) override;
115+
void StartEverything(Poco::Util::Application &self);
118116
void uninitialize() override;
119117
void reinitialize(Poco::Util::Application &self) override;
120118
void defineOptions(Poco::Util::OptionSet &options) override;
@@ -132,7 +130,7 @@ namespace OpenWifi {
132130
void Reload(const std::string &Sub);
133131
Types::StringVec GetSubSystems() const;
134132
Types::StringPairVec GetLogLevels();
135-
const Types::StringVec &GetLogLevelNames();
133+
static const Types::StringVec &GetLogLevelNames();
136134
uint64_t ConfigGetInt(const std::string &Key, uint64_t Default);
137135
uint64_t ConfigGetInt(const std::string &Key);
138136
uint64_t ConfigGetBool(const std::string &Key, bool Default);
@@ -166,12 +164,16 @@ namespace OpenWifi {
166164
const std::string &FormatterPattern,
167165
const std::string &root_env_var);
168166
inline bool AllowExternalMicroServices() const { return AllowExternalMicroServices_; }
167+
const ArgVec &Args() const { return Args_; }
168+
169+
inline void SetConfigContent(const std::string &Content) { ConfigContent_ = Content; }
169170

170171
private:
171172
static MicroService *instance_;
172173
bool HelpRequested_ = false;
173174
std::string LogDir_;
174175
std::string ConfigFileName_;
176+
std::string ConfigContent_;
175177
uint64_t ID_ = 1;
176178
Poco::SharedPtr<Poco::Crypto::RSAKey> AppKey_;
177179
bool DebugMode_ = false;
@@ -201,6 +203,7 @@ namespace OpenWifi {
201203
Poco::JWT::Signer Signer_;
202204
Poco::Logger &Logger_;
203205
Poco::ThreadPool TimerPool_{"timer:pool", 2, 32};
206+
ArgVec Args_;
204207
};
205208

206209
inline MicroService *MicroService::instance_ = nullptr;

src/framework/UI_WebSocketClientServer.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,9 @@ namespace OpenWifi {
5858
void UI_WebSocketClientServer::run() {
5959
Running_ = true;
6060
while (Running_) {
61-
Poco::Thread::trySleep(2000);
62-
63-
if (!Running_)
64-
break;
65-
61+
if(!Poco::Thread::trySleep(2000)) {
62+
break;
63+
}
6664
std::lock_guard G(LocalMutex_);
6765
for (const auto i : ToBeRemoved_) {
6866
// std::cout << "Erasing old WS UI connection..." << std::endl;

src/framework/ow_constants.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ namespace OpenWifi::RESTAPI::Protocol {
565565

566566
static const char *TRANSFER = "transfer";
567567
static const char *CERTUPDATE = "certupdate";
568+
static const char *POWERCYCLE = "powercycle";
568569
static const char *RRM = "rrm";
569570

570571
static const char *REQUIREMENTS = "requirements";
@@ -687,6 +688,7 @@ namespace OpenWifi::uCentralProtocol {
687688

688689
static const char *TRANSFER = "transfer";
689690
static const char *CERTUPDATE = "certupdate";
691+
static const char *POWERCYCLE = "powercycle";
690692
static const char *RRM = "rrm";
691693
static const char *ACTIONS = "actions";
692694

@@ -785,6 +787,7 @@ namespace OpenWifi::APCommands {
785787
rrm,
786788
certupdate,
787789
transfer,
790+
powercycle,
788791
unknown
789792
};
790793

@@ -799,7 +802,7 @@ namespace OpenWifi::APCommands {
799802
RESTAPI::Protocol::EVENTQUEUE, RESTAPI::Protocol::TELEMETRY,
800803
RESTAPI::Protocol::PING, RESTAPI::Protocol::SCRIPT,
801804
RESTAPI::Protocol::RRM, RESTAPI::Protocol::CERTUPDATE,
802-
RESTAPI::Protocol::TRANSFER
805+
RESTAPI::Protocol::TRANSFER, RESTAPI::Protocol::POWERCYCLE
803806
};
804807

805808
inline const char *to_string(Commands Cmd) { return uCentralAPCommands[(uint8_t)Cmd]; }

0 commit comments

Comments
 (0)