6#include <init.capnp.proxy.h>
24namespace fs = std::filesystem;
26static auto Spawn(
mp::EventLoop& loop,
const std::string& process_argv0,
const std::string& new_exe_name)
29 const int fd =
mp::SpawnProcess(pid, [&](
int fd) -> std::vector<std::string> {
30 fs::path path = process_argv0;
31 path.remove_filename();
32 path.append(new_exe_name);
33 return {path.string(), std::to_string(fd)};
35 return std::make_tuple(mp::ConnectStream<InitInterface>(loop, fd), pid);
38static void LogPrint(
bool raise,
const std::string& message)
40 if (raise)
throw std::runtime_error(message);
41 std::ofstream(
"debug.log", std::ios_base::app) << message << std::endl;
44int main(
int argc,
char** argv)
47 std::cout <<
"Usage: mpexample\n";
51 std::promise<mp::EventLoop*> promise;
52 std::thread loop_thread([&] {
54 promise.set_value(&loop);
59 auto [printer_init, printer_pid] =
Spawn(*loop, argv[0],
"mpprinter");
60 auto [calc_init, calc_pid] =
Spawn(*loop, argv[0],
"mpcalculator");
61 auto calc = calc_init->makeCalculator(printer_init->makePrinter());
64 std::cout <<
"Enter the equation, or \"exit\" to quit: ";
65 std::getline(std::cin, eqn);
66 if (eqn ==
"exit")
break;
67 calc->solveEquation(eqn);
75 std::cout <<
"Bye!" << std::endl;
Event loop implementation.
void loop()
Run event loop.
int main(int argc, char **argv)
static void LogPrint(bool raise, const std::string &message)
static auto Spawn(mp::EventLoop &loop, const std::string &process_argv0, const std::string &new_exe_name)
int WaitProcess(int pid)
Wait for a process to exit and return its exit code.
int SpawnProcess(int &pid, FdToArgsFn &&fd_to_args)
Spawn a new process that communicates with the current process over a socket pair.