Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
Exit indefinte wait loop if conference ends (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
djova-dolby authored Aug 29, 2023
1 parent 1a1a5d4 commit 2abda3b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions demo-content/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def stop_injection_process(folder):
injector = open(folder + "/pid", "r")
pid = injector.read()
os.kill(int(pid), signal.SIGTERM)
else:
print("The PID file has already been removed, app is not running")

def parse_injection_input():
'''
Expand Down
4 changes: 3 additions & 1 deletion src/linux/daemonize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ daemonize::daemonize(const std::string &log_dir) {
open("/dev/null", O_WRONLY);

pid_ = getpid();
std::ofstream pid_file(log_dir + "/pid");
pid_file_ = log_dir + "/pid";
std::ofstream pid_file(pid_file_);
pid_file << std::to_string(pid_);
}

daemonize::~daemonize() {
close(0);
close(1);
close(2);
remove(pid_file_.c_str());
}

void daemonize::wait_indefinitely() {
Expand Down
1 change: 1 addition & 0 deletions src/linux/daemonize.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class daemonize {
private:
void parent_exit(pid_t pid);
pid_t pid_{-1};
std::string pid_file_{};
sem_t semaphore_{};
};

Expand Down
20 changes: 17 additions & 3 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "utils/async_accumulator.h"
#include "utils/commands_handler.h"

#include <memory>
#include <vector>

using namespace dolbyio::comms::sample;
Expand All @@ -23,9 +24,8 @@ using namespace dolbyio::comms::sample;
#include <execinfo.h>
#include <signal.h>

// Global unique_ptr for linux daemonization, because I was too lazy
// to read sigaction and setting context for handler ;)
std::unique_ptr<daemonize> daemonize_ptr{nullptr};
// Global pointer for linux daemonization
std::shared_ptr<daemonize> daemonize_ptr{nullptr};

void signal_handler(int sig) {
if (sig == SIGTERM && daemonize_ptr)
Expand Down Expand Up @@ -120,7 +120,21 @@ int main(int argc, char** argv) {

// Run blocking loop
#if defined(__linux__)
// If the conference has ended then we should unblock the loop.
// Not ideal because you could techinically race here with signal
// handler, but for our purposes it should be ok.
std::weak_ptr<daemonize> weak_daemoize_ptr{daemonize_ptr};
sdk->conference()
.add_event_handler(
[weak_daemoize_ptr](
const dolbyio::comms::conference_status_updated& status) {
if (status.is_ended() && !weak_daemoize_ptr.expired()) {
weak_daemoize_ptr.lock()->unblock_indefinte_wait();
}
})
.on_error([](auto&&) {});
daemonize_ptr->wait_indefinitely();
daemonize_ptr.reset();
#else
while (!quit) {
command_handler.print_interactive_options();
Expand Down

0 comments on commit 2abda3b

Please sign in to comment.