Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/control_application/control.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@

#include <cstddef>

struct RunTargetInfo {
struct RunTargetInfo
{
char runTargetName[1024]{};
};

static constexpr char const* control_socket_path = "/sm_control";
static constexpr const char* control_socket_path = "/sm_control";
static constexpr std::size_t control_socket_capacity = 32;

#endif
15 changes: 10 additions & 5 deletions examples/control_application/control_app_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,33 @@
********************************************************************************/
#include <iostream>

#include "ipc_dropin/socket.hpp"
#include "control.hpp"
#include "ipc_dropin/socket.hpp"

int main(int argc, char** argv)
{
if(argc <= 1) {
if (argc <= 1)
{
std::cout << "Usage: " << argv[0] << " MyRunTargetName" << std::endl;
return EXIT_FAILURE;
}

ipc_dropin::Socket<static_cast<size_t>(sizeof(RunTargetInfo)), control_socket_capacity> sm_control_socket{};
if (sm_control_socket.connect(control_socket_path) != ipc_dropin::ReturnCode::kOk) {
if (sm_control_socket.connect(control_socket_path) != ipc_dropin::ReturnCode::kOk)
{
std::cerr << "Could not connect to control socket" << std::endl;
return EXIT_FAILURE;
}

RunTargetInfo info{};
std::strncpy(info.runTargetName, argv[1], sizeof(info.runTargetName) - 1);
if(ipc_dropin::ReturnCode::kOk == sm_control_socket.trySend(info)) {
if (ipc_dropin::ReturnCode::kOk == sm_control_socket.trySend(info))
{
std::cout << "Successfully sent request" << std::endl;
return EXIT_SUCCESS;
} else {
}
else
{
std::cerr << "Request could not be sent" << std::endl;
return EXIT_FAILURE;
}
Expand Down
43 changes: 27 additions & 16 deletions examples/control_application/control_daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,60 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
#include <atomic>
#include <csignal>
#include <thread>
#include <chrono>
#include <csignal>
#include <iostream>
#include <thread>

#include <score/mw/lifecycle/report_running.h>
#include <score/mw/lifecycle/control_client.h>
#include "ipc_dropin/socket.hpp"
#include "control.hpp"
#include "ipc_dropin/socket.hpp"
#include <score/mw/lifecycle/control_client.h>
#include <score/mw/lifecycle/report_running.h>

std::atomic<bool> exitRequested{false};
void signalHandler(int) {
void signalHandler(int)
{
exitRequested = true;
}

int main(int argc, char** argv) {
int main(int argc, char** argv)
{
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);

score::mw::lifecycle::report_running();

ipc_dropin::Socket<static_cast<size_t>(sizeof(RunTargetInfo)), control_socket_capacity> sm_control_socket{};
if (sm_control_socket.create(control_socket_path, 600) != ipc_dropin::ReturnCode::kOk) {
if (sm_control_socket.create(control_socket_path, 600) != ipc_dropin::ReturnCode::kOk)
{
std::cerr << "Could not create control socket" << std::endl;
return EXIT_FAILURE;
}

score::mw::lifecycle::ControlClient client;

score::safecpp::Scope<> scope{};
while (!exitRequested) {
while (!exitRequested)
{
RunTargetInfo info{};
if (ipc_dropin::ReturnCode::kOk == sm_control_socket.tryReceive(info)) {
if (ipc_dropin::ReturnCode::kOk == sm_control_socket.tryReceive(info))
{

std::string runTargetName{info.runTargetName};
std::cout << "Activating Run Target: " << runTargetName << std::endl;
client.ActivateRunTarget(runTargetName).Then({scope, [runTargetName](auto& result) noexcept {
if (!result) {
std::cerr << "Activating Run Target " << runTargetName << " failed with error: " << result.error().Message() << std::endl;
} else {
std::cout << "Activating Run Target " << runTargetName << " succeeded" << std::endl;
}
}});
if (!result)
{
std::cerr << "Activating Run Target " << runTargetName
<< " failed with error: "
<< result.error().Message() << std::endl;
}
else
{
std::cout << "Activating Run Target " << runTargetName
<< " succeeded" << std::endl;
}
}});
}

std::this_thread::sleep_for(std::chrono::milliseconds(100));
Expand Down
98 changes: 47 additions & 51 deletions examples/cpp_lifecycle_app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

#include <optional>
#include <unistd.h>
#include <iostream>
#include <chrono>
#include <ctime>
#include <iostream>
#include <optional>
#include <string>
#include <chrono>
#include <vector>

#ifdef __linux__
#include <linux/prctl.h>
#include <sys/prctl.h>
#include <linux/prctl.h>
#include <sys/prctl.h>
#endif

#include <score/mw/lifecycle/application.h>
Expand All @@ -31,55 +31,55 @@
struct Config
{
std::int32_t responseTimeInMs{100};
bool crashRequested{false};
bool crashRequested{false};
std::int32_t crashTimeInMs{1000};
bool failToStart{false};
bool verbose{false};
bool failToStart{false};
bool verbose{false};
};

std::string helpSstring =
"Usage:\n\
"Usage:\n\
-r <response time in ms> Worst case response time to SIGTERM signal in milliseconds.\n\
-c <crash time in ms> Simulate crash of the application, after specified time in milliseconds.\n\
-s Simulate failure during start-up of the application.\n\
-v Run in verbose mode.\n";

std::optional<Config> parseOptions(int argc, char *const *argv) noexcept
std::optional<Config> parseOptions(int argc, char* const* argv) noexcept
{
Config config{};
int c;
while ((c = getopt(argc, argv, ":r:c:svh")) != -1)
{
switch (static_cast<char>(c))
{
case 'r':
config.responseTimeInMs = std::stoi(optarg);
break;

case 'c':
config.crashRequested = true;
config.crashTimeInMs = std::stoi(optarg);
break;

case 's':
config.failToStart = true;
break;

case 'h':
std::cout << helpSstring;
return std::nullopt;

case 'v':
config.verbose = true;
break;

case '?':
std::cout << "Unrecognized option: -" << static_cast<char>(optopt) << std::endl;
std::cout << helpSstring;
return std::nullopt;

default:
break;
case 'r':
config.responseTimeInMs = std::stoi(optarg);
break;

case 'c':
config.crashRequested = true;
config.crashTimeInMs = std::stoi(optarg);
break;

case 's':
config.failToStart = true;
break;

case 'h':
std::cout << helpSstring;
return std::nullopt;

case 'v':
config.verbose = true;
break;

case '?':
std::cout << "Unrecognized option: -" << static_cast<char>(optopt) << std::endl;
std::cout << helpSstring;
return std::nullopt;

default:
break;
}
}
return config;
Expand All @@ -90,23 +90,23 @@ void set_process_name()
const char* identifier = getenv("PROCESSIDENTIFIER");
if (identifier != nullptr)
{
#ifdef __QNXNTO__
#ifdef __QNXNTO__
if (pthread_setname_np(pthread_self(), identifier) != 0)
{
std::cerr << "Failed to set QNX thread name" << std::endl;
}
#elif defined(__linux__)
#elif defined(__linux__)
if (prctl(PR_SET_NAME, identifier) < 0)
{
std::cerr << "Failed to set process name to " << identifier << std::endl;
}
#endif
#endif
}
}

class LifecycleApp final : public score::mw::lifecycle::Application
{
public:
public:
std::int32_t Initialize(const score::mw::lifecycle::ApplicationContext& appCtx) override
{
set_process_name();
Expand Down Expand Up @@ -156,10 +156,8 @@ class LifecycleApp final : public score::mw::lifecycle::Application
std::chrono::time_point<std::chrono::steady_clock> startTime = std::chrono::steady_clock::now();
std::chrono::duration<double, std::milli> runTime;

timespec req{
static_cast<time_t>(m_config.responseTimeInMs / 1000),
static_cast<long>((m_config.responseTimeInMs % 1000) * 1000000L)
};
timespec req{static_cast<time_t>(m_config.responseTimeInMs / 1000),
static_cast<long>((m_config.responseTimeInMs % 1000) * 1000000L)};

auto timeLastVerboseLog = std::chrono::steady_clock::now();

Expand All @@ -174,10 +172,8 @@ class LifecycleApp final : public score::mw::lifecycle::Application
{
if (timeTillCrash > 0)
{
timespec crash_req{
static_cast<time_t>(timeTillCrash / 1000),
static_cast<long>((timeTillCrash % 1000) * 1000000L)
};
timespec crash_req{static_cast<time_t>(timeTillCrash / 1000),
static_cast<long>((timeTillCrash % 1000) * 1000000L)};
nanosleep(&crash_req, nullptr);
}

Expand All @@ -201,7 +197,7 @@ class LifecycleApp final : public score::mw::lifecycle::Application
return EXIT_SUCCESS;
}

private:
private:
Config m_config{};
std::vector<char*> m_argvStorage{};
};
Expand Down
4 changes: 2 additions & 2 deletions examples/cpp_supervised_app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
#include <sys/prctl.h>
#endif

#include <score/mw/lifecycle/report_running.h>
#include <score/mw/log/rust/stdout_logger_init.h>
#include <score/mw/health/common.h>
#include <score/mw/health/health_monitor.h>
#include <score/mw/lifecycle/report_running.h>
#include <score/mw/log/rust/stdout_logger_init.h>
#include <thread>

/// @brief CLI configuration options for the demo_application process
Expand Down
2 changes: 1 addition & 1 deletion score/health_monitor/src/cpp/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
#include <score/assert.hpp>
#include "score/mw/health/common.h"
#include <score/assert.hpp>

namespace score::mw::health::internal
{
Expand Down
2 changes: 1 addition & 1 deletion score/health_monitor/src/cpp/deadline/deadline_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#ifndef SCORE_HM_DEADLINE_DEADLINE_MONITOR_H
#define SCORE_HM_DEADLINE_DEADLINE_MONITOR_H

#include <score/expected.hpp>
#include "score/mw/health/common.h"
#include "score/mw/health/tag.h"
#include <score/expected.hpp>
#include <functional>
#include <optional>

Expand Down
9 changes: 6 additions & 3 deletions score/health_monitor/src/cpp/heartbeat/heartbeat_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
********************************************************************************/
#include "score/mw/health/heartbeat_monitor.h"

namespace {
namespace
{
extern "C" {
using namespace score::mw::health;
using namespace score::mw::health::internal;
using namespace score::mw::health::heartbeat;

FFICode heartbeat_monitor_builder_create(uint32_t range_min_ms, uint32_t range_max_ms, FFIHandle* heartbeat_monitor_builder_handle_out);
FFICode heartbeat_monitor_builder_create(uint32_t range_min_ms,
uint32_t range_max_ms,
FFIHandle* heartbeat_monitor_builder_handle_out);
FFICode heartbeat_monitor_builder_destroy(FFIHandle heartbeat_monitor_builder_handle);
FFICode heartbeat_monitor_destroy(FFIHandle heartbeat_monitor_builder_handle);
FFICode heartbeat_monitor_heartbeat(FFIHandle heartbeat_monitor_builder_handle);
Expand All @@ -32,7 +35,7 @@ FFIHandle heartbeat_monitor_builder_create_wrapper(uint32_t range_min_ms, uint32
return handle;
}

}
} // namespace

namespace score::mw::health::heartbeat
{
Expand Down
2 changes: 1 addition & 1 deletion score/health_monitor/src/cpp/heartbeat/heartbeat_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#ifndef SCORE_HM_HEARTBEAT_HEARTBEAT_MONITOR_H
#define SCORE_HM_HEARTBEAT_HEARTBEAT_MONITOR_H

#include <score/expected.hpp>
#include "score/mw/health/common.h"
#include <score/expected.hpp>

namespace score::mw::health
{
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/complex_monitoring/control_client_mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@
#include <score/mw/lifecycle/control_client.h>
#include <score/mw/lifecycle/report_running.h>


TEST(ComplexMonitoring, ControlClientMock)
{
score::mw::lifecycle::ControlClient client;

ASSERT_TRUE(check_clean({test_end_location, fallback_file}));

TEST_STEP("Report running")
{
score::mw::lifecycle::report_running();
}

TEST_STEP("Launch monitored process")
{
score::cpp::stop_token stop_token;
Expand Down
Loading
Loading