load apps contexts into memory from a JSON file
This commit is contained in:
@@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"env":[
|
||||||
|
{ "VAR":"VAL", "VAR2":"VAL2" }
|
||||||
|
],
|
||||||
|
"apps":[
|
||||||
|
{
|
||||||
|
"name":"echo-1",
|
||||||
|
|
||||||
|
"output":"output.txt",
|
||||||
|
"cmd":"sh -c \"echo $VAR\"",
|
||||||
|
"prep-cmd":[
|
||||||
|
{ "do":"echo pre-1", "undo":"echo post-1" },
|
||||||
|
{ "do":"echo pre-2" },
|
||||||
|
{ "do":"echo pre-3", "undo":"echo post-3" }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name":"echo-2",
|
||||||
|
|
||||||
|
"output":"output.txt",
|
||||||
|
"cmd":"sleep 10",
|
||||||
|
"prep-cmd":[
|
||||||
|
{ "do":"echo pre-1", "undo":"echo post-1" },
|
||||||
|
{ "do":"echo pre-2" },
|
||||||
|
{ "do":"echo pre-3", "undo":"echo post-3" }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -12,7 +12,7 @@
|
|||||||
unique_id = 03904e64-51da-4fb3-9afd-a9f7ff70fea4
|
unique_id = 03904e64-51da-4fb3-9afd-a9f7ff70fea4
|
||||||
|
|
||||||
# The file where info on paired devices is stored
|
# The file where info on paired devices is stored
|
||||||
file_devices = devices.xml
|
file_devices = devices.json
|
||||||
|
|
||||||
# How long to wait in milliseconds for data from moonlight before shutting down the stream
|
# How long to wait in milliseconds for data from moonlight before shutting down the stream
|
||||||
ping_timeout = 2000
|
ping_timeout = 2000
|
||||||
|
|||||||
+1
-2
@@ -1,6 +1,5 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <streambuf>
|
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
@@ -38,7 +37,7 @@ nvhttp_t nvhttp {
|
|||||||
CERTIFICATE_FILE,
|
CERTIFICATE_FILE,
|
||||||
|
|
||||||
"03904e64-51da-4fb3-9afd-a9f7ff70fea4", // unique_id
|
"03904e64-51da-4fb3-9afd-a9f7ff70fea4", // unique_id
|
||||||
"devices.xml" // file_devices
|
"devices.json" // file_devices
|
||||||
};
|
};
|
||||||
|
|
||||||
bool whitespace(char ch) {
|
bool whitespace(char ch) {
|
||||||
|
|||||||
+73
-17
@@ -6,38 +6,94 @@
|
|||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <boost/property_tree/ptree.hpp>
|
||||||
|
#include <boost/property_tree/json_parser.hpp>
|
||||||
|
|
||||||
#include "nvhttp.h"
|
#include "nvhttp.h"
|
||||||
#include "stream.h"
|
#include "stream.h"
|
||||||
|
#include "config.h"
|
||||||
|
#include "process.h"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include <rs.h>
|
#include <rs.h>
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "config.h"
|
|
||||||
|
|
||||||
#include "process.h"
|
|
||||||
|
|
||||||
using namespace std::literals;
|
using namespace std::literals;
|
||||||
|
namespace pt = boost::property_tree;
|
||||||
|
|
||||||
|
std::optional<proc::proc_t> parse(const std::string& file_name) {
|
||||||
|
pt::ptree tree;
|
||||||
|
|
||||||
|
try {
|
||||||
|
pt::read_json(file_name, tree);
|
||||||
|
|
||||||
|
auto &apps_node = tree.get_child("apps"s);
|
||||||
|
auto &env_vars = tree.get_child("env"s);
|
||||||
|
|
||||||
|
boost::process::environment env = boost::this_process::environment();
|
||||||
|
|
||||||
|
std::unordered_map<std::string, proc::ctx_t> apps;
|
||||||
|
for(auto &[_,app_node] : apps_node) {
|
||||||
|
proc::ctx_t ctx;
|
||||||
|
|
||||||
|
auto &prep_nodes = app_node.get_child("prep-cmd"s);
|
||||||
|
auto name = app_node.get<std::string>("name"s);
|
||||||
|
auto output = app_node.get_optional<std::string>("output"s);
|
||||||
|
auto cmd = app_node.get<std::string>("cmd"s);
|
||||||
|
|
||||||
|
std::vector<proc::cmd_t> prep_cmds;
|
||||||
|
prep_cmds.reserve(prep_nodes.size());
|
||||||
|
for(auto &[_, prep_node] : prep_nodes) {
|
||||||
|
auto do_cmd = prep_node.get<std::string>("do"s);
|
||||||
|
auto undo_cmd = prep_node.get_optional<std::string>("undo"s);
|
||||||
|
|
||||||
|
if(undo_cmd) {
|
||||||
|
prep_cmds.emplace_back(std::move(do_cmd), std::move(*undo_cmd));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
prep_cmds.emplace_back(std::move(do_cmd));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(output) {
|
||||||
|
ctx.output = std::move(*output);
|
||||||
|
}
|
||||||
|
ctx.cmd = std::move(cmd);
|
||||||
|
ctx.prep_cmds = std::move(prep_cmds);
|
||||||
|
|
||||||
|
apps.emplace(std::move(name), std::move(ctx));
|
||||||
|
}
|
||||||
|
|
||||||
|
for(auto &[_,env_var] : env_vars) {
|
||||||
|
for(auto &[name,val] : env_var) {
|
||||||
|
env[name] = val.get_value<std::string>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return proc::proc_t {
|
||||||
|
std::move(env), std::move(apps)
|
||||||
|
};
|
||||||
|
} catch (std::exception &e) {
|
||||||
|
std::cout << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
std::vector<proc::cmd_t> pre_cmds {
|
auto proc_opt = parse(SUNSHINE_ASSETS_DIR "/apps.json");
|
||||||
{ "echo pre-1", "echo post-1" },
|
|
||||||
{ "echo pre-2", "" },
|
|
||||||
{ "echo pre-3", "echo post-3" }
|
|
||||||
};
|
|
||||||
|
|
||||||
std::unordered_map<std::string, proc::ctx_t> map {
|
if(!proc_opt) {
|
||||||
{ "echo", { std::move(pre_cmds), R"(echo \"middle\")", "output.txt" } }
|
return 7;
|
||||||
};
|
}
|
||||||
|
|
||||||
boost::process::environment env = boost::this_process::environment();
|
auto &proc = *proc_opt;
|
||||||
proc::proc_t proc(std::move(env), std::move(map));
|
|
||||||
|
|
||||||
proc.execute("echo"s);
|
|
||||||
|
|
||||||
|
proc.execute("echo-2");
|
||||||
std::this_thread::sleep_for(50ms);
|
std::this_thread::sleep_for(50ms);
|
||||||
|
|
||||||
proc.execute("echo"s);
|
proc.execute("echo-1");
|
||||||
|
|
||||||
std::this_thread::sleep_for(50ms);
|
std::this_thread::sleep_for(50ms);
|
||||||
return proc.running();
|
return proc.running();
|
||||||
|
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ std::string get_local_ip(int family) {
|
|||||||
std::string get_local_ip() { return get_local_ip(AF_INET); }
|
std::string get_local_ip() { return get_local_ip(AF_INET); }
|
||||||
|
|
||||||
void interrupt_process(std::uint64_t handle) {
|
void interrupt_process(std::uint64_t handle) {
|
||||||
kill((pid_t)handle, SIGTERM);
|
kill((pid_t)handle, SIGINT);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct display_attr_t {
|
struct display_attr_t {
|
||||||
|
|||||||
@@ -50,11 +50,11 @@ int proc_t::execute(const std::string &name) {
|
|||||||
|
|
||||||
auto &proc = it->second;
|
auto &proc = it->second;
|
||||||
|
|
||||||
_undo_begin = std::begin(proc.pre_cmds);
|
_undo_begin = std::begin(proc.prep_cmds);
|
||||||
_undo_it = _undo_begin;
|
_undo_it = _undo_begin;
|
||||||
|
|
||||||
if(!proc.cmd_output.empty() && proc.cmd_output != "null"sv) {
|
if(!proc.output.empty() && proc.output != "null"sv) {
|
||||||
_pipe.reset(fopen(proc.cmd_output.c_str(), "a"));
|
_pipe.reset(fopen(proc.output.c_str(), "a"));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::error_code ec;
|
std::error_code ec;
|
||||||
@@ -63,7 +63,7 @@ int proc_t::execute(const std::string &name) {
|
|||||||
_undo_pre_cmd();
|
_undo_pre_cmd();
|
||||||
});
|
});
|
||||||
|
|
||||||
for(; _undo_it != std::end(proc.pre_cmds); ++_undo_it) {
|
for(; _undo_it != std::end(proc.prep_cmds); ++_undo_it) {
|
||||||
auto &cmd = _undo_it->do_cmd;
|
auto &cmd = _undo_it->do_cmd;
|
||||||
|
|
||||||
std::cout << "Executing: ["sv << cmd << ']' << std::endl;
|
std::cout << "Executing: ["sv << cmd << ']' << std::endl;
|
||||||
@@ -81,11 +81,11 @@ int proc_t::execute(const std::string &name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Starting ["sv << proc.cmd << ']' << std::endl;
|
std::cout << "Starting ["sv << proc.cmd << ']' << std::endl;
|
||||||
if(proc.cmd_output.empty() || proc.cmd_output == "null"sv) {
|
if(proc.output.empty() || proc.output == "null"sv) {
|
||||||
_process = bp::child(proc.cmd, _env, bp::std_out > bp::null, bp::std_err > bp::null, ec);
|
_process = bp::child(proc.cmd, _env, bp::std_out > bp::null, bp::std_err > bp::null, ec);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_process = bp::child(proc.cmd, _env, bp::std_out > proc.cmd_output, bp::std_err > proc.cmd_output, ec);
|
_process = bp::child(proc.cmd, _env, bp::std_out > _pipe.get(), bp::std_err > _pipe.get(), ec);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ec) {
|
if(ec) {
|
||||||
|
|||||||
+5
-2
@@ -14,6 +14,9 @@ namespace proc {
|
|||||||
using file_t = util::safe_ptr_v2<FILE, int, fclose>;
|
using file_t = util::safe_ptr_v2<FILE, int, fclose>;
|
||||||
|
|
||||||
struct cmd_t {
|
struct cmd_t {
|
||||||
|
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)) {}
|
||||||
|
|
||||||
std::string do_cmd;
|
std::string do_cmd;
|
||||||
|
|
||||||
// Executed when proc_t has finished running, meant to reverse 'do_cmd' if applicable
|
// Executed when proc_t has finished running, meant to reverse 'do_cmd' if applicable
|
||||||
@@ -30,10 +33,10 @@ struct cmd_t {
|
|||||||
* filename -- The output of the commands are appended to filename
|
* filename -- The output of the commands are appended to filename
|
||||||
*/
|
*/
|
||||||
struct ctx_t {
|
struct ctx_t {
|
||||||
std::vector<cmd_t> pre_cmds;
|
std::vector<cmd_t> prep_cmds;
|
||||||
|
|
||||||
std::string cmd;
|
std::string cmd;
|
||||||
std::string cmd_output;
|
std::string output;
|
||||||
};
|
};
|
||||||
|
|
||||||
class proc_t {
|
class proc_t {
|
||||||
|
|||||||
Reference in New Issue
Block a user