2018-06-30 13:10:06 +00:00
|
|
|
#include "JavaRunner.h"
|
|
|
|
|
|
|
|
#include <dispatch/dispatch.h>
|
|
|
|
#include <subprocess.hpp>
|
2018-07-08 13:16:07 +00:00
|
|
|
#include <future>
|
2018-06-30 13:10:06 +00:00
|
|
|
|
|
|
|
using namespace subprocess;
|
2018-07-08 13:16:07 +00:00
|
|
|
using namespace std::experimental;
|
2018-06-30 13:10:06 +00:00
|
|
|
|
|
|
|
JavaRunner::JavaRunner(std::string javaBin, const fp_proc_t& execFn, const fp_t& cb)
|
|
|
|
: javaBinaryPath(javaBin), executingFn(execFn), exitCallbackFn(cb)
|
|
|
|
{
|
|
|
|
javaProcess = std::shared_ptr<Popen>(new Popen({javaBin.c_str(), "-version"}, defer_spawn{true}));
|
|
|
|
}
|
|
|
|
|
2018-07-08 13:16:07 +00:00
|
|
|
optional<std::future<int> > JavaRunner::execute()
|
2018-06-30 13:10:06 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
auto executingFn = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^{
|
|
|
|
this->executingFn(this);
|
|
|
|
});
|
|
|
|
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), executingFn);
|
|
|
|
dispatch_block_wait(executingFn, DISPATCH_TIME_FOREVER);
|
|
|
|
|
|
|
|
// Here, the process is done executing.
|
|
|
|
|
|
|
|
printf("Finished executingFn - Runs callbackFn\n");
|
|
|
|
this->exitCallbackFn();
|
2018-07-08 13:16:07 +00:00
|
|
|
return std::async(std::launch::async, []{ return 0; });
|
2018-06-30 13:10:06 +00:00
|
|
|
} catch (std::exception* ex) {
|
|
|
|
printf("ERROR: %s\n", ex->what());
|
2018-07-08 13:16:07 +00:00
|
|
|
return nullopt;
|
2018-06-30 13:10:06 +00:00
|
|
|
}
|
|
|
|
}
|