peerplays-fc/include/fc/json_rpc_process_client.hpp

55 lines
2 KiB
C++
Raw Normal View History

#pragma once
#include <fc/json_rpc_client.hpp>
2012-11-24 18:02:23 +00:00
#include <fc/json_rpc_stream_connection.hpp>
#include <fc/process.hpp>
2012-11-24 18:02:23 +00:00
#include <fc/filesystem.hpp>
namespace fc { namespace json {
template<typename InterfaceType>
class rpc_process_client : public ptr<InterfaceType,fc::json::detail::rpc_member> {
public:
fc::future<int> exec( const fc::path& exe, int opt = fc::process::open_all ) {
2012-11-24 18:02:23 +00:00
return exec( exe, fc::path("."), opt );
}
fc::future<int> exec( const fc::path& exe, const fc::path& wd,
int opt = fc::process::open_all ) {
return exec( exe, fc::vector<fc::string>(), wd, opt );
}
fc::future<int> exec( const fc::path& exe, fc::vector<fc::string>&& args ,
int opt = fc::process::open_all ) {
2012-11-24 18:02:23 +00:00
return exec( exe, fc::move(args), fc::path("."), opt );
}
fc::future<int> exec( const fc::path& exe, fc::vector<fc::string>&& args,
const fc::path& wd, int opt = fc::process::open_all ) {
2012-11-24 18:02:23 +00:00
auto r = _proc.exec( canonical(exe), fc::move(args), wd, opt );
_con.reset( new fc::json::rpc_stream_connection( _proc.out_stream(), _proc.in_stream() ) );
this->_vtable.reset(new fc::detail::vtable<InterfaceType,fc::json::detail::rpc_member>() );
2012-11-24 18:02:23 +00:00
rpc_connection::ptr p(_con);
this->_vtable->template visit_other<InterfaceType>( fc::json::detail::vtable_visitor(p) );
return r;
}
2012-11-25 00:39:19 +00:00
void kill() { _con->close(); _proc.kill(); }
/**
* @brief returns a stream that reads from the process' stderr
*/
fc::istream& err_stream() { return _proc.err_stream(); }
2012-11-24 18:02:23 +00:00
template<typename T>
void on_close( T&& f) { _con->on_close( fc::forward<T>(f) ); }
2012-11-26 15:40:44 +00:00
const fc::json::rpc_stream_connection::ptr& connection()const { return _con; }
2012-12-13 17:25:12 +00:00
~rpc_process_client() {
if(_con)
_con->close();
}
private:
fc::process _proc;
fc::json::rpc_stream_connection::ptr _con;
};
} }