2019-12-14 22:57:04 +00:00
|
|
|
//
|
|
|
|
// Created by loki on 12/14/19.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef SUNSHINE_PROCESS_H
|
|
|
|
#define SUNSHINE_PROCESS_H
|
|
|
|
|
|
|
|
#include <unordered_map>
|
2019-12-15 18:36:22 +00:00
|
|
|
#include <optional>
|
2019-12-14 22:57:04 +00:00
|
|
|
|
|
|
|
#include <boost/process.hpp>
|
2019-12-15 18:36:22 +00:00
|
|
|
|
2019-12-14 22:57:04 +00:00
|
|
|
#include "utility.h"
|
|
|
|
|
|
|
|
namespace proc {
|
|
|
|
using file_t = util::safe_ptr_v2<FILE, int, fclose>;
|
|
|
|
|
|
|
|
struct cmd_t {
|
2019-12-15 13:30:00 +00:00
|
|
|
cmd_t(std::string &&do_cmd, std::string &&undo_cmd) : do_cmd(std::move(do_cmd)), undo_cmd(std::move(undo_cmd)) {}
|
|
|
|
explicit cmd_t(std::string &&do_cmd) : do_cmd(std::move(do_cmd)) {}
|
|
|
|
|
2019-12-14 22:57:04 +00:00
|
|
|
std::string do_cmd;
|
|
|
|
|
|
|
|
// Executed when proc_t has finished running, meant to reverse 'do_cmd' if applicable
|
|
|
|
std::string undo_cmd;
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
* pre_cmds -- guaranteed to be executed unless any of the commands fail.
|
|
|
|
* cmd -- Runs indefinitely until:
|
|
|
|
* No session is running and a different set of commands it to be executed
|
|
|
|
* Command exits
|
|
|
|
* cmd_output --
|
|
|
|
* empty -- The output of the commands are appended to the output of sunshine
|
|
|
|
* "null" -- The output of the commands are discarded
|
|
|
|
* filename -- The output of the commands are appended to filename
|
|
|
|
*/
|
|
|
|
struct ctx_t {
|
2019-12-15 13:30:00 +00:00
|
|
|
std::vector<cmd_t> prep_cmds;
|
2019-12-14 22:57:04 +00:00
|
|
|
|
|
|
|
std::string cmd;
|
2019-12-15 13:30:00 +00:00
|
|
|
std::string output;
|
2019-12-14 22:57:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class proc_t {
|
|
|
|
public:
|
2019-12-15 18:36:22 +00:00
|
|
|
KITTY_DEFAULT_CONSTR_THROW(proc_t)
|
2019-12-14 22:57:04 +00:00
|
|
|
|
|
|
|
proc_t(
|
|
|
|
boost::process::environment &&env,
|
|
|
|
std::unordered_map<std::string, ctx_t> &&name_to_proc) :
|
|
|
|
_env(std::move(env)),
|
|
|
|
_name_to_proc(std::move(name_to_proc)) {}
|
|
|
|
|
|
|
|
int execute(const std::string &name);
|
|
|
|
bool running();
|
|
|
|
|
|
|
|
~proc_t();
|
|
|
|
|
2019-12-15 18:36:22 +00:00
|
|
|
const std::unordered_map<std::string, ctx_t> &get_apps() const;
|
2019-12-25 22:41:46 +00:00
|
|
|
void terminate();
|
2019-12-14 22:57:04 +00:00
|
|
|
|
2019-12-25 22:41:46 +00:00
|
|
|
private:
|
2019-12-14 22:57:04 +00:00
|
|
|
boost::process::environment _env;
|
|
|
|
std::unordered_map<std::string, ctx_t> _name_to_proc;
|
|
|
|
|
|
|
|
boost::process::child _process;
|
2019-12-15 21:47:55 +00:00
|
|
|
boost::process::group _process_handle;
|
|
|
|
|
2019-12-14 22:57:04 +00:00
|
|
|
file_t _pipe;
|
|
|
|
std::vector<cmd_t>::const_iterator _undo_it;
|
|
|
|
std::vector<cmd_t>::const_iterator _undo_begin;
|
|
|
|
};
|
|
|
|
|
2019-12-15 18:36:22 +00:00
|
|
|
void refresh(const std::string &file_name);
|
|
|
|
std::optional<proc::proc_t> parse(const std::string& file_name);
|
|
|
|
|
|
|
|
extern proc_t proc;
|
2019-12-14 22:57:04 +00:00
|
|
|
}
|
|
|
|
#endif //SUNSHINE_PROCESS_H
|