5 #define LOG(...) {fprintf(stderr, "%d: ", getpid());fprintf(stderr, __VA_ARGS__);fprintf(stderr, "\n");} 17 static void replace_all(std::string &str,
const std::string &search,
const std::string &replace) {
19 while ((start_pos = str.find(search, start_pos)) != std::string::npos) {
20 str.replace(start_pos, search.length(), replace);
21 start_pos += replace.length();
26 std::ifstream in(filename, std::ios::in | std::ios::binary);
29 in.seekg(0, std::ios::end);
30 contents.resize(in.tellg());
31 in.seekg(0, std::ios::beg);
32 in.read(&contents[0], contents.size());
36 throw std::runtime_error(
"Could not read file");
46 : stream(std::move(stream)), R(R), Rcallbacks(Rcallbacks), can_send_reply(false) {
59 #define CMD_CATCH } catch (const BinaryStream::stream_exception) { throw; } catch (const std::exception &e) { std::string s = e.what(); LOG("Command failed: %s", s.c_str()); sendReply(RIS_REPLY_ERROR); stream.write(s); } 66 throw std::runtime_error(
"Client sent the wrong magic number");
74 LOG(
"Exiting because the client requested it");
79 LOG(
"Setting value for %s", name.c_str());
88 LOG(
"Returning value for %s", name.c_str());
98 LOG(
"Setting callback for %s", name.c_str());
103 std::vector<int32_t> param_types;
104 param_types.reserve(paramcount+1);
105 param_types.push_back(result_type);
106 for (
size_t i=0;i<paramcount;i++) {
108 param_types.push_back(type);
115 LOG(
"Callback %s initialized", name.c_str());
125 std::string delimiter =
"\n\n";
129 end = source.find(delimiter, start);
130 if (end == std::string::npos)
132 std::string line = source.substr(start, end-start);
133 start = end+delimiter.length();
134 LOG(
"src: %s", line.c_str());
137 std::string lastline = source.substr(start);
138 LOG(
"src: %s", lastline.c_str());
140 LOG(
"Trying to return the result of the R code as a value with typeid %d", type);
149 LOG(
"Returning console output");
156 LOG(
"Initializing plot");
161 R.
parseEval(
"rserver_plot_tempfile = tempfile(\"rs_plot\", fileext=\".png\")");
162 R.
parseEval(
"png(rserver_plot_tempfile, width=" + std::to_string(width) +
", height=" + std::to_string(height)+
", bg=\"transparent\")");
167 LOG(
"Returning plot");
170 std::string filename = Rcpp::as<std::string>(
R[
"rserver_plot_tempfile"]);
172 std::remove(filename.c_str());
178 throw std::runtime_error(
"Client sent unknown command");
186 LOG(
"unknown type in sexp_from_stream: %d", type);
187 throw std::runtime_error(
"Unknown datatype in sexp_from_stream");
195 LOG(
"unknown type in sexp_to_stream: %d", type);
196 throw std::runtime_error(
"Unknown datatype in sexp_to_stream");
206 registerType<int16_t>();
207 registerType<uint16_t>();
208 registerType<int32_t>();
209 registerType<uint32_t>();
210 registerType<int64_t>();
211 registerType<uint64_t>();
212 registerType<float>();
213 registerType<double>();
214 registerType<std::string>();
218 registerType<std::vector<int16_t>>();
219 registerType<std::vector<uint16_t>>();
220 registerType<std::vector<int32_t>>();
221 registerType<std::vector<uint32_t>>();
222 registerType<std::vector<int64_t>>();
223 registerType<std::vector<uint64_t>>();
224 registerType<std::vector<float>>();
225 registerType<std::vector<double>>();
226 registerType<std::vector<std::string>>();
const char RIS_CMD_GETCONSOLE
InternalFunctionForRInsideServer_Impl< PreserveStorage > InternalFunctionForRInsideServer
const uint32_t RIS_MAGIC_NUMBER
const char RIS_CMD_INITPLOT
RInsideCallbacks & Rcallbacks
RInsideServer(BinaryStream &stream, RInside &R, RInsideCallbacks &Rcallbacks)
void parseEvalQ(const std::string &line)
const char RIS_CMD_GETPLOT
std::string getConsoleOutput()
void write(const char *buffer, size_t len)
const char RIS_CMD_SETVALUE
static std::string read_file_as_string(const std::string &filename)
int parseEval(const std::string &line, SEXP &ans)
static void registerDefaultTypes()
void sexp_to_stream(SEXP, int32_t type, bool include_reply=false)
static std::map< int32_t, std::function< SEXP(BinaryStream &)> > registry_sexp_from_stream
static void replace_all(std::string &str, const std::string &search, const std::string &replace)
const char RIS_REPLY_VALUE
void resetConsoleOutput()
const char RIS_CMD_GETVALUE
static std::map< int32_t, std::function< void(RInsideServer &, SEXP, bool)> > registry_sexp_to_stream
void sendReply(char reply)
size_t read(char *buffer, size_t len)
const char RIS_CMD_SETCALLBACK