#include <iostream>
#include <string>
namespace {
{
std::string_view path;
if (argc > 0) {
path = argv[0];
#ifdef FZ_WINDOWS
auto delim = path.find_last_of("/\\");
#else
auto delim = path.find_last_of("/");
#endif
if (delim == std::string::npos) {
path = std::string_view();
}
else {
path = path.substr(0, delim + 1);
}
}
}
#ifdef FZ_WINDOWS
auto suffix =
fzT(
".exe");
#else
#endif
}
{
public:
:
fz::event_handler(loop)
, process_(pool, *this)
{}
~runner()
{
process_.kill();
}
{
fz::dispatch<fz::process_event>(ev, this, &runner::on_process_event);
}
{
on_read();
}
}
void on_read()
{
std::string input;
while (!done_) {
char buf[100];
if (!r) {
return;
}
std::cerr << "Could not read from process" << std::endl;
exit(1);
}
std::cerr << "Unexpected EOF from process" << std::endl;
exit(1);
}
input += std::string(buf, r.
value_);
auto delim = input.find_first_of("\r\n");
while (delim != std::string::npos) {
std::string line = input.substr(0, delim);
input = input.substr(delim + 1);
delim = input.find_first_of("\r\n");
if (!line.empty()) {
std::cout << "Received line from process: " << line << std::endl;
if (line == "woof") {
done_ = true;
if (!process_.write("0\n")) {
std::cerr << "Sending data to the process failed. Looks like it could not be started or has quit early." << std::endl;
exit(1);
}
std::cout << "Told process to quit." << std::endl;
}
}
}
}
while (true) {
char buf[100];
if (!r) {
return;
}
std::cerr << "Could not read from process" << std::endl;
exit(1);
}
std::cerr << "Received the expected EOF from process" << std::endl;
break;
}
}
event_loop_.stop();
}
bool done_{};
};
int main(int argc, char *argv[])
{
runner h(pool, loop);
if (!h.process_.spawn(get_program_dir(argc, argv) +
fzT(
"timer_fizzbuzz") + suffix)) {
std::cerr << "Could not spawn process" << std::endl;
return 1;
}
std::cout << "Spawned process" << std::endl;
if (!r) {
std::cerr << "Sending data to the process failed. Looks like it could not be started or has quit early." << std::endl;
return 1;
}
}
std::cout << "Waiting on process to print woof..." << std::endl;
loop.run();
}